forked from yicaoyimuys/GoGameServer
/
api.go
executable file
·100 lines (83 loc) · 2.25 KB
/
api.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
package link
import (
"net"
"sync/atomic"
"time"
)
type SessionFetcher func(func(*Session))
type ServerProtocol interface {
NewListener(listener net.Listener) Listener
}
type ClientProtocol interface {
NewClientConn(conn net.Conn) (Conn, error)
}
type BroadcastProtocol interface {
Broadcast(msg interface{}, fetcher SessionFetcher) error
}
type Listener interface {
Accept() (Conn, error)
Handshake(Conn) error
Addr() net.Addr
Protocol() ServerProtocol
Close() error
}
type Conn interface {
Config() SessionConfig
LocalAddr() net.Addr
RemoteAddr() net.Addr
Receive(msg interface{}) error
Send(msg interface{}) (err error)
Close() error
}
var DefaultBroadcast = defaultBroadcast{}
type defaultBroadcast struct {
}
func (_ defaultBroadcast) Broadcast(msg interface{}, fetcher SessionFetcher) error {
fetcher(func(session *Session) {
session.AsyncSend(msg)
})
return nil
}
var autoSessionId uint64
func Listen(network, address string, protocol ServerProtocol) (Listener, error) {
listener, err := net.Listen(network, address)
if err != nil {
return nil, err
}
return protocol.NewListener(listener), nil
}
func Dial(network, address string, protocol ClientProtocol) (Conn, error) {
conn, err := net.Dial(network, address)
if err != nil {
return nil, err
}
return protocol.NewClientConn(conn)
}
func DialTimeout(network, address string, timeout time.Duration, protocol ClientProtocol) (Conn, error) {
conn, err := net.DialTimeout(network, address, timeout)
if err != nil {
return nil, err
}
return protocol.NewClientConn(conn)
}
func Serve(network, address string, protocol ServerProtocol) (*Server, error) {
listener, err := Listen(network, address, protocol)
if err != nil {
return nil, err
}
return NewServer(listener), nil
}
func Connect(network, address string, protocol ClientProtocol) (*Session, error) {
conn, err := Dial(network, address, protocol)
if err != nil {
return nil, err
}
return NewSession(atomic.AddUint64(&autoSessionId, 1), conn), nil
}
func ConnectTimeout(network, address string, timeout time.Duration, protocol ClientProtocol) (*Session, error) {
conn, err := DialTimeout(network, address, timeout, protocol)
if err != nil {
return nil, err
}
return NewSession(atomic.AddUint64(&autoSessionId, 1), conn), nil
}