Exemple #1
0
        /// <summary>
        /// This method is called when a message receive operation has been completed.
        /// </summary>
        /// <param name="socketError">a SocketError value that indicates whether Success or an error occurred</param>
        /// <param name="bytesTransferred">the number of bytes that were transferred</param>
        public void InCompleted(SocketError socketError, int bytesTransferred)
        {
            if (socketError != SocketError.Success)
            {
                m_socket.EventAcceptFailed(m_address.ToString(), socketError.ToErrorCode());

                // dispose old object
                m_acceptedSocket.Handle.Dispose();

                Accept();
            }
            else
            {
                m_acceptedSocket.InitOptions();

                var pgmSession = new PgmSession(m_acceptedSocket, m_options);

                IOThread ioThread = ChooseIOThread(m_options.Affinity);

                SessionBase session = SessionBase.Create(ioThread, false, m_socket, m_options, new Address(m_handle.LocalEndPoint));

                session.IncSeqnum();
                LaunchChild(session);
                SendAttach(session, pgmSession, false);
                m_socket.EventAccepted(m_address.ToString(), m_acceptedSocket.Handle);

                Accept();
            }
        }
Exemple #2
0
        /// <summary>
        /// This method is called when a message receive operation has been completed.
        /// </summary>
        /// <param name="socketError">a SocketError value that indicates whether Success or an error occurred</param>
        /// <param name="bytesTransferred">the number of bytes that were transferred</param>
        public void InCompleted(SocketError socketError, int bytesTransferred)
        {
            Assumes.NotNull(m_address);
            Assumes.NotNull(m_acceptedSocket);
            Assumes.NotNull(m_acceptedSocket.Handle);

            if (socketError != SocketError.Success)
            {
                m_socket.EventAcceptFailed(m_address.ToString(), socketError.ToErrorCode());

                // dispose old object
                m_acceptedSocket.Handle.Dispose();

                Accept();
            }
            else
            {
                //This if-case only concerns bound PGM Subscribers after the Ethernet cable has been unplugged (Publisher on same host)
                //or plugged in again (Publisher on different host).
                if (m_address.InterfaceAddress != null)
                {
                    try
                    {
                        m_acceptedSocket.Handle.SetSocketOption(PgmSocket.PgmLevel, PgmSocket.RM_ADD_RECEIVE_IF,
                                                                m_address.InterfaceAddress.GetAddressBytes());
                    }
                    catch
                    {
                        // dispose old object
                        m_acceptedSocket.Handle.Dispose();

                        Accept();
                        return;
                    }
                }

                m_acceptedSocket.InitOptions();

                var pgmSession = new PgmSession(m_acceptedSocket, m_options);

                IOThread?ioThread = ChooseIOThread(m_options.Affinity);

                Assumes.NotNull(ioThread);
                Assumes.NotNull(m_handle);

                SessionBase session = SessionBase.Create(ioThread, false, m_socket, m_options, new Address(m_handle.LocalEndPoint));

                session.IncSeqnum();
                LaunchChild(session);
                SendAttach(session, pgmSession, false);
                m_socket.EventAccepted(m_address.ToString(), m_acceptedSocket.Handle);

                Accept();
            }
        }
Exemple #3
0
        public static NetMQException Create(SocketError error, [CanBeNull] Exception innerException = null)
        {
            var errorCode = error.ToErrorCode();

#if DEBUG
            if (errorCode == 0)
            {
                var s = string.Format("(And within NetMQException.Create: Unanticipated error-code: {0})", error.ToString());
                return(Create(errorCode: errorCode, message: s, innerException: innerException));
            }
#endif

            return(Create(errorCode, innerException));
        }
Exemple #4
0
        public static NetMQException Create(SocketError error, Exception?innerException = null)
        {
            var errorCode = error.ToErrorCode();

#if DEBUG
            if (errorCode == 0)
            {
                var s = $"(And within NetMQException.Create: Unanticipated error-code: {error})";
                return(Create(errorCode: errorCode, message: s, innerException: innerException));
            }
#endif

            return(Create(errorCode, innerException));
        }
Exemple #5
0
        /// <summary>
        /// Create and return a new NetMQException with no Message containing the given SocketError and Exception.
        /// </summary>
        /// <param name="error">a SocketError that this exception will carry and expose via its ErrorCode property</param>
        /// <param name="innerException">an Exception that this exception will expose via its InnerException property</param>
        /// <returns>a new NetMQException</returns>
        
        public static NetMQException Create(SocketError error,  Exception innerException = null)
        {
            var errorCode = error.ToErrorCode();

#if DEBUG
            if (errorCode == 0)
            {
                var s = string.Format("(And within NetMQException.Create: Unanticipated error-code: {0})", error.ToString());
                return Create(errorCode: errorCode, message: s, innerException: innerException);
            }
#endif

            return Create(errorCode, innerException);
        }
        public static NetMQException Create(SocketError error, [CanBeNull] Exception innerException = null)
        {
            ErrorCode errorCode = error.ToErrorCode();

            #if DEBUG
            if (errorCode == 0)
            {
                string s = $"(And within NetMQException.Create: Unanticipated error-code: {error.ToString()})";
                return(NetMQException.Create(errorCode, s, innerException));
            }
            #endif

            return(NetMQException.Create(errorCode, innerException));
        }
Exemple #7
0
        /// <summary>
        /// This method is called when a message Send operation has been completed.
        /// </summary>
        /// <param name="socketError">a SocketError value that indicates whether Success or an error occurred</param>
        /// <param name="bytesTransferred">the number of bytes that were transferred</param>
        /// <exception cref="NetMQException">A non-recoverable socket error occurred.</exception>
        public override void OutCompleted(SocketError socketError, int bytesTransferred)
        {
            if (m_state == State.Connecting)
            {
                if (socketError == SocketError.Success)
                {
                    m_state     = State.Active;
                    m_writeSize = 0;

                    BeginSending();
                }
                else
                {
                    m_state = State.Error;
                    NetMQException.Create(socketError);
                }
            }
            else if (m_state == State.Active)
            {
                // We can write either all data or 0 which means rate limit reached.
                if (socketError == SocketError.Success && bytesTransferred == m_writeSize)
                {
                    m_writeSize = 0;

                    BeginSending();
                }
                else
                {
                    if (socketError == SocketError.ConnectionReset)
                    {
                        Error();
                    }
                    else
                    {
                        throw NetMQException.Create(socketError.ToErrorCode());
                    }
                }
            }
            else
            {
                Debug.Assert(false);
            }
        }
Exemple #8
0
        /// <summary>
        /// This is called when socket input has been completed.
        /// </summary>
        /// <param name="socketError">This indicates the status of the input operation - whether Success or some error.</param>
        /// <param name="bytesTransferred">the number of bytes that were transferred</param>
        /// <exception cref="NetMQException">A non-recoverable socket-error occurred.</exception>
        public void InCompleted(SocketError socketError, int bytesTransferred)
        {
            Assumes.NotNull(m_handle);
            Assumes.NotNull(m_endpoint);

            switch (socketError)
            {
            case SocketError.Success:
            {
                // TODO: check TcpFilters
                var acceptedSocket = m_handle.GetAcceptedSocket();

                if (!acceptedSocket.NoDelay)
                {
                    acceptedSocket.NoDelay = true;
                }

                if (m_options.TcpKeepalive != -1)
                {
                    acceptedSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, m_options.TcpKeepalive);

                    if (m_options.TcpKeepaliveIdle != -1 && m_options.TcpKeepaliveIntvl != -1)
                    {
                        var bytes = new ByteArraySegment(new byte[12]);

                        Endianness endian = BitConverter.IsLittleEndian ? Endianness.Little : Endianness.Big;

                        bytes.PutInteger(endian, m_options.TcpKeepalive, 0);
                        bytes.PutInteger(endian, m_options.TcpKeepaliveIdle, 4);
                        bytes.PutInteger(endian, m_options.TcpKeepaliveIntvl, 8);

                        acceptedSocket.IOControl(IOControlCode.KeepAliveValues, (byte[])bytes, null);
                    }
                }

                // Create the engine object for this connection.
                var engine = new StreamEngine(acceptedSocket, m_options, m_endpoint);

                // Choose I/O thread to run connector in. Given that we are already
                // running in an I/O thread, there must be at least one available.
                IOThread?ioThread = ChooseIOThread(m_options.Affinity);

                Assumes.NotNull(ioThread);

                // Create and launch a session object.
                // TODO: send null in address parameter, is unneeded in this case
                SessionBase session = SessionBase.Create(ioThread, false, m_socket, m_options, new Address(m_handle.LocalEndPoint));
                session.IncSeqnum();
                LaunchChild(session);

                SendAttach(session, engine, false);

                m_socket.EventAccepted(m_endpoint, acceptedSocket);

                Accept();
                break;
            }

            case SocketError.ConnectionReset:
            case SocketError.NoBufferSpaceAvailable:
            case SocketError.TooManyOpenSockets:
            {
                m_socket.EventAcceptFailed(m_endpoint, socketError.ToErrorCode());

                Accept();
                break;
            }

            default:
            {
                NetMQException exception = NetMQException.Create(socketError);

                m_socket.EventAcceptFailed(m_endpoint, exception.ErrorCode);
                throw exception;
            }
            }
        }
Exemple #9
0
        /// <summary>
        /// This method is called when a message Send operation has been completed.
        /// </summary>
        /// <param name="socketError">a SocketError value that indicates whether Success or an error occurred</param>
        /// <param name="bytesTransferred">the number of bytes that were transferred</param>
        /// <exception cref="NetMQException">A non-recoverable socket error occurred.</exception>
        public override void OutCompleted(SocketError socketError, int bytesTransferred)
        {
            if (m_state == State.Connecting)
            {
                if (socketError == SocketError.Success)
                {
                    m_state = State.Active;
                    m_writeSize = 0;

                    BeginSending();
                }
                else
                {
                    m_state = State.Error;
                    NetMQException.Create(socketError);
                }
            }
            else if (m_state == State.Active)
            {
                // We can write either all data or 0 which means rate limit reached.
                if (socketError == SocketError.Success && bytesTransferred == m_writeSize)
                {
                    m_writeSize = 0;

                    BeginSending();
                }
                else
                {
                    Debug.Assert(false);

                    throw NetMQException.Create(socketError.ToErrorCode());
                }
            }
            else
            {
                Debug.Assert(false);
            }
        }
Exemple #10
0
        /// <summary>
        /// This is called when socket input has been completed.
        /// </summary>
        /// <param name="socketError">This indicates the status of the input operation - whether Success or some error.</param>
        /// <param name="bytesTransferred">the number of bytes that were transferred</param>
        /// <exception cref="NetMQException">A non-recoverable socket-error occurred.</exception>
        public void InCompleted(SocketError socketError, int bytesTransferred)
        {
            switch (socketError)
            {
                case SocketError.Success:
                {
                    // TODO: check TcpFilters

                    m_acceptedSocket.NoDelay = true;

                    if (m_options.TcpKeepalive != -1)
                    {
                        m_acceptedSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, m_options.TcpKeepalive);

                        if (m_options.TcpKeepaliveIdle != -1 && m_options.TcpKeepaliveIntvl != -1)
                        {
                            var bytes = new ByteArraySegment(new byte[12]);

                            Endianness endian = BitConverter.IsLittleEndian ? Endianness.Little : Endianness.Big;

                            bytes.PutInteger(endian, m_options.TcpKeepalive, 0);
                            bytes.PutInteger(endian, m_options.TcpKeepaliveIdle, 4);
                            bytes.PutInteger(endian, m_options.TcpKeepaliveIntvl, 8);

                            m_acceptedSocket.IOControl(IOControlCode.KeepAliveValues, (byte[])bytes, null);
                        }
                    }

                    // Create the engine object for this connection.
                    var engine = new StreamEngine(m_acceptedSocket, m_options, m_endpoint);

                    // Choose I/O thread to run connector in. Given that we are already
                    // running in an I/O thread, there must be at least one available.
                    IOThread ioThread = ChooseIOThread(m_options.Affinity);

                    // Create and launch a session object. 
                    // TODO: send null in address parameter, is unneeded in this case
                    SessionBase session = SessionBase.Create(ioThread, false, m_socket, m_options, new Address(m_handle.LocalEndPoint));
                    session.IncSeqnum();
                    LaunchChild(session);

                    SendAttach(session, engine, false);

                    m_socket.EventAccepted(m_endpoint, m_acceptedSocket);

                    Accept();
                    break;
                }
                case SocketError.ConnectionReset:
                case SocketError.NoBufferSpaceAvailable:
                case SocketError.TooManyOpenSockets:
                {
                    m_acceptedSocket.Dispose();
                    m_socket.EventAcceptFailed(m_endpoint, socketError.ToErrorCode());

                    Accept();
                    break;
                }
                default:
                {
                    m_acceptedSocket.Dispose();

                    NetMQException exception = NetMQException.Create(socketError);

                    m_socket.EventAcceptFailed(m_endpoint, exception.ErrorCode);
                    throw exception;
                }
            }
        }
Exemple #11
0
        /// <summary>
        /// This method is called when a message receive operation has been completed.
        /// </summary>
        /// <param name="socketError">a SocketError value that indicates whether Success or an error occurred</param>
        /// <param name="bytesTransferred">the number of bytes that were transferred</param>
        public void InCompleted(SocketError socketError, int bytesTransferred)
        {
            if (socketError != SocketError.Success)
            {
                m_socket.EventAcceptFailed(m_address.ToString(), socketError.ToErrorCode());

                // dispose old object                
                m_acceptedSocket.Handle.Dispose();

                Accept();
            }
            else
            {
                m_acceptedSocket.InitOptions();

                var pgmSession = new PgmSession(m_acceptedSocket, m_options);

                IOThread ioThread = ChooseIOThread(m_options.Affinity);

                SessionBase session = SessionBase.Create(ioThread, false, m_socket, m_options, new Address(m_handle.LocalEndPoint));

                session.IncSeqnum();
                LaunchChild(session);
                SendAttach(session, pgmSession, false);
                m_socket.EventAccepted(m_address.ToString(), m_acceptedSocket.Handle);

                Accept();
            }
        }
Exemple #12
0
        public static NetMQException Create(SocketError error, [CanBeNull] Exception innerException = null)
        {
            var errorCode = error.ToErrorCode();

#if DEBUG
            if (errorCode == 0)
            {
                var s = $"(And within NetMQException.Create: Unanticipated error-code: {error.ToString()})";
                return Create(errorCode: errorCode, message: s, innerException: innerException);
            }
#endif

            return Create(errorCode, innerException);
        }