我正在用 C# 制作一个 IOCP 服务器,调用 win api。我有 1 个接受线程连接和工作线程,具体取决于 CPU 内核。
我的问题是,我有 2 个线程试图同时处理相同的数据,根据以下代码段,你能告诉我解决方案是什么,还是这是一个设计问题?
public class WorkerThread
{
public void Worker(NetSharedData data)
{
IntPtr bytestransfer = Marshal.AllocHGlobal(sizeof(UInt32));
IntPtr clientid = Marshal.AllocHGlobal(sizeof(UInt32));
IntPtr poverlapped = Marshal.AllocHGlobal(sizeof(UInt32));
Console.WriteLine("Worker thread running");
while (true)
{
Marshal.WriteInt32(clientid, 0);
Marshal.WriteInt32(bytestransfer, 0);
if (SocketInvoke.GetQueuedCompletionStatus(data.completionport, bytestransfer, clientid, poverlapped, SocketInvoke.INFINITE) == true)
{
if (Marshal.ReadInt32(poverlapped) == 0)
{
//thread shutdown
Console.WriteLine("Worker thread shutdown");
break;
}
Client client = data.Clients[Marshal.ReadInt32(clientid)];
if (Marshal.ReadInt32(bytestransfer) != 0)
{
if (client.operationtype == SocketInvoke.FD_WRITE)
{
if (client.send_data.ispending_operation == true)
{
client.SendLeft((uint)Marshal.ReadInt32(bytestransfer));
break;
}
}
if (client.operationtype == SocketInvoke.FD_READ)
{
if (!client.Recv((uint)Marshal.ReadInt32(bytestransfer)))
{
client.Close();
break;
}
if (data.OnRecv(client) == true)
{
//SendLeft test
}
}
}
}
}
}
}
/*
//THIS IS A CODE SNIPPET THAT BELONGS TO THE ACCEPT THREAD CLASS
*/
public virtual bool Recv(uint expected_data_transfer)
{
IntPtr pwsabuf;
IntPtr wsaoverlapped;
IntPtr bytes_recv = Marshal.AllocHGlobal(sizeof(int));
IntPtr flags = Marshal.AllocHGlobal(sizeof(int));
Marshal.WriteInt32(flags, 0);
set_total_transfer(SocketInvoke.FD_READ, expected_data_transfer);
pwsabuf = recv_data.rtn_wsabuffarray(recv_data.buffer, (uint)recv_data.bufflen);
wsaoverlapped = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SocketInvoke.WSAOVERLAPPED)));
Marshal.StructureToPtr(overlapped, wsaoverlapped, false);
SocketInvoke.FillMemory(wsaoverlapped, (uint)Marshal.SizeOf(typeof(SocketInvoke.WSAOVERLAPPED)), 0);
unsafe
{
setoptype(SocketInvoke.FD_READ);
if (SocketInvoke.WSARecv(Convert.ToUInt32(sock.Handle.ToInt32()), pwsabuf,
(uint)1, bytes_recv, flags, wsaoverlapped, (IntPtr)null) == SocketInvoke.SOCKET_ERROR)
{
if (Marshal.GetLastWin32Error() != SocketInvoke.WSA_IO_PENDING)
{
return false;
}
return true;
}
}
return true;
}
也可以使用 WSASend 和 WSARecv 获得部分发送或部分接收数据吗?