Golang makes a game server for the landlord [9]: pb foundation

According to the protocol of the previous article,     golang makes a game server for the landlord [7]: some thoughts on the server and protocol

 

message TDDZ
{
    message THeader
    {
        头部应该包含很多内容, 目前先不填
    }

  

    optional THeader    Header  = 1;
    optional int32      Command = 2;

    optional TGetServerInfoReq GetServerInfoReq = 11;
    optional TGetServerInfoRsp GetServerInfoRsp = 12;
    optional TGetTableInfoReq GetTableInfoReq = 13;
    optional TTableInfo TableInfo = 14;
    optional TGetTableInfoRsp GetTableInfoRsp = 15;
    optional TJoinTableReq JoinTableReq = 16;
    optional TJoinTableRsp JoinTableRsp = 17;
    optional TPlayerInfo PlayerInfo = 18;
    optional TPlayerComeInBc PlayerComeInBc = 19;
    optional TGameStartBc GameStartBc = 20;
    optional TDealCardBc DealCardBc = 21;
    optional TCallDZBc CallDZBc = 22;
    optional TCallDZReq CallDZReq = 23;
    optional TCallDZRsp CallDZRsp = 24;
    optional TOutCardBc OutCardBc = 25;
    optional TOutCardReq OutCardReq = 26;
    optional TOutCardRsp OutCardRsp = 27;
    optional TGameOverBc GameOverBc = 28;
}

Here is a final protocol that needs to be sent, which contains a collection of all the protocols that need to be sent

It doesn't look like a very good method, but it is the easiest method.

 

protoc.exe --plugin=protoc-gen=protoc-gen-go.exe --go_out=../ *.proto

Use the above command to convert the original pb file into the pb.go file used by golang

There are two files in it, one is protoc.exe that converts proto into pb intermediate file, you can use MAKE to compile an official version, and the other is gen-go.exe that turns pb intermediate file into pb.go

If you can't find it, you can download it here https://github.com/266game/tools/tree/master/protobuf2

After the command is successfully executed, a ddzpb.pb.go file will be generated, temporarily leave it alone.

 

-------------------------------------------------------------------------------------------------------------

Back to the client to send the protocol to the server

Get a new button Btn2

// OnFormCreate 主窗口创建事件
func (self *TMainForm) OnFormCreate(sender vcl.IObject) {
	self.SetCaption("这是一个游戏服务器")
	self.Btn1 = vcl.NewButton(self)           // 创建按钮
	self.Btn1.SetParent(self)                 //设置爸爸
	self.Btn1.SetBounds(10, 10, 88, 28)       //设置位置
	self.Btn1.SetCaption("开始连接")              //
	self.Btn1.SetOnClick(self.OnButton1Click) // 绑定按钮1点击事件

	self.Btn2 = vcl.NewButton(self)
	self.Btn2.SetParent(self)                 //设置爸爸
	self.Btn2.SetBounds(10, 50, 88, 28)       //设置位置
	self.Btn2.SetCaption("快速加入")              //
	self.Btn2.SetOnClick(self.OnButton2Click) // 绑定按钮1点击事件
}

// OnButton2Click 快速加入 按钮点击事件
func (self *TMainForm) OnButton2Click(sender vcl.IObject) {
	pack := &ddzpb.TDDZ{}
	pack.Command = proto.Int32(11) // 假装发送获取服务器信息
	buff, _ := proto.Marshal(pack)
	self.pClient.WritePack(buff)
}

Unpack on the server side

 

	// 启动服务器, 首先要连接中心服务器
	pServer := tcpserver.NewTCPServer()
	pServer.OnRead = func(pData *connection.TData) {
		//
		pack := &ddzpb.TDDZ{}
		proto.Unmarshal(pData.GetBuffer(), pack)
		log.Println("收到包", pack)
	}

Such a basic TCP is done

Guess you like

Origin blog.csdn.net/warrially/article/details/88799754