public Task HandleStream(IReceiverCallback callback, Stream stream) { if (Status == ListeningStatus.TooBusy) { return(stream.SendBuffer(WireProtocol.ProcessingFailureBuffer)); } return(WireProtocol.Receive(stream, callback, Address)); }
public ListeningAgent(IReceiverCallback callback, IPAddress ipaddr, int port, string protocol, CancellationToken cancellationToken) { Port = port; _callback = callback; _cancellationToken = cancellationToken; _listener = new TcpListener(new IPEndPoint(ipaddr, port)); _listener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); _socketHandling = new ActionBlock <Socket>(async s => { using (var stream = new NetworkStream(s, true)) { await WireProtocol.Receive(stream, _callback, _uri); } }); _uri = $"{protocol}://{ipaddr}:{port}/".ToUri(); }
public void Start(IReceiverCallback callback) { _listener = new TcpListener(new IPEndPoint(IPAddress.Loopback, _port)); _listener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); _socketHandling = new ActionBlock <Socket>(async s => { using (var stream = new NetworkStream(s, true)) { await WireProtocol.Receive(stream, callback, Address); } }); _receivingLoop = Task.Run(async() => { _listener.Start(); while (!_cancellationToken.IsCancellationRequested) { var socket = await _listener.AcceptSocketAsync(); _socketHandling.Post(socket); } }, _cancellationToken); }