shm*_*000 2 c++ sockets move raii
我有一个管理资源的类(网络套接字).
我编写了一个类ConnectionHandler来处理通过调用创建的网络套接字accept().
这个类在设计时考虑了RAII,当accept()调用时,返回的套接字放入a ConnectionHandler,当这超出范围时,析构函数关闭套接字.
我也ConnectionHandler通过将它们保存在地图中来跟踪我所有的打开(将套接字地址(IP:端口)映射到ConnectionHandler与该地址对应的地址).
我有一个问题,"将这些ConnectionHandler"放入地图".
我已经做到了这样一个ConnectionHandler无法复制(至少我相信我已经这样做了),但是在调用时,调用std::map::emplace了ConnectionHandler析构函数(可能是为了删除在该行某处创建的临时对象)和套接字已关闭.
如您所见,这会产生问题,因为现在套接字无法在程序中进一步使用.
我有什么方法可以阻止ConnectionHandler在将它驱逐到一个被破坏者的时候被叫出来std::map?
这是ConnectionHandler:头文件的代码:
class ConnectionHandler
{
private:
constexpr static long BUFFER_SIZE = 1 << 12; // 4K Buffer
SocketAddress peer; // This is kept around to be able to produce clear exception messages when something goes wrong
SocketFileDescriptor socket; // using SocketFileDescriptor = int;
public:
ConnectionHandler() noexcept = delete; // Default Constructor
explicit ConnectionHandler(SocketFileDescriptor socket, const SocketAddress& socketAddress) noexcept; // Value Constructor
ConnectionHandler (ConnectionHandler&& handler) noexcept; // Move Constructor
ConnectionHandler (const ConnectionHandler& handler) = delete; // Delete Copy Constructor
ConnectionHandler& operator= (ConnectionHandler&& handler) noexcept; // Move Assignment Operator
ConnectionHandler& operator= (const ConnectionHandler& handler) = delete; // Delete Copy Assignment Operator
~ConnectionHandler(); // Destructor
void close() noexcept; // Allow the owner to manually close the socket if necessary
void set_blocking (bool blocking) const; // Make the socket either blocking or non-blocking
friend std::ostream& operator<< (std::ostream& stream, const ConnectionHandler& handler); // Receive data from the socket
friend std::istream& operator>> (std::istream& stream, const ConnectionHandler& handler); // Send data to the socket
};
Run Code Online (Sandbox Code Playgroud)
并实施:
ConnectionHandler::ConnectionHandler(SocketFileDescriptor socket, const SocketAddress& socketAddress) noexcept: peer(socketAddress), socket(socket)
{
}
ConnectionHandler::ConnectionHandler(ConnectionHandler&& handler) noexcept: peer(std::move(handler.peer)), socket(handler.socket)
{
}
ConnectionHandler& ConnectionHandler::operator=(ConnectionHandler&& handler) noexcept
{
this->peer = std::move(handler.peer);
this->socket = handler.socket;
return *this;
}
ConnectionHandler::~ConnectionHandler()
{
if (this->socket > 0) // Check if the socket has been closed manually
// Don't bother setting the socket to -1, the object is being destroyed anyway
{
std::cout << "Closing socket from destructor " << this->socket << std::endl;
::close(this->socket);
}
}
void ConnectionHandler::close() noexcept
{
std::cout << "Closing socket from close() " << this->socket << std::endl; // Close the socket manually and indicate it is closed by setting it's value to -1
::close(this->socket);
this->socket = -1;
}
[...]
Run Code Online (Sandbox Code Playgroud)
这就是SocketAddress类的样子(对于IPv6不起作用,我知道):
class SocketAddress
{
private:
std::array<std::uint8_t, 4> ip;
std::uint16_t port;
public:
friend void swap (SocketAddress& sa1, SocketAddress& sa2) noexcept;
SocketAddress() noexcept;
explicit SocketAddress(struct sockaddr_storage* sockaddrStorage);
SocketAddress (const SocketAddress& address) = default;
SocketAddress (SocketAddress&& address) noexcept = default;
SocketAddress& operator= (SocketAddress address);
friend bool operator< (const SocketAddress& lhs, const SocketAddress& rhs) noexcept;
friend std::string to_string(const SocketAddress& address) noexcept;
};
Run Code Online (Sandbox Code Playgroud)
最后,这是创建ConnectionHandler并将其放在地图中的代码:
void Server::listenLoop() // acceptLoop() would be a better name
{
struct sockaddr_storage remoteAddr;
while(!stop) // stop is a std::atomic<bool>
{
[...] // accept() connections in a loop
SocketAddress address = SocketAddress(&remoteAddr);
this->incomingSockets.emplace(std::make_pair(address, ConnectionHandler(childFileDesc, address)));
}
[...]
}
Run Code Online (Sandbox Code Playgroud)
此函数在与主线程分开的线程上运行,线程保存在Server对象中,并在Server对象的析构函数中连接.