-
Notifications
You must be signed in to change notification settings - Fork 30
/
xtcp.go
124 lines (113 loc) · 3.67 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
package xtcp
import (
"fmt"
"io"
"time"
)
var (
// DefaultRecvBufSize is the default size of recv buf.
DefaultRecvBufSize = 4 << 10 // 4k
// DefaultSendBufListLen is the default length of send buf list.
DefaultSendBufListLen = 1 << 10 // 1k
// DefaultAsyncWrite is enable async write or not.
DefaultAsyncWrite = true
)
// StopMode define the stop mode of server and conn.
type StopMode uint8
const (
// StopImmediately mean stop directly, the cached data maybe will not send.
StopImmediately StopMode = iota
// StopGracefullyButNotWait stop and flush cached data.
StopGracefullyButNotWait
// StopGracefullyAndWait stop and block until cached data sended.
StopGracefullyAndWait
)
// LogLevel used to filter log message by the Logger.
type LogLevel uint8
// logging levels.
const (
Panic LogLevel = iota
Fatal
Error
Warn
Info
Debug
)
// Logger is the log interface
type Logger interface {
Log(l LogLevel, v ...interface{})
Logf(l LogLevel, format string, v ...interface{})
}
type emptyLogger struct {}
func (*emptyLogger)Log(l LogLevel, v ...interface{}) {}
func (*emptyLogger)Logf(l LogLevel, format string, v ...interface{}) {}
var logger Logger = &emptyLogger{}
// SetLogger set the logger
func SetLogger(l Logger) {
logger = l
}
// Handler is the event callback.
// Note : don't block in event handler.
type Handler interface {
// OnAccept mean server accept a new connect.
OnAccept(*Conn)
// OnConnect mean client connected to a server.
OnConnect(*Conn)
// OnRecv mean conn recv a packet.
OnRecv(*Conn, Packet)
// OnUnpackErr mean failed to unpack recved data.
OnUnpackErr(*Conn, []byte, error)
// OnClose mean conn is closed.
OnClose(*Conn)
}
// Packet is the unit of data.
type Packet interface {
fmt.Stringer
}
// Protocol use to pack/unpack Packet.
type Protocol interface {
// PackSize 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)
// Unpack 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
RecvBufSize int // default is DefaultRecvBufSize if you don't set.
SendBufListLen int // default is DefaultSendBufListLen if you don't set.
AsyncWrite bool // default is DefaultAsyncWrite if you don't set.
NoDelay bool // default is true
KeepAlive bool // default is false
KeepAlivePeriod time.Duration // default is 0, mean use system setting.
ReadDeadline time.Duration // default is 0, means Read will not time out.
WriteDeadline time.Duration // default is 0, means Write will not time out.
}
// 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,
RecvBufSize: DefaultRecvBufSize,
SendBufListLen: DefaultSendBufListLen,
AsyncWrite: DefaultAsyncWrite,
NoDelay: true,
KeepAlive: false,
}
}