public async Task ReceiveData(CancellationToken token) { StringBuilder sb = new StringBuilder(); while (true) { if (token.IsCancellationRequested) { UDP.Close(); break; } var buffer = await UDP.ReceiveAsync(); var remoteInfo = buffer.RemoteEndPoint; var bufferAsString = Encoding.UTF8.GetString(buffer.Buffer); sb.Append(bufferAsString); if (bufferAsString.EndsWith("\r\n", StringComparison.InvariantCultureIgnoreCase)) { var fullString = sb.ToString().Trim(); sb.Clear(); fullLineEventHandler.Invoke(this, new UDPJsonLineReceivedEventArgs { JSONString = fullString, RemoteInfo = remoteInfo }); } else if (bufferAsString.StartsWith("{", StringComparison.InvariantCultureIgnoreCase) && bufferAsString.EndsWith("}", StringComparison.InvariantCultureIgnoreCase) && bufferAsString.Contains("Publish")) { var fullString = sb.ToString(); var obj = JsonConvert.DeserializeObject(fullString); sb.Clear(); fullLineEventHandler.Invoke(this, new UDPJsonLineReceivedEventArgs { JSON = obj, JSONString = fullString, RemoteInfo = remoteInfo }); } } return; }