public void SendMsg(int cmd, int scmd, BaseMessageData body) { MessageData <BaseMessageData> data = new MessageData <BaseMessageData> (); HeadMsg head = new HeadMsg(); head.cmd = cmd; head.scmd = scmd; data.head = head; data.body = body; ByteBuffer _buff = new ByteBuffer(); byte[] bytes = data.ToBytes(); _buff.WriteInt32(bytes.Length); _buff.WriteBytes(bytes); bytes = _buff.ToBytes(); switch (Main.Self.clientType) { case ClientType.client: Main.Self.client.Send(bytes); break; case ClientType.server: Main.Self.server.Send(bytes); break; case ClientType.NULL: break; default: break; } }
public static void SendMsg(int cmd, int scmd, ByteBuffer buffer) { MessageData sendMsg = new MessageData(); HeadMsg head = new HeadMsg(); head.cmd = cmd; head.scmd = scmd; sendMsg.head = head; sendMsg.data = buffer.ToBytes(); byte[] bytes = ProtoBufTools.Serialize(sendMsg); ByteBuffer sendBuffer = new ByteBuffer(); sendBuffer.WriteInt32(bytes.Length); sendBuffer.WriteBytes(bytes); bytes = sendBuffer.ToBytes(); socket.Send(bytes); }
public void DealMsg(MessageReceiveData _data) { if (_data == null) { return; } byte[] _dataBytes = _data.receiveBytes; IPEndPoint receivePoint = _data.receivePoint; ByteBuffer _buff = new ByteBuffer(_dataBytes); int length = _buff.ReadInt32(); byte[] _bytes = _buff.ReadBytes(length); MessageData <BaseMessageData> data = ProtoBufTools.DeSerialize <MessageData <BaseMessageData> > (_bytes); if (data.body.dealFlg == 0) { if (Main.Self.clientType == ClientType.server) { if (data.head.cmd == 1 && data.head.scmd == 1) { Main.Self.server.AddPlayer(receivePoint); } else if (data.head.cmd == 1 && data.head.scmd == 2) { Main.Self.server.RemovePlayer(receivePoint); } byte[] _serverDealBytes = ServerDealMsg(data, receivePoint); ByteBuffer _serverDealBuff = new ByteBuffer(); _serverDealBuff.WriteInt32(_serverDealBytes.Length); _serverDealBuff.WriteBytes(_serverDealBytes); Main.Self.server.AddSendQueue(_serverDealBuff.ToBytes()); } } else { if (Main.Self.clientType == ClientType.server) { } else { } SwitchMsg(data); } }