/// <summary> /// 发送消息 /// </summary> public void SendMsg(ushort msgID, System.Object protoObj) { if (State != ENetworkState.Connected) { LogSystem.Log(ELogType.Warning, "Network is not connected."); return; } NetSendPackage package = new NetSendPackage(); package.Type = msgID; package.ProtoObj = protoObj; if (_channel != null) _channel.SendMsg(package); }
public override void Encode(System.Object msg) { NetSendPackage packet = (NetSendPackage)msg; if (packet.ProtoObj == null) { Channel.HandleError(false, $"NetProtoPackageParser encode fatal. Msg type is {packet.Type}"); return; } // 获取包体数据 byte[] bodyData = ProtobufHelper.Encode(packet.ProtoObj); // 写入长度 int packetSize = SEND_PACKAGE_HEAD_SIZE + bodyData.Length; _sendBuffer.WriteUShort((ushort)packetSize); // 写入包头 _sendBuffer.WriteUShort(packet.Type); // 写入包体 _sendBuffer.WriteBytes(bodyData, 0, bodyData.Length); }