private void OnSuggest(IPeerWireClient client, Int32 pieceIndex) { if (SuggestPiece != null) { SuggestPiece(client, pieceIndex); } }
private void OnHaveAll(IPeerWireClient client) { if (HaveAll != null) { HaveAll(client); } }
private void OnPort(IPeerWireClient client, UInt16 port) { if (Port != null) { Port(client, port); } }
private void ProcessExtended(IPeerWireClient client, int commandLength, byte[] payload) { Int32 msgId = payload[0]; var buffer = payload.GetBytes(1, commandLength - 1); if (msgId == 0) { var extendedHandshake = (BDict)BencodingUtils.Decode(buffer); var mDict = (BDict)extendedHandshake["m"]; foreach (KeyValuePair <string, IBencodingType> pair in mDict) { var i = (BInt)pair.Value; _extIncoming.Add(new ClientProtocolIDMap(client, pair.Key, (byte)i)); var ext = FindIBTExtensionByProtocol(pair.Key); ext?.OnHandshake(client, buffer); } } else { var protocol = FindIBTProtocolByMessageID(msgId); var ext = FindIBTExtensionByProtocol(protocol); ext?.OnExtendedMessage(client, buffer); } }
private void OnAllowFast(IPeerWireClient client, Int32 pieceIndex) { if (AllowedFast != null) { AllowedFast(client, pieceIndex); } }
public bool OnCommand(IPeerWireClient client, int commandLength, byte commandId, byte[] payload) { switch (commandId) { case 13: this.ProcessSuggest(client, payload); break; case 14: this.OnHaveAll(client); break; case 15: this.OnHaveNone(client); break; case 16: this.ProcessReject(client, payload); break; case 17: this.ProcessAllowFast(client, payload); break; } return(false); }
public void SendMessage(IPeerWireClient peerWireClient, IPEndPoint[] addedEndPoints, byte[] flags, IPEndPoint[] droppedEndPoints) { if (addedEndPoints == null && droppedEndPoints == null) return; BDict d = new BDict(); if (addedEndPoints != null) { byte[] added = new byte[addedEndPoints.Length * 6]; for (int x = 0; x < addedEndPoints.Length; x++) { addedEndPoints[x].Address.GetAddressBytes().CopyTo(added, x * 6); BitConverter.GetBytes((ushort)addedEndPoints[x].Port).CopyTo(added, (x * 6)+4); } d.Add("added", new BString { ByteValue = added }); } if (droppedEndPoints != null) { byte[] dropped = new byte[droppedEndPoints.Length * 6]; for (int x = 0; x < droppedEndPoints.Length; x++) { droppedEndPoints[x].Address.GetAddressBytes().CopyTo(dropped, x * 6); dropped.SetValue((ushort)droppedEndPoints[x].Port, (x * 6) + 2); } d.Add("dropped", new BString { ByteValue = dropped }); } _parent.SendExtended(peerWireClient, _parent.GetOutgoingMessageID(peerWireClient, this), BencodingUtils.EncodeBytes(d)); }
private void OnPort(IPeerWireClient client, UInt16 port) { if (Port != null) { Port(client, port); } }
private void OnSuggest(IPeerWireClient client, Int32 pieceIndex) { if (SuggestPiece != null) { SuggestPiece(client, pieceIndex); } }
private void OnReject(IPeerWireClient client, Int32 index, Int32 begin, Int32 length) { if (Reject != null) { Reject(client, index, begin, length); } }
private void OnHaveNone(IPeerWireClient client) { if (HaveNone != null) { HaveNone(client); } }
private void ProcessReject(IPeerWireClient client, byte[] payload) { Int32 index = Unpack.Int32(payload, 0, Unpack.Endianness.Big); Int32 begin = Unpack.Int32(payload, 4, Unpack.Endianness.Big); Int32 length = Unpack.Int32(payload, 8, Unpack.Endianness.Big); OnReject(client, index, begin, length); }
private void ProcessReject(IPeerWireClient client, byte[] payload) { Int32 index = UnpackHelper.Int32(payload, 0, UnpackHelper.Endianness.Big); Int32 begin = UnpackHelper.Int32(payload, 4, UnpackHelper.Endianness.Big); Int32 length = UnpackHelper.Int32(payload, 8, UnpackHelper.Endianness.Big); this.OnReject(client, index, begin, length); }
private void ProcessReject(IPeerWireClient client, byte[] payload) { var index = Unpack.Int32(payload, 0, Unpack.Endianness.Big); var begin = Unpack.Int32(payload, 4, Unpack.Endianness.Big); var length = Unpack.Int32(payload, 8, Unpack.Endianness.Big); OnReject(client, index, begin, length); }
public bool OnCommand(IPeerWireClient client, int commandLength, byte commandId, byte[] payload) { if (commandId == 20) { ProcessExtended(client, commandLength, payload); return true; } return false; }
public bool OnCommand(IPeerWireClient client, int commandLength, byte commandId, byte[] payload) { if (commandId != 20) { return(false); } ProcessExtended(client, commandLength, payload); return(true); }
public byte GetOutgoingMessageID(IPeerWireClient client, IBTExtension extension) { ClientProtocolIDMap map = this._extOutgoing.Find(f => f.Client == client && f.Protocol == extension.Protocol); if (map != null) { return(map.CommandID); } return(0); }
public bool OnCommand(IPeerWireClient client, int commandLength, byte commandId, byte[] payload) { if (commandId != 9) { return(false); } var port = Unpack.UInt16(payload, 0, Unpack.Endianness.Big); OnPort(client, port); return(true); }
public bool OnCommand(IPeerWireClient client, int commandLength, byte commandId, byte[] payload) { if (commandId == 9) { UInt16 port = UnpackHelper.UInt16(payload, 0, UnpackHelper.Endianness.Big); this.OnPort(client, port); return(true); } return(false); }
public void OnHandshake(IPeerWireClient peerWireClient, byte[] handshake) { BDict dict = (BDict)BencodingUtils.Decode(handshake); if (dict.ContainsKey("metadata_size")) { BInt size = (BInt)dict["metadata_size"]; _metadataSize = size; _pieceCount = (Int64)Math.Ceiling((double)_metadataSize / 16384); } RequestMetaData(peerWireClient); }
public bool OnCommand(IPeerWireClient client, int commandLength, byte commandId, byte[] payload) { if (commandId == 9) { UInt16 port = Unpack.UInt16(payload, 0, Unpack.Endianness.Big); OnPort(client, port); return true; } return false; }
public void OnHandshake(IPeerWireClient peerWireClient, byte[] handshake) { var dict = (BDict)BencodingUtils.Decode(handshake); if (dict.ContainsKey(METADATA_SIZE_KEY)) { var size = (BInt)dict[METADATA_SIZE_KEY]; _metadataSize = size; _pieceCount = (Int64)Math.Ceiling((double)_metadataSize / 16384); } RequestMetaData(peerWireClient); }
public void OnHandshake(IPeerWireClient peerWireClient, byte[] handshake) { BDict dict = (BDict)BencodingUtils.Decode(handshake); if (dict.ContainsKey("metadata_size")) { BInt size = (BInt)dict["metadata_size"]; this._metadataSize = size; this._pieceCount = (Int64)Math.Ceiling((double)this._metadataSize / 16384); } this.RequestMetaData(peerWireClient); }
public void OnExtendedMessage(IPeerWireClient peerWireClient, byte[] bytes) { BDict dict = (BDict)BencodingUtils.Decode(bytes); if (dict.ContainsKey("added")) { var trackerList = (BList)dict["added"]; foreach (var tracker in trackerList) { TrackerAdded?.Invoke(peerWireClient, this, tracker.ToString()); } } }
public void OnExtendedMessage(IPeerWireClient peerWireClient, byte[] bytes) { Int32 startAt = 0; BencodingUtils.Decode(bytes, ref startAt); _piecesReceived += 1; if (_pieceCount >= _piecesReceived) { _metadataBuffer = _metadataBuffer.Concat(bytes.Skip(startAt)).ToArray(); } if (_pieceCount == _piecesReceived) { var metadata = (BDict)BencodingUtils.Decode(_metadataBuffer); MetaDataReceived?.Invoke(peerWireClient, this, metadata); } }
public void OnExtendedMessage(IPeerWireClient peerWireClient, byte[] bytes) { BDict d = (BDict)BencodingUtils.Decode(bytes); if (d.ContainsKey("added") && d.ContainsKey("added.f")) { BString pexList = (BString)d["added"]; BString pexFlags = (BString)d["added.f"]; for (int i = 0; i < pexList.ByteValue.Length / 6; i++) { UInt32 ip = Unpack.UInt32(pexList.ByteValue, i * 6, Unpack.Endianness.Little); UInt16 port = Unpack.UInt16(pexList.ByteValue, (i * 6) + 4, Unpack.Endianness.Big); byte flags = pexFlags.ByteValue[i]; IPEndPoint ipAddr = new IPEndPoint(ip, port); if (Added != null) { Added(peerWireClient, this, ipAddr, flags); } } } if (d.ContainsKey("dropped")) { BString pexList = (BString)d["dropped"]; for (int i = 0; i < pexList.ByteValue.Length / 6; i++) { UInt32 ip = Unpack.UInt32(pexList.ByteValue, i * 6, Unpack.Endianness.Little); UInt16 port = Unpack.UInt16(pexList.ByteValue, (i * 6) + 4, Unpack.Endianness.Big); IPEndPoint ipAddr = new IPEndPoint(ip, port); if (Dropped != null) { Dropped(peerWireClient, this, ipAddr); } } } }
public void OnExtendedMessage(IPeerWireClient peerWireClient, byte[] bytes) { BDict d = (BDict) BencodingUtils.Decode(bytes); if (d.ContainsKey("added") && d.ContainsKey("added.f")) { BString pexList = (BString)d["added"]; BString pexFlags = (BString)d["added.f"]; for (int i = 0; i < pexList.ByteValue.Length/6; i++) { UInt32 ip = Unpack.UInt32(pexList.ByteValue, i*6, Unpack.Endianness.Little); UInt16 port = Unpack.UInt16(pexList.ByteValue, (i * 6) + 4, Unpack.Endianness.Big); byte flags = pexFlags.ByteValue[i]; IPEndPoint ipAddr = new IPEndPoint(ip, port); if (Added != null) { Added(peerWireClient, this, ipAddr, flags); } } } if (d.ContainsKey("dropped")) { BString pexList = (BString)d["dropped"]; for (int i = 0; i < pexList.ByteValue.Length / 6; i++) { UInt32 ip = Unpack.UInt32(pexList.ByteValue, i * 6, Unpack.Endianness.Little); UInt16 port = Unpack.UInt16(pexList.ByteValue, (i * 6) + 4, Unpack.Endianness.Big); IPEndPoint ipAddr = new IPEndPoint(ip, port); if (Dropped != null) { Dropped(peerWireClient, this, ipAddr); } } } }
public void OnExtendedMessage(IPeerWireClient peerWireClient, byte[] bytes) { Int32 startAt = 0; BencodingUtils.Decode(bytes, ref startAt); _piecesReceived += 1; if (_pieceCount >= _piecesReceived) { _metadataBuffer = _metadataBuffer.Concat(bytes.Skip(startAt)).ToArray(); } if (_pieceCount == _piecesReceived) { BDict metadata = (BDict)BencodingUtils.Decode(_metadataBuffer); if (MetaDataReceived != null) { MetaDataReceived(peerWireClient, this, metadata); } } }
public void SendMessage(IPeerWireClient peerWireClient, IPEndPoint[] addedEndPoints, byte[] flags, IPEndPoint[] droppedEndPoints) { if (addedEndPoints == null && droppedEndPoints == null) { return; } BDict d = new BDict(); if (addedEndPoints != null) { byte[] added = new byte[addedEndPoints.Length * 6]; for (int x = 0; x < addedEndPoints.Length; x++) { addedEndPoints[x].Address.GetAddressBytes().CopyTo(added, x * 6); BitConverter.GetBytes((ushort)addedEndPoints[x].Port).CopyTo(added, (x * 6) + 4); } d.Add("added", new BString { ByteValue = added }); } if (droppedEndPoints != null) { byte[] dropped = new byte[droppedEndPoints.Length * 6]; for (int x = 0; x < droppedEndPoints.Length; x++) { droppedEndPoints[x].Address.GetAddressBytes().CopyTo(dropped, x * 6); dropped.SetValue((ushort)droppedEndPoints[x].Port, (x * 6) + 2); } d.Add("dropped", new BString { ByteValue = dropped }); } _parent.SendExtended(peerWireClient, _parent.GetOutgoingMessageID(peerWireClient, this), BencodingUtils.EncodeBytes(d)); }
public void OnExtendedMessage(IPeerWireClient peerWireClient, byte[] bytes) { BDict dictionary = (BDict)BencodingUtils.Decode(bytes); if (!dictionary.ContainsKey(ADDED_KEY) && !dictionary.ContainsKey(ADDED_FLAGS_KEY) && !dictionary.ContainsKey(DROPPED_KEY)) { return; } if (dictionary.ContainsKey(ADDED_KEY) && dictionary.ContainsKey(ADDED_FLAGS_KEY)) { var pexList = (BString)dictionary[ADDED_KEY]; var pexFlags = (BString)dictionary[ADDED_FLAGS_KEY]; for (int i = 0; i < pexList.ByteValue.Length / 6; i++) { var ip = Unpack.UInt32(pexList.ByteValue, i * 6, Unpack.Endianness.Little); var port = Unpack.UInt16(pexList.ByteValue, (i * 6) + 4, Unpack.Endianness.Big); var flags = pexFlags.ByteValue[i]; var ipAddr = new IPEndPoint(ip, port); Added?.Invoke(peerWireClient, this, ipAddr, flags); } } else //if (d.ContainsKey(DROPPED_KEY)) { BString pexList = (BString)dictionary[DROPPED_KEY]; for (int i = 0; i < pexList.ByteValue.Length / 6; i++) { var ip = Unpack.UInt32(pexList.ByteValue, i * 6, Unpack.Endianness.Little); var port = Unpack.UInt16(pexList.ByteValue, (i * 6) + 4, Unpack.Endianness.Big); var ipAddr = new IPEndPoint(ip, port); Dropped?.Invoke(peerWireClient, this, ipAddr); } } }
public bool OnHandshake(IPeerWireClient client) { BDict handshakeDict = new BDict(); BDict mDict = new BDict(); byte i = 1; foreach (IBTExtension extension in _protocolExtensions) { _extOutgoing.Add(new ClientProtocolIDMap(client, extension.Protocol, i)); mDict.Add(extension.Protocol, new BInt(i)); i++; } handshakeDict.Add("m", mDict); String handshakeEncoded = BencodingUtils.EncodeString(handshakeDict); byte[] handshakeBytes = Encoding.ASCII.GetBytes(handshakeEncoded); Int32 length = 2 + handshakeBytes.Length; client.SendBytes((new byte[0]).Cat(Pack.Int32(length, Pack.Endianness.Big).Cat(new[] { (byte)20 }).Cat(new[] { (byte)0 }).Cat(handshakeBytes))); return true; }
public void OnExtendedMessage(IPeerWireClient peerWireClient, byte[] bytes) { Int32 startAt = 0; BencodingUtils.Decode(bytes, ref startAt); this._piecesReceived += 1; if (this._pieceCount >= this._piecesReceived) { this._metadataBuffer = this._metadataBuffer.Concat(bytes.Skip(startAt)).ToArray(); } if (this._pieceCount == this._piecesReceived) { BDict metadata = (BDict)BencodingUtils.Decode(this._metadataBuffer); if (MetaDataReceived != null) { MetaDataReceived(peerWireClient, this, metadata); } } }
public bool OnHandshake(IPeerWireClient client) { var handshakeDict = new BDict(); var mDict = new BDict(); byte i = 1; foreach (var extension in _protocolExtensions) { _extOutgoing.Add(new ClientProtocolIDMap(client, extension.Protocol, i)); mDict.Add(extension.Protocol, new BInt(i)); i++; } handshakeDict.Add("m", mDict); var handshakeEncoded = BencodingUtils.EncodeString(handshakeDict); var handshakeBytes = Encoding.ASCII.GetBytes(handshakeEncoded); var length = 2 + handshakeBytes.Length; client.SendBytes((new byte[0]).Cat(Pack.Int32(length, Pack.Endianness.Big).Cat(new[] { (byte)20 }).Cat(new[] { (byte)0 }).Cat(handshakeBytes))); return(true); }
public bool OnCommand(IPeerWireClient client, int commandLength, byte commandId, byte[] payload) { switch (commandId) { case 13: ProcessSuggest(client, payload); break; case 14: OnHaveAll(client); break; case 15: OnHaveNone(client); break; case 16: ProcessReject(client, payload); break; case 17: ProcessAllowFast(client, payload); break; } return false; }
public bool OnHandshake(IPeerWireClient client) { BDict handshakeDict = new BDict(); BDict mDict = new BDict(); byte i = 1; foreach (IBTExtension extension in this._protocolExtensions) { this._extOutgoing.Add(new ClientProtocolIDMap(client, extension.Protocol, i)); mDict.Add(extension.Protocol, new BInt(i)); i++; } handshakeDict.Add("m", mDict); string handshakeEncoded = BencodingUtils.EncodeString(handshakeDict); byte[] handshakeBytes = Encoding.ASCII.GetBytes(handshakeEncoded); Int32 length = 2 + handshakeBytes.Length; client.SendBytes((new byte[0]).Cat(PackHelper.Int32(length).Cat(new[] { (byte)20 }).Cat(new[] { (byte)0 }).Cat(handshakeBytes))); return(true); }
public void RequestMetaData(IPeerWireClient peerWireClient) { byte[] sendBuffer = new byte[0]; for (Int32 i = 0; i < _pieceCount; i++) { BDict masterBDict = new BDict { {"msg_type", (BInt) 0}, {"piece", (BInt) i} }; String encoded = BencodingUtils.EncodeString(masterBDict); byte[] buffer = Pack.Int32(2 + encoded.Length, Pack.Endianness.Big); buffer = buffer.Concat(new byte[] {20}).ToArray(); buffer = buffer.Concat(new[] { _parent.GetOutgoingMessageID(peerWireClient, this) }).ToArray(); buffer = buffer.Concat(Encoding.GetEncoding(1252).GetBytes(encoded)).ToArray(); sendBuffer = sendBuffer.Concat(buffer).ToArray(); } peerWireClient.SendBytes(sendBuffer); }
public void RequestMetaData(IPeerWireClient peerWireClient) { var sendBuffer = new byte[0]; for (Int32 i = 0; i < _pieceCount; i++) { var masterBDict = new BDict { { MESSAGE_TYPE_KEY, (BInt)0 }, { PIECE_KEY, (BInt)i } }; var encoded = BencodingUtils.EncodeString(masterBDict); var buffer = Pack.Int32(2 + encoded.Length, Pack.Endianness.Big); buffer = buffer.Concat(new byte[] { 20 }).ToArray(); buffer = buffer.Concat(new[] { _parent.GetOutgoingMessageID(peerWireClient, this) }).ToArray(); buffer = buffer.Concat(Encoding.GetEncoding(1252).GetBytes(encoded)).ToArray(); sendBuffer = sendBuffer.Concat(buffer).ToArray(); } peerWireClient.SendBytes(sendBuffer); }
public void RequestMetaData(IPeerWireClient peerWireClient) { byte[] sendBuffer = new byte[0]; for (Int32 i = 0; i < this._pieceCount; i++) { BDict masterBDict = new BDict { { "msg_type", (BInt)0 }, { "piece", (BInt)i } }; string encoded = BencodingUtils.EncodeString(masterBDict); byte[] buffer = PackHelper.Int32(2 + encoded.Length); buffer = buffer.Concat(new byte[] { 20 }).ToArray(); buffer = buffer.Concat(new[] { this._parent.GetOutgoingMessageID(peerWireClient, this) }).ToArray(); buffer = buffer.Concat(Encoding.GetEncoding(1252).GetBytes(encoded)).ToArray(); sendBuffer = sendBuffer.Concat(buffer).ToArray(); } peerWireClient.SendBytes(sendBuffer); }
public bool OnHandshake(IPeerWireClient client) { return false; }
private void OnAllowFast(IPeerWireClient client, Int32 pieceIndex) { if (AllowedFast != null) { AllowedFast(client, pieceIndex); } }
private void ProcessAllowFast(IPeerWireClient client, byte[] payload) { Int32 index = Unpack.Int32(payload, 0, Unpack.Endianness.Big); OnAllowFast(client, index); }
public ClientProtocolIDMap(IPeerWireClient client, string protocol, byte commandId) { Client = client; Protocol = protocol; CommandID = commandId; }
public void RegisterProtocolExtension(IPeerWireClient client, IBTExtension extension) { _protocolExtensions.Add(extension); extension.Init(this); }
public bool SendExtended(IPeerWireClient client, byte extMsgId, byte[] bytes) => client.SendBytes(new PeerMessageBuilder(20).Add(extMsgId).Add(bytes).Message());
public bool SendExtended(IPeerWireClient client, byte extMsgId, byte[] bytes) { return client.SendBytes(new PeerMessageBuilder(20).Add(extMsgId).Add(bytes).Message()); }
public void UnregisterProtocolExtension(IPeerWireClient client, IBTExtension extension) { _protocolExtensions.Remove(extension); extension.Deinit(); }
public ClientProtocolIDMap(IPeerWireClient client, String protocol, byte commandId) { Client = client; Protocol = protocol; CommandID = commandId; }
private void OnReject(IPeerWireClient client, Int32 index, Int32 begin, Int32 length) { if (Reject != null) { Reject(client, index, begin, length); } }
public byte GetOutgoingMessageID(IPeerWireClient client, IBTExtension extension) { ClientProtocolIDMap map = _extOutgoing.Find(f => f.Client == client && f.Protocol == extension.Protocol); if (map != null) { return map.CommandID; } return 0; }
public void OnHandshake(IPeerWireClient peerWireClient, byte[] handshake) { BDict d = (BDict)BencodingUtils.Decode(handshake); }
public byte GetOutgoingMessageID(IPeerWireClient client, IBTExtension extension) { var map = _extOutgoing.Find(f => f.Client == client && f.Protocol == extension.Protocol); return(map?.CommandID ?? 0); }
public void UnregisterProtocolExtension(IPeerWireClient client, IBTExtension extension) { _protocolExtensions.Remove(extension); extension.Deinit(); }
private void ProcessExtended(IPeerWireClient client, int commandLength, byte[] payload) { Int32 msgId = payload[0]; byte[] buffer = payload.GetBytes(1, commandLength - 1); if (msgId == 0) { BDict extendedHandshake = (BDict)BencodingUtils.Decode(buffer); BDict mDict = (BDict)extendedHandshake["m"]; foreach (KeyValuePair<string, IBencodingType> pair in mDict) { BInt i = (BInt)pair.Value; _extIncoming.Add(new ClientProtocolIDMap(client, pair.Key, (byte)i)); IBTExtension ext = FindIBTExtensionByProtocol(pair.Key); if (ext != null) { ext.OnHandshake(client, buffer); } } } else { String protocol = FindIBTProtocolByMessageID(msgId); IBTExtension ext = FindIBTExtensionByProtocol(protocol); if (ext != null) { ext.OnExtendedMessage(client, buffer); } } }
private void OnHaveAll(IPeerWireClient client) { if (HaveAll != null) { HaveAll(client); } }
private void OnHaveNone(IPeerWireClient client) { if (HaveNone != null) { HaveNone(client); } }