Represents an asynchronous TCP Connection that is synchronized for all critical operations. None of its methods are blocking. Therefore most actions are not completed when returning from a method but should be handled using the given events of this class. If this Connection is throttled, its Throttle will take care of the buffer size management. In that state it is illegal to change the buffersize of the throttled buffers (send- and/or receive- buffer respectively) since the throttling depends on a correctly adjusted buffersize.
 public void ApplyTo(IrcClient irc)
 {
     if (irc != null)
     {
         // add handler
         con = irc.Client;
         con.BytesReceived += rcvdHandler;
     }
     else
     {
         // remove handler
         con.BytesReceived -= rcvdHandler;
         con = null;
     }
     this.irc = irc;
 }
 public ConnectionTunnel()
 {
     SetupListener();
     server = new Connection();
 }
 private void ResetSendBuffer(Connection con)
 {
     con.SetSendBufferSize(Connection.DefaultSendBufferSize, true);
 }
        /// <summary>
        /// 
        /// </summary>
        public void HandleBytes(Connection con, ByteBuffer buf)
        {
            var packets = ExtractPackets(buf);
            foreach (var packet in packets)
            {
                var handlers = IrcProtocol.Instance[packet.Key];
                if (handlers == null)
                {
                    //if (IrcProtocol.Instance.DefaultPacketHandler == null) {
                    //    throw new NullReferenceException("DefaultPacketHandler has not been set.");
                    //}
                    IrcProtocol.Instance.DefaultPacketHandler(packet);
                }
                else
                {
                    if (handlers.Count == 1)
                    {
                        handlers[0](packet);
                    }
                    else if (handlers.Count > 1)
                    {
                        var pos = packet.Content.Position;
                        for (var i = 0; i < handlers.Count; i++)
                        {
                            var handler = handlers[i];
                            handler(packet);
                            packet.Content.Position = pos;
                        }
                    }
                }

                if (PacketReceived != null)
                {
                    PacketReceived(packet);
                }
            }
        }
 /// <summary>
 /// Removes an existent Connection from this throttle or does nothing if the connection
 /// does not belong to this throttle.
 /// </summary>
 public void Remove(Connection con)
 {
     lock (conLock)
     {
         Remove(cons.IndexOf(con));
     }
 }
 private void ResetReceiveBuffer(Connection con)
 {
     con.SetRcvBufferSize(Connection.DefaultReceiveBufferSize, true);
 }
 /// <summary>
 /// Adds a new connection to this throttle.
 /// </summary>
 public void Add(Connection con)
 {
     lock (conLock)
     {
         con.SetThrottle(this);
         cons.Add(con);
     }
     if (Active)
     {
         if (ThrottlesDownload)
             AdjustReceiveBuffer();
         if (ThrottlesUpload)
             AdjustSendBuffer();
     }
 }
 public void Add(Connection con)
 {
     cons.Add(con);
 }
 public ConnectionWriter(Connection con)
     : this()
 {
     Connection = con;
 }
 private void OnInternalSent(Connection con, int amount, bool hasReamining)
 {
     if (writing && !hasReamining)
         ContinueReadStreamData();
 }
 private void OnInternalDisconnected(Connection con, bool conLost)
 {
     if (writing)
         ResetAfterTransfer();
 }