Exemplo n.º 1
0
        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();
            }
        }
Exemplo n.º 2
0
        private static void ConnectionPoolManagerDemo()
        {
            int port = 13145;

            INetServerProvider    netServerProvider = NetServerProvider.CreateProvider();
            INetTokenPoolProvider tokenPool         = NetTokenPoolProvider.CreateProvider(60);

            tokenPool.ConnectionTimeout = 60;
            SocketToken _sToken = null;

            netServerProvider.AcceptHandler = new OnAcceptHandler((sToken) => {
                _sToken = sToken;
                tokenPool.InsertToken(new NetConnectionToken()
                {
                    Token = sToken
                });
            });

            bool isOk = netServerProvider.Start(port);

            if (isOk)
            {
                INetClientProvider netClientProvider = NetClientProvider.CreateProvider();
                netClientProvider.DisconnectedHanlder = new OnDisconnectedHandler((sToken) =>
                {
                    Console.WriteLine("client disconnected");
                });
                bool rt = netClientProvider.ConnectTo(port, "127.0.0.1");
                if (rt)
                {
                    while (tokenPool.Count == 0)
                    {
                        Thread.Sleep(10);
                    }
                    var  rtToken   = tokenPool.GetTokenBySocketToken(_sToken);
                    bool refreshRt = tokenPool.RefreshExpireToken(_sToken);
                    rt = tokenPool.RemoveToken(new NetConnectionToken()
                    {
                        Token = _sToken
                    });
                    Console.WriteLine("pool count:" + tokenPool.Count);
                    Console.ReadKey();
                }
            }
        }