forked from ngmoco/falcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_notwindows.go
45 lines (41 loc) · 1.09 KB
/
server_notwindows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// +build !windows
package falcore
import (
"net"
"syscall"
)
// only valid on non-windows
func (srv *Server) setupNonBlockingListener(err error, l *net.TCPListener) error {
// FIXME: File() returns a copied pointer. we're leaking it. probably doesn't matter
if srv.listenerFile, err = l.File(); err != nil {
return err
}
fd := int(srv.listenerFile.Fd())
if e := syscall.SetNonblock(fd, true); e != nil {
return e
}
if srv.sendfile {
if e := syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, srv.sockOpt, 1); e != nil {
return e
}
}
return nil
}
func (srv *Server) cycleNonBlock(c net.Conn) {
if srv.sendfile {
if tcpC, ok := c.(*net.TCPConn); ok {
if f, err := tcpC.File(); err == nil {
// f is a copy. must be closed
defer f.Close()
fd := int(f.Fd())
// Disable TCP_CORK/TCP_NOPUSH
syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, srv.sockOpt, 0)
// For TCP_NOPUSH, we need to force flush
c.Write([]byte{})
// Re-enable TCP_CORK/TCP_NOPUSH
syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, srv.sockOpt, 1)
syscall.SetNonblock(fd, true)
}
}
}
}