Browse Source

添加zinx-V0.1~zinx-V.10版本教程迭代example案例及对应的zinx包

master
aceld 6 years ago
parent
commit
2a69b8ea2d
  1. BIN
      examples/zinx_release/zinx-v0.1.tar.gz
  2. BIN
      examples/zinx_release/zinx-v0.2.tar.gz
  3. BIN
      examples/zinx_release/zinx-v0.3.tar.gz
  4. BIN
      examples/zinx_release/zinx-v0.4.tar.gz
  5. BIN
      examples/zinx_release/zinx-v0.5.tar.gz
  6. BIN
      examples/zinx_release/zinx-v0.6.tar.gz
  7. BIN
      examples/zinx_release/zinx-v0.7.tar.gz
  8. BIN
      examples/zinx_release/zinx-v0.8.tar.gz
  9. BIN
      examples/zinx_release/zinx-v0.9-0.10.tar.gz
  10. 67
      examples/zinx_version_ex/ZinxV0.10Test/Client0.go
  11. 67
      examples/zinx_version_ex/ZinxV0.10Test/Client1.go
  12. 85
      examples/zinx_version_ex/ZinxV0.10Test/Server.go
  13. 7
      examples/zinx_version_ex/ZinxV0.10Test/conf/zinx.json
  14. 67
      examples/zinx_version_ex/ZinxV0.11Test/Client0.go
  15. 67
      examples/zinx_version_ex/ZinxV0.11Test/Client1.go
  16. 86
      examples/zinx_version_ex/ZinxV0.11Test/Server.go
  17. 9
      examples/zinx_version_ex/ZinxV0.11Test/conf/zinx.json
  18. 30
      examples/zinx_version_ex/ZinxV0.11Test/mylog/zinx.log
  19. 42
      examples/zinx_version_ex/ZinxV0.1Test/Client.go
  20. 21
      examples/zinx_version_ex/ZinxV0.1Test/Server.go
  21. 42
      examples/zinx_version_ex/ZinxV0.2Test/Client.go
  22. 21
      examples/zinx_version_ex/ZinxV0.2Test/Server.go
  23. 42
      examples/zinx_version_ex/ZinxV0.3Test/Client.go
  24. 48
      examples/zinx_version_ex/ZinxV0.3Test/Server.go
  25. 42
      examples/zinx_version_ex/ZinxV0.4Test/Client.go
  26. 50
      examples/zinx_version_ex/ZinxV0.4Test/Server.go
  27. 6
      examples/zinx_version_ex/ZinxV0.4Test/conf/zinx.json
  28. 67
      examples/zinx_version_ex/ZinxV0.5Test/Client.go
  29. 42
      examples/zinx_version_ex/ZinxV0.5Test/Server.go
  30. 6
      examples/zinx_version_ex/ZinxV0.5Test/conf/zinx.json
  31. 67
      examples/zinx_version_ex/ZinxV0.6Test-V0.7Test/Client0.go
  32. 67
      examples/zinx_version_ex/ZinxV0.6Test-V0.7Test/Client1.go
  33. 52
      examples/zinx_version_ex/ZinxV0.6Test-V0.7Test/Server.go
  34. 6
      examples/zinx_version_ex/ZinxV0.6Test-V0.7Test/conf/zinx.json
  35. 67
      examples/zinx_version_ex/ZinxV0.8Test/Client0.go
  36. 67
      examples/zinx_version_ex/ZinxV0.8Test/Client1.go
  37. 52
      examples/zinx_version_ex/ZinxV0.8Test/Server.go
  38. 7
      examples/zinx_version_ex/ZinxV0.8Test/conf/zinx.json
  39. 67
      examples/zinx_version_ex/ZinxV0.9Test/Client0.go
  40. 67
      examples/zinx_version_ex/ZinxV0.9Test/Client1.go
  41. 70
      examples/zinx_version_ex/ZinxV0.9Test/Server.go
  42. 7
      examples/zinx_version_ex/ZinxV0.9Test/conf/zinx.json
  43. 54
      examples/zinx_version_ex/datapackDemo/Client.go
  44. 66
      examples/zinx_version_ex/datapackDemo/Server.go
  45. 41
      examples/zinx_version_ex/protoDemo/main.go
  46. 188
      examples/zinx_version_ex/protoDemo/pb/Person.pb.go
  47. 23
      examples/zinx_version_ex/protoDemo/pb/Person.proto

BIN
examples/zinx_release/zinx-v0.1.tar.gz

BIN
examples/zinx_release/zinx-v0.2.tar.gz

BIN
examples/zinx_release/zinx-v0.3.tar.gz

BIN
examples/zinx_release/zinx-v0.4.tar.gz

BIN
examples/zinx_release/zinx-v0.5.tar.gz

BIN
examples/zinx_release/zinx-v0.6.tar.gz

BIN
examples/zinx_release/zinx-v0.7.tar.gz

BIN
examples/zinx_release/zinx-v0.8.tar.gz

BIN
examples/zinx_release/zinx-v0.9-0.10.tar.gz

67
examples/zinx_version_ex/ZinxV0.10Test/Client0.go

@ -0,0 +1,67 @@
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)
}
}

67
examples/zinx_version_ex/ZinxV0.10Test/Client1.go

@ -0,0 +1,67 @@
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 n := 3; n >= 0; n-- {
//发封包message消息
dp := znet.NewDataPack()
msg, _ := dp.Pack(znet.NewMsgPackage(1,[]byte("Zinx V0.8 Client1 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)
}
}

85
examples/zinx_version_ex/ZinxV0.10Test/Server.go

@ -0,0 +1,85 @@
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.10"))
if err != nil {
fmt.Println(err)
}
}
//创建连接的时候执行
func DoConnectionBegin(conn ziface.IConnection) {
fmt.Println("DoConnecionBegin is Called ... ")
//设置两个链接属性,在连接创建之后
fmt.Println("Set conn Name, Home done!")
conn.SetProperty("Name", "Aceld")
conn.SetProperty("Home", "https://www.jianshu.com/u/35261429b7f1")
err := conn.SendMsg(2, []byte("DoConnection BEGIN..."))
if err != nil {
fmt.Println(err)
}
}
//连接断开的时候执行
func DoConnectionLost(conn ziface.IConnection) {
//在连接销毁之前,查询conn的Name,Home属性
if name, err:= conn.GetProperty("Name"); err == nil {
fmt.Println("Conn Property Name = ", name)
}
if home, err := conn.GetProperty("Home"); err == nil {
fmt.Println("Conn Property Home = ", home)
}
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()
}

7
examples/zinx_version_ex/ZinxV0.10Test/conf/zinx.json

@ -0,0 +1,7 @@
{
"Name":"zinx v-0.10 demoApp",
"Host":"127.0.0.1",
"TcpPort":7777,
"MaxConn":3,
"WorkerPoolSize":10
}

67
examples/zinx_version_ex/ZinxV0.11Test/Client0.go

@ -0,0 +1,67 @@
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)
}
}

67
examples/zinx_version_ex/ZinxV0.11Test/Client1.go

@ -0,0 +1,67 @@
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 n := 3; n >= 0; n-- {
//发封包message消息
dp := znet.NewDataPack()
msg, _ := dp.Pack(znet.NewMsgPackage(1,[]byte("Zinx V0.8 Client1 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)
}
}

86
examples/zinx_version_ex/ZinxV0.11Test/Server.go

@ -0,0 +1,86 @@
package main
import (
"zinx/ziface"
"zinx/zlog"
"zinx/znet"
)
//ping test 自定义路由
type PingRouter struct {
znet.BaseRouter
}
//Ping Handle
func (this *PingRouter) Handle(request ziface.IRequest) {
zlog.Debug("Call PingRouter Handle")
//先读取客户端的数据,再回写ping...ping...ping
zlog.Debug("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData()))
err := request.GetConnection().SendBuffMsg(0, []byte("ping...ping...ping"))
if err != nil {
zlog.Error(err)
}
}
type HelloZinxRouter struct {
znet.BaseRouter
}
//HelloZinxRouter Handle
func (this *HelloZinxRouter) Handle(request ziface.IRequest) {
zlog.Debug("Call HelloZinxRouter Handle")
//先读取客户端的数据,再回写ping...ping...ping
zlog.Debug("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData()))
err := request.GetConnection().SendBuffMsg(1, []byte("Hello Zinx Router V0.10"))
if err != nil {
zlog.Error(err)
}
}
//创建连接的时候执行
func DoConnectionBegin(conn ziface.IConnection) {
zlog.Debug("DoConnecionBegin is Called ... ")
//设置两个链接属性,在连接创建之后
zlog.Debug("Set conn Name, Home done!")
conn.SetProperty("Name", "Aceld")
conn.SetProperty("Home", "https://www.jianshu.com/u/35261429b7f1")
err := conn.SendMsg(2, []byte("DoConnection BEGIN..."))
if err != nil {
zlog.Error(err)
}
}
//连接断开的时候执行
func DoConnectionLost(conn ziface.IConnection) {
//在连接销毁之前,查询conn的Name,Home属性
if name, err:= conn.GetProperty("Name"); err == nil {
zlog.Error("Conn Property Name = ", name)
}
if home, err := conn.GetProperty("Home"); err == nil {
zlog.Error("Conn Property Home = ", home)
}
zlog.Debug("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()
}

9
examples/zinx_version_ex/ZinxV0.11Test/conf/zinx.json

@ -0,0 +1,9 @@
{
"Name":"zinx v-0.10 demoApp",
"Host":"127.0.0.1",
"TcpPort":7777,
"MaxConn":3,
"WorkerPoolSize":10,
"LogDir": "./mylog",
"LogFile":"zinx.log"
}

30
examples/zinx_version_ex/ZinxV0.11Test/mylog/zinx.log

@ -0,0 +1,30 @@
2019/04/24 18:26:02 [DEBUG]stdzlog.go:53: DoConnecionBegin is Called ...
2019/04/24 18:26:02 [DEBUG]stdzlog.go:53: Set conn Name, Home done!
2019/04/24 18:26:02 [DEBUG]stdzlog.go:53: Call PingRouter Handle
2019/04/24 18:26:02 [DEBUG]stdzlog.go:53: recv from client : msgId= 0 , data= Zinx V0.8 Client0 Test Message
2019/04/24 18:26:03 [DEBUG]stdzlog.go:53: Call PingRouter Handle
2019/04/24 18:26:03 [DEBUG]stdzlog.go:53: recv from client : msgId= 0 , data= Zinx V0.8 Client0 Test Message
2019/04/24 18:26:04 [DEBUG]stdzlog.go:53: Call PingRouter Handle
2019/04/24 18:26:04 [DEBUG]stdzlog.go:53: recv from client : msgId= 0 , data= Zinx V0.8 Client0 Test Message
2019/04/24 18:26:05 [DEBUG]stdzlog.go:53: Call PingRouter Handle
2019/04/24 18:26:05 [DEBUG]stdzlog.go:53: recv from client : msgId= 0 , data= Zinx V0.8 Client0 Test Message
2019/04/24 18:26:06 [ERROR]stdzlog.go:80: Conn Property Name = Aceld
2019/04/24 18:26:06 [ERROR]stdzlog.go:80: Conn Property Home = https://www.jianshu.com/u/35261429b7f1
2019/04/24 18:26:06 [DEBUG]stdzlog.go:53: DoConneciotnLost is Called ...
2019/04/24 18:34:07 [DEBUG]stdzlog.go:53: DoConnecionBegin is Called ...
2019/04/24 18:34:07 [DEBUG]stdzlog.go:53: Set conn Name, Home done!
2019/04/24 18:34:07 [DEBUG]stdzlog.go:53: Call PingRouter Handle
2019/04/24 18:34:07 [DEBUG]stdzlog.go:53: recv from client : msgId= 0 , data= Zinx V0.8 Client0 Test Message
2019/04/24 18:34:08 [DEBUG]stdzlog.go:53: Call PingRouter Handle
2019/04/24 18:34:08 [DEBUG]stdzlog.go:53: recv from client : msgId= 0 , data= Zinx V0.8 Client0 Test Message
2019/04/24 18:34:09 [DEBUG]stdzlog.go:53: Call PingRouter Handle
2019/04/24 18:34:09 [DEBUG]stdzlog.go:53: recv from client : msgId= 0 , data= Zinx V0.8 Client0 Test Message
2019/04/24 18:34:10 [DEBUG]stdzlog.go:53: Call PingRouter Handle
2019/04/24 18:34:10 [DEBUG]stdzlog.go:53: recv from client : msgId= 0 , data= Zinx V0.8 Client0 Test Message
2019/04/24 18:34:11 [DEBUG]stdzlog.go:53: Call PingRouter Handle
2019/04/24 18:34:11 [DEBUG]stdzlog.go:53: recv from client : msgId= 0 , data= Zinx V0.8 Client0 Test Message
2019/04/24 18:34:12 [DEBUG]stdzlog.go:53: Call PingRouter Handle
2019/04/24 18:34:12 [DEBUG]stdzlog.go:53: recv from client : msgId= 0 , data= Zinx V0.8 Client0 Test Message
2019/04/24 18:34:12 [ERROR]stdzlog.go:80: Conn Property Name = Aceld
2019/04/24 18:34:12 [ERROR]stdzlog.go:80: Conn Property Home = https://www.jianshu.com/u/35261429b7f1
2019/04/24 18:34:12 [DEBUG]stdzlog.go:53: DoConneciotnLost is Called ...

42
examples/zinx_version_ex/ZinxV0.1Test/Client.go

@ -0,0 +1,42 @@
package main
import (
"fmt"
"net"
"time"
)
/*
模拟客户端
*/
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 {
_, err := conn.Write([]byte("hahaha"))
if err !=nil {
fmt.Println("write error err ", err)
return
}
buf :=make([]byte, 512)
cnt, err := conn.Read(buf)
if err != nil {
fmt.Println("read buf error ")
return
}
fmt.Printf(" server call back : %s, cnt = %d\n", buf, cnt)
time.Sleep(1*time.Second)
}
}

21
examples/zinx_version_ex/ZinxV0.1Test/Server.go

@ -0,0 +1,21 @@
package main
import (
"zinx/znet"
)
//Server 模块的测试函数
func main() {
/*
服务端测试
*/
//1 创建一个server 句柄 s
s := znet.NewServer("[zinx V0.1]")
//2 开启服务
s.Serve()
}

42
examples/zinx_version_ex/ZinxV0.2Test/Client.go

@ -0,0 +1,42 @@
package main
import (
"fmt"
"net"
"time"
)
/*
模拟客户端
*/
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 {
_, err := conn.Write([]byte("hahaha"))
if err !=nil {
fmt.Println("write error err ", err)
return
}
buf :=make([]byte, 512)
cnt, err := conn.Read(buf)
if err != nil {
fmt.Println("read buf error ")
return
}
fmt.Printf(" server call back : %s, cnt = %d\n", buf, cnt)
time.Sleep(1*time.Second)
}
}

21
examples/zinx_version_ex/ZinxV0.2Test/Server.go

@ -0,0 +1,21 @@
package main
import (
"zinx/znet"
)
//Server 模块的测试函数
func main() {
/*
服务端测试
*/
//1 创建一个server 句柄 s
s := znet.NewServer("[zinx V0.1]")
//2 开启服务
s.Serve()
}

42
examples/zinx_version_ex/ZinxV0.3Test/Client.go

@ -0,0 +1,42 @@
package main
import (
"fmt"
"net"
"time"
)
/*
模拟客户端
*/
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 {
_, err := conn.Write([]byte("Zinx V0.3"))
if err !=nil {
fmt.Println("write error err ", err)
return
}
buf :=make([]byte, 512)
cnt, err := conn.Read(buf)
if err != nil {
fmt.Println("read buf error ")
return
}
fmt.Printf(" server call back : %s, cnt = %d\n", buf, cnt)
time.Sleep(1*time.Second)
}
}

48
examples/zinx_version_ex/ZinxV0.3Test/Server.go

@ -0,0 +1,48 @@
package main
import (
"fmt"
"zinx/ziface"
"zinx/znet"
)
//ping test 自定义路由
type PingRouter struct {
znet.BaseRouter
}
//Test PreHandle
func (this *PingRouter) PreHandle(request ziface.IRequest) {
fmt.Println("Call Router PreHandle")
_, err := request.GetConnection().GetTCPConnection().Write([]byte("before ping ....\n"))
if err !=nil {
fmt.Println("call back ping ping ping error")
}
}
//Test Handle
func (this *PingRouter) Handle(request ziface.IRequest) {
fmt.Println("Call PingRouter Handle")
_, err := request.GetConnection().GetTCPConnection().Write([]byte("ping...ping...ping\n"))
if err !=nil {
fmt.Println("call back ping ping ping error")
}
}
//Test PostHandle
func (this *PingRouter) PostHandle(request ziface.IRequest) {
fmt.Println("Call Router PostHandle")
_, err := request.GetConnection().GetTCPConnection().Write([]byte("After ping .....\n"))
if err !=nil {
fmt.Println("call back ping ping ping error")
}
}
func main(){
//创建一个server句柄
s := znet.NewServer("[zinx V0.3]")
s.AddRouter(&PingRouter{})
//2 开启服务
s.Serve()
}

42
examples/zinx_version_ex/ZinxV0.4Test/Client.go

@ -0,0 +1,42 @@
package main
import (
"fmt"
"net"
"time"
)
/*
模拟客户端
*/
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 {
_, err := conn.Write([]byte("Zinx V0.3"))
if err !=nil {
fmt.Println("write error err ", err)
return
}
buf :=make([]byte, 512)
cnt, err := conn.Read(buf)
if err != nil {
fmt.Println("read buf error ")
return
}
fmt.Printf(" server call back : %s, cnt = %d\n", buf, cnt)
time.Sleep(1*time.Second)
}
}

50
examples/zinx_version_ex/ZinxV0.4Test/Server.go

@ -0,0 +1,50 @@
package main
import (
"fmt"
"zinx/ziface"
"zinx/znet"
)
//ping test 自定义路由
type PingRouter struct {
znet.BaseRouter
}
//Test PreHandle
func (this *PingRouter) PreHandle(request ziface.IRequest) {
fmt.Println("Call Router PreHandle")
_, err := request.GetConnection().GetTCPConnection().Write([]byte("before ping ....\n"))
if err != nil {
fmt.Println("call back ping ping ping error")
}
}
//Test Handle
func (this *PingRouter) Handle(request ziface.IRequest) {
fmt.Println("Call PingRouter Handle")
_, err := request.GetConnection().GetTCPConnection().Write([]byte("ping...ping...ping\n"))
if err != nil {
fmt.Println("call back ping ping ping error")
}
}
//Test PostHandle
func (this *PingRouter) PostHandle(request ziface.IRequest) {
fmt.Println("Call Router PostHandle")
_, err := request.GetConnection().GetTCPConnection().Write([]byte("After ping .....\n"))
if err != nil {
fmt.Println("call back ping ping ping error")
}
}
func main() {
//创建一个server句柄
s := znet.NewServer()
//配置路由
s.AddRouter(&PingRouter{})
//开启服务
s.Serve()
}

6
examples/zinx_version_ex/ZinxV0.4Test/conf/zinx.json

@ -0,0 +1,6 @@
{
"Name":"zinx v-0.4 demoApp",
"Host":"127.0.0.1",
"TcpPort":7777,
"MaxConn":3
}

67
examples/zinx_version_ex/ZinxV0.5Test/Client.go

@ -0,0 +1,67 @@
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.5 Client 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)
}
}

42
examples/zinx_version_ex/ZinxV0.5Test/Server.go

@ -0,0 +1,42 @@
package main
import (
"fmt"
"zinx/ziface"
"zinx/znet"
)
//ping test 自定义路由
type PingRouter struct {
znet.BaseRouter
}
//Test 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().GetTCPConnection().Write([]byte("ping...ping...ping\n"))
if err != nil {
fmt.Println("call back ping ping ping error")
}
*/
err := request.GetConnection().SendMsg(1, []byte("ping...ping...ping"))
if err != nil {
fmt.Println(err)
}
}
func main() {
//创建一个server句柄
s := znet.NewServer()
//配置路由
s.AddRouter(&PingRouter{})
//开启服务
s.Serve()
}

6
examples/zinx_version_ex/ZinxV0.5Test/conf/zinx.json

@ -0,0 +1,6 @@
{
"Name":"zinx v-0.5 demoApp",
"Host":"127.0.0.1",
"TcpPort":7777,
"MaxConn":3
}

67
examples/zinx_version_ex/ZinxV0.6Test-V0.7Test/Client0.go

@ -0,0 +1,67 @@
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.6 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)
}
}

67
examples/zinx_version_ex/ZinxV0.6Test-V0.7Test/Client1.go

@ -0,0 +1,67 @@
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(1,[]byte("Zinx V0.6 Client1 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)
}
}

52
examples/zinx_version_ex/ZinxV0.6Test-V0.7Test/Server.go

@ -0,0 +1,52 @@
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().SendMsg(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().SendMsg(1, []byte("Hello Zinx Router V0.6"))
if err != nil {
fmt.Println(err)
}
}
func main() {
//创建一个server句柄
s := znet.NewServer()
//配置路由
s.AddRouter(0, &PingRouter{})
s.AddRouter(1, &HelloZinxRouter{})
//开启服务
s.Serve()
}

6
examples/zinx_version_ex/ZinxV0.6Test-V0.7Test/conf/zinx.json

@ -0,0 +1,6 @@
{
"Name":"zinx v-0.6 demoApp",
"Host":"127.0.0.1",
"TcpPort":7777,
"MaxConn":3
}

67
examples/zinx_version_ex/ZinxV0.8Test/Client0.go

@ -0,0 +1,67 @@
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)
}
}

67
examples/zinx_version_ex/ZinxV0.8Test/Client1.go

@ -0,0 +1,67 @@
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(1,[]byte("Zinx V0.8 Client1 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)
}
}

52
examples/zinx_version_ex/ZinxV0.8Test/Server.go

@ -0,0 +1,52 @@
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().SendMsg(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().SendMsg(1, []byte("Hello Zinx Router V0.8"))
if err != nil {
fmt.Println(err)
}
}
func main() {
//创建一个server句柄
s := znet.NewServer()
//配置路由
s.AddRouter(0, &PingRouter{})
s.AddRouter(1, &HelloZinxRouter{})
//开启服务
s.Serve()
}

7
examples/zinx_version_ex/ZinxV0.8Test/conf/zinx.json

@ -0,0 +1,7 @@
{
"Name":"zinx v-0.8 demoApp",
"Host":"127.0.0.1",
"TcpPort":7777,
"MaxConn":3,
"WorkerPoolSize":10
}

67
examples/zinx_version_ex/ZinxV0.9Test/Client0.go

@ -0,0 +1,67 @@
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)
}
}

67
examples/zinx_version_ex/ZinxV0.9Test/Client1.go

@ -0,0 +1,67 @@
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 n := 3; n >= 0; n-- {
//发封包message消息
dp := znet.NewDataPack()
msg, _ := dp.Pack(znet.NewMsgPackage(1,[]byte("Zinx V0.8 Client1 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)
}
}

70
examples/zinx_version_ex/ZinxV0.9Test/Server.go

@ -0,0 +1,70 @@
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()
}

7
examples/zinx_version_ex/ZinxV0.9Test/conf/zinx.json

@ -0,0 +1,7 @@
{
"Name":"zinx v-0.8 demoApp",
"Host":"127.0.0.1",
"TcpPort":7777,
"MaxConn":3,
"WorkerPoolSize":10
}

54
examples/zinx_version_ex/datapackDemo/Client.go

@ -0,0 +1,54 @@
package main
import (
"fmt"
"net"
"zinx/znet"
)
func main() {
//客户端goroutine,负责模拟粘包的数据,然后进行发送
conn, err := net.Dial("tcp", "127.0.0.1:7777")
if err != nil {
fmt.Println("client dial err:", err)
return
}
//创建一个封包对象 dp
dp := znet.NewDataPack()
//封装一个msg1包
msg1 := &znet.Message{
Id: 0,
DataLen: 5,
Data: []byte{'h', 'e', 'l', 'l', 'o'},
}
sendData1, err := dp.Pack(msg1)
if err != nil {
fmt.Println("client pack msg1 err:", err)
return
}
msg2 := &znet.Message{
Id: 1,
DataLen: 7,
Data: []byte{'w', 'o', 'r', 'l', 'd', '!', '!'},
}
sendData2, err := dp.Pack(msg2)
if err != nil {
fmt.Println("client temp msg2 err:", err)
return
}
//将sendData1,和 sendData2 拼接一起,组成粘包
sendData1 = append(sendData1, sendData2...)
//向服务器端写数据
conn.Write(sendData1)
//客户端阻塞
select {}
}

66
examples/zinx_version_ex/datapackDemo/Server.go

@ -0,0 +1,66 @@
package main
import (
"fmt"
"io"
"net"
"zinx/znet"
)
//只是负责测试datapack拆包,封包功能
func main() {
//创建socket TCP Server
listener, err := net.Listen("tcp", "127.0.0.1:7777")
if err != nil {
fmt.Println("server listen err:", err)
return
}
//创建服务器gotoutine,负责从客户端goroutine读取粘包的数据,然后进行解析
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("server accept err:", err)
}
//处理客户端请求
go func(conn net.Conn) {
//创建封包拆包对象dp
dp := znet.NewDataPack()
for {
//1 先读出流中的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))
}
}
}(conn)
}
//阻塞
select {}
}

41
examples/zinx_version_ex/protoDemo/main.go

@ -0,0 +1,41 @@
package main
import (
"fmt"
"github.com/golang/protobuf/proto"
"myDemo/protoDemo/pb"
)
func main() {
person := &pb.Person{
Name: "XiaoYuer",
Age: 16,
Emails: []string{"xiao_yu_er@sina.com", "yu_er@sina.cn"},
Phones: []*pb.PhoneNumber{
&pb.PhoneNumber{
Number: "13113111311",
Type: pb.PhoneType_MOBILE,
},
&pb.PhoneNumber{
Number: "14141444144",
Type: pb.PhoneType_HOME,
},
&pb.PhoneNumber{
Number: "19191919191",
Type: pb.PhoneType_WORK,
},
},
}
data, err := proto.Marshal(person)
if err != nil {
fmt.Println("marshal err:", err)
}
newdata := &pb.Person{}
err = proto.Unmarshal(data, newdata)
if err != nil {
fmt.Println("unmarshal err:", err)
}
fmt.Println(newdata)
}

188
examples/zinx_version_ex/protoDemo/pb/Person.pb.go

@ -0,0 +1,188 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: Person.proto
package pb
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
//enum为关键字,作用为定义一种枚举类型
type PhoneType int32
const (
PhoneType_MOBILE PhoneType = 0
PhoneType_HOME PhoneType = 1
PhoneType_WORK PhoneType = 2
)
var PhoneType_name = map[int32]string{
0: "MOBILE",
1: "HOME",
2: "WORK",
}
var PhoneType_value = map[string]int32{
"MOBILE": 0,
"HOME": 1,
"WORK": 2,
}
func (x PhoneType) String() string {
return proto.EnumName(PhoneType_name, int32(x))
}
func (PhoneType) EnumDescriptor() ([]byte, []int) {
return fileDescriptor_841ab6396175eaf3, []int{0}
}
//message为关键字,作用为定义一种消息类型
type Person struct {
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
Age int32 `protobuf:"varint,2,opt,name=age,proto3" json:"age,omitempty"`
Emails []string `protobuf:"bytes,3,rep,name=emails,proto3" json:"emails,omitempty"`
Phones []*PhoneNumber `protobuf:"bytes,4,rep,name=phones,proto3" json:"phones,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *Person) Reset() { *m = Person{} }
func (m *Person) String() string { return proto.CompactTextString(m) }
func (*Person) ProtoMessage() {}
func (*Person) Descriptor() ([]byte, []int) {
return fileDescriptor_841ab6396175eaf3, []int{0}
}
func (m *Person) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_Person.Unmarshal(m, b)
}
func (m *Person) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_Person.Marshal(b, m, deterministic)
}
func (m *Person) XXX_Merge(src proto.Message) {
xxx_messageInfo_Person.Merge(m, src)
}
func (m *Person) XXX_Size() int {
return xxx_messageInfo_Person.Size(m)
}
func (m *Person) XXX_DiscardUnknown() {
xxx_messageInfo_Person.DiscardUnknown(m)
}
var xxx_messageInfo_Person proto.InternalMessageInfo
func (m *Person) GetName() string {
if m != nil {
return m.Name
}
return ""
}
func (m *Person) GetAge() int32 {
if m != nil {
return m.Age
}
return 0
}
func (m *Person) GetEmails() []string {
if m != nil {
return m.Emails
}
return nil
}
func (m *Person) GetPhones() []*PhoneNumber {
if m != nil {
return m.Phones
}
return nil
}
//message为关键字,作用为定义一种消息类型可以被另外的消息类型嵌套使用
type PhoneNumber struct {
Number string `protobuf:"bytes,1,opt,name=number,proto3" json:"number,omitempty"`
Type PhoneType `protobuf:"varint,2,opt,name=type,proto3,enum=pb.PhoneType" json:"type,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *PhoneNumber) Reset() { *m = PhoneNumber{} }
func (m *PhoneNumber) String() string { return proto.CompactTextString(m) }
func (*PhoneNumber) ProtoMessage() {}
func (*PhoneNumber) Descriptor() ([]byte, []int) {
return fileDescriptor_841ab6396175eaf3, []int{1}
}
func (m *PhoneNumber) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_PhoneNumber.Unmarshal(m, b)
}
func (m *PhoneNumber) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_PhoneNumber.Marshal(b, m, deterministic)
}
func (m *PhoneNumber) XXX_Merge(src proto.Message) {
xxx_messageInfo_PhoneNumber.Merge(m, src)
}
func (m *PhoneNumber) XXX_Size() int {
return xxx_messageInfo_PhoneNumber.Size(m)
}
func (m *PhoneNumber) XXX_DiscardUnknown() {
xxx_messageInfo_PhoneNumber.DiscardUnknown(m)
}
var xxx_messageInfo_PhoneNumber proto.InternalMessageInfo
func (m *PhoneNumber) GetNumber() string {
if m != nil {
return m.Number
}
return ""
}
func (m *PhoneNumber) GetType() PhoneType {
if m != nil {
return m.Type
}
return PhoneType_MOBILE
}
func init() {
proto.RegisterEnum("pb.PhoneType", PhoneType_name, PhoneType_value)
proto.RegisterType((*Person)(nil), "pb.Person")
proto.RegisterType((*PhoneNumber)(nil), "pb.PhoneNumber")
}
func init() { proto.RegisterFile("Person.proto", fileDescriptor_841ab6396175eaf3) }
var fileDescriptor_841ab6396175eaf3 = []byte{
// 209 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x4c, 0x8f, 0xcd, 0x4a, 0xc4, 0x30,
0x14, 0x85, 0x4d, 0x13, 0x83, 0xbd, 0xe3, 0x4f, 0xb8, 0x0b, 0xc9, 0x32, 0xce, 0xc6, 0xa0, 0xd0,
0xc5, 0xf8, 0x06, 0xc2, 0xc0, 0x88, 0x8e, 0x1d, 0x82, 0xe0, 0xba, 0x81, 0xa0, 0x82, 0x4d, 0x42,
0x3b, 0x2e, 0xfa, 0xf6, 0x92, 0x34, 0x94, 0xd9, 0x7d, 0xf7, 0x3b, 0x70, 0x0e, 0x17, 0x2e, 0x0f,
0x6e, 0x18, 0x83, 0x6f, 0xe2, 0x10, 0x8e, 0x01, 0xab, 0x68, 0xd7, 0x01, 0xf8, 0xec, 0x10, 0x81,
0xf9, 0xae, 0x77, 0x92, 0x28, 0xa2, 0x6b, 0x93, 0x19, 0x05, 0xd0, 0xee, 0xcb, 0xc9, 0x4a, 0x11,
0x7d, 0x6e, 0x12, 0xe2, 0x2d, 0x70, 0xd7, 0x77, 0x3f, 0xbf, 0xa3, 0xa4, 0x8a, 0xea, 0xda, 0x94,
0x0b, 0xef, 0x81, 0xc7, 0xef, 0xe0, 0xdd, 0x28, 0x99, 0xa2, 0x7a, 0xb5, 0xb9, 0x69, 0xa2, 0x6d,
0x0e, 0xc9, 0xbc, 0xff, 0xf5, 0xd6, 0x0d, 0xa6, 0xc4, 0xeb, 0x1d, 0xac, 0x4e, 0x74, 0xea, 0xf3,
0x99, 0xca, 0x6e, 0xb9, 0xf0, 0x0e, 0xd8, 0x71, 0x8a, 0xf3, 0xf4, 0xf5, 0xe6, 0x6a, 0x69, 0xfb,
0x98, 0xa2, 0x33, 0x39, 0x7a, 0x78, 0x84, 0x7a, 0x51, 0x08, 0xc0, 0xf7, 0xed, 0xf3, 0xcb, 0xdb,
0x56, 0x9c, 0xe1, 0x05, 0xb0, 0x5d, 0xbb, 0xdf, 0x0a, 0x92, 0xe8, 0xb3, 0x35, 0xaf, 0xa2, 0xb2,
0x3c, 0xbf, 0xfc, 0xf4, 0x1f, 0x00, 0x00, 0xff, 0xff, 0xdf, 0x3c, 0xf1, 0xb8, 0x02, 0x01, 0x00,
0x00,
}

23
examples/zinx_version_ex/protoDemo/pb/Person.proto

@ -0,0 +1,23 @@
syntax = "proto3"; //
package pb; //go文件的包名
//message为关键字
message Person {
string name = 1; //
int32 age = 2; //
repeated string emails = 3; //repeated表示字段允许重复
repeated PhoneNumber phones = 4; //
}
//enum为关键字
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
//message为关键字使
message PhoneNumber {
string number = 1;
PhoneType type = 2;
}
Loading…
Cancel
Save