ET icon indicating copy to clipboard operation
ET copied to clipboard

KcpProtocalType.MSG类型的包头问题

Open zwing1201 opened this issue 3 years ago • 2 comments
trafficstars

你好,请教一下,KcpProtocalType.MSG 的包头大小是5还是9?

KChannel.cs

byte[] buffer = this.sendCache;
buffer.WriteTo(0, KcpProtocalType.MSG);
// 每个消息头部写下该channel的id;
buffer.WriteTo(1, this.LocalConn);
Marshal.Copy(bytes, buffer, 5, count);
this.socket.SendTo(buffer, 0, count + 5, SocketFlags.None, this.RemoteAddress);

KServices.cs

case KcpProtocalType.MSG: 
// 长度<9,不是Msg消息
if (messageLength < 9)
{
  break;
}
// 处理chanel
remoteConn = BitConverter.ToUInt32(this.cache, 1);
localConn = BitConverter.ToUInt32(this.cache, 5);

第二个UInt32的Conn也就是localConn ,在发送端没赋值,为什么接受端可以获取到?

zwing1201 avatar Apr 07 '22 09:04 zwing1201

localConn是kcp底层设置的

egametang avatar Apr 08 '22 02:04 egametang

感谢作者回复,我后面继续阅读代码,猜测也是这个问题。 如果要写的对称一点,发送端也是要多写4个自己的Conn,但是这样写是会省4个字节。

zwing1201 avatar Apr 08 '22 06:04 zwing1201