private async Task ReceiveCycle(CancellationToken token) { try { while (true) { token.ThrowIfCancellationRequested(); Result <byte[]> resut = await _dataReceiver.Receive(); if (resut.IsFailure) { Debug.WriteLine(resut.Error); } else { OnDataAvaliable?.Invoke(this, new ByteDataEventArgs { Data = resut.Value }); } } } catch (Exception ex) { Debug.WriteLine($"Error in ReceiveCycle {ex}"); } }
private void _endRecieve(IAsyncResult result) { int recieved = 0; try { recieved = _sock.EndReceive(result); // Gets the number of bytes recieved from the socket once it is avaliable, the actual data is in _buffer. } catch (SocketException) { Dispose(); } if (recieved > 0) { OnDataAvaliable?.Invoke(this, new DataEventArgs(_buffer.Take(recieved).ToArray())); // Trigger the OnDataAvaliable event } _sock.BeginReceive(_buffer, 0, RecieveBufferSize, 0, new AsyncCallback(_endRecieve), this); // Start waiting for the next portion of data }