Author: alanmc
Date: 2006-09-17 07:13:06 -0400 (Sun, 17 Sep 2006)
New Revision: 65527

Modified:
   trunk/bitsharp/src/MonoTorrent.Client/PeerConnections/PeerConnectionBase.cs
   trunk/bitsharp/src/MonoTorrent.Client/PeerConnections/TCPConnection.cs
   trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/BitfieldMessage.cs
   trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/CancelMessage.cs
   trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/ChokeMessage.cs
   
trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/FastPeerExtensions/SuggestPieceMessage.cs
   trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/HandshakeMessage.cs
   trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/HaveMessage.cs
   trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/IPeerMessage.cs
   trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/InterestedMessage.cs
   trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/KeepAliveMessage.cs
   trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/NotInterestedMessage.cs
   trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/PeerwireEncoder.cs
   trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/PieceMessage.cs
   trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/PortMessage.cs
   trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/RequestMessage.cs
   trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/UnchokeMessage.cs
Log:
Misc tidying. SuggestPieceMessages handled correctly

Modified: 
trunk/bitsharp/src/MonoTorrent.Client/PeerConnections/PeerConnectionBase.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PeerConnections/PeerConnectionBase.cs 
2006-09-17 11:02:06 UTC (rev 65526)
+++ trunk/bitsharp/src/MonoTorrent.Client/PeerConnections/PeerConnectionBase.cs 
2006-09-17 11:13:06 UTC (rev 65527)
@@ -43,43 +43,48 @@
     public abstract class PeerConnectionBase : IDisposable
     {
         #region Member Variables
+#warning Use these to request pieces
         /// <summary>
-        /// True if the peer supports the Fast Peers extension
+        /// Contains the indexs of all the pieces we can request even if choked
         /// </summary>
-        public bool SupportsFastPeer
+        public List<int> AllowedFastPieces
         {
-            get { return this.supportsFastPeer; }
-            set { this.supportsFastPeer = value; }
+            get { return this.allowedFastPieces; }
         }
-        private bool supportsFastPeer;
+        private List<int> allowedFastPieces;
 
 
         /// <summary>
-        /// The size of the send and recieve buffers. Defaults to 32bytes more 
than my default request size.
+        /// True if we are currently choking the peer
         /// </summary>
-        private static readonly int BufferSize = ((1 << 14) + 32);
+        public bool AmChoking
+        {
+            get { return this.amChoking; }
+            internal set { this.amChoking = value; }
+        }
+        private bool amChoking;
 
 
         /// <summary>
-        /// The time at which the last message was sent at
+        /// True if the peer has some pieces that we need
         /// </summary>
-        public DateTime LastMessageSent
+        public bool AmInterested
         {
-            get { return this.lastMessageSent; }
-            internal set { this.lastMessageSent = value; }
+            get { return this.amInterested; }
+            internal set { this.amInterested = value; }
         }
-        private DateTime lastMessageSent;
+        private bool amInterested;
 
 
         /// <summary>
-        /// The time at which the last message was received at
+        /// 
         /// </summary>
-        public DateTime LastMessageRecieved
+        public int AmRequestingPiecesCount
         {
-            get { return this.lastMessageRecieved; }
-            internal set { this.lastMessageRecieved = value; }
+            get { return this.amRequestingPiecesCount; }
+            set { this.amRequestingPiecesCount = value; }
         }
-        private DateTime lastMessageRecieved;
+        private int amRequestingPiecesCount;
 
 
         /// <summary>
@@ -94,49 +99,39 @@
 
 
         /// <summary>
-        /// The number of pieces that we've sent the peer.
+        /// The total number of bytes recieved into the current recieve buffer
         /// </summary>
-        public int PiecesSent
+        public int BytesRecieved
         {
-            get { return this.piecesSent; }
-            internal set { this.piecesSent = value; }
+            get { return this.bytesRecieved; }
+            set { this.bytesRecieved = value; }
         }
-        private int piecesSent;
+        private int bytesRecieved;
 
 
         /// <summary>
-        /// 
+        /// The total number of bytes sent from the current send buffer
         /// </summary>
-        public int IsRequestingPiecesCount
+        public int BytesSent
         {
-            get { return this.isRequestingPiecesCount; }
-            set { this.isRequestingPiecesCount = value; }
+            get { return this.bytesSent; }
+            set { this.bytesSent = value; }
         }
-        private int isRequestingPiecesCount;
+        private int bytesSent;
 
 
         /// <summary>
-        /// 
+        /// The total number of bytes to receive
         /// </summary>
-        public int AmRequestingPiecesCount
+        public int BytesToRecieve
         {
-            get { return this.amRequestingPiecesCount; }
-            set { this.amRequestingPiecesCount = value; }
+            get { return this.bytesToRecieve; }
+            set { this.bytesToRecieve = value; }
         }
-        private int amRequestingPiecesCount;
+        private int bytesToRecieve;
 
 
         /// <summary>
-        /// True if we are currently processing the peers message queue
-        /// </summary>
-        public bool ProcessingQueue
-        {
-            get { return this.processingQueue; }
-            set { this.processingQueue = value; }
-        }
-        private bool processingQueue;
-
-        /// <summary>
         /// The total bytes to send from the buffer
         /// </summary>
         public int BytesToSend
@@ -148,49 +143,78 @@
 
 
         /// <summary>
-        /// The total number of bytes sent from the current send buffer
+        /// The size of the send and recieve buffers. Defaults to 32bytes more 
than my default request size.
         /// </summary>
-        public int BytesSent
+        private const int BufferSize = ((1 << 14) + 32);
+
+
+        /// <summary>
+        /// The current encryption method being used to encrypt connections
+        /// </summary>
+        public IEncryptor Encryptor
         {
-            get { return this.bytesSent; }
-            set { this.bytesSent = value; }
+            get { return this.encryptor; }
+            internal set { this.encryptor = value; }
         }
-        private int bytesSent;
+        private IEncryptor encryptor;
 
 
         /// <summary>
-        /// The total number of bytes recieved into the current recieve buffer
+        /// True if the peer is currently choking us
         /// </summary>
-        public int BytesRecieved
+        public bool IsChoking
         {
-            get { return this.bytesRecieved; }
-            set { this.bytesRecieved = value; }
+            get { return this.isChoking; }
+            internal set { this.isChoking = value; }
         }
-        private int bytesRecieved;
+        private bool isChoking;
 
 
         /// <summary>
-        /// The total number of bytes to receive
+        /// True if the peer is currently interested in us
         /// </summary>
-        public int BytesToRecieve
+        public bool IsInterested
         {
-            get { return this.bytesToRecieve; }
-            set { this.bytesToRecieve = value; }
+            get { return this.isInterested; }
+            internal set { this.isInterested = value; }
         }
-        private int bytesToRecieve;
+        private bool isInterested;
 
 
         /// <summary>
-        /// This holds the peermessages waiting to be sent
+        /// 
         /// </summary>
-        private Queue<IPeerMessage> sendQueue;
+        public int IsRequestingPiecesCount
+        {
+            get { return this.isRequestingPiecesCount; }
+            set { this.isRequestingPiecesCount = value; }
+        }
+        private int isRequestingPiecesCount;
 
 
-        internal byte[] recieveBuffer;
-        internal byte[] sendBuffer;
+        /// <summary>
+        /// The time at which the last message was received at
+        /// </summary>
+        public DateTime LastMessageRecieved
+        {
+            get { return this.lastMessageRecieved; }
+            internal set { this.lastMessageRecieved = value; }
+        }
+        private DateTime lastMessageRecieved;
 
 
         /// <summary>
+        /// The time at which the last message was sent at
+        /// </summary>
+        public DateTime LastMessageSent
+        {
+            get { return this.lastMessageSent; }
+            internal set { this.lastMessageSent = value; }
+        }
+        private DateTime lastMessageSent;
+
+
+        /// <summary>
         /// The connection Monitor for this peer
         /// </summary>
         public ConnectionMonitor Monitor
@@ -201,58 +225,76 @@
 
 
         /// <summary>
-        /// True if the peer has some pieces that we need
+        /// The number of pieces that we've sent the peer.
         /// </summary>
-        public bool AmInterested
+        public int PiecesSent
         {
-            get { return this.amInterested; }
-            internal set { this.amInterested = value; }
+            get { return this.piecesSent; }
+            internal set { this.piecesSent = value; }
         }
-        private bool amInterested;
+        private int piecesSent;
 
 
         /// <summary>
-        /// True if we are currently choking the peer
+        /// The port the peer is listening on for DHT
         /// </summary>
-        public bool AmChoking
+        public ushort Port
         {
-            get { return this.amChoking; }
-            internal set { this.amChoking = value; }
+            get { return this.port; }
+            internal set { this.port = value; }
         }
-        private bool amChoking;
+        private ushort port;
 
 
         /// <summary>
-        /// True if the peer is currently choking us
+        /// True if we are currently processing the peers message queue
         /// </summary>
-        public bool IsChoking
+        public bool ProcessingQueue
         {
-            get { return this.isChoking; }
-            internal set { this.isChoking = value; }
+            get { return this.processingQueue; }
+            set { this.processingQueue = value; }
         }
-        private bool isChoking;
+        private bool processingQueue;
 
 
         /// <summary>
-        /// True if the peer is currently interested in us
+        /// The byte array used to buffer data before it's sent
         /// </summary>
-        public bool IsInterested
+        internal byte[] sendBuffer;
+
+
+        /// <summary>
+        /// This holds the peermessages waiting to be sent
+        /// </summary>
+        private Queue<IPeerMessage> sendQueue;
+
+
+        /// <summary>
+        /// True if the peer supports the Fast Peers extension
+        /// </summary>
+        public bool SupportsFastPeer
         {
-            get { return this.isInterested; }
-            internal set { this.isInterested = value; }
+            get { return this.supportsFastPeer; }
+            internal set { this.supportsFastPeer = value; }
         }
-        private bool isInterested;
+        private bool supportsFastPeer;
 
 
         /// <summary>
-        /// The current encryption method being used to encrypt connections
+        /// A list of pieces that this peer has suggested we download. These 
should be downloaded
+        /// with higher priority than standard pieces.
         /// </summary>
-        public IEncryptor Encryptor
+        public List<int> SuggestedPieces
         {
-            get { return this.encryptor; }
-            internal set { this.encryptor = value; }
+            get { return this.suggestedPieces; }
         }
-        private IEncryptor encryptor;
+        private List<int> suggestedPieces;
+
+
+        /// <summary>
+        /// The byte array used to buffer data while it's being received
+        /// </summary>
+        internal byte[] recieveBuffer;
         #endregion
 
 
@@ -266,6 +308,7 @@
             this.encryptor = encryptor;
             this.amChoking = true;
             this.isChoking = true;
+            this.allowedFastPieces = new List<int>();
             this.bitField = new BitField(bitfieldLength);
             this.monitor = new ConnectionMonitor();
             this.sendQueue = new Queue<IPeerMessage>(4);

Modified: trunk/bitsharp/src/MonoTorrent.Client/PeerConnections/TCPConnection.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PeerConnections/TCPConnection.cs      
2006-09-17 11:02:06 UTC (rev 65526)
+++ trunk/bitsharp/src/MonoTorrent.Client/PeerConnections/TCPConnection.cs      
2006-09-17 11:13:06 UTC (rev 65527)
@@ -39,17 +39,6 @@
     public class TCPConnection : PeerConnectionBase
     {
         #region Member Variables
-#warning Use these to request pieces
-        /// <summary>
-        /// Contains the indexs of all the pieces we can request even if choked
-        /// </summary>
-        public List<int> AllowedFastPieces
-        {
-            get { return this.allowedFastPieces; }
-        }
-        private List<int> allowedFastPieces;
-
-
         public IPEndPoint PeerEndPoint
         {
             get { return this.peerEndPoint; }
@@ -65,7 +54,6 @@
         public TCPConnection(string location, int bitfieldLength, IEncryptor 
encryptor)
             : base(bitfieldLength, encryptor)
         {
-            this.allowedFastPieces = new List<int>();
             string[] s = location.Split(':');
             if (s.Length != 2)
                 throw new ArgumentException("Location should be in the form 
ipaddress:port", "location");
@@ -77,7 +65,6 @@
         public TCPConnection(IPEndPoint endPoint, int bitfieldLength, 
IEncryptor encryptor)
             : base(bitfieldLength, encryptor)
         {
-            this.allowedFastPieces = new List<int>();
             this.peerEndPoint = endPoint;
             this.peerSocket = new Socket(AddressFamily.InterNetwork, 
SocketType.Stream, ProtocolType.Tcp);
         }
@@ -85,7 +72,6 @@
         public TCPConnection(Socket socket, int bitfieldLength, IEncryptor 
encryptor)
             : base(bitfieldLength, encryptor)
         {
-            this.allowedFastPieces = new List<int>();
             this.peerEndPoint = (IPEndPoint)socket.RemoteEndPoint;
             this.peerSocket = socket;
         }

Modified: trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/BitfieldMessage.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/BitfieldMessage.cs       
2006-09-17 11:02:06 UTC (rev 65526)
+++ trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/BitfieldMessage.cs       
2006-09-17 11:13:06 UTC (rev 65527)
@@ -75,7 +75,7 @@
         #endregion
 
 
-        #region Helper Methods
+        #region Methods
         /// <summary>
         /// Encodes the BitfieldMessage into the supplied buffer
         /// </summary>

Modified: trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/CancelMessage.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/CancelMessage.cs 
2006-09-17 11:02:06 UTC (rev 65526)
+++ trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/CancelMessage.cs 
2006-09-17 11:13:06 UTC (rev 65527)
@@ -148,19 +148,18 @@
             for (int i = 0; i < id.Peer.Connection.QueueLength; i++)
             {
                 msg = id.Peer.Connection.DeQueue();
-                if (!msg is PieceMessage)
+                if (!(msg is PieceMessage))
                 {
                     id.Peer.Connection.EnQueue(msg);
+                    continue;
                 }
-                else
+
+                PieceMessage piece = msg as PieceMessage;
+                if (!(piece.PieceIndex == this.pieceIndex
+                    && piece.StartOffset == this.startOffset
+                    && piece.BlockLength == this.requestLength))
                 {
-                    PieceMessage piece = msg as PieceMessage;
-                    if (!(piece.PieceIndex == this.pieceIndex
-                        && piece.StartOffset == this.startOffset
-                        && piece.BlockLength = this.requestLength))
-                    {
-                        id.Peer.Connection.EnQueue(msg);
-                    }
+                    id.Peer.Connection.EnQueue(msg);
                 }
             }
         }

Modified: trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/ChokeMessage.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/ChokeMessage.cs  
2006-09-17 11:02:06 UTC (rev 65526)
+++ trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/ChokeMessage.cs  
2006-09-17 11:13:06 UTC (rev 65527)
@@ -51,7 +51,7 @@
         #endregion
 
 
-        #region Helper Methods
+        #region Methods
         /// <summary>
         /// Encodes the ChokeMessage into the supplied buffer
         /// </summary>
@@ -88,7 +88,7 @@
             id.Peer.Connection.IsChoking = true;
 
             // If he supports fast peer, messages are only rejected with a 
RejectRequest message
-            if (!ClientEngine.SupportsFastPeer)
+            if (!id.Peer.Connection.SupportsFastPeer)
                 id.TorrentManager.PieceManager.RemoveRequests(id);
         }
 

Modified: 
trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/FastPeerExtensions/SuggestPieceMessage.cs
===================================================================
--- 
trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/FastPeerExtensions/SuggestPieceMessage.cs
        2006-09-17 11:02:06 UTC (rev 65526)
+++ 
trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/FastPeerExtensions/SuggestPieceMessage.cs
        2006-09-17 11:13:06 UTC (rev 65527)
@@ -61,7 +61,7 @@
 
         public void Handle(PeerConnectionID id)
         {
-#warning Store a list of recommended pieces for the PiecePicking algorithm
+            id.Peer.Connection.SuggestedPieces.Add(this.pieceIndex);
         }
 
 

Modified: trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/HandshakeMessage.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/HandshakeMessage.cs      
2006-09-17 11:02:06 UTC (rev 65526)
+++ trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/HandshakeMessage.cs      
2006-09-17 11:13:06 UTC (rev 65527)
@@ -211,6 +211,7 @@
             throw new NotImplementedException();
         }
 
+
         public int ByteLength
         {
 #warning Update this

Modified: trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/HaveMessage.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/HaveMessage.cs   
2006-09-17 11:02:06 UTC (rev 65526)
+++ trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/HaveMessage.cs   
2006-09-17 11:13:06 UTC (rev 65527)
@@ -38,8 +38,8 @@
     /// </summary>
     public class HaveMessage : IPeerMessage
     {
+        public const int MessageId = 4;
         private const int messageLength = 5;
-        public const int MessageId = 4;
 
 
         #region Member Variables
@@ -74,7 +74,7 @@
         #endregion
 
 
-        #region Helper Methods
+        #region Methods
         /// <summary>
         /// Encodes the HaveMessage into the supplied buffer
         /// </summary>
@@ -134,6 +134,7 @@
             id.Peer.IsSeeder = true;
         }
 
+
         /// <summary>
         /// Returns the length of the message in bytes
         /// </summary>

Modified: trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/IPeerMessage.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/IPeerMessage.cs  
2006-09-17 11:02:06 UTC (rev 65526)
+++ trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/IPeerMessage.cs  
2006-09-17 11:13:06 UTC (rev 65527)
@@ -48,7 +48,6 @@
         /// <summary>
         /// Decodes a peer message from the supplied buffer
         /// </summary>
-        /// <param name="id">The peer to decode the message from</param>
         /// <param name="buffer">The buffer to decode the message from</param>
         /// <param name="offset">The offset thats the message starts at</param>
         /// <param name="length">The maximum number of bytes to read from the 
buffer</param>

Modified: 
trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/InterestedMessage.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/InterestedMessage.cs     
2006-09-17 11:02:06 UTC (rev 65526)
+++ trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/InterestedMessage.cs     
2006-09-17 11:13:06 UTC (rev 65527)
@@ -38,8 +38,8 @@
     /// </summary>
     public class InterestedMessage : IPeerMessage
     {
+        public const int MessageId = 2;
         private const int messageLength = 1;
-        public const int MessageId = 2;
 
 
         #region Constructors
@@ -52,11 +52,10 @@
         #endregion
 
 
-        #region Helper Methods
+        #region Methods
         /// <summary>
         /// Encodes the InterestedMessage into the supplied buffer
         /// </summary>
-        /// <param name="id">The peer who we are about to send the message 
to</param>
         /// <param name="buffer">The buffer to encode the message to</param>
         /// <param name="offset">The offset at which to start encoding the 
data to</param>
         /// <returns>The number of bytes encoded into the buffer</returns>
@@ -81,6 +80,7 @@
             // No decoding needed.
         }
 
+
         /// <summary>
         /// Performs any necessary actions required to process the message
         /// </summary>

Modified: trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/KeepAliveMessage.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/KeepAliveMessage.cs      
2006-09-17 11:02:06 UTC (rev 65526)
+++ trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/KeepAliveMessage.cs      
2006-09-17 11:13:06 UTC (rev 65527)
@@ -49,7 +49,7 @@
         #endregion
 
 
-        #region Helper Methods
+        #region Methods
         /// <summary>
         /// Encodes the KeepAliveMessage into the supplied buffer
         /// </summary>
@@ -71,7 +71,6 @@
         /// <summary>
         /// Decodes a KeepAliveMessage from the supplied buffer
         /// </summary>
-        /// <param name="id">The peer to decode the message from</param>
         /// <param name="buffer">The buffer to decode the message from</param>
         /// <param name="offset">The offset thats the message starts at</param>
         /// <param name="length">The maximum number of bytes to read from the 
buffer</param>
@@ -80,6 +79,7 @@
             // No decoding needed
         }
 
+
         /// <summary>
         /// Performs any necessary actions required to process the message
         /// </summary>

Modified: 
trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/NotInterestedMessage.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/NotInterestedMessage.cs  
2006-09-17 11:02:06 UTC (rev 65526)
+++ trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/NotInterestedMessage.cs  
2006-09-17 11:13:06 UTC (rev 65527)
@@ -79,6 +79,7 @@
             // No decoding needed
         }
 
+
         /// <summary>
         /// Performs any necessary actions required to process the message
         /// </summary>

Modified: trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/PeerwireEncoder.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/PeerwireEncoder.cs       
2006-09-17 11:02:06 UTC (rev 65526)
+++ trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/PeerwireEncoder.cs       
2006-09-17 11:13:06 UTC (rev 65527)
@@ -50,6 +50,8 @@
             switch (buffer[offset])
             {
                 case AllowedFastMessage.MessageId:
+                    if (!ClientEngine.SupportsFastPeer)
+                        throw new ProtocolException("Message not supported");
                     message = new AllowedFastMessage();
                     break;
 
@@ -66,10 +68,14 @@
                     break;
 
                 case ExtendedListMessage.MessageId: // 20
+                    if (!ClientEngine.SupportsFastPeer)
+                        throw new ProtocolException("Message not supported");
                     message = new ExtendedListMessage();
                     break;
 
                 case HaveAllMessage.MessageId:
+                    if (!ClientEngine.SupportsFastPeer)
+                        throw new ProtocolException("Message not supported");
                     message = new HaveAllMessage();
                     break;
 
@@ -78,13 +84,15 @@
                     break;
 
                 case HaveNoneMessage.MessageId:
+                    if (!ClientEngine.SupportsFastPeer)
+                        throw new ProtocolException("Message not supported");
                     message = new HaveNoneMessage();
                     break;
 
                 case InterestedMessage.MessageId:   // 2
                     message = new InterestedMessage();
                     break;
- 
+
                 case NotInterestedMessage.MessageId:// 3
                     message = new NotInterestedMessage();
                     break;
@@ -96,8 +104,10 @@
                 case PortMessage.MessageId:         // 9
                     message = new PortMessage();
                     break;
-                    
+
                 case RejectRequestMessage.MessageId:
+                    if (!ClientEngine.SupportsFastPeer)
+                        throw new ProtocolException("Message not supported");
                     message = new RejectRequestMessage();
                     break;
 
@@ -106,6 +116,8 @@
                     break;
 
                 case SuggestPieceMessage.MessageId:
+                    if (!ClientEngine.SupportsFastPeer)
+                        throw new ProtocolException("Message not supported");
                     message = new RequestMessage();
                     break;
 
@@ -115,6 +127,8 @@
 
 
                 case 21:                            // An "extended" message
+                    if (!ClientEngine.SupportsFastPeer)
+                        throw new ProtocolException("Message not supported");
                     switch 
(IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, ++offset)))
                     {
                         default:
@@ -122,8 +136,7 @@
                     }
 
                 default:
-#warning LOG THESE
-                    return null;        
+                    throw new ProtocolException("Unknown message: " + 
BitConverter.ToString(buffer, offset, count));
             }
 
             // The message length is given in the second byte and the message 
body follows directly after that

Modified: trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/PieceMessage.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/PieceMessage.cs  
2006-09-17 11:02:06 UTC (rev 65526)
+++ trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/PieceMessage.cs  
2006-09-17 11:13:06 UTC (rev 65527)
@@ -110,7 +110,6 @@
         /// <summary>
         /// Encodes the PieceMessage into the supplied buffer
         /// </summary>
-        /// <param name="id">The peer who we are about to send the message 
to</param>
         /// <param name="buffer">The buffer to encode the message to</param>
         /// <param name="offset">The offset at which to start encoding the 
data to</param>
         /// <returns>The number of bytes encoded into the buffer</returns>
@@ -131,7 +130,6 @@
         /// <summary>
         /// Decodes a PieceMessage from the supplied buffer
         /// </summary>
-        /// <param name="id">The peer to decode the message from</param>
         /// <param name="buffer">The buffer to decode the message from</param>
         /// <param name="offset">The offset thats the message starts at</param>
         /// <param name="length">The maximum number of bytes to read from the 
buffer</param>

Modified: trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/PortMessage.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/PortMessage.cs   
2006-09-17 11:02:06 UTC (rev 65526)
+++ trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/PortMessage.cs   
2006-09-17 11:13:06 UTC (rev 65527)
@@ -73,7 +73,7 @@
         #endregion
 
 
-        #region Helper Methods
+        #region Methods
         /// <summary>
         /// Encodes the PortMessage into the supplied buffer
         /// </summary>
@@ -82,8 +82,8 @@
         /// <returns>The number of bytes encoded into the buffer</returns>
         public int Encode(byte[] buffer, int offset)
         {
+            buffer[offset + 4] = (byte)MessageId;
             
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(messageLength)),
 0, buffer, offset, 4);
-            buffer[offset + 4] = (byte)MessageId;
             
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(this.port)),
 0, buffer, offset + 5, 2);
 
             return (messageLength + 4);
@@ -101,6 +101,8 @@
         {
             this.port = 
(ushort)IPAddress.NetworkToHostOrder(BitConverter.ToUInt16(buffer, offset));
         }
+
+
         /// <summary>
         /// Performs any necessary actions required to process the message
         /// </summary>
@@ -110,6 +112,7 @@
             id.Peer.Connection.Port = this.port;
         }
 
+
         /// <summary>
         /// Returns the length of the message in bytes
         /// </summary>

Modified: trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/RequestMessage.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/RequestMessage.cs        
2006-09-17 11:02:06 UTC (rev 65526)
+++ trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/RequestMessage.cs        
2006-09-17 11:13:06 UTC (rev 65527)
@@ -38,8 +38,8 @@
     /// </summary>
     public class RequestMessage : IPeerMessage
     {
+        public const int MessageId = 6;
         private const int messageLength = 13;
-        public const int MessageId = 6;
 
         #region Member Variables
         /// <summary>
@@ -95,7 +95,7 @@
         #endregion
 
 
-        #region Helper Methods
+        #region Methods
         /// <summary>
         /// Encodes the RequestMessage into the supplied buffer
         /// </summary>
@@ -138,7 +138,7 @@
         /// <param name="id">The Peer who's message will be handled</param>
         public void Handle(PeerConnectionID id)
         {
-            if (this.requestLength > (32768 * 2))
+            if (this.requestLength > (65536))
                 ClientEngine.connectionManager.CleanupSocket(id);
 
             if (!id.Peer.Connection.AmChoking)

Modified: trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/UnchokeMessage.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/UnchokeMessage.cs        
2006-09-17 11:02:06 UTC (rev 65526)
+++ trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/UnchokeMessage.cs        
2006-09-17 11:13:06 UTC (rev 65527)
@@ -38,9 +38,10 @@
     /// </summary>
     public class UnchokeMessage : IPeerMessage
     {
+        public const int MessageId = 1;
         private const int messageLength = 1;
-        public const int MessageId = 1;
 
+
         #region Constructors
         /// <summary>
         /// Creates a new UnchokeMessage
@@ -51,7 +52,7 @@
         #endregion
 
 
-        #region Helper Methods
+        #region Methods
         /// <summary>
         /// Encodes the UnChokeMessage into the supplied buffer
         /// </summary>
@@ -69,9 +70,8 @@
 
 
         /// <summary>
-        /// Decodes a RequestMessage from the supplied buffer
+        /// Decodes an UnchokeMessage from the supplied buffer
         /// </summary>
-        /// <param name="id">The peer to decode the message from</param>
         /// <param name="buffer">The buffer to decode the message from</param>
         /// <param name="offset">The offset thats the message starts at</param>
         /// <param name="length">The maximum number of bytes to read from the 
buffer</param>

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to