로거 완성
This commit is contained in:
64
impl/Socket/Socket.cpp
Normal file
64
impl/Socket/Socket.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#include "Socket/Socket.hpp"
|
||||
#include "Socket/Log.hpp"
|
||||
#include "precomp.hpp"
|
||||
|
||||
namespace Chattr {
|
||||
|
||||
Socket::Socket(int domain, int type, int 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::~Socket() {
|
||||
if (!valid_)
|
||||
return;
|
||||
#ifdef _WIN32
|
||||
closesocket(sock_);
|
||||
WSACleanup();
|
||||
#elif __linux__
|
||||
::close(sock_);
|
||||
#endif
|
||||
}
|
||||
|
||||
Socket::operator SOCKET() const {
|
||||
if (valid_)
|
||||
return sock_;
|
||||
spdlog::critical("No valid socket created.");
|
||||
return INVALID_SOCKET;
|
||||
};
|
||||
|
||||
void Socket::bind(int __fd, const sockaddr *__addr) {
|
||||
bind(__fd, (struct sockaddr *)__addr, sizeof(*__addr));
|
||||
}
|
||||
|
||||
void Socket::bind(int __fd, const sockaddr *__addr, socklen_t __len) {
|
||||
int retVal = ::bind(__fd, __addr, __len);
|
||||
if (retVal == INVALID_SOCKET)
|
||||
log::critical("bind()");
|
||||
}
|
||||
|
||||
void Socket::bind(int __fd, const sockaddr_in *__addr) {
|
||||
bind(__fd, (struct sockaddr *)__addr, sizeof(*__addr));
|
||||
}
|
||||
|
||||
void Socket::bind(int __fd, const sockaddr_in *__addr, socklen_t __len) {
|
||||
bind(__fd, (struct sockaddr *)__addr, __len);
|
||||
}
|
||||
|
||||
void Socket::bind(int __fd, const sockaddr_in6 *__addr) {
|
||||
bind(__fd, (struct sockaddr *)__addr, sizeof(*__addr));
|
||||
}
|
||||
|
||||
void Socket::bind(int __fd, const sockaddr_in6 *__addr, socklen_t __len) {
|
||||
bind(__fd, (struct sockaddr *)__addr, __len);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user