public static void Send(Int16 key, IMessage msg) { var typeBytes = LittleEndianByte.GetBytes(key); var dataBytes = msg.ToByteArray(); var sendBytes = typeBytes.Concat(dataBytes).ToArray(); m_ClientSocket.Send(sendBytes); }
public void Send(Socket cfd, byte[] data) { if (!m_OnlineClients.ContainsKey(cfd)) { return; } Int16 len = (Int16)data.Length; byte[] lenBytes = LittleEndianByte.GetBytes(len);/* BitConverter.GetBytes(len);*/ byte[] sendBytes = lenBytes.Concat(data).ToArray(); cfd.BeginSend(sendBytes, 0, sendBytes.Length, 0, SendCallback, cfd); }
public static void Connect(String ip, Int32 port) { m_ClientSocket = new ClientSocket(); m_ClientSocket.onReceive.AddListener(delegate(byte[] data) { Int16 type = LittleEndianByte.GetInt16(data, 0); if (m_ProtocMap.ContainsKey(type)) { m_ProtocMap[type].Invoke(data, 2, data.Length - 2); } }); m_ClientSocket.Connect("127.0.0.1", 8888); }
public static void Send(Socket cfd, Int16 key, IMessage msg) { if (m_ServerSocket == null) { return; } var typeBytes = LittleEndianByte.GetBytes(key); var dataBytes = msg.ToByteArray(); var sendBytes = typeBytes.Concat(dataBytes).ToArray(); m_ServerSocket.Send(cfd, sendBytes); }
public void Send(Socket cfd, byte[] data) { if (!cfd.Connected) { return; } Int16 len = (Int16)data.Length; byte[] lenBytes = LittleEndianByte.GetBytes(len); byte[] sendBytes = lenBytes.Concat(data).ToArray(); cfd.BeginSend(sendBytes, 0, sendBytes.Length, 0, SendCallback, cfd); }
public static void Bind(String ip, Int32 port) { if (m_ServerSocket != null) { return; } m_ServerSocket = new ServerSocket(); m_ServerSocket.Bind(ip, port); m_ServerSocket.onReceive.AddListener(delegate(Socket cfd, byte[] data) { Int16 id = LittleEndianByte.GetInt16(data, 0); if (m_ProtocMap.ContainsKey(id)) { m_ProtocMap[id].Invoke(cfd, data, 2, data.Length - 2); } }); }
public void Update(Int32 count) { if (count <= 0) { return; } lock (recvBuffer) { recvBuffer.Add(count); while (recvBuffer.size > 0) { Int32 totalLen = recvBuffer.size; if (m_BuildBuffer != null) { Int32 remainLen = m_BuildBuffer.GetFreeLength(); Int32 receiveLen = (remainLen <= totalLen ? remainLen : totalLen); Int32 size = m_BuildBuffer.Add(recvBuffer, 0, receiveLen); recvBuffer.Remove(size); if (m_BuildBuffer.GetFreeLength() <= 0) { // 已完全接收一条数据 lock (m_DataQueue) { m_DataQueue.Enqueue(m_BuildBuffer); } m_BuildBuffer = null; } } else if (recvBuffer.size >= 2) { Int16 len = LittleEndianByte.GetInt16(recvBuffer); recvBuffer.Remove(2); m_BuildBuffer = new ByteArray(len); } else { // 数据接收完毕或不足凑一个 Int16 break; } } } }
// 若放入数据前队列是空时,会返回打包后的数据 public ByteArray Add(byte[] data) { Int16 len = (Int16)data.Length; byte[] lenBytes = LittleEndianByte.GetBytes(len); byte[] sendBytes = lenBytes.Concat(data).ToArray(); ByteArray res = null; lock (m_DataQueue) { m_DataQueue.Enqueue(new ByteArray(sendBytes)); if (m_DataQueue.Count == 1) { res = m_DataQueue.First(); } } return(res); }