/// <summary> /// Initializes a new instance of the <see cref="AsyncConnectData"/> class. /// </summary> /// <param name="endpoint">The endpoint.</param> /// <param name="tcp">The TCP.</param> public AsyncConnectData(IPEndPoint endpoint, TcpClient tcp) { endpoint.CannotBeNull(); tcp.CannotBeNull(); this.Endpoint = endpoint; this.Tcp = tcp; }
/// <summary> /// Writes the value to the buffer at the specified offset. /// </summary> /// <param name="buffer">The buffer.</param> /// <param name="offset">The offset.</param> /// <param name="value">The value.</param> public static void Write(byte[] buffer, ref int offset, IPEndPoint value) { buffer.CannotBeNullOrEmpty(); offset.MustBeGreaterThanOrEqualTo(0); offset.MustBeLessThan(buffer.Length); value.CannotBeNull(); Write(buffer, ref offset, value.Address); Write(buffer, ref offset, (ushort)value.Port); }
/// <summary> /// Adds the peer. /// </summary> /// <param name="endpoint">The endpoint.</param> private void AddSeeder(IPEndPoint endpoint) { endpoint.CannotBeNull(); TcpClient tcp; lock (((IDictionary)this.peers).SyncRoot) { if (!this.peers.ContainsKey(endpoint)) { // set up tcp client tcp = new TcpClient(); tcp.ReceiveBufferSize = (int)Math.Max(this.TorrentInfo.BlockLength, this.TorrentInfo.PieceHashes.Count()) + 100; tcp.SendBufferSize = tcp.ReceiveBufferSize; tcp.Client.ReceiveTimeout = (int)TimeSpan.FromSeconds(60).TotalMilliseconds; tcp.Client.SendTimeout = tcp.Client.ReceiveTimeout; tcp.BeginConnect(endpoint.Address, endpoint.Port, this.PeerConnected, new AsyncConnectData(endpoint, tcp)); } } }
/// <summary> /// Executes the UDP request. /// </summary> /// <param name="endpoint">The endpoint.</param> /// <param name="bytes">The payload bytes.</param> public static void ExecuteUdpRequest(this IPEndPoint endpoint, byte[] bytes) { endpoint.CannotBeNull(); bytes.CannotBeNullOrEmpty(); int bufferSize = 4096; TimeSpan sendTimeout = TimeSpan.FromSeconds(5); TimeSpan receiveTimeout = TimeSpan.FromSeconds(5); IAsyncResult asyncResult; int count; // execute request using (UdpClient udp = new UdpClient()) { udp.Client.SendTimeout = (int)sendTimeout.TotalMilliseconds; udp.Client.SendBufferSize = bytes.Length; udp.Client.ReceiveTimeout = (int)receiveTimeout.TotalMilliseconds; udp.Client.ReceiveBufferSize = bufferSize; udp.Connect(endpoint); asyncResult = udp.BeginSend(bytes, bytes.Length, null, null); if (asyncResult.AsyncWaitHandle.WaitOne(receiveTimeout)) { // stop reading count = udp.EndSend(asyncResult); if (count == bytes.Length) { // ok } } else { // timeout } udp.Close(); } }
/// <summary> /// Initializes a new instance of the <see cref="AnnounceMessage" /> class. /// </summary> /// <param name="connectionId">The connection identifier.</param> /// <param name="transactionId">The transaction identifier.</param> /// <param name="infoHash">The information hash.</param> /// <param name="peerId">The peer identifier.</param> /// <param name="downloaded">The downloaded.</param> /// <param name="left">The left.</param> /// <param name="uploaded">The uploaded.</param> /// <param name="trackingEvent">The tracking event.</param> /// <param name="key">The key.</param> /// <param name="numberWanted">The number wanted.</param> /// <param name="endpoint">The endpoint.</param> public AnnounceMessage(long connectionId, int transactionId, string infoHash, string peerId, long downloaded, long left, long uploaded, TrackingEvent trackingEvent, uint key, int numberWanted, IPEndPoint endpoint) : base(TrackingAction.Announce, transactionId) { infoHash.CannotBeNullOrEmpty(); infoHash.Length.MustBeEqualTo(40); peerId.CannotBeNullOrEmpty(); peerId.Length.MustBeGreaterThanOrEqualTo(20); downloaded.MustBeGreaterThanOrEqualTo(0); left.MustBeGreaterThanOrEqualTo(0); uploaded.MustBeGreaterThanOrEqualTo(0); endpoint.CannotBeNull(); this.ConnectionId = connectionId; this.InfoHash = infoHash; this.PeerId = peerId; this.Downloaded = downloaded; this.Left = left; this.Uploaded = uploaded; this.TrackingEvent = trackingEvent; this.Key = key; this.NumberWanted = numberWanted; this.Endpoint = endpoint; }