Beispiel #1
0
        private static void ConnectionPoolManagerDemo()
        {
            int port = 13145;

            var netServerProvider           = NetTool.CreateTcpSever();
            INetTokenPoolProvider tokenPool = NetTool.CreateTokenPoolProvider(60);

            tokenPool.ConnectionTimeout = 60;
            SocketToken _sToken = null;

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

            bool isOk = netServerProvider.Start(port);

            if (isOk)
            {
                var netClientProvider = NetTool.CreateTcpClient();
                netClientProvider.DisconnectedCallback = new OnDisconnectedHandler((sToken) =>
                {
                    Log.L("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);
                    Log.L("pool count:" + tokenPool.Count);
                    Console.ReadKey();
                }
            }
        }
Beispiel #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();
            }
        }