#### 9.5 使用Zinx-V0.9完成應用程序
好了,現在我們基本上已經將全部的連接管理的功能集成到Zinx中了,接下來就需要測試一下鏈接管理模塊是否可以使用了。
寫一個服務端:
> Server.go
```go
package main
import (
"fmt"
"zinx/ziface"
"zinx/znet"
)
//ping test 自定義路由
type PingRouter struct {
znet.BaseRouter
}
//Ping Handle
func (this *PingRouter) Handle(request ziface.IRequest) {
fmt.Println("Call PingRouter Handle")
//先讀取客戶端的數據,再回寫ping...ping...ping
fmt.Println("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData()))
err := request.GetConnection().SendBuffMsg(0, []byte("ping...ping...ping"))
if err != nil {
fmt.Println(err)
}
}
type HelloZinxRouter struct {
znet.BaseRouter
}
//HelloZinxRouter Handle
func (this *HelloZinxRouter) Handle(request ziface.IRequest) {
fmt.Println("Call HelloZinxRouter Handle")
//先讀取客戶端的數據,再回寫ping...ping...ping
fmt.Println("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData()))
err := request.GetConnection().SendBuffMsg(1, []byte("Hello Zinx Router V0.8"))
if err != nil {
fmt.Println(err)
}
}
//創建連接的時候執行
func DoConnectionBegin(conn ziface.IConnection) {
fmt.Println("DoConnecionBegin is Called ... ")
err := conn.SendMsg(2, []byte("DoConnection BEGIN..."))
if err != nil {
fmt.Println(err)
}
}
//連接斷開的時候執行
func DoConnectionLost(conn ziface.IConnection) {
fmt.Println("DoConneciotnLost is Called ... ")
}
func main() {
//創建一個server句柄
s := znet.NewServer()
//注冊鏈接hook回調函數
s.SetOnConnStart(DoConnectionBegin)
s.SetOnConnStop(DoConnectionLost)
//配置路由
s.AddRouter(0, &PingRouter{})
s.AddRouter(1, &HelloZinxRouter{})
//開啟服務
s.Serve()
}
```
我們這里注冊了兩個Hook函數一個是鏈接初始化之后`DoConnectionBegin()`和鏈接停止之前`DoConnectionLost()`。
`DoConnectionBegin()`會發給客戶端一個消息2的文本,并且在服務端打印一個調試信息"DoConnecionBegin is Called ... "
`DoConnectionLost()`在服務端打印一個調試信息"DoConneciotnLost is Called ... "
客戶端:
> Client.go
```go
package main
import (
"fmt"
"io"
"net"
"time"
"zinx/znet"
)
/*
模擬客戶端
*/
func main() {
fmt.Println("Client Test ... start")
//3秒之后發起測試請求,給服務端開啟服務的機會
time.Sleep(3 * time.Second)
conn,err := net.Dial("tcp", "127.0.0.1:7777")
if err != nil {
fmt.Println("client start err, exit!")
return
}
for {
//發封包message消息
dp := znet.NewDataPack()
msg, _ := dp.Pack(znet.NewMsgPackage(0,[]byte("Zinx V0.8 Client0 Test Message")))
_, err := conn.Write(msg)
if err !=nil {
fmt.Println("write error err ", err)
return
}
//先讀出流中的head部分
headData := make([]byte, dp.GetHeadLen())
_, err = io.ReadFull(conn, headData) //ReadFull 會把msg填充滿為止
if err != nil {
fmt.Println("read head error")
break
}
//將headData字節流 拆包到msg中
msgHead, err := dp.Unpack(headData)
if err != nil {
fmt.Println("server unpack err:", err)
return
}
if msgHead.GetDataLen() > 0 {
//msg 是有data數據的,需要再次讀取data數據
msg := msgHead.(*znet.Message)
msg.Data = make([]byte, msg.GetDataLen())
//根據dataLen從io中讀取字節流
_, err := io.ReadFull(conn, msg.Data)
if err != nil {
fmt.Println("server unpack data err:", err)
return
}
fmt.Println("==> Recv Msg: ID=", msg.Id, ", len=", msg.DataLen, ", data=", string(msg.Data))
}
time.Sleep(1*time.Second)
}
}
```
代碼不變。
啟動服務端
```
$go run Server.go
```
啟動客戶端
```
$go run Client.go
```
服務端結果:
```bash
$ go run Server.go
Add api msgId = 0
Add api msgId = 1
[START] Server name: zinx v-0.8 demoApp,listenner at IP: 127.0.0.1, Port 7777 is starting
[Zinx] Version: V0.4, MaxConn: 3, MaxPacketSize: 4096
start Zinx server zinx v-0.8 demoApp succ, now listenning...
Worker ID = 9 is started.
Worker ID = 5 is started.
Worker ID = 6 is started.
Worker ID = 7 is started.
Worker ID = 8 is started.
Worker ID = 1 is started.
Worker ID = 0 is started.
Worker ID = 2 is started.
Worker ID = 3 is started.
Worker ID = 4 is started.
connection add to ConnManager successfully: conn num = 1
---> CallOnConnStart....
DoConnecionBegin is Called ...
[Writer Goroutine is running]
[Reader Goroutine is running]
Add ConnID= 0 request msgID= 0 to workerID= 0
Call PingRouter Handle
recv from client : msgId= 0 , data= Zinx V0.8 Client0 Test Message
Add ConnID= 0 request msgID= 0 to workerID= 0
Call PingRouter Handle
recv from client : msgId= 0 , data= Zinx V0.8 Client0 Test Message
Add ConnID= 0 request msgID= 0 to workerID= 0
Call PingRouter Handle
recv from client : msgId= 0 , data= Zinx V0.8 Client0 Test Message
Add ConnID= 0 request msgID= 0 to workerID= 0
Call PingRouter Handle
recv from client : msgId= 0 , data= Zinx V0.8 Client0 Test Message
Add ConnID= 0 request msgID= 0 to workerID= 0
Call PingRouter Handle
recv from client : msgId= 0 , data= Zinx V0.8 Client0 Test Message
read msg head error read tcp4 127.0.0.1:7777->127.0.0.1:49510: read: connection reset by peer
Conn Stop()...ConnID = 0
---> CallOnConnStop....
DoConneciotnLost is Called ...
connection Remove ConnID= 0 successfully: conn num = 0
127.0.0.1:49510 [conn Reader exit!]
127.0.0.1:49510 [conn Writer exit!]
```
客戶端結果:
```bash
$ go run Client0.go
Client Test ... start
==> Recv Msg: ID= 2 , len= 21 , data= DoConnection BEGIN...
==> Recv Msg: ID= 0 , len= 18 , data= ping...ping...ping
==> Recv Msg: ID= 0 , len= 18 , data= ping...ping...ping
==> Recv Msg: ID= 0 , len= 18 , data= ping...ping...ping
==> Recv Msg: ID= 0 , len= 18 , data= ping...ping...ping
^Csignal: interrupt
```
客戶端創建成功,回調Hook已經執行,并且Conn被添加到ConnManager 中, conn num = 1,
當我們手動CTRL+C 關閉客戶端的時候, 服務器ConnManager已經成功將Conn摘掉,conn num = 0.
同時服務端也打印出 conn停止之后的回調信息。
- 一、引言
- 1、寫在前面
- 2、初探Zinx架構
- 二、初識Zinx框架
- 1. Zinx-V0.1-基礎Server
- 2.Zinx-V0.2-簡單的連接封裝與業務綁定
- 三、Zinx框架基礎路由模塊
- 3.1 IRequest 消息請求抽象類
- 3.2 IRouter 路由配置抽象類
- 3.3 Zinx-V0.3-集成簡單路由功能
- 3.4 Zinx-V0.3代碼實現
- 3.5 使用Zinx-V0.3完成應用程序
- 四、Zinx的全局配置
- 4.1 Zinx-V0.4增添全局配置代碼實現
- 4.2 使用Zinx-V0.4完成應用程序
- 五、Zinx的消息封裝
- 5.1 創建消息封裝類型
- 5.2 消息的封包與拆包
- 5.3 Zinx-V0.5代碼實現
- 5.4 使用Zinx-V0.5完成應用程序
- 六、Zinx的多路由模式
- 6.1 創建消息管理模塊
- 6.2 Zinx-V0.6代碼實現
- 6.3 使用Zinx-V0.6完成應用程序
- 七、Zinx的讀寫分離模型
- 7.1 Zinx-V0.7代碼實現
- 7.2 使用Zinx-V0.7完成應用程序
- 八、Zinx的消息隊列及多任務機制
- 8.1 創建消息隊列
- 8.2 創建及啟動Worker工作池
- 8.3 發送消息給消息隊列
- 8.4 Zinx-V0.8代碼實現
- 8.5 使用Zinx-V0.8完成應用程序
- 九、Zinx的鏈接管理
- 9.1 創建鏈接管理模塊
- 9.2 鏈接管理模塊集成到Zinx中
- 9.3 鏈接的帶緩沖的發包方法
- 9.4 注冊鏈接啟動/停止自定義Hook方法功能
- 9.5 使用Zinx-V0.9完成應用程序
- 十、Zinx的連接屬性設置
- 10.1 給鏈接添加鏈接配置接口
- 10.2 鏈接屬性方法實現
- 10.3 鏈接屬性Zinx-V0.10單元測試
- 基于Zinx的應用案例
- 一、應用案例介紹
- 二、服務器應用基礎協議
- 三、MMO多人在線游戲AOI算法
- 3.1 網絡法實現AOI算法
- 3.2 實現AOI格子結構
- 3.3 實現AOI管理模塊
- 3.4 求出九宮格
- 3.5 AOI格子添加刪除操作
- 3.6 AOI模塊單元測試
- 四、數據傳輸協議protocol buffer
- 4.1 簡介
- 4.2 數據交換格式
- 4.3 protobuf環境安裝
- 4.4 protobuf語法
- 4.5 編譯protobuf
- 4.6 利用protobuf生成的類來編碼
- 五、MMO游戲的Proto3協議
- 六、構建項目與用戶上線
- 6.1 構建項目
- 6.2用戶上線流程
- 七、世界聊天系統實現
- 7.1 世界管理模塊
- 7.2 世界聊天系統實現
- 八、上線位置信息同步
- 九、移動位置與AOI廣播(未跨越格子)
- 十、玩家下線
- 十一、移動與AOI廣播(跨越格子)