/
main.go
174 lines (155 loc) · 4.14 KB
/
main.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"fmt"
"log"
"net"
"github.com/golang/protobuf/proto"
"github.com/xfxdev/xtcp"
"github.com/xfxdev/xtcp/_example/exampleproto"
)
type processor func(c *xtcp.Conn, msg proto.Message)
type exampleLogger struct {}
func (*exampleLogger)Log(l xtcp.LogLevel, v ...interface{}) {
if l != xtcp.Debug {
log.Output(2, logLevel2Str[l]+" "+fmt.Sprint(v...))
}
}
func (*exampleLogger)Logf(l xtcp.LogLevel, format string, v ...interface{}) {
if l != xtcp.Debug {
log.Output(2, logLevel2Str[l]+" "+fmt.Sprintf(format, v...))
}
}
var (
logger = &exampleLogger{}
protocol = &ProtobufProtocol{}
mapProcessor = make(map[string]processor)
msghello = "client : Hello"
msgByeBye = "client : ByeBye"
msgHi = "server : Hi"
msgBye = "server : Bye"
logLevel2Str = []string{
"[P]",
"[F]",
"[E]",
"[W]",
"[I]",
"[D]",
}
)
func init() {
mapProcessor[proto.MessageName((*exampleproto.LoginRequest)(nil))] = loginRequestProcess
mapProcessor[proto.MessageName((*exampleproto.LoginResponse)(nil))] = loginResponseProcess
mapProcessor[proto.MessageName((*exampleproto.Chat)(nil))] = chatProcess
xtcp.SetLogger(logger)
}
func loginRequestProcess(c *xtcp.Conn, msg proto.Message) {
if request, ok := msg.(*exampleproto.LoginRequest); ok {
logger.Logf(xtcp.Info, "login request - account : %v, pw : %v", request.GetAccount(), request.GetPassword())
response := &exampleproto.LoginResponse{
Ret: proto.Int32(1),
}
buf, err := protocol.Pack(NewProtobufPacket(response))
if err != nil {
logger.Log(xtcp.Info, "failed to pack msg : ", err)
return
}
c.Send(buf)
}
}
func loginResponseProcess(c *xtcp.Conn, msg proto.Message) {
if response, ok := msg.(*exampleproto.LoginResponse); ok {
if response.GetRet() == 1 {
logger.Log(xtcp.Info, "login success.")
// start chat after login success.
chatMsg := &exampleproto.Chat{
Msg: proto.String(msghello),
}
c.SendPacket(NewProtobufPacket(chatMsg))
} else {
logger.Log(xtcp.Info, "login failed.")
}
}
}
func chatProcess(c *xtcp.Conn, msg proto.Message) {
if chatMsg, ok := msg.(*exampleproto.Chat); ok {
logger.Log(xtcp.Info, " - ", chatMsg.GetMsg())
var strResponseMsg string
switch c.UserData.(string) {
case "server":
switch chatMsg.GetMsg() {
case msghello:
strResponseMsg = msgHi
case msgByeBye:
strResponseMsg = msgBye
}
case "client":
switch chatMsg.GetMsg() {
case msgHi:
strResponseMsg = msgByeBye
case msgBye:
c.Stop(xtcp.StopGracefullyButNotWait)
return
}
}
msgChatResponse := &exampleproto.Chat{
Msg: proto.String(strResponseMsg),
}
c.SendPacket(NewProtobufPacket(msgChatResponse))
}
}
type myHandler struct {
}
func (h *myHandler) OnAccept(c *xtcp.Conn) {
logger.Log(xtcp.Info, "accept : ", c)
c.UserData = "server"
}
func (h *myHandler) OnConnect(c *xtcp.Conn) {
logger.Log(xtcp.Info, "connected : ", c)
c.UserData = "client"
}
func (h *myHandler) OnRecv(c *xtcp.Conn, p xtcp.Packet) {
if protobufPacket, ok := p.(*ProtobufPacket); ok {
proc := mapProcessor[proto.MessageName(protobufPacket.Msg)]
if proc != nil {
proc(c, protobufPacket.Msg)
} else {
logger.Log(xtcp.Info, "no processor for Packet : ", p)
}
}
}
func (h *myHandler) OnUnpackErr(c *xtcp.Conn, buf []byte, err error) {
}
func (h *myHandler) OnClose(c *xtcp.Conn) {
logger.Logf(xtcp.Info, "close : %v", c.RawConn.RemoteAddr())
}
func main() {
h := &myHandler{}
opts := xtcp.NewOpts(h, protocol)
l, err := net.Listen("tcp", ":")
if err != nil {
logger.Log(xtcp.Info, "listen err : ", err)
return
}
server := xtcp.NewServer(opts)
go func() {
server.Serve(l)
}()
client := xtcp.NewConn(opts)
clientClosed := make(chan struct{})
go func() {
err := client.DialAndServe(l.Addr().String())
if err != nil {
logger.Log(xtcp.Info, "client dial err : ", err)
}
close(clientClosed)
}()
// send login request.
msgLoginRequest := &exampleproto.LoginRequest{
Account: proto.String("123"),
Password: proto.String("456"),
}
client.SendPacket(NewProtobufPacket(msgLoginRequest))
<-clientClosed
server.Stop(xtcp.StopGracefullyAndWait)
logger.Log(xtcp.Info, "server and client stoped. thanks.")
}