Okay, I read many questions involving writing high scale-able servers but I never really came across a good answer. Anyway I want to create a high scale-able clients, which handles lots of data and connections. What I created was a simple client using SocketAsyncEventArgs and C#5 async/await like this:
public async Task<int> SendAsync(byte[] buffer, int offset, int size)
{
var socketArgs = new SocketAsyncEventArgs();
var tcs = new TaskCompletionSource<int>();
socketArgs.SetBuffer(buffer, offset, size);
socketArgs.Completed += (sender, args) =>
{
tcs.SetResult(socketArgs.BytesTransferred);
LastSocketError = socketArgs.SocketError;
};
if (_clientSocket.SendAsync(socketArgs))
return await tcs.Task;
LastSocketError = socketArgs.SocketError;
return socketArgs.BytesTransferred;
}
And that for ConnectAsync, ReceiveAsync and AcceptAcync. This works great for the client part, but on my server I don't know how to get it right.. (I would end up creating a Thread for every Client for receiving.) I can use the APM (Begin/End) or using EventHandler but that kills the purpose of using async/await and in the end it isn't memory efficient when creating a new SocketAsyncEventArgs for every call. I tried creating a Pool (using a ConcurrentBag) but then I still had to create a TaskCompletionSource over and over (as you can use it only once.)
So I don't think this is a good idea of creating this, although I really like it.. What would be a good design to create a high scale-able and high performance server?