server.go
834 Bytes
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
package net
import (
"fmt"
"net"
"pro2d/conf"
"pro2d/utils"
"sync"
)
type Server struct {
SConf *conf.SConf
Clients *sync.Map
}
func NewServer(sConf *conf.SConf) *Server {
return &Server{
SConf: sConf,
Clients: new(sync.Map),
}
}
func (s *Server) OnRecv(msg *MsgPkg) {
utils.Sugar.Debugf("cmd: %d, data: %s", msg.Head.Cmd, msg.Body)
}
func (s *Server) OnClose(conn *Connection) {
s.Clients.Delete(conn.Id)
}
func (s *Server)Start() error {
port := fmt.Sprintf(":%d", s.SConf.Port)
l, err := net.Listen("tcp", port)
if err != nil {
return err
}
utils.Sugar.Debugf("listen on %s\n", port)
id := 0
for {
conn, err := l.Accept()
if err != nil {
return err
}
id++
client := NewConn(id, conn, s)
s.Clients.Store(id, client)
go client.Start()
}
}
func (s *Server)Stop() {
}