Esempio n. 1
0
        public bool Receive(ref ReceivedSmartPacket receivedPacket)
        {
            bool haveResult = false;
            var  packet     = new ReceivedPacket();

            while (true)
            {
                if (SubSock.Receive(ref packet))
                {
                    try
                    {
                        haveResult = OnReceive(packet.Buffer, packet.Offset, packet.Length, packet.EndPoint,
                                               ref receivedPacket);
                    }
                    catch (Exception)
                    {
                        haveResult = false;
                        _buffersPool.Put(packet.Buffer);
                    }
                }
                else
                {
                    break;
                }

                if (haveResult)
                {
                    break;
                }
            }

            return(haveResult);
        }
Esempio n. 2
0
        private void ReceiveLoop()
        {
            while (!_closing)
            {
                var      buffer      = _buffersPool.Get(MTUSafe);
                var      bufferInUse = false;
                EndPoint remoteEP    = _receiveEndPoint;
                try
                {
                    var bytesReceived = SysSock.ReceiveFrom(buffer, MTUSafe, SocketFlags.None, ref remoteEP);
                    //ntrf: On windows we will get EMSGSIZE error if message was truncated, but Mono on Unix will fill up the
                    //      whole buffer silently. We detect this case by allowing buffer to be slightly larger, than our typical
                    //      packet, and dropping any packet, that did fill the whole thing.
                    if (bytesReceived > 0 && bytesReceived <= MTU)
                    {
                        var packet = new ReceivedPacket();
                        packet.Buffer   = buffer;
                        packet.Offset   = 0;
                        packet.Length   = bytesReceived;
                        packet.EndPoint = (IPEndPoint)remoteEP;

                        bufferInUse = true;

                        _recvQueue.Add(packet);

                        if (_recvQueue.Count > RecvQueueLimit)
                        {
                            // Trash oldest
                            if (_recvQueue.TryTake(out packet))
                            {
                                _buffersPool.Put(packet.Buffer);
                            }
                        }
                    }
                    else
                    {
                        // If blocking call isn't actually blocking, sleep for a while
                        Thread.Sleep(10);
                    }
                }
                catch (SocketException se)
                {
                    // Ignore harmless errors
                    if (!HarmlessErrors.Contains(se.SocketErrorCode))
                    {
                        _excQueue.Add(se);
                        break;
                    }
                }

                if (!bufferInUse)
                {
                    _buffersPool.Put(buffer);
                }
            }
        }
Esempio n. 3
0
        public override bool Receive(ref ReceivedPacket packet)
        {
            if (_excQueue.TryTake(out var e))
            {
                _logger.Exception(e);
                throw e;
            }

            return(_recvQueue.TryTake(out packet));
        }
Esempio n. 4
0
        private void ReceiveLoop()
        {
            while (!_closing)
            {
                var      buffer     = _buffersPool.Get(MTU);
                var      bufferUsed = false;
                EndPoint remoteEP   = _receiveEndPoint;
                try
                {
                    var bytesReceived = SysSock.ReceiveFrom(buffer, MTU, SocketFlags.None, ref remoteEP);
                    if (bytesReceived > 0)
                    {
                        var packet = new ReceivedPacket();
                        packet.Buffer   = buffer;
                        packet.Offset   = 0;
                        packet.Length   = bytesReceived;
                        packet.EndPoint = (IPEndPoint)remoteEP;

                        bufferUsed = true;

                        _recvQueue.Add(packet);

                        if (_recvQueue.Count > RecvQueueLimit)
                        {
                            // Trash oldest
                            if (_recvQueue.TryTake(out packet))
                            {
                                _buffersPool.Put(packet.Buffer);
                            }
                        }
                    }
                    else
                    {
                        // If blocking call isn't actually blocking, sleep for a while
                        Thread.Sleep(10);
                    }
                }
                catch (Exception e)
                {
                    _logger.Exception(e);
                }

                if (!bufferUsed)
                {
                    _buffersPool.Put(buffer);
                }
            }
        }
Esempio n. 5
0
        public override bool Receive(ref ReceivedPacket packet)
        {
            try
            {
                if (SysSock.Available == 0)
                {
                    return(false);
                }
            }
            catch (Exception e)
            {
                _logger.Exception(e);
                return(false);
            }

            var      buffer   = _buffersPool.Get(MTU);
            EndPoint remoteEP = _remoteEndPoint;

            try
            {
                int bytesReceived = 0;
                if (_connectedMode)
                {
                    bytesReceived = SysSock.Receive(buffer, MTU, SocketFlags.None);
                }
                else
                {
                    bytesReceived = SysSock.ReceiveFrom(buffer, MTU, SocketFlags.None, ref remoteEP);
                }

                if (bytesReceived > 0)
                {
                    packet.Buffer   = buffer;
                    packet.Offset   = 0;
                    packet.Length   = bytesReceived;
                    packet.EndPoint = (IPEndPoint)remoteEP;

                    return(true);
                }
            }
            catch (Exception e)
            {
                _logger.Exception(e);
            }

            _buffersPool.Put(buffer);
            return(false);
        }
Esempio n. 6
0
        public bool Receive(ref ReceivedSmartPacket receivedPacket)
        {
            bool haveResult = false;
            var  packet     = new ReceivedPacket();

            while (true)
            {
                try
                {
                    packet.EndPoint = null;
                    if (SubSock.Receive(ref packet))
                    {
                        try
                        {
                            haveResult = OnReceive(packet.Buffer, packet.Offset, packet.Length, packet.EndPoint,
                                                   ref receivedPacket);
                        }
                        catch (SocketException)
                        {
                            haveResult = false;
                            _buffersPool.Put(packet.Buffer);
                            if (packet.EndPoint != null && _seqStates.ContainsKey(packet.EndPoint))
                            {
                                Close(packet.EndPoint, _seqStates[packet.EndPoint]);
                            }
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                catch (SocketException)
                {
                    Close();
                }

                if (haveResult)
                {
                    break;
                }
            }

            return(haveResult);
        }
Esempio n. 7
0
 public abstract bool Receive(ref ReceivedPacket packet);
Esempio n. 8
0
 public override bool Receive(ref ReceivedPacket packet)
 {
     return(_recvQueue.TryTake(out packet));
 }
Esempio n. 9
0
        public override bool Receive(ref ReceivedPacket packet)
        {
            try
            {
                if (SysSock.Available == 0)
                {
                    return(false);
                }
            }
            catch (SocketException se)
            {
                // Ignore harmless errors
                if (!HarmlessErrors.Contains(se.SocketErrorCode))
                {
                    _logger.Exception(se);
                    throw;
                }
            }

            var      buffer      = _buffersPool.Get(MTUSafe);
            var      bufferInUse = false;
            EndPoint remoteEP    = _remoteEndPoint;

            try
            {
                int bytesReceived = 0;
                if (_connectedMode)
                {
                    bytesReceived = SysSock.Receive(buffer, MTUSafe, SocketFlags.None);
                }
                else
                {
                    bytesReceived = SysSock.ReceiveFrom(buffer, MTUSafe, SocketFlags.None, ref remoteEP);
                }

                //ntrf: On windows we will get EMSGSIZE error if message was truncated, but Mono on Unix will fill up the
                //      whole buffer silently. We detect this case by allowing buffer to be slightly larger, than our typical
                //      packet, and dropping any packet, that did fill the whole thing.
                if (bytesReceived > 0 && bytesReceived <= MTU)
                {
                    packet.Buffer   = buffer;
                    bufferInUse     = true;
                    packet.Offset   = 0;
                    packet.Length   = bytesReceived;
                    packet.EndPoint = (IPEndPoint)remoteEP;

                    return(true);
                }
            }
            catch (SocketException se)
            {
                // Ignore harmless errors
                if (!HarmlessErrors.Contains(se.SocketErrorCode))
                {
                    _logger.Exception(se);
                    throw;
                }
            }
            finally
            {
                // We don't return buffer here if it is to be processed by client
                if (!bufferInUse)
                {
                    _buffersPool.Put(buffer);
                }
            }

            return(false);
        }
Esempio n. 10
0
        public override bool Receive(ref ReceivedPacket packet)
        {
            byte[] buffer      = null;
            var    bufferInUse = false;

            try
            {
                if (!Begin())
                {
                    return(false);
                }

                if (!SysSock.Poll(0, SelectMode.SelectRead))
                {
                    return(false);
                }

                buffer = _buffersPool.Get(MTUSafe);
                EndPoint remoteEP = _receiveEndPoint;

                var bytesReceived = SysSock.ReceiveFrom(buffer, MTUSafe, SocketFlags.None, ref remoteEP);
                //ntrf: On windows we will get EMSGSIZE error if message was truncated, but Mono on Unix will fill up the
                //      whole buffer silently. We detect this case by allowing buffer to be slightly larger, than our typical
                //      packet, and dropping any packet, that did fill the whole thing.
                if (bytesReceived > 0 && bytesReceived <= MTU)
                {
                    packet.Buffer   = buffer;
                    bufferInUse     = true;
                    packet.Offset   = 0;
                    packet.Length   = bytesReceived;
                    packet.EndPoint = (IPEndPoint)remoteEP;

                    return(true);
                }
            }
            catch (SocketException sx)
            {
                var err = sx.SocketErrorCode;

                // Ignore harmless errors
                // On Windows machines we might have a case of ICMP Port Unreachable being delivered,
                // which causes ECONNRESET on next receive call
                if (!HarmlessErrors.Contains(err))
                {
                    End();

                    // Our socket might be killed by iOS. Recreate the socket.
                    if (err != SocketError.NotConnected)
                    {
                        _logger.Exception(sx);
                        throw;
                    }
                }
            }
            finally
            {
                // We don't return buffer here if it is to be processed by client
                if (buffer != null && !bufferInUse)
                {
                    _buffersPool.Put(buffer);
                }
            }

            return(false);
        }