Beispiel #1
0
        /// <summary>
        ///     匹配对应的Command命令
        /// </summary>
        /// <param name="tcpClient"></param>
        /// <param name="packet"></param>
        /// <returns></returns>
        public static P2PCommand FindCommand(P2PTcpClient tcpClient, ReceivePacket packet)
        {
            P2PCommand command = null;

            if (Global.AllowAnonymous.Contains(packet.CommandType) || tcpClient.IsAuth)
            {
                if (Global.CommandDict.ContainsKey(packet.CommandType))
                {
                    Type type = Global.CommandDict[packet.CommandType];
                    command = Activator.CreateInstance(type, tcpClient, packet.GetBytes()) as P2PCommand;
                }
                else
                {
                    LogUtils.Warning($"{tcpClient.RemoteEndPoint}请求了未知命令{packet.CommandType}");
                }
            }
            else
            {
                tcpClient.Close();
                if (tcpClient.ToClient != null && tcpClient.ToClient.Connected)
                {
                    tcpClient.ToClient.Close();
                }
                LogUtils.Warning($"拦截{tcpClient.RemoteEndPoint}未授权命令");
            }
            return(command);
        }
        public void TestSocketParse()
        {
            byte[]      data       = ("这是一条测试数据").ToBytes();
            SendPacket  sendPacket = new SendPacket(data);
            List <byte> dataList   = new List <byte>();

            dataList.AddRange(sendPacket.PackData());
            dataList.AddRange(sendPacket.PackData());
            data = dataList.ToArray();
            ReceivePacket ReceivePacket = new ReceivePacket();

            while (data.Length > 0)
            {
                if (ReceivePacket.ParseData(ref data))
                {
                    string str = ReceivePacket.GetBytes().ToStringUnicode();
                    Console.WriteLine(str);
                    Assert.AreEqual("这是一条测试数据", str);
                    ReceivePacket = new ReceivePacket();
                }
                else
                {
                    Assert.Fail();
                    break;
                }
            }
        }