Author: alanmc
Date: 2006-09-10 12:26:31 -0400 (Sun, 10 Sep 2006)
New Revision: 65178
Modified:
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/BitfieldMessage.cs
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/CancelMessage.cs
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/ChokeMessage.cs
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/HandshakeMessage.cs
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/HaveMessage.cs
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/InterestedMessage.cs
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/KeepAliveMessage.cs
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/NotInterestedMessage.cs
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/PeerwireEncoder.cs
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/PieceMessage.cs
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/PortMessage.cs
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/RequestMessage.cs
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/UnchokeMessage.cs
Log:
Updated all PeerMessages to have a public const MessageId which i use for
identifying messages (to remove some previous hardcoding).
Modified:
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/BitfieldMessage.cs
===================================================================
---
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/BitfieldMessage.cs
2006-09-10 16:25:12 UTC (rev 65177)
+++
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/BitfieldMessage.cs
2006-09-10 16:26:31 UTC (rev 65178)
@@ -39,7 +39,7 @@
/// </summary>
public class BitfieldMessage : IPeerMessage
{
- private const int messageId = 5;
+ public const int MessageId = 5;
#region Member Variables
/// <summary>
@@ -85,7 +85,7 @@
/// <returns>The number of bytes encoded into the buffer</returns>
public int Encode(byte[] buffer, int offset)
{
- buffer[offset + 4] = (byte)messageId;
+ buffer[offset + 4] = (byte)MessageId;
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(bitField.LengthInBytes
+ 1)), 0, buffer, offset, 4);
this.bitField.ToByteArray(buffer, offset + 5);
Modified:
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/CancelMessage.cs
===================================================================
---
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/CancelMessage.cs
2006-09-10 16:25:12 UTC (rev 65177)
+++
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/CancelMessage.cs
2006-09-10 16:26:31 UTC (rev 65178)
@@ -39,7 +39,7 @@
public class CancelMessage : IPeerMessage
{
private const int messageLength = 13;
- private const int messageId = 8;
+ public const int MessageId = 8;
#region Member Variables
@@ -108,7 +108,7 @@
/// <returns>The number of bytes encoded into the buffer</returns>
public int Encode(byte[] buffer, int offset)
{
- buffer[offset + 4] = (byte)messageId;
+ buffer[offset + 4] = (byte)MessageId;
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(messageLength)),
0, buffer, offset, 4);
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(this.pieceIndex)),
0, buffer, offset + 5, 4);
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(this.startOffset)),
0, buffer, offset + 9, 4);
Modified:
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/ChokeMessage.cs
===================================================================
---
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/ChokeMessage.cs
2006-09-10 16:25:12 UTC (rev 65177)
+++
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/ChokeMessage.cs
2006-09-10 16:26:31 UTC (rev 65178)
@@ -39,7 +39,7 @@
public class ChokeMessage : IPeerMessage
{
private const int messageLength = 1;
- private const int messageId = 0;
+ public const int MessageId = 0;
#region Constructors
/// <summary>
@@ -62,7 +62,7 @@
public int Encode(byte[] buffer, int offset)
{
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(messageLength)),
0, buffer, offset, 4);
- buffer[offset + 4] = (byte)messageId;
+ buffer[offset + 4] = (byte)MessageId;
return (messageLength + 4);
}
Modified:
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/HandshakeMessage.cs
===================================================================
---
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/HandshakeMessage.cs
2006-09-10 16:25:12 UTC (rev 65177)
+++
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/HandshakeMessage.cs
2006-09-10 16:26:31 UTC (rev 65178)
@@ -30,15 +30,17 @@
using System;
using MonoTorrent.Common;
+using System.Net;
namespace MonoTorrent.Client.PeerMessages
{
-#warning Make this an IPeerMessage too... Possibly rewrite for neatness
/// <summary>
///
/// </summary>
- public class HandshakeMessage
+ public class HandshakeMessage : IPeerMessage
{
+ private readonly byte ExtendedFlag;
+ private readonly byte FastPeersFlag;
#region Member Variables
/// <summary>
/// The length of the protocol string
@@ -78,6 +80,26 @@
get { return this.peerId; }
}
private string peerId;
+
+
+ /// <summary>
+ /// True if the peer supports the Azureus Extended protocol
+ /// </summary>
+ public bool SupportsExtended
+ {
+ get { return this.supportsExtended; }
+ }
+ private bool supportsExtended;
+
+
+ /// <summary>
+ /// True if the peer supports the Bittorrent FastPeerExtensions
+ /// </summary>
+ public bool SupportsFastPeer
+ {
+ get { return this.supportsFastPeer; }
+ }
+ private bool supportsFastPeer;
#endregion
@@ -87,6 +109,13 @@
/// </summary>
public HandshakeMessage()
{
+ this.FastPeersFlag = 0x04;
+
+ if (BitConverter.IsLittleEndian)
+ this.ExtendedFlag = 1<<7;
+
+ else
+ this.ExtendedFlag = 1;
}
@@ -97,6 +126,8 @@
/// <param name="peerId">The ID of the peer</param>
/// <param name="protocolString">The protocol string to send</param>
public HandshakeMessage(byte[] infoHash, string peerId, string
protocolString)
+ :this()
+
{
this.infoHash = infoHash;
this.peerId = peerId;
@@ -106,7 +137,7 @@
#endregion
- #region Helper Methods
+ #region Methods
/// <summary>
/// Encodes the HandshakeMessage into the supplied buffer
/// </summary>
@@ -115,7 +146,7 @@
/// <returns>The number of bytes encoded into the buffer</returns>
public int Encode(byte[] buffer, int offset)
{
- int i = 0;
+ int i = offset;
// Copy in the length of the protocol string
buffer[i++] = (byte)protocolString.Length;
@@ -124,7 +155,9 @@
System.Text.Encoding.ASCII.GetBytes(protocolString, 0,
protocolString.Length, buffer, i);
i += protocolString.Length;
- // Skip past the 8 reserved bytes
+ // The 8 reserved bits are here
+ buffer[i] |= this.ExtendedFlag;
+ buffer[i + 7] |= this.FastPeersFlag;
i += 8;
// Copy in the infohash
@@ -156,13 +189,32 @@
i += protocolStringLength; // Next
bytes are protocol string
i += 8; // 8
reserved bytes
this.infoHash = new byte[20];
- //Debug.WriteLine("03 Allocated: " +
this.infoHash.Length.ToString());
+
Buffer.BlockCopy(buffer, i, this.infoHash, 0, infoHash.Length);
i += infoHash.Length; // 20 byte
infohash
peerId = System.Text.Encoding.ASCII.GetString(buffer, i, 20);
i += 20; // 20 byte
peerid
+ CheckForSupports(buffer, offset + protocolStringLength + 1);
}
+
+ private void CheckForSupports(byte[] buffer, int
reservedBytesStartIndex)
+ {
+ this.supportsExtended = (this.ExtendedFlag &
buffer[reservedBytesStartIndex]) != 0;
+ this.supportsFastPeer = (this.FastPeersFlag &
buffer[reservedBytesStartIndex + 7]) != 0;
+ }
+
+
+ public void Handle(PeerConnectionID id)
+ {
+ throw new NotImplementedException();
+ }
+
+ public int ByteLength
+ {
+#warning Update this
+ get { return 68; }
+ }
#endregion
@@ -176,6 +228,7 @@
return "HandshakeMessage";
}
+
public override bool Equals(object obj)
{
HandshakeMessage msg = obj as HandshakeMessage;
@@ -190,6 +243,7 @@
&& this.protocolString == msg.protocolString);
}
+
public override int GetHashCode()
{
return (this.infoHash.GetHashCode() ^ this.peerId.GetHashCode() ^
this.protocolString.GetHashCode());
Modified:
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/HaveMessage.cs
===================================================================
---
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/HaveMessage.cs
2006-09-10 16:25:12 UTC (rev 65177)
+++
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/HaveMessage.cs
2006-09-10 16:26:31 UTC (rev 65178)
@@ -39,7 +39,7 @@
public class HaveMessage : IPeerMessage
{
private const int messageLength = 5;
- private const int messageId = 4;
+ public const int MessageId = 4;
#region Member Variables
@@ -85,7 +85,7 @@
public int Encode(byte[] buffer, int offset)
{
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(messageLength)),
0, buffer, offset, 4);
- buffer[offset + 4] = (byte)messageId;
+ buffer[offset + 4] = (byte)MessageId;
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(pieceIndex)),
0, buffer, offset + 5, 4);
return (messageLength + 4);
Modified:
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/InterestedMessage.cs
===================================================================
---
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/InterestedMessage.cs
2006-09-10 16:25:12 UTC (rev 65177)
+++
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/InterestedMessage.cs
2006-09-10 16:26:31 UTC (rev 65178)
@@ -39,7 +39,7 @@
public class InterestedMessage : IPeerMessage
{
private const int messageLength = 1;
- private const int messageId = 2;
+ public const int MessageId = 2;
#region Constructors
@@ -63,7 +63,7 @@
public int Encode(byte[] buffer, int offset)
{
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(messageLength)),
0, buffer, offset, 4);
- buffer[offset + 4] = (byte)messageId;
+ buffer[offset + 4] = (byte)MessageId;
return (messageLength + 4);
}
Modified:
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/KeepAliveMessage.cs
===================================================================
---
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/KeepAliveMessage.cs
2006-09-10 16:25:12 UTC (rev 65177)
+++
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/KeepAliveMessage.cs
2006-09-10 16:26:31 UTC (rev 65178)
@@ -36,7 +36,7 @@
public class KeepAliveMessage : IPeerMessage
{
private const int messageLength = -1; // has no payload
- private const int messageId = -1; // Has no messageId
+ public const int MessageId = -1; // Has no messageId
#region Constructors
Modified:
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/NotInterestedMessage.cs
===================================================================
---
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/NotInterestedMessage.cs
2006-09-10 16:25:12 UTC (rev 65177)
+++
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/NotInterestedMessage.cs
2006-09-10 16:26:31 UTC (rev 65178)
@@ -39,7 +39,7 @@
public class NotInterestedMessage : IPeerMessage
{
private const int messageLength = 1;
- private const int messageId = 3;
+ public const int MessageId = 3;
#region Constructors
/// <summary>
@@ -62,7 +62,7 @@
public int Encode(byte[] buffer, int offset)
{
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(messageLength)),
0, buffer, offset, 4);
- buffer[offset + 4] = (byte)messageId;
+ buffer[offset + 4] = (byte)MessageId;
return (messageLength + 4);
}
Modified:
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/PeerwireEncoder.cs
===================================================================
---
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/PeerwireEncoder.cs
2006-09-10 16:25:12 UTC (rev 65177)
+++
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/PeerwireEncoder.cs
2006-09-10 16:26:31 UTC (rev 65178)
@@ -28,6 +28,8 @@
+using System;
+using System.Net;
namespace MonoTorrent.Client.PeerMessages
{
/// <summary>
@@ -47,36 +49,78 @@
// The first byte tells us what kind of message it is
switch (buffer[offset])
{
- case 0:
+ case AllowedFastMessage.MessageId:
+ message = new AllowedFastMessage();
+ break;
+
+ case BitfieldMessage.MessageId: // 5
+ message = new
BitfieldMessage(manager.Torrent.Pieces.Length);
+ break;
+
+ case CancelMessage.MessageId: // 8
+ message = new CancelMessage();
+ break;
+
+ case ChokeMessage.MessageId: // 0
message = new ChokeMessage();
break;
- case 1:
- message = new UnchokeMessage();
+
+ case ExtendedListMessage.MessageId: // 20
+ message = new ExtendedListMessage();
break;
- case 2:
+
+ case HaveAllMessage.MessageId:
+ message = new HaveAllMessage();
+ break;
+
+ case HaveMessage.MessageId: // 4
+ message = new HaveMessage();
+ break;
+
+ case HaveNoneMessage.MessageId:
+ message = new HaveNoneMessage();
+ break;
+
+ case InterestedMessage.MessageId: // 2
message = new InterestedMessage();
break;
- case 3:
+
+ case NotInterestedMessage.MessageId:// 3
message = new NotInterestedMessage();
break;
- case 4:
- message = new HaveMessage();
+
+ case PieceMessage.MessageId: // 7
+ message = new PieceMessage(manager.DiskManager);
break;
- case 5:
- message = new
BitfieldMessage(manager.Torrent.Pieces.Length);
+
+ case PortMessage.MessageId: // 9
+ message = new PortMessage();
break;
- case 6:
+
+ case RejectRequestMessage.MessageId:
+ message = new RejectRequestMessage();
+ break;
+
+ case RequestMessage.MessageId: // 6
message = new RequestMessage();
break;
- case 7:
- message = new PieceMessage(manager.DiskManager);
+
+ case SuggestPieceMessage.MessageId:
+ message = new RequestMessage();
break;
- case 8:
- message = new CancelMessage();
+
+ case UnchokeMessage.MessageId: // 1
+ message = new UnchokeMessage();
break;
- case 9:
- message = new PortMessage();
- break;
+
+
+ case 21: // An "extended" message
+ switch
(IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, ++offset)))
+ {
+ default:
+ throw new NotSupportedException();
+ }
+
default:
#warning LOG THESE
return null;
Modified:
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/PieceMessage.cs
===================================================================
---
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/PieceMessage.cs
2006-09-10 16:25:12 UTC (rev 65177)
+++
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/PieceMessage.cs
2006-09-10 16:26:31 UTC (rev 65178)
@@ -38,7 +38,7 @@
/// </summary>
public class PieceMessage : IPeerMessage
{
- private const int messageId = 7;
+ public const int MessageId = 7;
private const int messageLength = 9;
#region Member Variables
@@ -116,7 +116,7 @@
/// <returns>The number of bytes encoded into the buffer</returns>
public int Encode(byte[] buffer, int offset)
{
- buffer[offset + 4] = (byte)messageId;
+ buffer[offset + 4] = (byte)MessageId;
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(messageLength
+ blockLength)), 0, buffer, offset, 4);
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(this.pieceIndex)),
0, buffer, offset + 5, 4);
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(this.startOffset)),
0, buffer, offset + 9, 4);
Modified:
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/PortMessage.cs
===================================================================
---
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/PortMessage.cs
2006-09-10 16:25:12 UTC (rev 65177)
+++
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/PortMessage.cs
2006-09-10 16:26:31 UTC (rev 65178)
@@ -39,7 +39,7 @@
public class PortMessage : IPeerMessage
{
private const int messageLength = 3;
- private const int messageId = 9;
+ public const int MessageId = 9;
#region Member Variables
/// <summary>
@@ -84,7 +84,7 @@
public int Encode(byte[] buffer, int offset)
{
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(messageLength)),
0, buffer, offset, 4);
- buffer[offset + 4] = (byte)messageId;
+ buffer[offset + 4] = (byte)MessageId;
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(this.port)),
0, buffer, offset + 5, 2);
return (messageLength + 4);
Modified:
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/RequestMessage.cs
===================================================================
---
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/RequestMessage.cs
2006-09-10 16:25:12 UTC (rev 65177)
+++
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/RequestMessage.cs
2006-09-10 16:26:31 UTC (rev 65178)
@@ -39,7 +39,7 @@
public class RequestMessage : IPeerMessage
{
private const int messageLength = 13;
- private const int messageId = 6;
+ public const int MessageId = 6;
#region Member Variables
/// <summary>
@@ -106,7 +106,7 @@
public int Encode(byte[] buffer, int offset)
{
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(messageLength)),
0, buffer, offset, 4);
- buffer[offset + 4] = (byte)messageId;
+ buffer[offset + 4] = (byte)MessageId;
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(this.pieceIndex)),
0, buffer, offset + 5, 4);
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(this.startOffset)),
0, buffer, offset + 9, 4);
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(this.requestLength)),
0, buffer, offset + 13, 4);
Modified:
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/UnchokeMessage.cs
===================================================================
---
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/UnchokeMessage.cs
2006-09-10 16:25:12 UTC (rev 65177)
+++
branches/BitSharp/bitsharp/src/MonoTorrent.Client/PeerMessages/UnchokeMessage.cs
2006-09-10 16:26:31 UTC (rev 65178)
@@ -39,7 +39,7 @@
public class UnchokeMessage : IPeerMessage
{
private const int messageLength = 1;
- private const int messageId = 1;
+ public const int MessageId = 1;
#region Constructors
/// <summary>
@@ -62,7 +62,7 @@
public int Encode(byte[] buffer, int offset)
{
Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(messageLength)),
0, buffer, offset, 4);
- buffer[offset + 4] = (byte)messageId;
+ buffer[offset + 4] = (byte)MessageId;
return (messageLength + 4);
}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches