示例#1
0
        ///<summary>
        /// Manually reads a packet from the underlying networkstream
        /// <para/>
        /// Please note that this will only return anything if <see cref="CryptoConnection.ConnectionFlags"/> has the <see cref="CryptoConnectionFlags.ManualRead"/> bitfield set
        /// </summary>
        /// <param name="size">The final size of the packet that was read</param>
        /// <returns>The packet that was read</returns>
        public Packet ReadPacket(out int size)
        {
            while (this.packetQueue.Count < 1)
            {
                Thread.Sleep(1);
                if (!this.Connected)
                {
                    throw new InvalidOperationException("Disconnected");
                }
            }
            PacketWithSize pws = this.packetQueue.Dequeue();

            size = pws.Size;
            return(pws.Packet);
        }
示例#2
0
        ///<summary>
        /// Manually reads a packet from the underlying networkstream
        /// <para/>
        /// Please note that this will only return anything if <see cref="CryptoConnection.ConnectionFlags"/> has the <see cref="CryptoConnectionFlags.ManualRead"/> bitfield set
        /// </summary>
        /// <param name="timeout">The timeout (in milliseconds) to wait before throwing a TimeoutException</param>
        /// <param name="size">The final size of the packet</param>
        /// <returns>The packet that was read</returns>
        public Packet ReadPacket(int timeout, out int size)
        {
            DateTime opStart = DateTime.Now;

            while (this.packetQueue.Count < 1)
            {
                Thread.Sleep(1);
                if ((DateTime.Now - opStart).TotalMilliseconds >= timeout)
                {
                    throw new TimeoutException("Operation timed out");
                }
                else if (!this.Connected)
                {
                    throw new InvalidOperationException("Disconnected");
                }
            }
            PacketWithSize pws = this.packetQueue.Dequeue();

            size = pws.Size;
            return(pws.Packet);
        }
示例#3
0
 private void OnPacketReceived(Packet packet, int size)
 {
     if ((this.ConnectionFlags & CryptoConnectionFlags.ManualRead) == CryptoConnectionFlags.ManualRead)
     {
         PacketWithSize pws = new PacketWithSize {
             Packet = packet, Size = size
         };
         while (packetQueue.Count >= maxQueueSize) // Choking
         {
             Thread.Sleep(10);
         }
         this.packetQueue.Enqueue(pws);
     }
     else
     {
         PacketReceivedArgs args = new PacketReceivedArgs {
             P = packet, S = size, D = this.PacketReceived
         };
         if (this.PacketReceived != null)
         {
             this.context.Post(new SendOrPostCallback((o) =>
             {
                 PacketReceivedArgs a = (PacketReceivedArgs)o;
                 a.D.Invoke(a.P, a.S);
             }), args);
         }
         else
         {
             while (this.queuedEvents.Count >= maxQueueSize) // Choking
             {
                 Thread.Sleep(10);
             }
             this.queuedEvents.Enqueue(args);
         }
     }
 }