This repository has been archived by the owner on Apr 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconn.go
84 lines (70 loc) · 1.57 KB
/
conn.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package fastsocket
import (
"fmt"
"io"
"net"
"os"
"syscall"
"time"
)
func newConn(fd int, lad, rad *Addr) (conn *NonBlockingConn, err error) {
conn = &NonBlockingConn{
fd: fd,
lad: lad,
rad: rad,
file: os.NewFile(uintptr(fd), fmt.Sprintf("fsocket.tcp.%d", fd)),
}
err = conn.setNoDelay(true)
if err != nil {
return
}
err = conn.setNonblock(true)
return
}
type NonBlockingConn struct {
file *os.File
fd int
lad *Addr
rad *Addr
}
func (c *NonBlockingConn) setNonblock(nonblocking bool) error {
return syscall.SetNonblock(c.fd, nonblocking)
}
func (c *NonBlockingConn) setNoDelay(noDelay bool) error {
return syscall.SetsockoptInt(c.fd, syscall.IPPROTO_TCP, syscall.TCP_NODELAY, boolInt(noDelay))
}
func (c *NonBlockingConn) File() (*os.File, error) {
return c.file, nil
}
func (c *NonBlockingConn) Read(b []byte) (n int, err error) {
n, err = fixCount(syscall.Read(c.fd, b))
if n == 0 && len(b) > 0 && err == nil {
return 0, io.EOF
}
return
}
func (c *NonBlockingConn) Write(b []byte) (n int, err error) {
n, err = fixCount(syscall.Write(c.fd, b))
if n != len(b) && err == nil {
err = io.ErrShortWrite
}
return
}
func (c *NonBlockingConn) Close() error {
return syscall.Close(c.fd)
}
func (c *NonBlockingConn) LocalAddr() net.Addr {
return c.lad
}
func (c *NonBlockingConn) RemoteAddr() net.Addr {
return c.rad
}
func (c *NonBlockingConn) SetDeadline(t time.Time) error {
return nil
}
func (c *NonBlockingConn) SetReadDeadline(t time.Time) error {
return nil
}
func (c *NonBlockingConn) SetWriteDeadline(t time.Time) error {
return nil
}