Exemple #1
0
 public bool Init(IPEndPoint LocalIpEnd)
 {
     if (!status.Equals(0))
     {
         return(false);
     }
     ProtocolMgr.RegisterProtocol(ProtocolMgr.Data_SetTcpId, AddNewClient2Dic);
     CreateTcpListener(LocalIpEnd);
     //this.getIpHandler = getIpHandler;
     this.tmpClientDic = new Dictionary <string, ClientWithTcp>();
     this.clientDic    = new Dictionary <int, string>();
     PauseClient();
     return(true);
     //clientPort = new IPEndPoint(IPAddress.Broadcast, port);
 }
Exemple #2
0
        private void AcceptTcpClient()
        {
            while (true)
            {
                switch (status)
                {
                case 1:
                    break;

                case 2:
                    continue;

                default:
                    goto StopServer;
                }
                if (tcpListener.Pending())
                {
                    TcpClient     tcpClient = tcpListener.AcceptTcpClient();
                    ClientWithTcp client    = new ClientWithTcp();
                    string        tcpId     = ToolsMgr.GetUUID();
                    client.tcpId = tcpId;
                    if (client.Init(tcpClient))
                    {
                        client.StartClient();
                        client.timeTick = System.DateTime.Now.Ticks;
                        lock (tmpClientDic)
                        {
                            tmpClientDic.Add(tcpId, client);
                        }
                        TcpRequestGetClientId pack = ProtocolMgr.GetPackageRequest <TcpRequestGetClientId>(-2);
                        client.AddReqPackage(pack);
                    }
                    else
                    {
                        LogAgent.LogError("tcpClient客户端初始化失败 + \t" + ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString() + "\r\n");
                        client.DestoryTcpServer();
                        tcpClient.Close();
                    }
                }
            }
StopServer:
            Interlocked.Add(ref status, 1);
            LogAgent.Log("!关闭Udp循环线程");
        }
Exemple #3
0
        /// <summary>
        /// 销毁
        /// </summary>
        public void DestoryTcpServer()
        {
            StopClient();
            lock (tmpClientDic)
            {
                foreach (var pairs in tmpClientDic)
                {
                    pairs.Value.DestoryTcpServer();
                }
                tmpClientDic.Clear();
                tmpClientDic = null;
            }

            lock (clientDic)
            {
                clientDic.Clear();
                clientDic = null;
            }

            ProtocolMgr.UnRegisterProtocol(ProtocolMgr.Data_SetTcpId, AddNewClient2Dic);

            SetDefaultClient();
        }