我正在处理C#中的套接字编程。我需要构建一个客户端应用程序,它与给定协议的服务器进行通信。
我成功实现了异步发送方法,但是我在实现接收算法方面遇到了麻烦。同步接收方法工作正常。
首先,我需要不断读取传入的消息并确认它们。为了正确,每个收到的消息必须有一个终结符(0x0c)
我构建了一个名为MessageFlow的多线程类,它有三个线程:一个负责发送消息,另一个负责接收消息,第三个负责解释收到的消息并做一些事情。
接收线程的worker函数看起来像这样
private void ReadSocketWorker()
{
while (this.canRun)
{
xComClient.Receive();
xComClient.receiveDone.WaitOne();
Thread.Sleep(10);
}
}
XComClient是我的类,具有套接字以及发送和接收消息的所有方法。
public void Receive()
{
try
{
StateObject state = new StateObject();
state.workSocket = socketClient;
socketClient.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
throw e;
}
}
private void ReceiveCallback(IAsyncResult ar)
{
try
{
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
// Read data from the remote device.
int iReadBytes = client.EndReceive(ar);
if (iReadBytes > state.GetBufferSize())
{
byte[] bytesReceived = new byte[iReadBytes];
Buffer.BlockCopy(state.buffer, 0, bytesReceived, 0, iReadBytes);
state.responseList.Enqueue(bytesReceived);
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
else
{
byte[] bytesReceived = new byte[iReadBytes];
Buffer.BlockCopy(state.buffer, 0, bytesReceived, 0, iReadBytes);
state.responseList.Enqueue(bytesReceived);
BuildReceivedMessage(state);
receiveDone.Set();
}
}
catch (Exception e)
{
throw e;
}
}
public class StateObject
{
public Socket workSocket = null;
public const int BufferSize = 20480;
public byte[] buffer = new byte[BufferSize];
public Queue<byte[]> responseList = new Queue<byte[]>();
public int GetBufferSize()
{
return BufferSize;
}
}
我做错了什么?
答案 0 :(得分:2)
在没有其他任何操作的线程中使用异步I / O确实没有任何意义。我会重新考虑这个设计决定。
答案 1 :(得分:1)
您的设计是同步和异步编程的混合体。正确设计的异步类根本不需要使用任何线程,但让.NET管理线程。
我真的希望throw e;
仅在示例中。因为它正在破坏堆栈跟踪(因此隐藏了异常发生的地方)。您可以阅读我的文章Don't catch that exception以及标记为exceptions
接收方法如下所示:
void OnReceive(IAsyncResult ar)
{
AppendInternalReadBuffer();
CheckInternalReadBufferForMessageAndProcessIt();
ReadAgain();
}
这将阻止服务器每个客户端一次处理多个消息。如果您不想这样做(这使事情变得复杂),您可以使用ThreadPool
中的CheckInternalReadBufferForMessageAndProcessIt
。