public void Enqueue(MessageMetaInfo message) { Messages.Enqueue(message); }
public void IngressMessage(MessageMetaInfo message) { }
protected void WorkerProc() { EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); byte[] buffer = new byte[BufferSize]; while (true) { if (socket.SocketType == SocketType.Dgram) { Task <SocketReceiveFromResult> udpAsyncResult = socket.ReceiveFromAsync(new ArraySegment <byte>(buffer), SocketFlags.None, remoteEndPoint); try { udpAsyncResult.Wait(cancellationToken); } catch (OperationCanceledException) { break; } int bytesReceived = udpAsyncResult.Result.ReceivedBytes; if (bytesReceived > 0) { MessageMetaInfo message = new MessageMetaInfo { RawMessage = Encoding.ASCII.GetString(buffer, 0, bytesReceived) }; message.MetaData.Add("SenderIP", (udpAsyncResult.Result.RemoteEndPoint as IPEndPoint)?.Address.ToString()); message.MetaData.Add("ReciveTimestamp", DateTime.Now); message.MetaData.Add("Channel", "UDP"); foreach (YASLSQueue queue in OutputQueues) { queue.Enqueue(message); } } } if (socket.SocketType == SocketType.Stream) { Task <Socket> tcpAsyncAcceptResult = socket.AcceptAsync(); try { tcpAsyncAcceptResult.Wait(cancellationToken); } catch { break; } Socket innerSocket = tcpAsyncAcceptResult.Result; bool isCancelled = false; StringBuilder stringBuilder = new StringBuilder(); int bytesRead = 0; do { Task <int> tcpAsyncReceiveResult = innerSocket.ReceiveAsync(new ArraySegment <byte>(buffer), SocketFlags.None); try { tcpAsyncReceiveResult.Wait(cancellationToken); } catch { isCancelled = true; break; } bytesRead = tcpAsyncReceiveResult.Result; if (bytesRead > 0) { stringBuilder.Append(Encoding.ASCII.GetString(buffer, 0, tcpAsyncReceiveResult.Result)); } } while (bytesRead > 0); // propagate cancellation if (isCancelled) { break; } MessageMetaInfo message = new MessageMetaInfo { RawMessage = stringBuilder.ToString() }; message.MetaData.Add("SenderIP", (tcpAsyncAcceptResult.Result.RemoteEndPoint as IPEndPoint)?.Address.ToString()); message.MetaData.Add("ReciveTimestamp", DateTime.Now); message.MetaData.Add("Channel", "TCP"); foreach (YASLSQueue queue in OutputQueues) { queue.Enqueue(message); } } } }