예제 #1
0
        private void _server_ClientStatusChanged(object sender, ClientStatusChangedEventArgs args)
        {
            if (!IsRunning)
            {
                return;
            }
            long proxyClientID = args.ClientID;

            if (args.Status == ClientStatus.Connected)
            {
                TcpClientEx tcpClient = new TcpClientEx(RawPacketSpliter.Default);
                tcpClient.Tag = proxyClientID;
                tcpClient.ClientStatusChanged += TcpClient_ClientStatusChanged;
                tcpClient.MessageReceived     += TcpClient_MessageReceived;
                Task.Factory.StartNew(() =>
                {
                    bool isConnected = tcpClient.Connect(RemoteIP, (ushort)RemotePort);
                    if (isConnected)
                    {
                        bool isOK = _clientDict.TryAdd(proxyClientID, tcpClient);
                        System.Diagnostics.Debug.Assert(isOK, "add new client failed");
                    }
                    else
                    {
                        _server.CloseClient(proxyClientID);
                    }
                    if (_waitConnDict.ContainsKey(proxyClientID))
                    {
                        _waitConnDict[proxyClientID].Set();
                    }
                });
            }
            else if (args.Status == ClientStatus.Closed)
            {
                if (_clientDict.TryRemove(proxyClientID, out TcpClientEx client))
                {
                    client.Close();
                }
            }
            ClientCount = _server.Clients.Count;
            if (ClientCountChanged != null)
            {
                ClientCountChangedEventArgs countChangedArgs = new ClientCountChangedEventArgs()
                {
                    NewCount = ClientCount
                };
                ClientCountChanged(this, countChangedArgs);
            }
        }