private static void _SendMessageAsyc(int opcode, byte[] data, ActionForReceive action) { // // if (IsConnected == false) { // ConnectServerAndLogin (); // if (!IsConnected) { // throw new Exception("server is not connect"); // } // } try { int id = Interlocked.Increment(ref idIndex); ByteBuffer buffer = ByteBuffer.Allocate(data.Length + 12); buffer.WriteInt(data.Length); buffer.WriteInt(opcode); buffer.WriteInt(id); buffer.WriteBytes(data); byte[] sendData = buffer.ToArray(); clientSocket.Send(sendData); dic.Add(id, action); } catch (Exception e) { Debug.Log("send fail," + e); // IsConnected = false; // clientSocket.Shutdown(SocketShutdown.Both); // clientSocket.Close(); throw new Exception(e.Message); } }
/// <summary> /// 发送数据给服务器 /// </summary> // public void SendMessage(string data) // { // if (IsConnected == false) // return; // try // { // ByteBuffer buffer = ByteBuffer.Allocate(512); // buffer.WriteString(data); // clientSocket.Send(WriteMessage(buffer.ToBytes())); // } // catch // { // IsConnected = false; // clientSocket.Shutdown(SocketShutdown.Both); // clientSocket.Close(); // } // } public void SendMessageAsyc(int id, int opcode, byte[] data, ActionForReceive action) { // if (IsConnected == false) { return; } try { ByteBuffer buffer = ByteBuffer.Allocate(512); buffer.WriteInt(data.Length); buffer.WriteInt(opcode); buffer.WriteInt(id); buffer.WriteBytes(data); byte[] sendData = buffer.ToArray(); clientSocket.Send(sendData); dic.Add(id, action); Debug.Log("send success,size = " + data.Length + ",id:" + id); } catch (Exception e) { Debug.Log("send fail," + e); IsConnected = false; clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); } }
public static void AddServerSendReceive(int opcode, ActionForReceive actionFroReceive) { if (serverSendData.ContainsKey(opcode)) // 可以被覆盖 { serverSendData.Remove(opcode); } serverSendData.Add(opcode, actionFroReceive); }
public static void SendMessageAsyc(int opcode, byte[] data, ActionForReceive action) { if (checkRepeatSend(opcode)) { Debug.LogWarning("repead send " + opcode); return; } if (!IsConnected && !IsConnecting) { needConnectOnce = true; } AsyncObject asyncObject = new AsyncObject(); asyncObject.action = action; asyncObject.Data = data; asyncObject.Opcode = opcode; sendQueue.Enqueue(asyncObject); }
private static void doReceivePacket(int opcode, int id, byte[] data) { if (opcode == (int)MiGongOpcode.SCSendEatBean) { SCSendEatBean sc = SCSendEatBean.Deserialize(data); Debug.Log("opcode:MiGongOpcode.SCEatBean" + sc.Beans.Count); foreach (PBEatBeanInfo bean in sc.Beans) { Debug.Log("" + bean.UserId + "," + bean.BeanPos); // 谁吃的 } } if (dic.ContainsKey(id)) { if (opcode == (int)BaseOpcode.SCException) { SCException exception = SCException.Deserialize(data); resoveSCException(exception); } else { ActionForReceive action = dic[id]; if (action != null) { AsyncObject o = new AsyncObject(); o.Opcode = opcode; o.Data = data; o.action = action; lock (invokeQueue) { invokeQueue.Enqueue(o); } } } dic.Remove(id); } else if (syncObjects.ContainsKey(id)) { if (opcode == (int)BaseOpcode.SCException) { SCException exception = SCException.Deserialize(data); resoveSCException(exception); } else { } SyncObject syncObject = syncObjects[id]; Monitor.Enter(syncObject); syncObject.Data = data; syncObject.Opcode = opcode; syncObjects.Remove(id); Monitor.Pulse(syncObject); Monitor.Exit(syncObject); } else { // 推送消息 if (opcode == (int)BaseOpcode.SCException) { SCException exception = SCException.Deserialize(data); resoveSCException(exception); } else { if (serverSendData.ContainsKey(opcode)) { ActionForReceive action = serverSendData[opcode]; AsyncObject o = new AsyncObject(); o.Opcode = opcode; o.Data = data; o.action = action; lock (invokeQueue) { invokeQueue.Enqueue(o); } } // Debug.Log("opcode = "+opcode+","); } } }
/// <summary> /// 接受网络数据 /// </summary> private void _onReceiveSocket() { byte[] _tmpReceiveBuff = new byte[1024]; ByteBuffer buf = ByteBuffer.Allocate(10240); int length = 0; bool isReadHead = false; int size = 0; // int opcode = 0; int id = 0; while (true) { if (!clientSocket.Connected) { // _isConnected = false; // _ReConnect(); break; } try { int receiveLength = clientSocket.Receive(_tmpReceiveBuff); if (receiveLength > 0) { buf.WriteBytes(_tmpReceiveBuff); length += receiveLength; if (length < 12) { continue; } if (!isReadHead) { size = buf.ReadInt(); opcode = buf.ReadInt(); id = buf.ReadInt(); Debug.Log("size:" + size + ",opcode:" + opcode + ",id:" + id); isReadHead = true; if (size > length - 12) { continue; } } if (size > length - 12) { continue; } byte[] data = new byte[size]; buf.ReadBytes(data, 0, size); buf.Clear(); isReadHead = false; length = 0; if (dic.ContainsKey(id)) { ActionForReceive action = dic[id]; if (action != null) { action.Invoke(opcode, data); } } } } catch (System.Exception e) { Debug.Log("e:" + e); clientSocket.Disconnect(true); clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); break; } } }