diff --git a/SteamQueryNet/SteamQueryNet/Services/UdpWrapper.cs b/SteamQueryNet/SteamQueryNet/Services/UdpWrapper.cs index 4f24a23..de9f0b3 100644 --- a/SteamQueryNet/SteamQueryNet/Services/UdpWrapper.cs +++ b/SteamQueryNet/SteamQueryNet/Services/UdpWrapper.cs @@ -1,5 +1,5 @@ using SteamQueryNet.Interfaces; - +using System; using System.Net; using System.Net.Sockets; using System.Threading.Tasks; @@ -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 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 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(); + } } } }