private static void ConnectionPoolTest()
        {
            INetServerProvider    serverProvider = NetServerProvider.CreateProvider(4096, 2);
            INetTokenPoolProvider poolProvider   = NetTokenPoolProvider.CreateProvider(60);

            poolProvider.TimerEnable(false);

            int port = 12345;

            serverProvider.DisconnectedHanlder = new OnDisconnectedHandler((s) =>
            {
                Console.WriteLine(s.TokenIpEndPoint + "server disconnected");
            });
            serverProvider.AcceptHandler = new OnAcceptHandler((s) =>
            {
                poolProvider.InsertToken(new NetConnectionToken(s));
            });
            bool isStart = serverProvider.Start(port);

            if (isStart)
            {
again:
                for (int i = 0; i < 2; ++i)
                {
                    INetClientProvider clientProvider = NetClientProvider.CreateProvider();
                    clientProvider.DisconnectedHanlder = new OnDisconnectedHandler((s) =>
                    {
                        // Console.WriteLine(s.TokenIpEndPoint + " client disconnected");
                    });
                    bool isConnected = clientProvider.ConnectTo(port, "127.0.0.1");

                    Console.WriteLine(isConnected);
                }
                Console.WriteLine(poolProvider.Count);
                string info = Console.ReadLine();
                if (info == "again")
                {
                    while (poolProvider.Count > 0)
                    {
                        // poolProvider.Clear(true);
                        var item = poolProvider.GetTopToken();
                        if (item != null)
                        {
                            serverProvider.CloseToken(item.Token);
                            poolProvider.RemoveToken(item, false);
                        }
                    }
                    goto again;
                }
                else if (info == "stop")
                {
                    serverProvider.Stop();
                    goto again;
                }
                Console.ReadKey();
            }
        }
Exemple #2
0
        private static void ConnectionPoolTest()
        {
            var serverProvider = NetTool.CreateTcpSever(4096, 2);
            INetTokenPoolProvider poolProvider = NetTool.CreateTokenPoolProvider(60);
            List <IFramework.Net.Tcp.ITcpClientProvider> clientPool = new List <IFramework.Net.Tcp.ITcpClientProvider> ();

            poolProvider.TimerEnable(false);

            int port = 12345;

            serverProvider.DisconnectedCallback = new OnDisconnectedHandler((s) =>
            {
                Log.L("server disconnected:" + s.TokenId);
            });
            serverProvider.AcceptedCallback = new OnAcceptedHandler((s) =>
            {
                Log.L("accept:" + s.TokenId);
                poolProvider.InsertToken(new NetConnectionToken(s));
            });
            serverProvider.ReceivedOffsetCallback = new OnReceivedSegmentHandler((token) => {
                Log.L("server receive" + token.sToken.TokenId + ":" + Encoding.Default.GetString(token.Data.buffer, token.Data.offset, token.Data.size));
            });
            bool isStart = serverProvider.Start(port);

            if (isStart)
            {
again:
                for (int i = 0; i < 3; ++i)
                {
                    var clientProvider = NetTool.CreateTcpClient();
                    clientProvider.DisconnectedCallback = new OnDisconnectedHandler((s) =>
                    {
                        Log.L(" client disconnected:" + s.TokenId);
                    });
                    //clientProvider.ReceiveOffsetHandler = new OnReceiveOffsetHandler((SegmentToken session) =>
                    //{
                    //    Log.L(session.sToken.TokenIpEndPoint + Encoding.Default.GetString(session.Data.buffer,
                    //        session.Data.offset, session.Data.size));
                    //});
                    bool isConnected = clientProvider.ConnectTo(port, "127.0.0.1");
                    if (isConnected)
                    {
                        clientPool.Add(clientProvider);
                    }

                    Log.L("connect:" + isConnected);
                }
send:
                Log.L(poolProvider.Count);
                string info = Console.ReadLine();

                if (info == "send")
                {
                    for (int i = 0; i < poolProvider.Count; ++i)
                    {
                        var item = poolProvider.GetTokenById(i);
                        if (item == null)
                        {
                            continue;
                        }

                        serverProvider.Send(new SegmentToken(item.Token, Encoding.Default.GetBytes(DateTime.Now.ToString())));
                        Thread.Sleep(1000);
                        // poolProvider.Clear(true);
                        //var item = poolProvider.GetTopToken();
                        //if (item != null)
                        //{
                        //    serverProvider.CloseToken(item.Token);
                        //    poolProvider.RemoveToken(item, false);
                        //}
                    }
                    goto send;
                }
                else if (info == "stop")
                {
                    serverProvider.Stop();
                    goto again;
                }
                else if (info == "clear")
                {
                    poolProvider.Clear();
                    clientPool.Clear();

                    goto again;
                }
                else if (info == "client")
                {
                    for (int i = 0; i < clientPool.Count; ++i)
                    {
                        clientPool[i].Send(new SegmentOffset(Encoding.Default.GetBytes(DateTime.Now.ToString())));
                        Thread.Sleep(200);
                    }
                    goto send;
                }
                Console.ReadKey();
            }
        }