forked from xfxdev/xtcp
/
xtcp.go
167 lines (150 loc) · 4.75 KB
/
xtcp.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package xtcp
import (
"fmt"
"io"
)
var (
// DefaultSendListLen is the default length of send list.
DefaultSendListLen = 16 // channel size
// DefaultRecvBufInitSize is the default init size of recv buf.
DefaultRecvBufInitSize = 1 << 10 // 1k
// DefaultRecvBufMaxSize is the default max size of recv buf.
DefaultRecvBufMaxSize = 4 << 10 // 4k
// DefaultSendBufInitSize is the default init size of send buf.
DefaultSendBufInitSize = 1 << 10 // 1k
// DefaultSendBufMaxSize is the default max size of send buf.
DefaultSendBufMaxSize = 2 << 10 // 4k
)
// StopMode define the stop mode of server and conn.
type StopMode uint8
const (
// StopImmediately mean stop directly.
StopImmediately StopMode = iota
// StopGracefullyButNotWait mean stop gracefully but not wait.
StopGracefullyButNotWait
// StopGracefullyAndWait mean stop and wait.
StopGracefullyAndWait
)
// EventType is the conn event type.
type EventType int
func (et EventType) String() string {
switch et {
case EventAccept:
return "accept"
case EventConnected:
return "connected"
case EventSend:
return "send"
case EventRecv:
return "recv"
case EventClosed:
return "closed"
default:
return "<unknown xtcp event>"
}
}
const (
// EventAccept mean server accept a new connect.
EventAccept EventType = iota
// EventConnected mean client connected to a server.
EventConnected
// EventSend mean conn send a packet.
EventSend
// EventRecv mean conn recv a packet.
EventRecv
// EventClosed mean conn is closed.
EventClosed
)
// Handler is the event callback.
// p will be nil when event is EventAccept/EventConnected/EventClosed
type Handler interface {
OnEvent(et EventType, c *Conn, p Packet)
}
// Packet is the unit of data.
type Packet interface {
fmt.Stringer
}
// Protocol use to pack/unpack Packet.
type Protocol interface {
// return the size need for pack the Packet.
PackSize(p Packet) int
// PackTo pack the Packet to w.
// The return value n is the number of bytes written;
// Any error encountered during the write is also returned.
PackTo(p Packet, w io.Writer) (int, error)
// Pack pack the Packet to new created buf.
Pack(p Packet) ([]byte, error)
// try to unpack the buf to Packet. If return len > 0, then buf[:len] will be discard.
// The following return conditions must be implement:
// (nil, 0, nil) : buf size not enough for unpack one Packet.
// (nil, len, err) : buf size enough but error encountered.
// (p, len, nil) : unpack succeed.
Unpack(buf []byte) (Packet, int, error)
}
// Options is the options used for net conn.
type Options struct {
Handler Handler
Protocol Protocol
SendListLen int // default is DefaultSendListLen if you don't set.
RecvBufInitSize int // default is DefaultRecvBufInitSize if you don't set.
RecvBufMaxSize int // default is DefaultRecvBufMaxSize if you don't set.
SendBufInitSize int // default is DefaultSendBufInitSize if you don't set.
SendBufMaxSize int // default is DefaultSendBufMaxSize if you don't set.
}
// NewOpts create a new options and set some default value.
// will panic if handler or protocol is nil.
// eg: opts := NewOpts().SetSendListLen(len).SetRecvBufInitSize(len)...
func NewOpts(h Handler, p Protocol) *Options {
if h == nil || p == nil {
panic("xtcp.NewOpts: nil handler or protocol")
}
return &Options{
Handler: h,
Protocol: p,
SendListLen: DefaultSendListLen,
RecvBufInitSize: DefaultRecvBufInitSize,
RecvBufMaxSize: DefaultRecvBufMaxSize,
SendBufInitSize: DefaultSendBufInitSize,
SendBufMaxSize: DefaultSendBufMaxSize,
}
}
// SetSendListLen set init size of the recv buf, 0 mean DefaultSendListLen.
func (opts *Options) SetSendListLen(len int) *Options {
if len < 0 {
panic("xtcp.Options.SetSendListLen: negative size")
}
opts.SendListLen = len
return opts
}
// SetRecvBufInitSize set init size of the recv buf, 0 mean DefaultRecvBufInitSize.
func (opts *Options) SetRecvBufInitSize(s int) *Options {
if s < 0 {
panic("xtcp.Options.SetRecvBufInitSize: negative size")
}
opts.RecvBufInitSize = s
return opts
}
// SetRecvBufMaxSize set max size of the recv buf, 0 mean DefaultRecvBufMaxSize.
func (opts *Options) SetRecvBufMaxSize(s int) *Options {
if s < 0 {
panic("xtcp.Options.SetRecvBufMaxSize: negative size")
}
opts.RecvBufMaxSize = s
return opts
}
// SetSendBufInitSize set init size of the recv buf, 0 mean DefaultSendBufInitSize.
func (opts *Options) SetSendBufInitSize(s int) *Options {
if s < 0 {
panic("xtcp.Options.SetSendBufInitSize: negative size")
}
opts.SendBufInitSize = s
return opts
}
// SetSendBufMaxSize set max size of the recv buf, 0 mean DefaultSendBufMaxSize.
func (opts *Options) SetSendBufMaxSize(s int) *Options {
if s < 0 {
panic("xtcp.Options.SetSendBufMaxSize: negative size")
}
opts.SendBufMaxSize = s
return opts
}