forked from leesper/tao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
56 lines (43 loc) · 1.14 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
package main
import (
"net"
"runtime"
"github.com/leesper/holmes"
"github.com/leesper/tao"
"github.com/leesper/tao/examples/echo"
)
// EchoServer represents the echo server.
type EchoServer struct {
*tao.Server
}
// NewEchoServer returns an EchoServer.
func NewEchoServer() *EchoServer {
onConnect := tao.OnConnectOption(func(conn tao.WriteCloser) bool {
holmes.Infoln("on connect")
return true
})
onClose := tao.OnCloseOption(func(conn tao.WriteCloser) {
holmes.Infoln("closing client")
})
onError := tao.OnErrorOption(func(conn tao.WriteCloser) {
holmes.Infoln("on error")
})
onMessage := tao.OnMessageOption(func(msg tao.Message, conn tao.WriteCloser) {
holmes.Infoln("receving message")
})
return &EchoServer{
tao.NewServer(onConnect, onClose, onError, onMessage),
}
}
func main() {
defer holmes.Start().Stop()
runtime.GOMAXPROCS(runtime.NumCPU())
tao.Register(echo.Message{}.MessageNumber(), echo.DeserializeMessage, echo.ProcessMessage)
l, err := net.Listen("tcp", ":12345")
if err != nil {
holmes.Fatalf("listen error %v", err)
}
echoServer := NewEchoServer()
defer echoServer.Stop()
echoServer.Start(l)
}