package znet import ( "fmt" "github.com/aceld/zinx/ziface" "io" "net" "testing" "time" ) // run in terminal: // go test -v ./znet -run=TestServer /* 模拟客户端 */ func ClientTest() { fmt.Println("Client Test ... start") //3秒之后发起测试请求,给服务端开启服务的机会 time.Sleep(3 * time.Second) conn, err := net.Dial("tcp", "127.0.0.1:8999") if err != nil { fmt.Println("client start err, exit!") return } for { dp := NewDataPack() msg, _ := dp.Pack(NewMsgPackage(1, []byte("client test message"))) _, err := conn.Write(msg) if err != nil { fmt.Println("client write err: ", err) return } //先读出流中的head部分 headData := make([]byte, dp.GetHeadLen()) _, err = io.ReadFull(conn, headData) if err != nil { fmt.Println("client read head err: ", err) return } // 将headData字节流 拆包到msg中 msgHead, err := dp.Unpack(headData) if err != nil { fmt.Println("client unpack head err: ", err) return } if msgHead.GetDataLen() > 0 { //msg 是有data数据的,需要再次读取data数据 msg := msgHead.(*Message) msg.Data = make([]byte, msg.GetDataLen()) //根据dataLen从io中读取字节流 _, err := io.ReadFull(conn, msg.Data) if err != nil { fmt.Println("client unpack data err") return } fmt.Printf("==> Client receive Msg: Id = %d, len = %d , data = %s\n", msg.Id, msg.DataLen, msg.Data) } time.Sleep(time.Second) } } /* 模拟服务器端 */ //ping test 自定义路由 type PingRouter struct { BaseRouter } //Test PreHandle func (this *PingRouter) PreHandle(request ziface.IRequest) { fmt.Println("Call Router PreHandle") err := request.GetConnection().SendMsg(1, []byte("before ping ....\n")) if err != nil { fmt.Println("preHandle SendMsg err: ", err) } } //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().SendMsg(1, []byte("ping...ping...ping\n")) if err != nil { fmt.Println("Handle SendMsg err: ", err) } } //Test PostHandle func (this *PingRouter) PostHandle(request ziface.IRequest) { fmt.Println("Call Router PostHandle") err := request.GetConnection().SendMsg(1, []byte("After ping .....\n")) if err != nil { fmt.Println("Post SendMsg err: ", err) } } func TestServer(t *testing.T) { //创建一个server句柄 s := NewServer() s.AddRouter(1, &PingRouter{}) // 客户端测试 go ClientTest() //2 开启服务 go s.Serve() select { case <-time.After(time.Second * 10): return } }