-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcpsocket.go
58 lines (49 loc) · 1.19 KB
/
tcpsocket.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
package netgo
import (
"net"
"time"
)
type tcpSocket struct {
socketBase
}
func (tc *tcpSocket) SendBuffers(buffs net.Buffers, deadline ...time.Time) (int64, error) {
d := time.Time{}
if len(deadline) > 0 && !deadline[0].IsZero() {
d = deadline[0]
}
if err := tc.conn.SetWriteDeadline(d); err != nil {
return 0, err
} else {
return buffs.WriteTo(tc.conn.(*net.TCPConn))
}
}
var _ Socket = &tcpSocket{}
func NewTcpSocket(conn *net.TCPConn, packetReceiver ...PacketReceiver) Socket {
s := &tcpSocket{}
s.init(conn, packetReceiver...)
return s
}
func ListenTCP(nettype string, service string, onNewclient func(*net.TCPConn)) (net.Listener, func(), error) {
tcpAddr, err := net.ResolveTCPAddr(nettype, service)
if nil != err {
return nil, nil, err
}
listener, err := net.ListenTCP(nettype, tcpAddr)
if nil != err {
return nil, nil, err
}
serve := func() {
for {
conn, e := listener.Accept()
if e == nil {
onNewclient(conn.(*net.TCPConn))
} else if ne, ok := e.(*net.OpError); ok && ne.Temporary() {
time.Sleep(time.Millisecond * 10)
continue
} else {
return
}
}
}
return listener, serve, nil
}