コード例 #1
0
        public void InCompleted(SocketError socketError, int bytesTransferred)
        {
            if (socketError != SocketError.Success)
            {
                if (socketError == SocketError.ConnectionReset || socketError == SocketError.NoBufferSpaceAvailable ||
                    socketError == SocketError.TooManyOpenSockets)
                {
                    m_socket.EventAcceptFailed(m_endpoint, ErrorHelper.SocketErrorToErrorCode(socketError));

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

                    NetMQException exception = NetMQException.Create(socketError);

                    m_socket.EventAcceptFailed(m_endpoint, exception.ErrorCode);
                    throw exception;
                }
            }
            else
            {
                // TODO: check TcpFilters

                m_acceptedSocket.NoDelay = true;

                // Utils.TuneTcpKeepalives(m_acceptedSocket, m_options.TcpKeepalive, m_options.TcpKeepaliveCnt, m_options.TcpKeepaliveIdle, m_options.TcpKeepaliveIntvl);

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

                //  Choose I/O thread to run connecter 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 unneed 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();
            }
        }
コード例 #2
0
        //  Close the listening socket.
        private void Close()
        {
            if (m_handle == null)
            {
                return;
            }

            try
            {
                m_handle.Dispose();
                m_socket.EventClosed(m_endpoint, m_handle);
            }
            catch (SocketException ex)
            {
                m_socket.EventCloseFailed(m_endpoint, ErrorHelper.SocketErrorToErrorCode(ex.SocketErrorCode));
            }

            if (m_acceptedSocket != null)
            {
                try
                {
                    m_acceptedSocket.Dispose();
                }
                catch (SocketException)
                {
                }
            }

            m_acceptedSocket = null;
            m_handle         = null;
        }
コード例 #3
0
 public void Destroy()
 {
     if (m_handle != null)
     {
         try
         {
             m_handle.Dispose();
         }
         catch (SocketException)
         {}
         m_handle = null;
     }
 }
コード例 #4
0
ファイル: TcpConnecter.cs プロジェクト: xuzhe35/netmq
 //  Close the connecting socket.
 private void Close()
 {
     Debug.Assert(m_s != null);
     try
     {
         m_s.Dispose();
         m_socket.EventClosed(m_endpoint, m_s);
         m_s = null;
     }
     catch (SocketException ex)
     {
         m_socket.EventCloseFailed(m_endpoint, ErrorHelper.SocketErrorToErrorCode(ex.SocketErrorCode));
     }
 }
コード例 #5
0
        public void Destroy()
        {
            Debug.Assert(!m_plugged);

            if (m_handle != null)
            {
                try
                {
                    m_handle.Dispose();
                }
                catch (SocketException)
                {}
                m_handle = null;
            }
        }
コード例 #6
0
ファイル: TcpListener.cs プロジェクト: r618/netmq
        /// <summary>
        /// Close the listening socket.
        /// </summary>
        private void Close()
        {
            if (m_handle == null)
            {
                return;
            }

            try
            {
                m_handle.Dispose();
                m_socket.EventClosed(m_endpoint, m_handle);
            }
            catch (SocketException ex)
            {
                m_socket.EventCloseFailed(m_endpoint, ex.SocketErrorCode.ToErrorCode());
            }

            m_handle = null;
        }
コード例 #7
0
ファイル: PgmListener.cs プロジェクト: fhchina/netmq
        private void Close()
        {
            if (m_handle == null)
            {
                return;
            }

            try
            {
                m_handle.Dispose();
                m_socket.EventClosed(m_address.ToString(), m_handle);
            }
            catch (SocketException ex)
            {
                m_socket.EventCloseFailed(m_address.ToString(), ErrorHelper.SocketErrorToErrorCode(ex.SocketErrorCode));
            }
            catch (NetMQException ex)
            {
                m_socket.EventCloseFailed(m_address.ToString(), ex.ErrorCode);
            }
            m_handle = null;
        }
コード例 #8
0
        public void InCompleted(SocketError socketError, int bytesTransferred)
        {
            if (socketError != SocketError.Success)
            {
                if (socketError == SocketError.ConnectionReset || socketError == SocketError.NoBufferSpaceAvailable ||
                    socketError == SocketError.TooManyOpenSockets)
                {
                    m_socket.EventAcceptFailed(m_endpoint, ErrorHelper.SocketErrorToErrorCode(socketError));

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

                    NetMQException exception = NetMQException.Create(socketError);

                    m_socket.EventAcceptFailed(m_endpoint, exception.ErrorCode);
                    throw exception;
                }
            }
            else
            {
                // 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)
                    {
                        ByteArraySegment 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.
                StreamEngine engine = new StreamEngine(m_acceptedSocket, m_options, m_endpoint);

                //  Choose I/O thread to run connecter 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 unneed 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();
            }
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: sk8tz/AsyncIO
        public static void Main(string[] args)
        {
            CompletionPort completionPort = CompletionPort.Create();

            AutoResetEvent listenerEvent = new AutoResetEvent(false);
            AutoResetEvent clientEvent   = new AutoResetEvent(false);
            AutoResetEvent serverEvent   = new AutoResetEvent(false);

            AsyncSocket listener = AsyncSocket.Create(AddressFamily.InterNetwork,
                                                      SocketType.Stream, ProtocolType.Tcp);

            completionPort.AssociateSocket(listener, listenerEvent);

            AsyncSocket server = AsyncSocket.Create(AddressFamily.InterNetwork,
                                                    SocketType.Stream, ProtocolType.Tcp);

            completionPort.AssociateSocket(server, serverEvent);

            AsyncSocket client = AsyncSocket.Create(AddressFamily.InterNetwork,
                                                    SocketType.Stream, ProtocolType.Tcp);

            completionPort.AssociateSocket(client, clientEvent);

            Task.Factory.StartNew(() =>
            {
                CompletionStatus completionStatus;

                while (true)
                {
                    var result = completionPort.GetQueuedCompletionStatus(-1, out completionStatus);

                    if (result)
                    {
                        Console.WriteLine("{0} {1} {2}", completionStatus.SocketError,
                                          completionStatus.OperationType, completionStatus.BytesTransferred);

                        if (completionStatus.State != null)
                        {
                            AutoResetEvent resetEvent = (AutoResetEvent)completionStatus.State;
                            resetEvent.Set();
                        }
                    }
                }
            });

            listener.Bind(IPAddress.Any, 5555);
            listener.Listen(1);

            client.Connect("localhost", 5555);

            listener.Accept(server);


            listenerEvent.WaitOne();
            clientEvent.WaitOne();

            byte[] sendBuffer = new byte[1] {
                2
            };
            byte[] recvBuffer = new byte[1];

            client.Send(sendBuffer);
            server.Receive(recvBuffer);

            clientEvent.WaitOne();
            serverEvent.WaitOne();

            server.Dispose();
            client.Dispose();
            Console.ReadLine();
        }
コード例 #10
0
 /// <summary>
 /// Close.
 /// </summary>
 public void Dispose()
 {
     socket.Dispose();
 }
コード例 #11
0
        public void SendReceive()
        {
            CompletionPort completionPort = CompletionPort.Create();

            bool exception = false;

            var task = Task.Factory.StartNew(() =>
            {
                bool cancel = false;

                while (!cancel)
                {
                    CompletionStatus [] completionStatuses = new CompletionStatus[10];

                    int removed;

                    completionPort.GetMultipleQueuedCompletionStatus(-1, completionStatuses, out removed);

                    for (int i = 0; i < removed; i++)
                    {
                        if (completionStatuses[i].OperationType == OperationType.Signal)
                        {
                            cancel = true;
                        }
                        else if (completionStatuses[i].SocketError == SocketError.Success)
                        {
                            EventWaitHandle manualResetEvent = (EventWaitHandle)completionStatuses[i].State;
                            manualResetEvent.Set();
                        }
                        else
                        {
                            exception = true;
                        }
                    }
                }
            });

            AutoResetEvent clientEvent   = new AutoResetEvent(false);
            AutoResetEvent acceptedEvent = new AutoResetEvent(false);

            AsyncSocket listener = AsyncSocket.CreateIPv4Tcp();

            completionPort.AssociateSocket(listener, acceptedEvent);
            listener.Bind(IPAddress.Any, 0);
            int port = listener.LocalEndPoint.Port;

            listener.Listen(1);

            listener.Accept();

            AsyncSocket clientSocket = AsyncSocket.CreateIPv4Tcp();

            completionPort.AssociateSocket(clientSocket, clientEvent);
            clientSocket.Bind(IPAddress.Any, 0);
            clientSocket.Connect("localhost", port);

            clientEvent.WaitOne();
            acceptedEvent.WaitOne();

            var serverSocket = listener.GetAcceptedSocket();

            AutoResetEvent serverEvent = new AutoResetEvent(false);

            completionPort.AssociateSocket(serverSocket, serverEvent);

            byte[] recv = new byte[1];
            serverSocket.Receive(recv);

            byte[] data = new[] { (byte)1 };

            clientSocket.Send(data);
            clientEvent.WaitOne(); // wait for data to be send

            serverEvent.WaitOne(); // wait for data to be received

            Assert.AreEqual(1, recv[0]);

            completionPort.Signal(null);
            task.Wait();

            Assert.IsFalse(exception);

            completionPort.Dispose();
            listener.Dispose();
            serverSocket.Dispose();
            clientSocket.Dispose();
        }