Skip to content

Commit

Permalink
net protobuf message test
Browse files Browse the repository at this point in the history
  • Loading branch information
jzy committed Mar 9, 2022
1 parent 565738d commit 999b50f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion game-gate/manager/client_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func clientChannelInactive(channel network.Channel) {
}

} else {
log.Warn("用户退出 ip:", channel.RemoteAddr(), " 无用户信息")
log.Warn("用户退出 ip: %v 无用户信息", channel.RemoteAddr())
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package net
package manager

import (
"encoding/binary"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/jzyong/go-mmo-server/src/core/log"
network "github.com/jzyong/go-mmo-server/src/core/network/tcp"
"github.com/jzyong/go-mmo-server/src/message"
"github.com/jzyong/GameServer4g/game-message/message"
"github.com/jzyong/golib/log"
network "github.com/jzyong/golib/network/tcp"
"io"
"net"
"testing"
Expand All @@ -30,7 +30,7 @@ func ClientTest(i int32) {
var err error
clientConn, err = net.Dial("tcp", "192.168.110.2:6060")
if err != nil {
log.Errorf("client start err, exit! %v", err)
log.Error("client start err, exit! %v", err)
return
}

Expand All @@ -52,12 +52,12 @@ func switchReceiveMessage() {
buffMsgLength := make([]byte, 4)
// read len
if _, err := io.ReadFull(clientConn, buffMsgLength); err != nil {
log.Error("read msg length error", err)
log.Error("read msg length error %v", err)
}
var msgLength = uint32(binary.LittleEndian.Uint32(buffMsgLength))
//最大长度验证
if msgLength > 10000 {
log.Warnf("消息太长:%d\n", msgLength)
log.Warn("消息太长:%d\n", msgLength)
}
msgData := make([]byte, msgLength)
if _, err := io.ReadFull(clientConn, msgData); err != nil {
Expand All @@ -69,7 +69,7 @@ func switchReceiveMessage() {
//拆包,得到msgid 和 数据 放在msg中
msg2, err := dp2.Unpack(msgData, msgLength)
if err != nil {
log.Error("unpack error ", err)
log.Error("unpack error %v", err)
return
}
fmt.Println("==> Recv Msg: ID=", msg2.GetMsgId(), ", len=", msgLength)
Expand All @@ -95,7 +95,7 @@ func sendMsg(mid message.MID, message proto.Message) {
msg, _ := dp.Pack(network.NewClientMessage(int32(mid), data))
_, err = clientConn.Write(msg)
if err != nil {
log.Errorf("消息:%v 发送失败 %v ", mid, err)
log.Error("消息:%v 发送失败 %v ", mid, err)
return
}
}
Expand All @@ -113,7 +113,7 @@ func heartRequest() {
func heartResponse(tcpMessage network.TcpMessage) {
response := &message.PlayerHeartResponse{}
proto.Unmarshal(tcpMessage.GetData(), response)
log.Infof("收到心跳:%v", response)
log.Info("收到心跳:%v", response)
}

//用户登录
Expand All @@ -129,7 +129,7 @@ func userLoginRequest() {
func userLoginResponse(tcpMessage network.TcpMessage) {
response := &message.UserLoginResponse{}
proto.Unmarshal(tcpMessage.GetData(), response)
log.Infof("用户信息:%v", response)
log.Info("用户信息:%v", response)
}

func TestConnectClientServer(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package net
package manager

import (
"encoding/binary"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/jzyong/go-mmo-server/src/core/log"
network "github.com/jzyong/go-mmo-server/src/core/network/tcp"
"github.com/jzyong/go-mmo-server/src/message"
"github.com/jzyong/GameServer4g/game-message/message"
"github.com/jzyong/golib/log"
network "github.com/jzyong/golib/network/tcp"
"io"
"math/rand"
"net"
Expand All @@ -24,9 +24,9 @@ func GameTest(i int32) {

fmt.Println("Game Test ... start")

conn, err := net.Dial("tcp", "127.0.0.1:6061")
conn, err := net.Dial("tcp", "192.168.110.2:6061")
if err != nil {
log.Errorf("Game start err, exit! %v", err)
log.Error("Game start err, exit! %v", err)
return
}

Expand All @@ -40,7 +40,7 @@ func GameTest(i int32) {
msg, _ := dp.Pack(network.NewInnerMessage(int32(message.MID_ServerListReq), data, 1, 2))
_, err = conn.Write(msg)
if err != nil {
log.Error("client write err: ", err)
log.Error("client write err: %v", err)
return
}

Expand All @@ -50,12 +50,12 @@ func GameTest(i int32) {
buffMsgLength := make([]byte, 4)
// read len
if _, err := io.ReadFull(conn, buffMsgLength); err != nil {
log.Error("read msg length error", err)
log.Error("read msg length error %v", err)
}
var msgLength = uint32(binary.LittleEndian.Uint32(buffMsgLength))
//最大长度验证
if msgLength > 10000 {
log.Warnf("消息太长:%d\n", msgLength)
log.Warn("消息太长:%d\n", msgLength)
}
msgData := make([]byte, msgLength)
if _, err := io.ReadFull(conn, msgData); err != nil {
Expand All @@ -67,14 +67,14 @@ func GameTest(i int32) {
//拆包,得到msgid 和 数据 放在msg中
msg2, err := dp2.Unpack(msgData, msgLength)
if err != nil {
log.Error("unpack error ", err)
log.Error("unpack error %v", err)
return
}
fmt.Println("==> Recv Msg: ID=", msg2.GetMsgId(), ", len=", msgLength, ", data=", string(msg2.GetData()), ",time=", msg2.GetTime())
response := &message.ServerListResponse{}
proto.Unmarshal(msg2.GetData(), response)

log.Infof("收到消息:%v", response)
log.Info("收到消息:%v", response)

time.Sleep(time.Second)
}
Expand Down

0 comments on commit 999b50f

Please sign in to comment.