I have a simple console app which waits for incoming connections and then pipes/tunnels any incoming connection to a local service which is bound to the loopback interfaces.
The methods which perform this tunneling are async and long running - they will only terminate when the internal service stops or the client disconnect (which can be hours in duration). Currently this works but I am uncertain if the approach is correct as I do not "await" on the long running operations. I'm curious what is the proper way to handle this?
The compiler actually complains: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
public async Task Start(CancellationToken ct)
{
_socket.Bind(_portMapping.Source);
_socket.Listen(MaxQueuedConnections);
var args = new SocketAsyncEventArgs();
var awaitable = new SocketAwaitable(args);
while (!ct.IsCancellationRequested)
{
args.AcceptSocket = null;
await _socket.AcceptAsync(awaitable);
var listenerSource = args.AcceptSocket;
var feed = new MyForwarder(_portMapping.Remote.Address,
_portMapping.Remote.Port, MaxRetries);
feed.Connect();
if (feed.Socket.Connected && listenerSource != null && listenerSource.Connected)
{
// Both calls are async and long running. How should I handle as I want
// to continue to listen for an incoming connection
feed.Pipe(listenerSource, ct);
ForwardHelper.Pipe(listenerSource, feed.Socket, ct);
}
}
}