Is there a better way to structure a method that returns a task that queues several inbound socket connections?
public Task Start(CancellationToken token)
{
return Task.Run(
() =>
{
var acceptCount = new SemaphoreSlim(10);
while (!token.IsCancellationRequested)
{
acceptCount.Wait(token);
if (token.IsCancellationRequested)
break;
var args = new SocketAsyncEventArgs();
args.UserToken = acceptCount;
args.Completed += AcceptInbound;
if (!this.socket.AcceptAsync(args))
AcceptInbound(this.socket, args);
}
},
token);
}
private void AcceptInbound(object sender, SocketAsyncEventArgs e)
{
((SemaphoreSlim)e.UserToken).Release();
Socket connectedClient = e.AcceptSocket;
}
connectedClient
s going to be processed further? What should happen ifAcceptInbound()
throws an exception? \$\endgroup\$