public static DefinedStructure.PacketInfo CreatePack(int id, int size, byte[] data) { DefinedStructure.PacketInfo receivePack = new DefinedStructure.PacketInfo(); receivePack._id = id; receivePack._totalSize = size; receivePack._data = new byte[1016]; Array.Copy(data, receivePack._data, data.Length); return(receivePack); }
public void AddFromQueue(Queue <PacketClass> fromClient) { if (_socketList.Count != 0) { for (int n = 0; n < _socketList.Count; n++) { byte[] buffer; int recvLen; if (_socketList[n].ReceiveBuffer(out buffer, out recvLen)) { DefinedStructure.PacketInfo pInfo = new DefinedStructure.PacketInfo(); pInfo = (DefinedStructure.PacketInfo)ConvertPacket.ByteArrayToStructure(buffer, pInfo.GetType(), recvLen); PacketClass packet = new PacketClass(pInfo._id, pInfo._data, pInfo._totalSize, n, _socketList[n]._UUID); fromClient.Enqueue(packet); } } } }
void Accept() { try { while (true) { if (_waitServer != null && _waitServer.Poll(0, SelectMode.SelectRead)) { _socketManager.AddSocket(new SocketClass(_waitServer.Accept())); Console.WriteLine("Client Accept"); } _socketManager.AddFromQueue(_fromClientQueue); if (_dbServer != null && _dbServer.Poll(0, SelectMode.SelectRead)) { byte[] buffer = new byte[1032]; int recvLen = _dbServer.Receive(buffer); if (recvLen > 0) { DefinedStructure.PacketInfo pToClient = new DefinedStructure.PacketInfo(); pToClient = (DefinedStructure.PacketInfo)ConvertPacket.ByteArrayToStructure(buffer, pToClient.GetType(), recvLen); PacketClass packet = new PacketClass(pToClient._id, pToClient._data, pToClient._totalSize); _toServerQueue.Enqueue(packet); } } Thread.Sleep(10); } } catch (ThreadInterruptedException e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } }