forked from apache/dubbo-getty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec.go
55 lines (45 loc) · 1.93 KB
/
codec.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
/******************************************************
# DESC : codec interface
# MAINTAINER : Alex Stocks
# LICENCE : Apache License 2.0
# EMAIL : alexstocks@foxmail.com
# MOD : 2016-08-17 11:20
# FILE : codec.go
******************************************************/
package getty
// NewSessionCallback will be invoked when server accepts a new client connection or client connects to server successfully.
// if there are too many client connections or u do not want to connect a server again, u can return non-nil error. And
// then getty will close the new session.
type NewSessionCallback func(*Session) error
// Reader is used to unmarshal a complete pkg from buffer
type Reader interface {
// Parse pkg from buffer and if possible return a complete pkg
// If length of buf is not long enough, u should return {nil,0, nil}
// The second return value is the length of the pkg.
Read(*Session, []byte) (interface{}, int, error)
}
// Writer is used to marshal pkg and write to session
type Writer interface {
Write(*Session, interface{}) error
}
// packet handler interface
type ReadWriter interface {
Reader
Writer
}
// EventListener is used to process pkg that recved from remote session
type EventListener interface {
// invoked when session opened
// If the return error is not nil, @Session will be closed.
OnOpen(*Session) error
// invoked when session closed. when @Session got error, session will not invoke @OnClose but invoke @OnError.
OnClose(*Session)
// invoked when got error.
OnError(*Session, error)
// invoked periodically, its period can be set by (Session)SetCronPeriod
OnCron(*Session)
// invoked when receive packge. Pls attention that do not handle long time logic processing in this func.
// Y'd better set the package's maximum length. If the message's length is greater than it, u should
// should return err and getty will close this connection soon.
OnMessage(*Session, interface{})
}