Skip to content

Added Receive and Send timeouts #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions SteamQueryNet/SteamQueryNet/Services/UdpWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using SteamQueryNet.Interfaces;

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
Expand All @@ -8,46 +8,69 @@ namespace SteamQueryNet.Services
{
internal sealed class UdpWrapper : IUdpClient
{
private readonly UdpClient _udpClient;
private readonly UdpClient udpClient;
private readonly int sendTimeout;
private readonly int receiveTimeout;

public UdpWrapper(IPEndPoint localIpEndPoint, int sendTimeout, int receiveTimeout)
{
_udpClient = new UdpClient(localIpEndPoint);
_udpClient.Client.SendTimeout = sendTimeout;
_udpClient.Client.ReceiveTimeout = receiveTimeout;
udpClient = new UdpClient(localIpEndPoint);
this.sendTimeout = sendTimeout;
this.receiveTimeout = receiveTimeout;
}

public bool IsConnected
{
get
{
return this._udpClient.Client.Connected;
return udpClient.Client.Connected;
}
}

public void Close()
{
this._udpClient.Close();
udpClient.Close();
}

public void Connect(IPEndPoint remoteIpEndpoint)
{
this._udpClient.Connect(remoteIpEndpoint);
udpClient.Connect(remoteIpEndpoint);
}

public void Dispose()
{
this._udpClient.Dispose();
udpClient.Dispose();
}

public Task<UdpReceiveResult> ReceiveAsync()
{
return this._udpClient.ReceiveAsync();
var asyncResult = udpClient.BeginReceive(null, null);
asyncResult.AsyncWaitHandle.WaitOne(receiveTimeout);
if (asyncResult.IsCompleted)
{
IPEndPoint remoteEP = null;
byte[] receivedData = udpClient.EndReceive(asyncResult, ref remoteEP);
return Task.FromResult(new UdpReceiveResult(receivedData, remoteEP));
}
else
{
throw new TimeoutException();
}
}

public Task<int> SendAsync(byte[] datagram, int bytes)
{
return this._udpClient.SendAsync(datagram, bytes);
var asyncResult = udpClient.BeginSend(datagram, bytes, null, null);
asyncResult.AsyncWaitHandle.WaitOne(sendTimeout);
if (asyncResult.IsCompleted)
{
int num = udpClient.EndSend(asyncResult);
return Task.FromResult(num);
}
else
{
throw new TimeoutException();
}
}
}
}