45 lines
942 B
C++
45 lines
942 B
C++
#include "Socket/Socket_Init.hpp"
|
|
#include "Socket/Log.hpp"
|
|
#include "precomp.hpp"
|
|
|
|
namespace Chattr {
|
|
|
|
Socket_Init::Socket_Init(std::int32_t domain, std::int32_t type, std::int32_t protocol)
|
|
{
|
|
#ifdef _WIN32
|
|
WSADATA wsa;
|
|
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
|
|
log::critical("WSAStartup()");
|
|
#endif
|
|
sock_ = socket(domain, type, protocol);
|
|
if (sock_ == INVALID_SOCKET)
|
|
log::critical("socket()");
|
|
|
|
valid_ = true;
|
|
}
|
|
|
|
Socket_Init::~Socket_Init() {
|
|
if (!valid_)
|
|
return;
|
|
#ifdef _WIN32
|
|
closesocket(sock_);
|
|
WSACleanup();
|
|
#elif __linux__
|
|
close(sock_);
|
|
#endif
|
|
}
|
|
|
|
Socket_Init::operator SOCKET() const {
|
|
if (valid_)
|
|
return sock_;
|
|
log::critical("No valid socket created.", false);
|
|
return INVALID_SOCKET;
|
|
};
|
|
|
|
void Socket_Init::bind(int __fd, const sockaddr *__addr, socklen_t __len) {
|
|
int retVal = ::bind(__fd, __addr, __len);
|
|
if (retVal == INVALID_SOCKET)
|
|
log::critical("bind()");
|
|
}
|
|
|
|
} |