/
server.go
66 lines (58 loc) · 1.23 KB
/
server.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
package znet
import (
"fmt"
"github.com/FelixStarship/go11/leo/leov2.0/ziface"
"net"
)
type Server struct {
Name string
IPVersion string
IP string
Port int
Route ziface.IRoute
}
func (s *Server) Start() {
fmt.Printf("[START] Server listener at IP:%s,Port:%d,is starting\n", s.IP, s.Port)
go func() {
addr, err := net.ResolveTCPAddr(s.IPVersion, fmt.Sprintf("%s:%d", s.IP, s.Port))
if err != nil {
fmt.Println("resolve tcp addr err:", err)
return
}
listener, err := net.ListenTCP(s.IPVersion, addr)
if err != nil {
fmt.Println("listen", s.IPVersion, "err", err)
return
}
fmt.Println("start Leo server ", s.Name, "success,now listening...")
var cid uint32
cid = 0
for {
conn, err := listener.AcceptTCP()
if err != nil {
fmt.Println("Accept err", err)
continue
}
go NewConnection(conn, cid, s.Route).Start()
cid++
}
}()
}
func (s *Server) Server() {
s.Start()
select {}
}
func (s *Server) Stop() {
fmt.Println("[STOP] Leo server,name", s.Name)
}
func (s *Server) AddRoute(route ziface.IRoute) {
s.Route = route
}
func NewServer(name string) ziface.IServer {
return &Server{
Name: name,
IPVersion: "tcp4",
IP: "0.0.0.0",
Port: 7777,
}
}