Rebuild directory structure

This commit is contained in:
2026-07-22 22:02:06 +08:00
parent 973b2e60de
commit c5e4d5a3a5
13 changed files with 209 additions and 253 deletions
+10
View File
@@ -0,0 +1,10 @@
add_library(
wrapper
./socket.cpp
./address.cpp
)
target_link_libraries(
wrapper
PUBLIC ws2_32
)
+1
View File
@@ -0,0 +1 @@
#include "address.hpp"
+71
View File
@@ -0,0 +1,71 @@
#include <WinSock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
struct AddrInfoPtr {
struct addrinfo *info;
AddrInfoPtr() noexcept:
info(nullptr) {}
~AddrInfoPtr() noexcept {
freeaddrinfo(info);
}
addrinfo** operator&() noexcept {
return &info;
}
addrinfo& operator*() noexcept {
return *info;
}
addrinfo* operator->() noexcept {
return info;
}
const addrinfo& operator*() const noexcept {
return *info;
}
operator addrinfo*() noexcept {
return info;
}
};
struct AddrInfo {
struct addrinfo info;
static AddrInfoPtr get_local_addrinfo(PCSTR port) {
AddrInfo hints;
hints->ai_family = AF_INET;
hints->ai_socktype = SOCK_STREAM;
hints->ai_protocol = IPPROTO_TCP;
hints->ai_flags = AI_PASSIVE;
return hints.get_addrinfo(NULL, port);
}
AddrInfo() noexcept {
ZeroMemory(&info, sizeof(info));
}
~AddrInfo() noexcept = default;
addrinfo* operator->() noexcept {
return &info;
}
addrinfo* operator&() noexcept {
return &info;
}
operator const addrinfo&() const noexcept {
return info;
}
operator addrinfo&() noexcept {
return info;
}
AddrInfoPtr get_addrinfo(PCSTR node, PCSTR service) {
AddrInfoPtr ptr;
int result = getaddrinfo(node, service, &info, &ptr);
if (result == SOCKET_ERROR) throw result;
return ptr;
}
};
+21
View File
@@ -0,0 +1,21 @@
#include "socket.hpp"
void Socket::from_addrinfo(const addrinfo &info, AddrAction action) {
s = socket(info.ai_family, info.ai_socktype, info.ai_protocol);
if (s == INVALID_SOCKET) throw -1;
int result = SOCKET_ERROR;
switch (action) {
case NO_ACTION:
break;
case CONNECT:
result = connect(s, info.ai_addr, (int)info.ai_addrlen);
if (result == SOCKET_ERROR) throw result;
break;
case BIND:
result = bind(s, info.ai_addr, (int)info.ai_addrlen);
if (result == SOCKET_ERROR) throw result;
result = listen(s, SOMAXCONN);
if (result == SOCKET_ERROR) throw result;
break;
}
}
+83
View File
@@ -0,0 +1,83 @@
#include <stdio.h>
#include <WinSock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <vector>
struct WSA {
WSADATA wsa;
explicit WSA(WORD version = MAKEWORD(2, 2)) {
int result = WSAStartup(version, &wsa);
if (result) throw result;
}
~WSA() noexcept {
WSACleanup();
}
};
struct Buffer {
std::vector<char> data;
explicit Buffer(int len):
data(len, 0) {}
char* get() noexcept {
return data.data();
}
const char* get() const noexcept {
return data.data();
}
int len() const noexcept {
return data.size();
}
};
struct Socket {
enum AddrAction {
NO_ACTION,
CONNECT,
BIND,
};
SOCKET s;
struct sockaddr addr;
explicit Socket(SOCKET init = INVALID_SOCKET) noexcept:
s(init) {}
~Socket() noexcept {
closesocket(s);
}
operator SOCKET() const noexcept {
return s;
}
explicit operator bool() const noexcept {
return is_valid();
}
bool is_valid() const noexcept {
return s != INVALID_SOCKET;
}
void from_addrinfo(const struct addrinfo &info, AddrAction action = NO_ACTION);
Socket accept_client() const noexcept {
return Socket(accept(s, NULL, NULL));
}
void stop_send() const {
int result = shutdown(s, SD_SEND);
if (result == SOCKET_ERROR) throw result;
}
int send_data(const Buffer &data) const noexcept {
return send(s, data.get(), data.len(), 0);
}
int recv_data(Buffer &data) const noexcept {
return recv(s, data.get(), data.len(), 0);
}
};