Skip to content

Commit

Permalink
re factoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bizzehdee committed May 12, 2014
1 parent b1581e2 commit 1dbbad2
Show file tree
Hide file tree
Showing 19 changed files with 585 additions and 353 deletions.
24 changes: 13 additions & 11 deletions Extensions/UTMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR

using System.Linq;
using System.Net.Torrent.BEncode;
using System.Net.Torrent.Misc;
using System.Net.Torrent.ProtocolExtensions;
using System.Text;

namespace System.Net.Torrent.Extensions
{
public class UTMetadata : IBTExtension
{
private PeerWireClient _peerWireClient;
private Int64 _metadataSize;
private Int64 _pieceCount;
private Int64 _piecesReceived;
private byte[] _metadataBuffer;
private ExtendedProtocolExtensions _parent;

public string Protocol
{
Expand All @@ -49,18 +51,18 @@ public string Protocol

public event Action<PeerWireClient, IBTExtension, BDict> MetaDataReceived;

public void Init(PeerWireClient peerWireClient)
{
public void Init(ExtendedProtocolExtensions parent)
{
_parent = parent;
_metadataBuffer = new byte[0];
_peerWireClient = peerWireClient;
}

public void Deinit(PeerWireClient peerWireClient)
public void Deinit()
{

}

public void OnHandshake(PeerWireClient peerWireClient, byte[] handshake)
public void OnHandshake(PeerWireClient peerWireClient, byte[] handshake)
{
BDict dict = (BDict)BencodingUtils.Decode(handshake);
if (dict.ContainsKey("metadata_size"))
Expand All @@ -70,10 +72,10 @@ public void OnHandshake(PeerWireClient peerWireClient, byte[] handshake)
_pieceCount = (Int64)Math.Ceiling((double)_metadataSize / 16384);
}

RequestMetaData();
RequestMetaData(peerWireClient);
}

public void OnExtendedMessage(PeerWireClient peerWireClient, byte[] bytes)
public void OnExtendedMessage(PeerWireClient peerWireClient, byte[] bytes)
{
Int32 startAt = 0;
BDict dict = (BDict)BencodingUtils.Decode(bytes, ref startAt);
Expand All @@ -95,7 +97,7 @@ public void OnExtendedMessage(PeerWireClient peerWireClient, byte[] bytes)
}
}

public void RequestMetaData()
public void RequestMetaData(PeerWireClient peerWireClient)
{
byte[] sendBuffer = new byte[0];

Expand All @@ -108,13 +110,13 @@ public void RequestMetaData()

byte[] buffer = Pack.Int32(2 + encoded.Length, Pack.Endianness.Big);
buffer = buffer.Concat(new byte[] {20}).ToArray();
buffer = buffer.Concat(new byte[] {(byte) _peerWireClient.GetOutgoingMessageID(this)}).ToArray();
buffer = buffer.Concat(new byte[] { (byte)_parent.GetOutgoingMessageID(peerWireClient, this) }).ToArray();
buffer = buffer.Concat(Encoding.GetEncoding(1252).GetBytes(encoded)).ToArray();

sendBuffer = sendBuffer.Concat(buffer).ToArray();
}

_peerWireClient.Socket.Send(sendBuffer);
peerWireClient.Socket.Send(sendBuffer);
}

}
Expand Down
20 changes: 12 additions & 8 deletions Extensions/UTPeerExchange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
*/

using System.Net.Torrent.BEncode;
using System.Net.Torrent.Misc;
using System.Net.Torrent.ProtocolExtensions;

namespace System.Net.Torrent.Extensions
{
public class UTPeerExchange : IBTExtension
{
private ExtendedProtocolExtensions _parent;

public string Protocol
{
get { return "ut_pex"; }
Expand All @@ -42,22 +46,22 @@ public string Protocol
public event Action<PeerWireClient, IBTExtension, IPEndPoint, byte> Added;
public event Action<PeerWireClient, IBTExtension, IPEndPoint> Dropped;

public void Init(PeerWireClient peerWireClient)
{
}
public void Init(ExtendedProtocolExtensions parent)
{
_parent = parent;
}

public void Deinit(PeerWireClient peerWireClient)
public void Deinit()
{

}

public void OnHandshake(PeerWireClient peerWireClient, byte[] handshake)
public void OnHandshake(PeerWireClient peerWireClient, byte[] handshake)
{
BDict d = (BDict)BencodingUtils.Decode(handshake);
}

public void OnExtendedMessage(PeerWireClient peerWireClient, byte[] bytes)
public void OnExtendedMessage(PeerWireClient peerWireClient, byte[] bytes)
{
BDict d = (BDict) BencodingUtils.Decode(bytes);
if (d.ContainsKey("added") && d.ContainsKey("added.f"))
Expand Down Expand Up @@ -130,7 +134,7 @@ public void SendMessage(PeerWireClient peerWireClient, IPEndPoint[] addedEndPoin
d.Add("dropped", new BString { ByteValue = dropped });
}

peerWireClient.SendExtended(peerWireClient.GetOutgoingMessageID(this), BencodingUtils.EncodeBytes(d));
_parent.SendExtended(peerWireClient, _parent.GetOutgoingMessageID(peerWireClient, this), BencodingUtils.EncodeBytes(d));
}
}
}
1 change: 1 addition & 0 deletions HTTPTrackerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
using System.IO;
using System.Linq;
using System.Net.Torrent.BEncode;
using System.Net.Torrent.Misc;
using System.Text;

namespace System.Net.Torrent
Expand Down
2 changes: 1 addition & 1 deletion IWireIO.cs → IO/IWireIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace System.Net.Torrent
namespace System.Net.Torrent.IO
{
public interface IWireIO
{
Expand Down
85 changes: 2 additions & 83 deletions WireIO.cs → IO/WireIO.Tcp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR

using System.Net.Sockets;

namespace System.Net.Torrent
namespace System.Net.Torrent.IO
{
public class WireIO
public partial class WireIO
{
public class Tcp : IWireIO
{
Expand Down Expand Up @@ -135,86 +135,5 @@ public IWireIO EndAccept(IAsyncResult ar)
return new Tcp(_socket.EndAccept(ar));
}
}

public class Udp : IWireIO
{
private readonly Socket _socket;
private IPEndPoint _endPoint;

public int Timeout
{
get
{
return _socket.ReceiveTimeout/1000;
}
set
{
_socket.ReceiveTimeout = value*1000;
_socket.SendTimeout = value*1000;
}
}

public bool Connected
{
get { return _socket.Connected; }
}

public Udp()
{
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
}

public void Connect(IPEndPoint endPoint)
{
_endPoint = endPoint;
}

public void Disconnect()
{
_socket.Disconnect(true);
}

public int Send(byte[] bytes)
{
return _socket.SendTo(bytes, _endPoint);
}

public int Receive(ref byte[] bytes)
{
EndPoint recFrom = new IPEndPoint(IPAddress.Any, _endPoint.Port);

return _socket.ReceiveFrom(bytes, ref recFrom);
}

public IAsyncResult BeginReceive(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
{
throw new NotImplementedException();
}

public int EndReceive(IAsyncResult asyncResult)
{
throw new NotImplementedException();
}

public void Listen(EndPoint ep)
{
throw new NotImplementedException();
}

public IWireIO Accept()
{
throw new NotImplementedException();
}

public IAsyncResult BeginAccept(AsyncCallback callback)
{
throw new NotImplementedException();
}

public IWireIO EndAccept(IAsyncResult ar)
{
throw new NotImplementedException();
}
}
}
}
118 changes: 118 additions & 0 deletions IO/WireIO.Udp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
Copyright (c) 2013, Darren Horrocks
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Neither the name of Darren Horrocks nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System.Net.Sockets;

namespace System.Net.Torrent.IO
{
public partial class WireIO
{
public class Udp : IWireIO
{
private readonly Socket _socket;
private IPEndPoint _endPoint;

public int Timeout
{
get
{
return _socket.ReceiveTimeout / 1000;
}
set
{
_socket.ReceiveTimeout = value * 1000;
_socket.SendTimeout = value * 1000;
}
}

public bool Connected
{
get { return _socket.Connected; }
}

public Udp()
{
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
}

public void Connect(IPEndPoint endPoint)
{
_endPoint = endPoint;
}

public void Disconnect()
{
_socket.Disconnect(true);
}

public int Send(byte[] bytes)
{
return _socket.SendTo(bytes, _endPoint);
}

public int Receive(ref byte[] bytes)
{
EndPoint recFrom = new IPEndPoint(IPAddress.Any, _endPoint.Port);

return _socket.ReceiveFrom(bytes, ref recFrom);
}

public IAsyncResult BeginReceive(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
{
return _socket.BeginReceive(buffer, offset, size, SocketFlags.None, callback, state);
}

public int EndReceive(IAsyncResult asyncResult)
{
return _socket.EndReceive(asyncResult);
}

public void Listen(EndPoint ep)
{
throw new NotImplementedException();
}

public IWireIO Accept()
{
throw new NotImplementedException();
}

public IAsyncResult BeginAccept(AsyncCallback callback)
{
throw new NotImplementedException();
}

public IWireIO EndAccept(IAsyncResult ar)
{
throw new NotImplementedException();
}
}
}
}
17 changes: 8 additions & 9 deletions IBTExtension.cs → IProtocolExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR

namespace System.Net.Torrent
{
public interface IBTExtension
{
String Protocol { get; }
void Init(PeerWireClient peerWireClient);
void Deinit(PeerWireClient peerWireClient);
void OnHandshake(PeerWireClient peerWireClient, byte[] handshake);
void OnExtendedMessage(PeerWireClient peerWireClient, byte[] bytes);
}
}
public interface IProtocolExtension
{
byte[] ByteMask { get; }
byte[] CommandIDs { get; }
bool OnHandshake(PeerWireClient client);
bool OnCommand(PeerWireClient client, Int32 commandLength, byte commandId, byte[] payload);
}
}
Loading

0 comments on commit 1dbbad2

Please sign in to comment.