private async Task SendRequestAsync(UdpTrackerMessage message) { var encodedMessage = message.Encode(); var dataWriter1 = new DataWriter(_tracker.OutputStream); try { dataWriter1.WriteBytes(encodedMessage); await dataWriter1.StoreAsync(); } finally { dataWriter1.DetachStream(); } ClientEngine.MainLoop.QueueTimeout(RetryDelay, () => { lock (_lock) { if (_timeout == 0) { return(false); } if (_timeout <= 4) { _timeout++; try { var dataWriter = new DataWriter(_tracker.OutputStream); try { dataWriter.WriteBytes(encodedMessage); dataWriter.StoreAsync().AsTask().Wait(); } finally { dataWriter.DetachStream(); } } catch (Exception exception1) { var exception = exception1; lock (_lock) { _timeout = 0; } _taskCompletionSource.TrySetException(exception); return(false); } return(true); } _timeout = 0; _taskCompletionSource.TrySetException(new Exception("Announce timeout.")); } return(false); }); }
async void SendAsync(UdpClient client, UdpTrackerMessage msg, CancellationToken token) { byte[] buffer = msg.Encode(); try { do { client.Send(buffer, buffer.Length); await Task.Delay(RetryDelay, token); }while (!token.IsCancellationRequested); } catch { } }
void SendAsync(UdpClient client, UdpTrackerMessage msg, CancellationToken token) { byte[] buffer = msg.Encode(); client.Send(buffer, buffer.Length); ClientEngine.MainLoop.QueueTimeout(RetryDelay, () => { try { if (!token.IsCancellationRequested) { client.Send(buffer, buffer.Length); } return(!token.IsCancellationRequested); } catch { return(false); } }); }
void Check(UdpTrackerMessage message, MessageType type) { byte[] e = message.Encode(); Assert.AreEqual(e.Length, message.ByteLength, "#1"); Assert.IsTrue(Toolbox.ByteMatch(e, UdpTrackerMessage.DecodeMessage(e, 0, e.Length, type).Encode()), "#2"); }