-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBeseInterface.go
More file actions
63 lines (57 loc) · 1.41 KB
/
BeseInterface.go
File metadata and controls
63 lines (57 loc) · 1.41 KB
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
package network
import (
"net"
)
// tcp 服务端 or 客户端监听接口
type ListenTcp interface {
SetUrl(address *Url) // 设置监听地址
Url() *Url // 地址
SetEvent(event Event) // 设置信息事件
Event() Event // 信息事件
SetProtocol(protocol Protocol) // 设置解析协议
Protocol() Protocol // 协议对象
Close() // 主动关闭
ListenAndServe() // 启动监听,阻塞
SetNewConnect(func(listen ListenTcp, conn net.Conn) Connect)
}
// 连接实例
type Connect interface {
SetHeader(header Header)
Header() Header
GetCon() net.Conn
Close()
Id() uint32
SetUid(uid string)
Uid() string
Send(msg interface{}) bool
SendByte(msg []byte) bool
SendString(msg string) bool
GetIp() uint32
GetPort() uint16
}
type Event interface {
OnStart(listen ListenTcp)
// 新链接
OnConnect(connect Connect)
// 新信息
OnMessage(connect Connect, message []byte)
// 链接关闭
OnClose(connect Connect)
// 发送错误
OnError(listen ListenTcp, err error)
}
type Protocol interface {
// 初始化
Init()
// 第一次连接,通常获取头信息
OnConnect(conn net.Conn) (Header, error)
// 读入处理
Read(conn net.Conn) ([]byte, error)
// 发送处理
Write(conn net.Conn, msg []byte) error
}
type Header interface {
Has(key string) bool
Get(key string) string
Set(data string)
}