.
This commit is contained in:
@@ -22,14 +22,7 @@ Socket::Socket(int domain, int type, int protocol) {
|
||||
}
|
||||
|
||||
Socket::~Socket() {
|
||||
if (!valid_)
|
||||
return;
|
||||
#ifdef _WIN32
|
||||
closesocket(sock_);
|
||||
WSACleanup();
|
||||
#elif __linux__
|
||||
::close(sock_);
|
||||
#endif
|
||||
destruct();
|
||||
}
|
||||
|
||||
void Socket::init(int domain, int type, int protocol) {
|
||||
@@ -45,6 +38,17 @@ void Socket::init(int domain, int type, int protocol) {
|
||||
valid_ = true;
|
||||
}
|
||||
|
||||
void Socket::destruct() {
|
||||
if (!valid_)
|
||||
return;
|
||||
#ifdef _WIN32
|
||||
closesocket(sock_);
|
||||
WSACleanup();
|
||||
#elif __linux__
|
||||
::close(sock_);
|
||||
#endif
|
||||
}
|
||||
|
||||
Socket::operator SOCKET() const {
|
||||
if (valid_)
|
||||
return sock_;
|
||||
@@ -52,30 +56,25 @@ Socket::operator SOCKET() const {
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
void Socket::move(const SOCKET __sock) {
|
||||
if (__sock == INVALID_SOCKET)
|
||||
void Socket::move(const SOCKET __sock)
|
||||
{
|
||||
if (__sock == INVALID_SOCKET)
|
||||
log::critical("socket()");
|
||||
|
||||
if (valid_) {
|
||||
#ifdef _WIN32
|
||||
closesocket(sock_);
|
||||
WSACleanup();
|
||||
#elif __linux__
|
||||
::close(sock_);
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
#ifdef _WIN32
|
||||
WSADATA wsa;
|
||||
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
|
||||
log::critical("WSAStartup()");
|
||||
#endif
|
||||
}
|
||||
destruct();
|
||||
|
||||
sock_ = __sock;
|
||||
valid_ = true;
|
||||
};
|
||||
|
||||
Socket& Socket::move() {
|
||||
std::shared_ptr<Socket> socket = std::make_shared<Socket>();
|
||||
valid_ = false;
|
||||
socket->move(sock_);
|
||||
|
||||
return *socket;
|
||||
};
|
||||
|
||||
void Socket::bind(sockaddr *__addr) {
|
||||
bind((struct sockaddr *)__addr, sizeof(sockaddr));
|
||||
}
|
||||
@@ -104,4 +103,20 @@ void Socket::bind(sockaddr_in6 *__addr, socklen_t __len) {
|
||||
bind((struct sockaddr *)__addr, __len);
|
||||
}
|
||||
|
||||
}
|
||||
Socket::Socket(Socket&& other_) {
|
||||
other_.valid_ = false;
|
||||
sock_ = other_.sock_;
|
||||
valid_ = true;
|
||||
}
|
||||
|
||||
Socket& Socket::operator=(Socket && other_) {
|
||||
other_.valid_ = false;
|
||||
sock_ = other_.sock_;
|
||||
valid_ = true;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user