Rebuild directory structure
This commit is contained in:
+10
-23
@@ -7,39 +7,26 @@ set(CMAKE_CXX_STANDARD 17)
|
|||||||
set(CMAKE_C_STANDARD_REQUIRED True)
|
set(CMAKE_C_STANDARD_REQUIRED True)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||||
|
|
||||||
add_library(
|
|
||||||
wrapper
|
|
||||||
./socket.cpp
|
|
||||||
)
|
|
||||||
|
|
||||||
target_include_directories(
|
|
||||||
wrapper
|
|
||||||
PUBLIC .
|
|
||||||
)
|
|
||||||
|
|
||||||
add_executable(
|
add_executable(
|
||||||
test_server
|
test_server
|
||||||
./main.cpp
|
./main.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
add_subdirectory(./socket)
|
||||||
|
add_subdirectory(./test)
|
||||||
|
|
||||||
target_include_directories(
|
target_include_directories(
|
||||||
test_server
|
test_server
|
||||||
PUBLIC .
|
PUBLIC ./pool
|
||||||
|
PUBLIC ./socket
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(
|
||||||
|
test_pool
|
||||||
|
PUBLIC ./pool
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(
|
target_link_libraries(
|
||||||
test_server
|
test_server
|
||||||
PUBLIC wrapper
|
PUBLIC wrapper
|
||||||
)
|
)
|
||||||
|
|
||||||
# ThreadPool + LockFreeQueue test (header-only, no link deps)
|
|
||||||
add_executable(
|
|
||||||
test_pool
|
|
||||||
./test_pool.cpp
|
|
||||||
./pool.cpp
|
|
||||||
)
|
|
||||||
|
|
||||||
target_include_directories(
|
|
||||||
test_pool
|
|
||||||
PUBLIC .
|
|
||||||
)
|
|
||||||
|
|||||||
@@ -1,22 +1,20 @@
|
|||||||
|
#include "pool.hpp"
|
||||||
#include "socket.hpp"
|
#include "socket.hpp"
|
||||||
|
#include "address.hpp"
|
||||||
|
|
||||||
int main(int argc, char **argv) try {
|
int main(int argc, char **argv) try {
|
||||||
int iResult = 0;
|
ThreadPool pool;
|
||||||
|
|
||||||
WSA wsa;
|
WSA wsa;
|
||||||
|
|
||||||
|
auto server = pool.submit([]() -> int {
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
|
||||||
AddrInfoPtr ptr;
|
AddrInfoPtr ptr;
|
||||||
AddrInfo hints;
|
AddrInfoPtr result = AddrInfo::get_local_addrinfo("17900");
|
||||||
|
|
||||||
hints->ai_family = AF_INET;
|
|
||||||
hints->ai_socktype = SOCK_STREAM;
|
|
||||||
hints->ai_protocol = IPPROTO_TCP;
|
|
||||||
hints->ai_flags = AI_PASSIVE;
|
|
||||||
|
|
||||||
AddrInfoPtr result = hints.get_addrinfo(NULL, "8088");
|
|
||||||
|
|
||||||
Socket listen_socket;
|
Socket listen_socket;
|
||||||
|
|
||||||
listen_socket.from_addrinfo(*ptr, Socket::BIND);
|
listen_socket.from_addrinfo(*ptr, Socket::BIND);
|
||||||
|
|
||||||
Socket client_socket = listen_socket.accept_client();
|
Socket client_socket = listen_socket.accept_client();
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
// pool.cpp — ThreadPool & LockFreeQueue implementation file
|
|
||||||
//
|
|
||||||
// Note: All template implementations are in pool.hpp (header-only).
|
|
||||||
// This file exists for future non-template extensions or
|
|
||||||
// explicit template instantiations if needed.
|
|
||||||
|
|
||||||
#include "pool.hpp"
|
|
||||||
-130
@@ -1,130 +0,0 @@
|
|||||||
#include "socket.hpp"
|
|
||||||
|
|
||||||
#pragma comment(lib, "ws2_32.lib")
|
|
||||||
|
|
||||||
WSA::WSA(WORD version) {
|
|
||||||
int result = WSAStartup(version, &wsa);
|
|
||||||
if (result) throw result;
|
|
||||||
}
|
|
||||||
WSA::~WSA() noexcept {
|
|
||||||
WSACleanup();
|
|
||||||
}
|
|
||||||
|
|
||||||
AddrInfoPtr::AddrInfoPtr() noexcept:
|
|
||||||
info(nullptr) {}
|
|
||||||
AddrInfoPtr::~AddrInfoPtr() noexcept {
|
|
||||||
freeaddrinfo(info);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
addrinfo** AddrInfoPtr::operator&() noexcept {
|
|
||||||
return &info;
|
|
||||||
}
|
|
||||||
addrinfo& AddrInfoPtr::operator*() noexcept {
|
|
||||||
return *info;
|
|
||||||
}
|
|
||||||
addrinfo* AddrInfoPtr::operator->() noexcept {
|
|
||||||
return info;
|
|
||||||
}
|
|
||||||
|
|
||||||
const addrinfo& AddrInfoPtr::operator*() const noexcept {
|
|
||||||
return *info;
|
|
||||||
}
|
|
||||||
|
|
||||||
AddrInfoPtr::operator addrinfo *() noexcept {
|
|
||||||
return info;
|
|
||||||
}
|
|
||||||
|
|
||||||
AddrInfo::AddrInfo() noexcept {
|
|
||||||
ZeroMemory(&info, sizeof(info));
|
|
||||||
}
|
|
||||||
|
|
||||||
addrinfo* AddrInfo::operator->() noexcept {
|
|
||||||
return &info;
|
|
||||||
}
|
|
||||||
addrinfo* AddrInfo::operator&() noexcept {
|
|
||||||
return &info;
|
|
||||||
}
|
|
||||||
|
|
||||||
AddrInfo::operator const addrinfo&() const noexcept {
|
|
||||||
return info;
|
|
||||||
}
|
|
||||||
AddrInfo::operator addrinfo&() noexcept {
|
|
||||||
return info;
|
|
||||||
}
|
|
||||||
|
|
||||||
AddrInfoPtr AddrInfo::get_addrinfo(PCSTR node, PCSTR service) {
|
|
||||||
AddrInfoPtr ptr;
|
|
||||||
int result = getaddrinfo(node, service, &info, &ptr);
|
|
||||||
if (result == SOCKET_ERROR) throw result;
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
Socket::Socket(SOCKET init) noexcept:
|
|
||||||
s(init) {}
|
|
||||||
Socket::~Socket() noexcept {
|
|
||||||
closesocket(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
Socket::operator SOCKET() const noexcept {
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
Socket::operator bool() const noexcept {
|
|
||||||
return is_valid();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Socket::is_valid() const noexcept {
|
|
||||||
return s != INVALID_SOCKET;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Socket Socket::accept_client() const noexcept {
|
|
||||||
return Socket(accept(s, NULL, NULL));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Socket::stop_send() const {
|
|
||||||
int result = shutdown(s, SD_SEND);
|
|
||||||
if (result == SOCKET_ERROR) throw result;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Socket::send_data(const Buffer &data) const {
|
|
||||||
return send(s, data.get(), data.len(), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int Socket::recv_data(Buffer &data) const {
|
|
||||||
return recv(s, data.get(), data.len(), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
Buffer::Buffer(int len):
|
|
||||||
size(len), data(new char[len]) {}
|
|
||||||
|
|
||||||
char *Buffer::get() {
|
|
||||||
return data.get();
|
|
||||||
}
|
|
||||||
const char *Buffer::get() const {
|
|
||||||
return data.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
int Buffer::len() const {
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
-83
@@ -1,83 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#include <WinSock2.h>
|
|
||||||
#include <ws2tcpip.h>
|
|
||||||
#include <iphlpapi.h>
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
struct WSA {
|
|
||||||
WSADATA wsa;
|
|
||||||
|
|
||||||
explicit WSA(WORD version = MAKEWORD(2, 2));
|
|
||||||
~WSA() noexcept;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct AddrInfoPtr {
|
|
||||||
struct addrinfo *info;
|
|
||||||
|
|
||||||
AddrInfoPtr() noexcept;
|
|
||||||
~AddrInfoPtr() noexcept;
|
|
||||||
|
|
||||||
addrinfo** operator&() noexcept;
|
|
||||||
addrinfo& operator*() noexcept;
|
|
||||||
addrinfo* operator->() noexcept;
|
|
||||||
const addrinfo& operator*() const noexcept;
|
|
||||||
|
|
||||||
operator addrinfo*() noexcept;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct AddrInfo {
|
|
||||||
struct addrinfo info;
|
|
||||||
|
|
||||||
AddrInfo() noexcept;
|
|
||||||
~AddrInfo() noexcept = default;
|
|
||||||
|
|
||||||
addrinfo* operator->() noexcept;
|
|
||||||
addrinfo* operator&() noexcept;
|
|
||||||
|
|
||||||
operator const addrinfo&() const noexcept;
|
|
||||||
operator addrinfo&() noexcept;
|
|
||||||
|
|
||||||
AddrInfoPtr get_addrinfo(PCSTR node, PCSTR service);
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Socket {
|
|
||||||
enum AddrAction {
|
|
||||||
NO_ACTION,
|
|
||||||
CONNECT,
|
|
||||||
BIND,
|
|
||||||
};
|
|
||||||
|
|
||||||
SOCKET s;
|
|
||||||
struct sockaddr addr;
|
|
||||||
|
|
||||||
explicit Socket(SOCKET init = INVALID_SOCKET) noexcept;
|
|
||||||
~Socket() noexcept;
|
|
||||||
|
|
||||||
operator SOCKET() const noexcept;
|
|
||||||
|
|
||||||
explicit operator bool() const noexcept;
|
|
||||||
bool is_valid() const noexcept;
|
|
||||||
|
|
||||||
void from_addrinfo(const struct addrinfo &info, AddrAction action = NO_ACTION);
|
|
||||||
|
|
||||||
Socket accept_client() const noexcept;
|
|
||||||
|
|
||||||
void stop_send() const;
|
|
||||||
|
|
||||||
int send_data(const Buffer &data) const;
|
|
||||||
int recv_data(Buffer &data) const;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Buffer {
|
|
||||||
std::unique_ptr<char[]> data;
|
|
||||||
int size;
|
|
||||||
|
|
||||||
Buffer(int len);
|
|
||||||
|
|
||||||
char* get();
|
|
||||||
const char* get() const;
|
|
||||||
|
|
||||||
int len() const;
|
|
||||||
};
|
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
add_library(
|
||||||
|
wrapper
|
||||||
|
./socket.cpp
|
||||||
|
./address.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(
|
||||||
|
wrapper
|
||||||
|
PUBLIC ws2_32
|
||||||
|
)
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
#include "address.hpp"
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
# ThreadPool + LockFreeQueue test (header-only, no link deps)
|
||||||
|
add_executable(
|
||||||
|
test_pool
|
||||||
|
./pool.cpp
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user