Beispiel #1
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);
                    }
                }
            }
        }