AMe*_*ars 1 c++ constructor copy winsock handle
在使用win32 API方面,我是一个新手,所以请多多包涵。
我目前正在从事一个涉及Winsock的C ++项目,但是对于复制构造函数,我对使用事件对象HANDLE类型的正确方法感到困惑。
概述(下面的代码):在尝试使用IOCP并使所有内容保持可伸缩性时,我有一个线程检查多个接受事件。每个ServerConnection对象都拥有由WSACreateEvent()创建的其自己的接受事件对象,其关联的低级套接字以及相关的状态/变量。
我的问题是,我正在尝试实施“三巨头”,但我不确定如何“复制”手柄。
DuplicateHandle()似乎创建了一个新的句柄,但它指向同一个对象,但是就ServerConnection“副本”而言,这是没有意义的(我们想要一个具有相同状态的新对象,对吗?)。
至于使用复制分配运算符,我不确定它将对事件对象HANDLEs做什么。
ServerConnection.h
class ServerConnection
{
public:
//...constructors, destructors, etc...
virtual HANDLE getAcceptEvent();
virtual void setAcceptEvent(HANDLE eventObj);
protected:
private:
HANDLE assocAcceptEvent;
//..other variables...
};
Run Code Online (Sandbox Code Playgroud)
ServerConnection.cpp
ServerConnection::ServerConnection(ServerConnection &that)
{
//blah blah...other vars
//? This does not seem right as the HANDLE is logically a pointer;
//Assigning like this just points another handle to the same event obj
//If the other ServerConnection object closes the handle...not good.
this.assocAcceptEvent = that.assocAcceptEvent;
//The only thing that make slightly more sense, is just to create a whole new one
//if I answered my own question, then great...but I wanted to make sure
this.assocAcceptEvent = WSACreateEvent();
//assume check for WSACreateEvent failing with WSAGetLastError() and
//handle appropriately
}
Run Code Online (Sandbox Code Playgroud)
您无法以一种明智的方式复制该句柄(即,DuplicateHandle
它无法执行您想要的操作)这一事实使我认为该对象不可复制...
您必须问自己的问题是:ServerConnection的副本实际上意味着什么?您将在哪里/如何使用它?
我不知道答案,因为我不知道您使用ServerConnection做整体设计的意思-但是它说ServerConnection的事实使我认为它是“连接”到某物的东西,因此不能轻易复制。
归档时间: |
|
查看次数: |
774 次 |
最近记录: |