using System.Net;
using System.Net.Sockets;
using System.Text;
+using System.Threading;
namespace Hazel.Udp
{
public UdpBroadcastListener(int port)
{
this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
+ this.socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
this.endpoint = new IPEndPoint(IPAddress.Any, port);
this.socket.Bind(this.endpoint);
}
var result = this.socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref endpt, this.HandleData, null);
if (result.CompletedSynchronously)
{
- this.HandleData(result);
+ ThreadPool.QueueUserWorkItem(_ => this.HandleData(result));
}
}
catch
/// <summary>
/// Holds the last ID allocated.
/// </summary>
- private int lastIDAllocated = ushort.MaxValue + 1;
+ private int lastIDAllocated = 0;
/// <summary>
/// The packets of data that have been transmitted reliably and not acknowledged.
/// Handles acknowledgement packets to us.
/// </summary>
/// <param name="bytes">The buffer containing the data.</param>
- private void AcknowledgementMessageReceive(byte[] bytes)
+ private void AcknowledgementMessageReceive(byte[] bytes, int bytesReceived)
{
this.pingsSinceAck = 0;
}
}
- Statistics.LogReliableReceive(0, bytes.Length);
+ Statistics.LogReliableReceive(bytesReceived - 3, bytesReceived);
}
/// <summary>
/// <inheritdoc />
public abstract partial class UdpConnection : NetworkConnection
{
- protected static readonly byte[] EmptyDisconnectBytes = new byte[] { (byte)UdpSendOption.Disconnect };
+ public static readonly byte[] EmptyDisconnectBytes = new byte[] { (byte)UdpSendOption.Disconnect };
/// <summary>
/// Writes the given bytes to the connection.
//Handle acknowledgments
case (byte)UdpSendOption.Acknowledgement:
- AcknowledgementMessageReceive(message.Buffer);
+ AcknowledgementMessageReceive(message.Buffer, bytesReceived);
message.Recycle();
break;
//We need to acknowledge hello and ping messages but dont want to invoke any events!
case (byte)UdpSendOption.Ping:
ProcessReliableReceive(message.Buffer, 1, out id);
- Statistics.LogHelloReceive(message.Length);
+ Statistics.LogHelloReceive(bytesReceived);
message.Recycle();
break;
case (byte)UdpSendOption.Hello:
ProcessReliableReceive(message.Buffer, 1, out id);
- Statistics.LogHelloReceive(message.Length);
+ Statistics.LogHelloReceive(bytesReceived);
break;
case (byte)UdpSendOption.Disconnect:
//Treat everything else as unreliable
default:
InvokeDataReceived(SendOption.None, message, 1, bytesReceived);
- Statistics.LogUnreliableReceive(message.Length - 1, message.Length);
+ Statistics.LogUnreliableReceive(bytesReceived - 1, bytesReceived);
break;
}
}
public int MinConnectionLength = 0;
- public delegate bool AcceptConnectionCheck(out byte[] response);
+ public delegate bool AcceptConnectionCheck(byte[] input, out byte[] response);
public AcceptConnectionCheck AcceptConnection;
/// <summary>
{
message = MessageReader.GetSized(BufferSize);
- socket.BeginReceiveFrom(message.Buffer, 0, message.Buffer.Length, SocketFlags.None, ref remoteEP, ReadCallback, message);
+ var result = socket.BeginReceiveFrom(message.Buffer, 0, message.Buffer.Length, SocketFlags.None, ref remoteEP, ReadCallback, message);
+ if (result.CompletedSynchronously)
+ {
+ this.Logger("Operation completed synchronously");
+ }
}
catch (SocketException sx)
{
if (AcceptConnection != null)
{
- if (!AcceptConnection(out var response))
+ if (!AcceptConnection(message.Buffer, out var response))
{
message.Recycle();
SendData(response, response.Length, remoteEndPoint);