Пример #1
0
        public void RemoteEndPoint()
        {
            CompletionPort completionPort = CompletionPort.Create();

            var listenSocket = AsyncSocket.CreateIPv4Tcp();

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

            listenSocket.Listen(1);

            listenSocket.Accept();

            var clientSocket = AsyncSocket.CreateIPv4Tcp();

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

            CompletionStatus completionStatus;

            bool accepted  = false;
            bool connected = false;

            while (!accepted || !connected)
            {
                completionPort.GetQueuedCompletionStatus(-1, out completionStatus);

                if (completionStatus.AsyncSocket == clientSocket && !connected)
                {
                    Assert.AreEqual(OperationType.Connect, completionStatus.OperationType);
                    Assert.AreEqual(SocketError.Success, completionStatus.SocketError);
                    connected = true;
                }
                else if (completionStatus.AsyncSocket == listenSocket && !accepted)
                {
                    Assert.AreEqual(OperationType.Accept, completionStatus.OperationType);
                    Assert.AreEqual(SocketError.Success, completionStatus.SocketError);
                    accepted = true;
                }
                else
                {
                    Assert.Fail();
                }
            }

            var server = listenSocket.GetAcceptedSocket();

            Assert.AreEqual(clientSocket.LocalEndPoint, server.RemoteEndPoint);
            Assert.AreEqual(clientSocket.RemoteEndPoint, server.LocalEndPoint);

            completionPort.Dispose();
            listenSocket.Dispose();
            server.Dispose();
            clientSocket.Dispose();
        }
Пример #2
0
        public void Destroy()
        {
            if (!m_stopped)
            {
                try
                {
                    m_worker.Join();
                }
                catch (Exception)
                {}

                m_stopped = true;

                m_completionPort.Dispose();
            }
        }
Пример #3
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();
        }