/
main.go
130 lines (96 loc) · 2.72 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
package main
import (
"fmt"
"github.com/toophy/chat_client/proto"
"github.com/toophy/toogo"
)
// 主线程
type MasterThread struct {
toogo.Thread
}
// 首次运行
func (this *MasterThread) On_firstRun() {
}
// 响应线程最先运行
func (this *MasterThread) On_preRun() {
// 处理各种最先处理的问题
}
// 响应线程运行
func (this *MasterThread) On_run() {
}
// 响应线程退出
func (this *MasterThread) On_end() {
}
// 响应网络事件
func (this *MasterThread) On_netEvent(m *toogo.Tmsg_net) bool {
name_fix := m.Name
if len(name_fix) == 0 {
name_fix = fmt.Sprintf("Conn[%d]", m.SessionId)
}
switch m.Msg {
case "listen failed":
this.LogFatal("%s : Listen failed[%s]", name_fix, m.Info)
case "listen ok":
this.LogInfo("%s : Listen(%s) ok.", name_fix, toogo.GetSessionById(m.SessionId).GetIPAddress())
case "accept failed":
this.LogFatal(m.Info)
return false
case "accept ok":
this.LogDebug("%s : Accept ok", name_fix)
case "connect failed":
this.LogError("%s : Connect failed[%s]", name_fix, m.Info)
case "connect ok":
this.LogDebug("%s : Connect ok", name_fix)
p := toogo.NewPacket(128, m.SessionId)
msgLogin := new(proto.C2G_login)
msgLogin.Account = "liusl"
msgLogin.Time = 123
msgLogin.Sign = "wokao"
msgLogin.Write(p)
this.LogInfo("send C2G_login")
toogo.SendPacket(p)
case "read failed":
this.LogError("%s : Connect read[%s]", name_fix, m.Info)
case "pre close":
this.LogDebug("%s : Connect pre close", name_fix)
case "close failed":
this.LogError("%s : Connect close failed[%s]", name_fix, m.Info)
case "close ok":
this.LogDebug("%s : Connect close ok.", name_fix)
}
return true
}
// -- 当网络消息包解析出现问题, 如何处理?
func (this *MasterThread) On_packetError(sessionId uint64) {
toogo.CloseSession(this.Get_thread_id(), sessionId)
}
// 注册消息
func (this *MasterThread) On_registNetMsg() {
this.RegistNetMsg(proto.G2C_login_ret_Id, this.on_g2c_login_ret)
this.RegistNetMsg(proto.S2C_chat_Id, this.on_s2c_chat)
}
func (this *MasterThread) on_g2c_login_ret(pack *toogo.PacketReader, sessionId uint64) bool {
msg := new(proto.G2C_login_ret)
msg.Read(pack)
this.LogInfo("on_g2c_login_ret")
p := toogo.NewPacket(128, sessionId)
if p != nil {
msgSend := new(proto.C2S_chat)
msgSend.Channel = 1
msgSend.Data = "你好,世界!"
msgSend.Write(p)
toogo.SendPacket(p)
}
return true
}
func (this *MasterThread) on_s2c_chat(pack *toogo.PacketReader, sessionId uint64) bool {
msg := new(proto.S2C_chat)
msg.Read(pack)
this.LogInfo("Chat : %s", msg.Data)
return true
}
func main() {
main_thread := new(MasterThread)
main_thread.Init_thread(main_thread, toogo.Tid_master, "master", 1000, 100, 10000)
toogo.Run(main_thread)
}