示例#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 Initialize(bool isServer = false)
        {
            NetCommonIO netIO = new NetCommonIO();

            netIO.Initialize(SelectorType.IOCP, 500);

            INetSelector selector = netIO.Selector();

            if (null != selector)
            {
                selector.RegAcceptedDelegate(PrintAcceptLog);
                selector.RegConnectedDelegate(PrintConnectedLog);
                selector.RegSentDelegate(PrintSentLog);
                selector.RegReceivedDelegate(PrintReceiveLog);
                selector.RegDisconnectedDelegate(PrintDisconnectedLog);
                selector.Start();

                if (!isServer)
                {
                    //client
                    List <ClientThreadT> client_threads = new List <ClientThreadT>();

                    Random rand = new Random(Guid.NewGuid().GetHashCode());
                    for (int i = 0; i < 100; ++i)
                    {
                        SelItem     clientItem   = new SelItem();
                        AsyncSocket clientSocket = AsyncSocket.CreateIPv4Tcp();
                        selector.AssociateSocket(clientSocket, clientItem);
                        clientSocket.Bind(IPAddress.Any, 0);
                        Console.WriteLine($"Client ip:{NetUtil.ToIportString(clientSocket.LocalEndPoint)}");

                        int           name   = rand.Next();
                        ClientThreadT thread = new ClientThreadT(clientSocket, clientItem, name.ToString());
                        client_threads.Add(thread);
                    }

                    Console.WriteLine("Wait client threads to be end ...");

                    bool finished = false;
                    while (!finished)
                    {
                        finished = true;
                        int count = 0;
                        foreach (var thread in client_threads)
                        {
                            ++count;
                            if (!thread.finished())
                            {
                                finished = false;
                                Thread.Sleep(50);
                                break;
                            }
                        }
                        if (finished)
                        {
                            break;
                        }
                    }

                    Console.WriteLine("Client threads all finished.");
                    selector.Stop();
                }
                else
                {
                    //server
                    try
                    {
                        SelItem     accept_item = new SelItem();
                        AsyncSocket listener    = AsyncSocket.CreateIPv4Tcp();
                        selector.AssociateSocket(listener, accept_item);
                        listener.Bind(IPAddress.Any, 56665);
                        listener.Listen(int.MaxValue);

                        Console.WriteLine("Server started :");

                        //begin to accept
                        while (true)
                        {
                            listener.Accept();
                            accept_item.Handle.WaitOne();

                            var     serverSocket = listener.GetAcceptedSocket();
                            SelItem server_item  = new SelItem();
                            selector.AssociateSocket(serverSocket, server_item);

                            NetData.ReceiveData(serverSocket);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }
        }
示例#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();
        }