-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.go
85 lines (64 loc) · 1.84 KB
/
net.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
package dnet
import (
"errors"
"io"
"net"
)
var (
ErrSessionClosed = errors.New("dnet: session is closed. ")
ErrNilMsgCallBack = errors.New("dnet: session without msgCallback")
ErrSendMsgNil = errors.New("dnet: session send msg is nil")
ErrSendChanFull = errors.New("dnet: session send channel is full")
ErrSendTimeout = errors.New("dnet: send timeout. ")
ErrReadTimeout = errors.New("dnet: read timeout. ")
)
type Session interface {
// connection
NetConn() interface{}
// RemoteAddr returns the remote network address.
RemoteAddr() net.Addr
// LocalAddr returns the local network address.
LocalAddr() net.Addr
// Send data will be encoded by the encoder and sent
Send(o interface{}) error
// SetContext binding session data
SetContext(ctx interface{})
// Context returns binding session data
Context() interface{}
// Close closes the session.
Close(reason error)
// IsClosed returns has it been closed
IsClosed() bool
}
// AcceptorHandle type interface
type AcceptorHandler interface {
// handler to invokes
OnConnection(conn net.Conn)
}
type AcceptorHandlerFunc func(conn net.Conn)
func (handler AcceptorHandlerFunc) OnConnection(conn net.Conn) {
// handler to invokes
handler(conn)
}
// Deprecated: HandleFunc returns AcceptorHandlerFunc with the handler function.
func HandleFunc(handler func(conn net.Conn)) AcceptorHandlerFunc {
return handler
}
// Acceptor type interface
type Acceptor interface {
// Serve listen and serve with AcceptorHandler
Serve(handler AcceptorHandler) error
// ServeFunc listen and serve with AcceptorHandlerFunc
ServeFunc(handler AcceptorHandlerFunc) error
// Stop stop the acceptor
Stop()
// Addr returns address of the listener
Addr() net.Addr
}
// Codec
type Codec interface {
// Encode
Encode(o interface{}) ([]byte, error)
// Decode
Decode(reader io.Reader) (interface{}, error)
}