private void ProcessNetEvent(byte[] msgBytes) { int nHeadSize = Marshal.SizeOf(typeof(TCP_Head)); TCP_Head tcpHead = (TCP_Head)NetDefine.BytesToStru <TCP_Head>(msgBytes); int nBufferSize = msgBytes.Length - nHeadSize; byte[] buffer = new byte[nBufferSize]; Array.Copy(msgBytes, nHeadSize, buffer, 0, nBufferSize); Debug.Log(string.Format("<color=yellow>收到服务器数据长度:{0} 主命令:{1} 子命令:{2} </color>", msgBytes.Length, tcpHead.Cmd.Main_ID, tcpHead.Cmd.Sub_ID)); GApp.NetMgr.AddNetMessage(tcpHead.Cmd.Main_ID, tcpHead.Cmd.Sub_ID, buffer); }
public void SendAsync(ushort main_id, ushort sub_id, byte[] data = null, Action <Hashtable> callback = null, Hashtable hashtable = null) { TCP_Head tcpHead = new TCP_Head(); tcpHead.Cmd.Main_ID = main_id; tcpHead.Cmd.Sub_ID = sub_id; int nHeadSize = Marshal.SizeOf(tcpHead); int nSendSize = nHeadSize; if (data != null) { nSendSize += data.Length; } tcpHead.Info.Buffer_Size = (ushort)nSendSize; byte[] SendBuffer = new byte[nSendSize]; var Headbuffer = NetDefine.StruToBytes(tcpHead); Array.Copy(Headbuffer, SendBuffer, nHeadSize); if (data != null) { Array.Copy(data, 0, SendBuffer, nHeadSize, data.Length); } socket.BeginSend(SendBuffer, 0, nSendSize, SocketFlags.None, (ar) => { try { socket.EndSend(ar); if (callback != null) { callback(hashtable); } } catch (SocketException se) { ReConn(se.ErrorCode); } }, socket); }