Beispiel #1
0
        private void ProcessPacket(Packet p)
        {
            if (p == null)
            {
                return;
            }
            DateTime   start = Time.Now;
            MethodInfo info;

            if (!PacketMethods.TryGetValue(p.PacketType, out info))
            {
                PacketMethods[p.PacketType] = info = GetType().GetMethod("Process", new[] { p.PacketType });
            }
            if (info == null)
            {
                throw new NotImplementedException($"未执行异常: 方式过程({p.PacketType}).");
            }

            info.Invoke(this, new object[] { p });

            if (!Monitor)
            {
                return;
            }

            TimeSpan        execution = Time.Now - start;
            DiagnosticValue value;


            if (!Diagnostics.TryGetValue(p.PacketType.FullName, out value))
            {
                Diagnostics[p.PacketType.FullName] = value = new DiagnosticValue {
                    Name = p.PacketType.FullName
                }
            }
            ;

            value.Count++;
            value.TotalTime += execution;
            value.TotalSize += p.Length;


            if (execution > value.LargestTime)
            {
                value.LargestTime = execution;
            }

            if (p.Length > value.LargestSize)
            {
                value.LargestSize = p.Length;
            }
        }
Beispiel #2
0
        public virtual void Process()
        {
            if (TcpClient == null || !TcpClient.Connected)
            {
                TryDisconnect();
                return;
            }

            while (!ReceiveList.IsEmpty && !Disconnecting)
            {
                try
                {
                    Packet p;
                    if (!ReceiveList.TryDequeue(out p))
                    {
                        continue;
                    }
                    ProcessPacket(p);
                }
                catch (NotImplementedException ex)
                {
                    OnException(this, ex);
                }
                catch (Exception ex)
                {
                    OnException(this, ex);
                    throw;
                }
            }

            if (Time.Now >= TimeOutTime)
            {
                if (!Disconnecting)
                {
                    TrySendDisconnect(new G.Disconnect {
                        Reason = DisconnectReason.TimedOut
                    });
                }
                else
                {
                    TryDisconnect();
                }

                return;
            }

            if (!Disconnecting && Sending)
            {
                UpdateTimeOut();
            }

            if (SendList.IsEmpty || Sending)
            {
                return;
            }

            List <byte> data = new List <byte>();

            while (!SendList.IsEmpty)
            {
                Packet p;

                if (!SendList.TryDequeue(out p))
                {
                    continue;
                }
                if (p == null)
                {
                    continue;
                }

                try
                {
                    byte[] bytes = p.GetPacketBytes();

                    data.AddRange(bytes);
                }
                catch (Exception ex)
                {
                    OnException?.Invoke(this, ex);
                    Disconnecting = true;
                    return;
                }

                if (!Monitor)
                {
                    continue;
                }

                DiagnosticValue value;
                Type            type = p.GetType();

                if (!Diagnostics.TryGetValue(type.FullName, out value))
                {
                    Diagnostics[type.FullName] = value = new DiagnosticValue {
                        Name = type.FullName
                    }
                }
                ;

                value.Count++;
                value.TotalSize += p.Length;

                if (p.Length > value.LargestSize)
                {
                    value.LargestSize = p.Length;
                }
            }

            BeginSend(data);
        }