Пример #1
0
        private int FillBuffer(List <ArraySegment <byte> > buffer)
        {
            Socket socketCapture = null;

            try
            {
                if (gatewayConnection.Socket == null || !gatewayConnection.Socket.Connected)
                {
                    gatewayConnection.Connect();
                }
                socketCapture = gatewayConnection.Socket;
                if (socketCapture != null && socketCapture.Connected)
                {
                    var bytesRead = socketCapture.Receive(buffer);
                    if (bytesRead == 0)
                    {
                        throw new EndOfStreamException("Socket closed");
                    }
                    return(bytesRead);
                }
            }
            catch (Exception ex)
            {
                // Only try to reconnect if we're not shutting down
                if (Cts.IsCancellationRequested)
                {
                    return(0);
                }

                Log.Warn(ErrorCode.Runtime_Error_100158, String.Format("Exception receiving from gateway {0}: {1}", gatewayConnection.Address, ex.Message));
                gatewayConnection.MarkAsDisconnected(socketCapture);
                return(0);
            }
            return(0);
        }
Пример #2
0
        private bool FillBuffer(List <ArraySegment <byte> > buffer, int length)
        {
            var offset = 0;

            while (offset < length)
            {
                Socket socketCapture = null;
                if (Cts.IsCancellationRequested)
                {
                    return(false);
                }

                try
                {
                    if (gatewayConnection.Socket == null || !gatewayConnection.Socket.Connected)
                    {
                        gatewayConnection.Connect();
                    }
                    socketCapture = gatewayConnection.Socket;
                    if (socketCapture != null && socketCapture.Connected)
                    {
                        var bytesRead = socketCapture.Receive(ByteArrayBuilder.BuildSegmentList(buffer, offset));
                        if (bytesRead == 0)
                        {
                            throw new EndOfStreamException("Socket closed");
                        }
                        offset += bytesRead;
                    }
                }
                catch (Exception ex)
                {
                    // Only try to reconnect if we're not shutting down
                    if (Cts.IsCancellationRequested)
                    {
                        return(false);
                    }

                    if (ex is SocketException)
                    {
                        Log.Warn(ErrorCode.Runtime_Error_100158, "Exception receiving from gateway " + gatewayConnection.Address);
                    }
                    gatewayConnection.MarkAsDisconnected(socketCapture);
                    return(false);
                }
            }
            return(true);
        }
Пример #3
0
        private int FillBuffer(List <ArraySegment <byte> > bufferSegments)
        {
            try
            {
                if (gatewayConnection.Socket == null || !gatewayConnection.Socket.Connected)
                {
                    gatewayConnection.Connect();
                }
                if (!Equals(socket, gatewayConnection.Socket))
                {
                    buffer.Reset();
                    socket = gatewayConnection.Socket;
                }
                if (socket != null && socket.Connected)
                {
                    var bytesRead = socket.Receive(bufferSegments);
                    if (bytesRead == 0)
                    {
                        throw new EndOfStreamException("Socket closed");
                    }
                    return(bytesRead);
                }
            }
            catch (Exception ex)
            {
                buffer.Reset();
                // Only try to reconnect if we're not shutting down
                if (Cts.IsCancellationRequested)
                {
                    return(0);
                }

                Log.Warn(ErrorCode.Runtime_Error_100158, $"Exception receiving from gateway {gatewayConnection.Address}: {ex.Message}", ex);
                gatewayConnection.MarkAsDisconnected(socket);
                socket = null;
                return(0);
            }
            return(0);
        }