the old data maybe overwritten with new one
seatrix opened this issue · comments
Tony commented
if send fast,the old data maybe overwritten with new one
Tony commented
fix it :
`
public void Start()
{
State state = new State(_MaxDatagramSize);
_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_Socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true);
if (String.IsNullOrEmpty(_Ip))
{
_Socket.Bind(new IPEndPoint(IPAddress.Any, _Port)); // broadcast endpoint
}
else
{
_Socket.Bind(new IPEndPoint(_IPAddress, _Port));
}
_Events.HandleStarted(this);
_Socket.BeginReceiveFrom(state.Buffer, 0, _MaxDatagramSize, SocketFlags.None, ref _Endpoint, _ReceiveCallback = (ar) =>
{
try
{
State so = (State)ar.AsyncState;
int bytes = _Socket.EndReceiveFrom(ar, ref _Endpoint);
string ipPort = _Endpoint.ToString();
string ip = null;
int port = 0;
Common.ParseIpPort(ipPort, out ip, out port);
if (!_RemoteSockets.Contains(ipPort))
{
_RemoteSockets.AddReplace(ipPort, _Socket);
EndpointDetected?.Invoke(this, new EndpointMetadata(ip, port));
}
if (bytes == so.Buffer.Length)
{
DatagramReceived?.Invoke(this, new Datagram(ip, port, so.Buffer));
}
else
{
byte[] buffer = new byte[bytes];
Buffer.BlockCopy(so.Buffer, 0, buffer, 0, bytes);
// Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, 50));
DatagramReceived?.Invoke(this, new Datagram(ip, port, buffer));
}
_Socket.BeginReceiveFrom(so.Buffer, 0, _MaxDatagramSize, SocketFlags.None, ref _Endpoint, _ReceiveCallback, so);
}
catch (Exception)
{
_Events.HandleStopped(this);
}
}, state);
}
`
Joel Christner commented
Sorry I'm just now seeing this! Any interest in putting this into a PR? Cheers and thanks for catching this!
Joel Christner commented
Cancel that, i see it now!
Joel Christner commented
Merged and published in NuGet v1.2.2, thanks for your contribution!