일단 서버구현 끝
This commit is contained in:
1
impl/Socket/Chattering.log
Normal file
1
impl/Socket/Chattering.log
Normal file
@@ -0,0 +1 @@
|
||||
[2025-04-22 23:42:27.045] [Chattering Logger] [critical] [listen()] Operation not supported
|
||||
@@ -10,11 +10,7 @@ Socket::Socket(int domain, int type, int protocol) {
|
||||
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;
|
||||
init(domain, type, protocol);
|
||||
}
|
||||
|
||||
Socket::~Socket() {
|
||||
@@ -28,37 +24,76 @@ Socket::~Socket() {
|
||||
#endif
|
||||
}
|
||||
|
||||
void Socket::init(int domain, int type, int protocol) {
|
||||
if (domain == AF_INET)
|
||||
bindAddr.length = sizeof(sockaddr_in);
|
||||
else if (domain == AF_INET6)
|
||||
bindAddr.length = sizeof(sockaddr_in6);
|
||||
|
||||
sock_ = ::socket(domain, type, protocol);
|
||||
if (sock_ == INVALID_SOCKET)
|
||||
log::critical("socket()");
|
||||
|
||||
valid_ = true;
|
||||
}
|
||||
|
||||
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);
|
||||
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
|
||||
}
|
||||
|
||||
sock_ = __sock;
|
||||
valid_ = true;
|
||||
};
|
||||
|
||||
void Socket::bind(const sockaddr *__addr) {
|
||||
bind((struct sockaddr *)__addr, sizeof(*__addr));
|
||||
}
|
||||
|
||||
void Socket::bind(const sockaddr *__addr, socklen_t __len) {
|
||||
bindAddr.length = __len;
|
||||
std::memcpy(&bindAddr, __addr, __len);
|
||||
int retVal = ::bind(sock_, __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(const sockaddr_in *__addr) {
|
||||
bind((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(const sockaddr_in *__addr, socklen_t __len) {
|
||||
bind((struct sockaddr *)__addr, __len);
|
||||
}
|
||||
|
||||
void Socket::bind(int __fd, const sockaddr_in6 *__addr) {
|
||||
bind(__fd, (struct sockaddr *)__addr, sizeof(*__addr));
|
||||
void Socket::bind(const sockaddr_in6 *__addr) {
|
||||
bind((struct sockaddr *)__addr, sizeof(*__addr));
|
||||
}
|
||||
|
||||
void Socket::bind(int __fd, const sockaddr_in6 *__addr, socklen_t __len) {
|
||||
bind(__fd, (struct sockaddr *)__addr, __len);
|
||||
void Socket::bind(const sockaddr_in6 *__addr, socklen_t __len) {
|
||||
bind((struct sockaddr *)__addr, __len);
|
||||
}
|
||||
|
||||
}
|
||||
26
impl/Socket/TCPSocket.cpp
Normal file
26
impl/Socket/TCPSocket.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "Socket/TCPSocket.hpp"
|
||||
#include "Socket/Log.hpp"
|
||||
#include "precomp.hpp"
|
||||
|
||||
namespace Chattr {
|
||||
|
||||
void TCPSocket::init(int domain) {
|
||||
sock_ = ::socket(domain, SOCK_STREAM, 0);
|
||||
if (sock_ == INVALID_SOCKET)
|
||||
log::critical("socket()");
|
||||
|
||||
valid_ = true;
|
||||
}
|
||||
|
||||
void TCPSocket::listen(int __n) {
|
||||
int retVal = ::listen(sock_, __n);
|
||||
if (retVal == INVALID_SOCKET)
|
||||
log::critical("listen()");
|
||||
}
|
||||
|
||||
void TCPSocket::accept(TCPSocket& newSock, Address& __addr) {
|
||||
newSock.move(::accept(sock_, &__addr.sockaddr, &__addr.length));
|
||||
if (newSock == INVALID_SOCKET)
|
||||
log::critical("accept()");
|
||||
}
|
||||
}
|
||||
8
impl/Socket/config.json
Normal file
8
impl/Socket/config.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"IP Version" : 6,
|
||||
"Listen Port" : 9010,
|
||||
"LogLevel" : 1,
|
||||
"LogfileCount" : 5,
|
||||
"LogfileName" : "Chattering.log",
|
||||
"LogfileSize" : 4294967295
|
||||
}
|
||||
Reference in New Issue
Block a user