-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp.go
100 lines (85 loc) · 2.11 KB
/
tcp.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"net"
"strconv"
"time"
)
var DefaultWriteTimeout = time.Duration(20 * time.Second)
type TcpConnection struct {
*net.TCPConn
head []byte
ReadBuf []byte //读取的byte数组
WriteBuf []byte //写入的byte数组
}
type TcpServer struct {
*net.TCPListener
tcpAddr string
}
func NewTcpServer(address string) (*TcpServer, error) {
tcpServer := &TcpServer{
tcpAddr: address,
}
tcpAddr, err := net.ResolveTCPAddr("tcp", address)
if err != nil {
return nil, err
}
tcpListener, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
return nil, err
}
tcpServer.TCPListener = tcpListener
return tcpServer, nil
}
func (this *TcpServer) Accept() (*TcpConnection, error) {
tcpConn, err := this.TCPListener.AcceptTCP()
if err != nil {
return nil, err
}
tcpConnection := &TcpConnection{TCPConn: tcpConn}
tcpConnection.head = make([]byte, 8)
return tcpConnection, nil
}
// 获取连接ip
func (this *TcpConnection) RemoteIp() string {
remoteAddr := this.RemoteAddr().(*net.TCPAddr)
return remoteAddr.IP.String()
}
// 获取连接port
func (this *TcpConnection) RemotePort() string {
remoteAddr := this.RemoteAddr().(*net.TCPAddr)
return strconv.Itoa(remoteAddr.Port)
}
// 一次读取的字节数,循环读取
func (this *TcpConnection) Read(length int, timeout time.Duration) ([]byte, error) {
if len(this.ReadBuf) > length {
err := this.RealRead(this.ReadBuf[0:length], timeout)
return this.ReadBuf[0:length], err
}
data := make([]byte, length)
err := this.RealRead(data, timeout)
return data, err
}
// 实际的tcp read
func (this *TcpConnection) RealRead(data []byte, timeout time.Duration) error {
// 设置读写超时时间
this.TCPConn.SetReadDeadline(time.Now().Add(timeout))
length := len(data)
n := 0
for length > 0 {
n, err := this.TCPConn.Read(data[n:])
if err != nil {
return err
}
if length > 0 {
data = data[n:]
}
length = length - n
}
return nil
}
func (this *TcpConnection) Write(stream []byte) error {
// 设置写超时时间
this.TCPConn.SetWriteDeadline(time.Now().Add(DefaultWriteTimeout))
_, err := this.TCPConn.Write(stream)
return err
}