Beispiel #1
0
        public static void ListenTcp <T>(P2PTcpClient tcpClient) where T : ReceivePacket
        {
            try
            {
                Guid          curGuid    = Global.CurrentGuid;
                byte[]        buffer     = new byte[P2PGlobal.P2PSocketBufferSize];
                NetworkStream tcpStream  = tcpClient.GetStream();
                ReceivePacket msgReceive = Activator.CreateInstance(typeof(T)) as ReceivePacket;
                while (tcpClient.Connected && curGuid == Global.CurrentGuid)
                {
                    int curReadLength = tcpStream.ReadSafe(buffer, 0, buffer.Length);
                    if (curReadLength > 0)
                    {
                        byte[] refData = buffer.Take(curReadLength).ToArray();
                        while (msgReceive.ParseData(ref refData))
                        {
                            LogUtils.Debug($"命令类型:{msgReceive.CommandType}");
                            // 执行command
                            using (P2PCommand command = FindCommand(tcpClient, msgReceive))
                            {
                                command?.Excute();
                            }
                            //重置msgReceive
                            msgReceive.Reset();
                            if (refData.Length <= 0)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtils.Error($"【错误】Global_Func.ListenTcp:{Environment.NewLine}{ex}");
            }

            if (Global.TcpMap.ContainsKey(tcpClient.ClientName))
            {
                if (Global.TcpMap[tcpClient.ClientName].TcpClient == tcpClient)
                {
                    Global.TcpMap.Remove(tcpClient.ClientName);
                }
            }
            //如果tcp已关闭,需要关闭相关tcp
            try
            {
                tcpClient.ToClient?.Close();
            }
            catch { }
            LogUtils.Debug($"tcp连接{tcpClient.RemoteEndPoint}已断开");
        }
Beispiel #2
0
            private void BeginReadReceiveCallback(IAsyncResult asyncResult)
            {
                try
                {
                    int byteLen = Stream.EndRead(asyncResult);
                    if (byteLen <= 0)
                    {
                        Server.ClientDictionary[ID].Disconnect();
                        return;
                    }

                    byte[] data = new byte[byteLen];
                    Array.Copy(ReceiveBuffer, data, byteLen);

                    ReceivePacket.Reset(HandleData(data));
                    StreamBeginRead();
                }
                catch (Exception exception)
                {
                    Console.WriteLine($"\nError in BeginReadReceiveCallback of client {ID}...\nError: {exception}");
                    Server.ClientDictionary[ID].Disconnect();
                }
            }
        public static void ListenTcp <T>(P2PTcpClient tcpClient) where T : ReceivePacket
        {
            try
            {
                Guid          curGuid   = AppCenter.Instance.CurrentGuid;
                byte[]        buffer    = new byte[P2PGlobal.P2PSocketBufferSize];
                NetworkStream tcpStream = tcpClient.GetStream();
                tcpClient.ReceiveBufferSize = P2PGlobal.P2PSocketBufferSize;
                ReceivePacket msgReceive = Activator.CreateInstance(typeof(T)) as ReceivePacket;

                int   maxTotal      = 1024 * 50 * 2;
                int[] recieveLength = new int[2];
                int   lastSecond    = -1;


                while (tcpClient.Connected && curGuid == AppCenter.Instance.CurrentGuid)
                {
                    int curReadLength = tcpStream.ReadSafe(buffer, 0, buffer.Length);
                    if (curReadLength > 0)
                    {
                        byte[] refData = buffer.Take(curReadLength).ToArray();
                        if (tcpClient.IsSpeedLimit)
                        {
                            int curSecond = DateTime.Now.Second;
                            if (DateTime.Now.Second != lastSecond)
                            {
                                recieveLength[curSecond % 2] = 0;
                            }
                            lastSecond = curSecond;
                            recieveLength[curSecond % 2] += curReadLength;
                            if (recieveLength.Sum() > maxTotal)
                            {
                                Thread.Sleep(1000);
                            }
                        }


                        while (msgReceive.ParseData(ref refData))
                        {
                            LogUtils.Debug($"命令类型:{msgReceive.CommandType}");
                            // 执行command
                            using (P2PCommand command = FindCommand(tcpClient, msgReceive))
                            {
                                command?.Excute();
                            }
                            //重置msgReceive
                            msgReceive.Reset();
                            if (refData.Length <= 0)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtils.Error($"【错误】Global_Func.ListenTcp:{Environment.NewLine}{ex}");
            }

            if (ClientCenter.Instance.TcpMap.ContainsKey(tcpClient.ClientName))
            {
                if (ClientCenter.Instance.TcpMap[tcpClient.ClientName].TcpClient == tcpClient)
                {
                    ClientCenter.Instance.TcpMap.Remove(tcpClient.ClientName);
                }
            }
            //如果tcp已关闭,需要关闭相关tcp
            try
            {
                tcpClient.ToClient?.SafeClose();
            }
            catch { }
            LogUtils.Debug($"tcp连接{tcpClient.RemoteEndPoint}已断开");
        }