这是一个实现的草图,它结合了 usr 的出色byte[1]
解决方法建议,并展示了如何在不牺牲性能的情况下将有些繁琐的Socket.xxxAsync
方法完全隐藏在 a中。SimpleAsyncSocket
一个简单的异步回显服务器使用SimpleAsyncSocket
可能如下所示。
readonly static Encoding Enc = new UTF8Encoding(false);
SimpleAsyncSocket _simpleSocket;
void StartEchoServer(Socket socket)
{
_simpleSocket = new SimpleAsyncSocket(socket, OnSendCallback,
_receiveBufferPool, OnReceiveCallback);
}
bool OnReceiveCallback(SimpleAsyncSocket socket,
ArraySegment<byte> bytes)
{
var str = Enc.GetString(bytes.Array, bytes.Offset, bytes.Count);
_simpleSocket.SendAsync(new ArraySegment<byte>(Enc.GetBytes(str)));
return false;
}
void OnSendCallback(SimpleAsyncSocket asyncSocket,
ICollection<ArraySegment<byte>> collection, SocketError arg3)
{
var bytes = collection.First();
var str = Enc.GetString(bytes.Array, bytes.Offset, bytes.Count);
}
这是实现的草图:
class SimpleAsyncSocket
{
private readonly Socket _socket;
private readonly Pool<byte[]> _receiveBufferPool;
private readonly SocketAsyncEventArgs _recvAsyncEventArgs;
private readonly SocketAsyncEventArgs _sendAsyncEventArgs;
private readonly byte[] _waitForReceiveEventBuffer = new byte[1];
private readonly Queue<ArraySegment<byte>> _sendBuffers = new Queue<ArraySegment<byte>>();
public SimpleAsyncSocket(Socket socket, Action<SimpleAsyncSocket, ICollection<ArraySegment<byte>>, SocketError> sendCallback,
Pool<byte[]> receiveBufferPool, Func<SimpleAsyncSocket, ArraySegment<byte>, bool> receiveCallback)
{
if (socket == null) throw new ArgumentNullException("socket");
if (sendCallback == null) throw new ArgumentNullException("sendCallback");
if (receiveBufferPool == null) throw new ArgumentNullException("receiveBufferPool");
if (receiveCallback == null) throw new ArgumentNullException("receiveCallback");
_socket = socket;
_sendAsyncEventArgs = new SocketAsyncEventArgs();
_sendAsyncEventArgs.UserToken = sendCallback;
_sendAsyncEventArgs.Completed += SendCompleted;
_receiveBufferPool = receiveBufferPool;
_recvAsyncEventArgs = new SocketAsyncEventArgs();
_recvAsyncEventArgs.UserToken = receiveCallback;
_recvAsyncEventArgs.Completed += ReceiveCompleted;
_recvAsyncEventArgs.SetBuffer(_waitForReceiveEventBuffer, 0, 1);
ReceiveAsyncWithoutTheHassle(_recvAsyncEventArgs);
}
public void SendAsync(ArraySegment<byte> buffer)
{
lock (_sendBuffers)
_sendBuffers.Enqueue(buffer);
StartOrContinueSending();
}
private void StartOrContinueSending(bool calledFromCompleted = false)
{
lock (_waitForReceiveEventBuffer) // reuse unrelated object for locking
{
if (!calledFromCompleted && _sendAsyncEventArgs.BufferList != null)
return; // still sending
List<ArraySegment<byte>> buffers = null;
lock (_sendBuffers)
{
if (_sendBuffers.Count > 0)
{
buffers = new List<ArraySegment<byte>>(_sendBuffers);
_sendBuffers.Clear();
}
}
_sendAsyncEventArgs.BufferList = buffers; // nothing left to send
if (buffers == null)
return;
}
if (!_socket.SendAsync(_sendAsyncEventArgs))
// Someone on stackoverflow claimed that invoking the Completed
// handler synchronously might end up blowing the stack, which
// does sound possible. To avoid that guy finding my code and
// downvoting me for it (and maybe just because it's the right
// thing to do), let's leave the call stack via the ThreadPool
ThreadPool.QueueUserWorkItem(state => SendCompleted(this, _sendAsyncEventArgs));
}
private void SendCompleted(object sender, SocketAsyncEventArgs args)
{
switch (args.LastOperation)
{
case SocketAsyncOperation.Send:
{
try
{
var bytesTransferred = args.BytesTransferred;
var sendCallback = (Action<SimpleAsyncSocket, ICollection<ArraySegment<byte>>, SocketError>)args.UserToken;
// for the moment, I believe the following commented-out lock is not
// necessary, but still have to think it through properly
// lock (_waitForReceiveEventBuffer) // reuse unrelated object for locking
{
sendCallback(this, args.BufferList, args.SocketError);
}
StartOrContinueSending(true);
}
catch (Exception e)
{
args.BufferList = null;
// todo: log and disconnect
}
break;
}
case SocketAsyncOperation.None:
break;
default:
throw new Exception("Unsupported operation: " + args.LastOperation);
}
}
private void ReceiveCompleted(object sender, SocketAsyncEventArgs args)
{
switch (args.LastOperation)
{
case SocketAsyncOperation.Receive:
{
var bytesTransferred = args.BytesTransferred;
var buffer = args.Buffer;
if (args.BytesTransferred == 0) // remote end closed connection
{
args.SetBuffer(null, 0, 0);
if (buffer != _waitForReceiveEventBuffer)
_receiveBufferPool.Return(buffer);
// todo: disconnect event
return;
}
if (buffer == _waitForReceiveEventBuffer)
{
if (args.BytesTransferred == 1)
{
// we received one byte, there's probably more!
var biggerBuffer = _receiveBufferPool.Take();
biggerBuffer[0] = _waitForReceiveEventBuffer[0];
args.SetBuffer(biggerBuffer, 1, biggerBuffer.Length - 1);
ReceiveAsyncWithoutTheHassle(args);
}
else
throw new Exception("What the heck");
}
else
{
var callback = (Func<SimpleAsyncSocket, ArraySegment<byte>, bool>)args.UserToken;
bool calleeExpectsMoreDataImmediately = false;
bool continueReceiving = false;
try
{
var count = args.Offset == 1
// we set the first byte manually from _waitForReceiveEventBuffer
? bytesTransferred + 1
: bytesTransferred;
calleeExpectsMoreDataImmediately = callback(this, new ArraySegment<byte>(buffer, 0, count));
continueReceiving = true;
}
catch (Exception e)
{
// todo: log and disconnect
}
finally
{
if (!calleeExpectsMoreDataImmediately)
{
args.SetBuffer(_waitForReceiveEventBuffer, 0, 1);
_receiveBufferPool.Return(buffer);
}
}
if (continueReceiving)
ReceiveAsyncWithoutTheHassle(args);
}
break;
}
case SocketAsyncOperation.None:
break;
default:
throw new Exception("Unsupported operation: " + args.LastOperation);
}
}
private void ReceiveAsyncWithoutTheHassle(SocketAsyncEventArgs args)
{
if (!_socket.ReceiveAsync(args))
// Someone on stackoverflow claimed that invoking the Completed
// handler synchronously might end up blowing the stack, which
// does sound possible. To avoid that guy finding my code and
// downvoting me for it (and maybe just because it's the right
// thing to do), let's leave the call stack via the ThreadPool
ThreadPool.QueueUserWorkItem(state => ReceiveCompleted(this, args));
}
}