我正在尝试创建一个IOCP TCP客户端,我的代码如下所示:
TCPClient.h:
#pragma once
typedef struct
{
WSAOVERLAPPED Overlapped;
SOCKET Socket;
WSABUF wsaBuf;
char Buffer[1024];
DWORD Flags;
DWORD BytesSent;
DWORD BytesToSend;
} PER_IO_DATA, * LPPER_IO_DATA;
class TCPClient
{
public:
TCPClient();
~TCPClient();
bool Connect(const std::string strIpAddress, UINT32 uPort);
bool Disconnect();
bool SendCommand(const std::string strCommandName);
bool ReceiveResponse();
private:
static DWORD WINAPI ClientWorkerThread(LPVOID lpParameter);
private:
SOCKET m_socket;
PER_IO_DATA *m_pPerIoData;
};
TCPClient.cpp:
#include "StdAfx.h"
#include "TCPClient.h"
TCPClient::TCPClient() :
m_pPerIoData(NULL)
{
}
TCPClient::~TCPClient()
{
}
bool TCPClient::Connect(const std::string strIpAddress, UINT32 uPort)
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR)
return false;
HANDLE hCompletionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
if (!hCompletionPort)
return false;
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
for (DWORD dwIndex = 0; dwIndex < systemInfo.dwNumberOfProcessors; dwIndex++)
{
HANDLE hThread = CreateThread(NULL, 0, ClientWorkerThread, hCompletionPort, 0, NULL);
CloseHandle(hThread);
}
m_socket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
if (m_socket == INVALID_SOCKET)
{
WSACleanup();
return false;
}
sockaddr_in server;
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(strIpAddress.c_str());
server.sin_port = htons(uPort);
CreateIoCompletionPort((HANDLE)m_socket, hCompletionPort, 0, 0);
if (WSAConnect(m_socket, (LPSOCKADDR)&server, sizeof(server), NULL, NULL, NULL, NULL) == SOCKET_ERROR)
{
WSACleanup();
return false;
}
return true;
}
bool TCPClient::Disconnect()
{
if (m_socket)
closesocket(m_socket);
WSACleanup();
return true;
}
bool TCPClient::SendCommand(const std::string strCommandName)
{
m_pPerIoData = new PER_IO_DATA;
ZeroMemory(m_pPerIoData, sizeof(PER_IO_DATA));
strcpy(m_pPerIoData->Buffer, strCommandName.c_str());
m_pPerIoData->Overlapped.hEvent = WSACreateEvent();
m_pPerIoData->Socket = m_socket;
m_pPerIoData->wsaBuf.buf = m_pPerIoData->Buffer;
m_pPerIoData->wsaBuf.len = strlen(m_pPerIoData->Buffer);
m_pPerIoData->BytesToSend = m_pPerIoData->wsaBuf.len;
DWORD dwNumSent;
if (WSASend(m_socket, &(m_pPerIoData->wsaBuf), 1, &dwNumSent, 0, &(m_pPerIoData->Overlapped), NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != WSA_IO_PENDING)
{
delete m_pPerIoData;
return 0;
}
}
while (TRUE)
Sleep(1000);
return true;
}
bool TCPClient::ReceiveResponse()
{
return true;
}
DWORD WINAPI TCPClient::ClientWorkerThread(LPVOID lpParameter)
{
HANDLE hCompletionPort = (HANDLE)lpParameter;
DWORD NumBytesRecv = 0;
ULONG CompletionKey;
LPPER_IO_DATA PerIoData;
while (GetQueuedCompletionStatus(hCompletionPort, &NumBytesRecv, &CompletionKey, (LPOVERLAPPED*)&PerIoData, INFINITE))
{
if (!PerIoData)
continue;
if (NumBytesRecv == 0)
{
std::cout << "Server disconnected!\r\n\r\n";
}
else
{
// use PerIoData->Buffer as needed...
std::cout << std::string(PerIoData->Buffer, NumBytesRecv);
PerIoData->wsaBuf.len = sizeof(PerIoData->Buffer);
PerIoData->Flags = 0;
if (WSARecv(PerIoData->Socket, &(PerIoData->wsaBuf), 1, &NumBytesRecv, &(PerIoData->Flags), &(PerIoData->Overlapped), NULL) == 0)
continue;
if (WSAGetLastError() == WSA_IO_PENDING)
continue;
}
closesocket(PerIoData->Socket);
delete PerIoData;
}
return 0;
}
main.cpp中:
#include "stdafx.h"
#include "TCPClient.h"
int main()
{
TCPClient client;
client.Connect("127.0.0.1", 8888);
client.SendCommand("Hello command\r\n");
return 0;
}
我对“m_pPerIoData”的使用显然是错误的,因为我每次执行SendCommand()时都在新建,而不是正确删除它。
编辑2:
我已经对上面的现有代码进行了一些重命名(客户端 - &gt;连接),因为这让我很困惑。
某些背景:
我的顶级课程看起来像这样:
#pragma once
#include "Connection.h"
#include "Uncopyable.h"
#include "ConnectionFactory.h"
#include "CommandName.h"
class Ecr : private Uncopyable
{
public:
Ecr(const std::string& rstrConnectionInfo)
: m_spConnection(ConnectionFactory::CreateConnection(rstrConnectionInfo))
{
//Initialise();
}
~Ecr()
{
//Shutdown();
}
bool Initialise()
{
if (!m_spConnection)
return false;
m_spConnection->Initialise();
return true;
}
bool Shutdown()
{
if (!m_spConnection)
return false;
m_spConnection->Shutdown();
return true;
}
bool Connect()
{
if (!m_spConnection)
return false;
if (!m_spConnection->Connect())
return false;
return true;
}
bool Disconnect()
{
m_spConnection->Disconnect();
return true;
}
bool Logon(const std::vector<BYTE>& rvecCommandOptions)
{
m_spConnection->SendCommand(CommandName::Logon(), rvecCommandOptions);
return true;
}
bool Logoff()
{
m_spConnection->SendCommand(CommandName::Logoff());
return true;
}
// ... more commands follow.
private:
ConnectionPtr m_spConnection;
};
我已经获得了由ECR类创建的TcpConnection类,并完成了所有艰苦的工作。 Ecr和Connection类都是不可复制的。
Connection.h:
#pragma once
#include "Uncopyable.h"
class CommandName;
class Connection : private Uncopyable
{
public:
Connection(const std::string& rstrConnectionInfo);
virtual ~Connection();
virtual bool Initialise() = 0;
virtual bool Shutdown() = 0;
virtual bool Connect() = 0;
virtual bool Disconnect() = 0;
virtual bool SendCommand(const CommandName& rCommandName, const std::vector<BYTE>& rvecCommandOptions) = 0;
virtual bool ReceiveResponse() = 0;
bool SendCommand(const CommandName& rCommandName);
private:
std::string m_strConnectionInfo;
};
typedef std::tr1::shared_ptr<Connection> ConnectionPtr;
TcpConnection.h:
#pragma once
#include "connection.h"
class TcpConnection : public Connection
{
public:
TcpConnection(const std::string& rstrConnectionInfo);
~TcpConnection();
// Connection
bool Initialise();
bool Shutdown();
bool Connect();
bool Disconnect();
bool SendCommand(const CommandName& rCommandName, const std::vector<BYTE>& rvecCommandOptions);
bool ReceiveResponse();
static DWORD WINAPI WorkerThread(LPVOID lpParam);
private:
SOCKET m_socket;
HANDLE m_hIocp;
};
TcpConnection.cpp:
#include "StdAfx.h"
#include "TcpConnection.h"
#include "CommandBuilderTcp.h"
TcpConnection::TcpConnection(const std::string& rstrConnectionInfo)
: Connection(rstrConnectionInfo)
, m_socket(INVALID_SOCKET)
, m_hIocp(INVALID_HANDLE_VALUE)
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
m_socket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
}
TcpConnection::~TcpConnection()
{
}
bool TcpConnection::Initialise()
{
// Set up threads for using IOCP.
m_hIocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
for (DWORD dwIndex = 0; dwIndex < systemInfo.dwNumberOfProcessors; dwIndex++)
{
HANDLE hThread = CreateThread(NULL, 0, WorkerThread, m_hIocp, 0, NULL);
CloseHandle(hThread);
}
CreateIoCompletionPort((HANDLE)m_socket, m_hIocp, 0, 0);
return true;
}
bool TcpConnection::Shutdown()
{
// Release threads.
return true;
}
bool TcpConnection::Connect()
{
if (m_socket)
return true;
// Hard-coding IP address and port number for now.
sockaddr_in server;
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("10.0.9.80");
server.sin_port = htons(22000);
WSAConnect(m_socket, (LPSOCKADDR)&server, sizeof(server), NULL, NULL, NULL, NULL);
return true;
}
bool TcpConnection::Disconnect()
{
if (m_socket)
closesocket(m_socket);
WSACleanup();
return true;
}
bool TcpConnection::SendCommand(const CommandName& rCommandName, const std::vector<BYTE>& rvecCommandOptions)
{
// Build full command from rCommandName and rvecCommandOptions and send to server.
return true;
}
bool TcpConnection::ReceiveResponse()
{
return true;
}
DWORD WINAPI TcpConnection::WorkerThread(LPVOID lpParam)
{
// Call GetQueuedCompletionStatus in a loop
return 0;
}
主机应用程序然后为每个ECR设备创建一个DLL实例:
Ecr ecr("ipaddress+port");
ecr.Initialise(); // Or do this in Ecr's ctor?
ecr.Connect();
BYTE arrCommandOptions[] = {0x00, 0x00, 0x00, 0x01, 0x18, 0xA6, 0x00, 0x01, 0x49, 0x08, 0x26};
std::vector<BYTE> vecCommandOptions(arrCommandOptions, arrCommandOptions + sizeof(arrCommandOptions) / sizeof(arrCommandOptions[0]));
ecr.Logon(vecCommandOptions);
ecr.Logoff();
ecr.Disconnect();
ecr.Shutdown(); // Or do this in Ecr's dtor?
return 0;
我希望TcpConnection级别的所有IOCP相关内容,而不是Ecr级别,因为我不希望Ecr关心如何在下面完成数据传输。
我的想法不会起作用吗?
答案 0 :(得分:3)
每个I / O数据必须存在于所讨论的I / O操作的生命周期中。因此,您可能希望动态分配它,并在完成后将其集中以便重用。我使用引用计数系统,但这只是因为我可以保持每个I / O数据的存活时间超过单个I / O操作。 I / O特定数据的生命周期如下:
您可能希望查看执行此操作的某些IOCP代码(并以可伸缩的方式处理事物的线程方面),请参阅here。
答案 1 :(得分:2)
您展示的内容基于我在another question中提供给您的IOCP代码。但是,该代码旨在在同一个套接字上跨多个IOCP操作重用单个PER_IO_DATA
实例,因为您只是在客户端中读取并且只在服务器中写入。现在您在客户端混合读取和写入,因此您需要通过合并我之前提供的所有代码来调整工作线程以支持两种类型的IOCP操作,然后在{{1}中添加一个额外的标志所以完成的操作知道它是读操作还是写操作。
关于你的问题:
我应该在哪里做m_pPerIoData =新的PER_IO_DATA?
使用全班PER_IO_DATA
成员没有多大意义。 m_pPerIoData
正在创建一个新的PER_IO_DATA,这很好,当工作线程检测到所有SendCommand()
数据已完成发送或套接字已被发送时,您只需要delete
它闭合。
将指向PER_IO_DATA的指针作为成员变量是否有意义?
不是写操作,不是。不过,你可以让一个班级成员阅读。除非PER_IO_DATA::Buffer
在准备好阅读单个响应时创建新的SendCommand()
。当收到完整响应或套接字关闭时,工作线程需要PER_IO_DATA
。