You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
3.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. package znet
  2. import (
  3. "fmt"
  4. "io"
  5. "net"
  6. "testing"
  7. "time"
  8. "github.com/aceld/zinx/ziface"
  9. )
  10. // run in terminal:
  11. // go test -v ./znet -run=TestServer
  12. /*
  13. 模拟客户端
  14. */
  15. func ClientTest(i uint32) {
  16. fmt.Println("Client Test ... start")
  17. //3秒之后发起测试请求,给服务端开启服务的机会
  18. time.Sleep(3 * time.Second)
  19. conn, err := net.Dial("tcp", "127.0.0.1:8999")
  20. if err != nil {
  21. fmt.Println("client start err, exit!")
  22. return
  23. }
  24. for {
  25. dp := NewDataPack()
  26. msg, _ := dp.Pack(NewMsgPackage(i, []byte("client test message")))
  27. _, err := conn.Write(msg)
  28. if err != nil {
  29. fmt.Println("client write err: ", err)
  30. return
  31. }
  32. //先读出流中的head部分
  33. headData := make([]byte, dp.GetHeadLen())
  34. _, err = io.ReadFull(conn, headData)
  35. if err != nil {
  36. fmt.Println("client read head err: ", err)
  37. return
  38. }
  39. // 将headData字节流 拆包到msg中
  40. msgHead, err := dp.Unpack(headData)
  41. if err != nil {
  42. fmt.Println("client unpack head err: ", err)
  43. return
  44. }
  45. if msgHead.GetDataLen() > 0 {
  46. //msg 是有data数据的,需要再次读取data数据
  47. msg := msgHead.(*Message)
  48. msg.Data = make([]byte, msg.GetDataLen())
  49. //根据dataLen从io中读取字节流
  50. _, err := io.ReadFull(conn, msg.Data)
  51. if err != nil {
  52. fmt.Println("client unpack data err")
  53. return
  54. }
  55. fmt.Printf("==> Client receive Msg: ID = %d, len = %d , data = %s\n", msg.ID, msg.DataLen, msg.Data)
  56. }
  57. time.Sleep(time.Second)
  58. }
  59. }
  60. /*
  61. 模拟服务器端
  62. */
  63. //ping test 自定义路由
  64. type PingRouter struct {
  65. BaseRouter
  66. }
  67. //Test PreHandle
  68. func (this *PingRouter) PreHandle(request ziface.IRequest) {
  69. fmt.Println("Call Router PreHandle")
  70. err := request.GetConnection().SendMsg(1, []byte("before ping ....\n"))
  71. if err != nil {
  72. fmt.Println("preHandle SendMsg err: ", err)
  73. }
  74. }
  75. //Test Handle
  76. func (this *PingRouter) Handle(request ziface.IRequest) {
  77. fmt.Println("Call PingRouter Handle")
  78. //先读取客户端的数据,再回写ping...ping...ping
  79. fmt.Println("recv from client : msgID=", request.GetMsgID(), ", data=", string(request.GetData()))
  80. err := request.GetConnection().SendMsg(1, []byte("ping...ping...ping\n"))
  81. if err != nil {
  82. fmt.Println("Handle SendMsg err: ", err)
  83. }
  84. }
  85. //Test PostHandle
  86. func (this *PingRouter) PostHandle(request ziface.IRequest) {
  87. fmt.Println("Call Router PostHandle")
  88. err := request.GetConnection().SendMsg(1, []byte("After ping .....\n"))
  89. if err != nil {
  90. fmt.Println("Post SendMsg err: ", err)
  91. }
  92. }
  93. type HelloRouter struct {
  94. BaseRouter
  95. }
  96. func (this *HelloRouter) Handle(request ziface.IRequest) {
  97. fmt.Println("call helloRouter Handle")
  98. fmt.Printf("receive from client msgID=%d, data=%s\n", request.GetMsgID(), string(request.GetData()))
  99. err := request.GetConnection().SendMsg(2, []byte("hello zix hello Router"))
  100. if err != nil {
  101. fmt.Println(err)
  102. }
  103. }
  104. func DoConnectionBegin(conn ziface.IConnection) {
  105. fmt.Println("DoConnectionBegin is Called ... ")
  106. err := conn.SendMsg(2, []byte("DoConnection BEGIN..."))
  107. if err != nil {
  108. fmt.Println(err)
  109. }
  110. }
  111. //连接断开的时候执行
  112. func DoConnectionLost(conn ziface.IConnection) {
  113. fmt.Println("DoConnectionLost is Called ... ")
  114. }
  115. func TestServer(t *testing.T) {
  116. //创建一个server句柄
  117. s := NewServer()
  118. //注册链接hook回调函数
  119. s.SetOnConnStart(DoConnectionBegin)
  120. s.SetOnConnStop(DoConnectionLost)
  121. // 多路由
  122. s.AddRouter(1, &PingRouter{})
  123. s.AddRouter(2, &HelloRouter{})
  124. // 客户端测试
  125. go ClientTest(1)
  126. go ClientTest(2)
  127. //2 开启服务
  128. go s.Serve()
  129. select {
  130. case <-time.After(time.Second * 10):
  131. return
  132. }
  133. }