public void Send(UInt32 id, UInt32 protocol, Int32 cmd, byte[] msg) { uint body_key = Convert.ToUInt32(CRC32Cls.GetCRC32(msg)); byte[] id_bytes = BitConverter.GetBytes(id); //id byte[] protocol_bytes = BitConverter.GetBytes(protocol); //protocol byte[] cmd_bytes = BitConverter.GetBytes(cmd); //cmd byte[] body_key_bytes = BitConverter.GetBytes(body_key); //cmd UInt32 total_length = (UInt32)(id_bytes.Length + protocol_bytes.Length + cmd_bytes.Length + body_key_bytes.Length + msg.Length + 4); byte[] data = new byte[total_length]; BitConverter.GetBytes(total_length).CopyTo(data, 0); id_bytes.CopyTo(data, 4); protocol_bytes.CopyTo(data, 8); cmd_bytes.CopyTo(data, 12); body_key_bytes.CopyTo(data, 16); msg.CopyTo(data, 20); try { socket.SendTo(data, 0, data.Length, SocketFlags.None, srvIpEnd); if (index == 2) { index = 1; } UnityEngine.Debug.LogWarning("==发送Udp验证数据=="); } catch { } }
public byte[] GetData(UInt32 id, UInt32 protocol, Int32 cmd, byte[] msg) { uint body_key = Convert.ToUInt32(CRC32Cls.GetCRC32(msg)); byte[] id_bytes = BitConverter.GetBytes(id); //id byte[] protocol_bytes = BitConverter.GetBytes(protocol); //protocol byte[] cmd_bytes = BitConverter.GetBytes(cmd); //cmd byte[] body_key_bytes = BitConverter.GetBytes(body_key); //cmd UInt32 total_length = (UInt32)(id_bytes.Length + protocol_bytes.Length + cmd_bytes.Length + body_key_bytes.Length + msg.Length + 4); //消息体结构:消息体长度+消息体 byte[] data = new byte[total_length]; BitConverter.GetBytes(total_length).CopyTo(data, 0); id_bytes.CopyTo(data, 4); protocol_bytes.CopyTo(data, 8); cmd_bytes.CopyTo(data, 12); body_key_bytes.CopyTo(data, 16); msg.CopyTo(data, 20); return(data); }
public void Send(UInt32 id, UInt32 protocol, Int32 cmd, byte[] msg) { //UnityEngine.Main.Debug("msg:" + msg.Length); uint body_key = Convert.ToUInt32(CRC32Cls.GetCRC32(msg)); byte[] id_bytes = BitConverter.GetBytes(id); //id byte[] protocol_bytes = BitConverter.GetBytes(protocol); //protocol byte[] cmd_bytes = BitConverter.GetBytes(cmd); //cmd byte[] body_key_bytes = BitConverter.GetBytes(body_key); //cmd UInt32 total_length = (UInt32)(id_bytes.Length + protocol_bytes.Length + cmd_bytes.Length + body_key_bytes.Length + msg.Length + 4); //UnityEngine.Main.Debug("send3333"); //消息体结构:消息体长度+消息体 byte[] data = new byte[total_length]; BitConverter.GetBytes(total_length).CopyTo(data, 0); id_bytes.CopyTo(data, 4); protocol_bytes.CopyTo(data, 8); cmd_bytes.CopyTo(data, 12); body_key_bytes.CopyTo(data, 16); msg.CopyTo(data, 20); //UnityEngine.Main.Debug("send44444"); //socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(send_back), null); string str = ""; for (int i = 0; i < data.Length; i++) { str += data[i].ToString(); } //UnityEngine.Main.Debug("发送数据:" + str); //UnityEngine.Main.Debug("数据长度:" + data.Length); m_Kcp.Send(data); _send = true; m_NeedUpdateFlag = true; }
public void Send(UInt32 id, UInt32 protocol, Int32 cmd, byte[] msg) { if (parent.state < 0) { return; } uint body_key = Convert.ToUInt32(CRC32Cls.GetCRC32(msg)); UInt32 total_length = (UInt32)(20 + msg.Length); byte[] data = new byte[total_length]; //消息体结构:消息体长度+消息体 BitConverter.GetBytes(total_length).CopyTo(data, 0); BitConverter.GetBytes(id).CopyTo(data, 4); BitConverter.GetBytes(protocol).CopyTo(data, 8); BitConverter.GetBytes(cmd).CopyTo(data, 12); BitConverter.GetBytes(body_key).CopyTo(data, 16); msg.CopyTo(data, 20); try { parent.HasSend(); socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(send_back), null); ////=====测试====== //string str = ""; //for (int i = 0; i < data.Length; i++) //{ // str += Convert.ToString(data[i], 16); //} //Debug.Log("发送命令:" + cmd); } catch (Exception ex) { UnityEngine.Debug.LogError("发送数据错误:" + ex.ToString()); parent.DisConn(); } }