private void ReceiveCallback(object sender, SocketAsyncEventArgs e) { if (e.BytesTransferred == 0) { } else { int count = e.BytesTransferred; byte[] data = new byte[count]; Buffer.BlockCopy(e.Buffer, 0, data, 0, count); MemoryStream stream = new MemoryStream(data); while (stream.Position < stream.Length) { DTLSRecord record = DTLSRecord.Deserialise(stream); if (record != null) { record.RemoteEndPoint = e.RemoteEndPoint; SocketAddress address = record.RemoteEndPoint.Serialize(); Session session = _Sessions.GetSession(address); if (session == null) { ThreadPool.QueueUserWorkItem(ProcessRecord, record); } else { CheckSession(session, record); } } } Socket socket = sender as Socket; if (socket != null) { System.Net.EndPoint remoteEndPoint; if (socket.AddressFamily == AddressFamily.InterNetwork) { remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); } else { remoteEndPoint = new IPEndPoint(IPAddress.IPv6Any, 0); } e.RemoteEndPoint = remoteEndPoint; e.SetBuffer(0, 4096); socket.ReceiveFromAsync(e); } } }
private void _ReceiveCallback(byte[] recvData, EndPoint ip) { if (recvData == null) { throw new ArgumentNullException(nameof(recvData)); } if (ip == null) { throw new ArgumentNullException(nameof(ip)); } if (!recvData.Any()) { //nothing received? return? return; } if (recvData.Length < 13) { this._RecvDataBuffer = this._RecvDataBuffer.Concat(recvData).ToArray(); return; } var length = BitConverter.ToUInt16(recvData.Skip(11).Take(2).Reverse().ToArray(), 0); if (recvData.Length < length) { this._RecvDataBuffer = this._RecvDataBuffer.Concat(recvData).ToArray(); return; } var fullData = this._RecvDataBuffer.Concat(recvData).ToArray(); this._RecvDataBuffer = new byte[0]; using (var stream = new MemoryStream(fullData)) { while (stream.Position < stream.Length) { var record = DTLSRecord.Deserialise(stream); record.RemoteEndPoint = ip; this._Records.Add(record); } } }
private void ReceiveCallback(object sender, SocketAsyncEventArgs e) { if (e.BytesTransferred == 0) { } else { int count = e.BytesTransferred; byte[] data = new byte[count]; Buffer.BlockCopy(e.Buffer, 0, data, 0, count); MemoryStream stream = new MemoryStream(data); while (stream.Position < stream.Length) { DTLSRecord record = DTLSRecord.Deserialise(stream); if (record != null) { record.RemoteEndPoint = e.RemoteEndPoint; _Records.Add(record); _TriggerProcessRecords.Set(); } } Socket socket = sender as Socket; if (socket != null) { System.Net.EndPoint remoteEndPoint; if (socket.AddressFamily == AddressFamily.InterNetwork) { remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); } else { remoteEndPoint = new IPEndPoint(IPAddress.IPv6Any, 0); } e.RemoteEndPoint = remoteEndPoint; e.SetBuffer(0, 4096); socket.ReceiveFromAsync(e); } } }
private void ReceiveCallback(object sender, SocketAsyncEventArgs e) { if (e.BytesTransferred == 0) { #if DEBUG Console.WriteLine($"ReceiveCallback got 0 bytes"); #endif } else { int count = e.BytesTransferred; #if DEBUG Console.WriteLine($"ReceiveCallback got {count} bytes"); #endif byte[] data = new byte[count]; Buffer.BlockCopy(e.Buffer, 0, data, 0, count); MemoryStream stream = new MemoryStream(data); while (stream.Position < stream.Length) { DTLSRecord record = DTLSRecord.Deserialise(stream); if (record != null) { record.RemoteEndPoint = e.RemoteEndPoint; SocketAddress address = record.RemoteEndPoint.Serialize(); Session session = _Sessions.GetSession(address); if (session == null) { #if DEBUG Console.WriteLine($"session was null"); #endif ThreadPool.QueueUserWorkItem(ProcessRecord, record); } else { CheckSession(session, record); } } } if (sender is Socket socket) { System.Net.EndPoint remoteEndPoint; if (socket.AddressFamily == AddressFamily.InterNetwork) { remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); } else { remoteEndPoint = new IPEndPoint(IPAddress.IPv6Any, 0); } e.RemoteEndPoint = remoteEndPoint; e.SetBuffer(0, 4096); bool pending = socket.ReceiveFromAsync(e); if (!pending) { // If ReceiveFromAsync returns false, the callback will not be triggered automatically so we must call it ourselves. ReceiveCallback(sender, e); } } } }