public void Send(IPEndPoint endPoint, byte[] buffer, int offset, int length, bool reliable, bool putBufferToPool) { var packet = new SmartPacketToSend(); packet.EndPoint = endPoint; packet.Buffer = buffer; packet.Offset = offset; packet.Length = length; packet.PutBufferToPool = putBufferToPool; packet.Reliable = reliable; _sendQueue.Add(packet); }
private void IOLoop() { bool active = false; while (!_closing) { lock (_syncObject) { _socket.Tick(); } SmartPacketToSend packetToSend = new SmartPacketToSend(); if (_sendQueue.Count > 0) { active = true; packetToSend = _sendQueue.Take(); lock (_syncObject) { _socket.Send(packetToSend.EndPoint, packetToSend.Buffer, packetToSend.Offset, packetToSend.Length, packetToSend.Reliable); } if (packetToSend.PutBufferToPool) { _buffersPool.Put(packetToSend.Buffer); } } ReceivedSmartPacket receivedPacket = new ReceivedSmartPacket(); bool received; lock (_syncObject) { received = _socket.Receive(ref receivedPacket); } if (received) { active = true; _recvQueue.Add(receivedPacket); } if (!active) { Thread.Sleep(10); } } }
private void IOLoop() { while (!_closing) { try { bool active = false; _socket.Tick(); State = _socket.State; SmartPacketToSend packetToSend = new SmartPacketToSend(); if (_sendQueue.Count > 0) { active = true; packetToSend = _sendQueue.Take(); _socket.Send(packetToSend.EndPoint, packetToSend.Buffer, packetToSend.Offset, packetToSend.Length, packetToSend.Reliable); if (packetToSend.PutBufferToPool) { _buffersPool.Put(packetToSend.Buffer); } } ReceivedSmartPacket receivedPacket = new ReceivedSmartPacket(); if (_socket.Receive(ref receivedPacket)) { active = true; _recvQueue.Add(receivedPacket); } if (!active) { Thread.Sleep(10); } } catch (Exception) { _closing = true; } } _socket.Close(); State = PixocketState.NotConnected; }