tcp/udp 추가, todo:iocp openssl 지원하도록 업데이트하기
This commit is contained in:
42
include/precomp.h
Normal file
42
include/precomp.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <glm/glm.hpp>
|
||||
#include <gsl/gsl>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <chrono>
|
||||
#include <limits>
|
||||
|
||||
#include "spdlog/spdlog.h"
|
||||
#include "utilities.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#define NOMINMAX
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <ws2bth.h>
|
||||
#include <ws2def.h>
|
||||
#include <windows.h>
|
||||
#define in_addr_t ULONG
|
||||
#elif __linux__
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/eventfd.h>
|
||||
#define SOCKET int
|
||||
#define INVALID_SOCKET -1
|
||||
#define SOCKET_ERROR -1
|
||||
#else
|
||||
#error "이 플랫폼은 지원되지 않습니다."
|
||||
#endif
|
||||
|
||||
#define MAX_BUFFERED_FRAMES (2)
|
||||
22
include/session/session.h
Normal file
22
include/session/session.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ssl.h>
|
||||
|
||||
#include "socket/iocp.h"
|
||||
#include "utils/thread_pool.h"
|
||||
|
||||
namespace happytanuki {
|
||||
|
||||
enum class SessionProtocol { UDP, TCP, TLS, QUIC };
|
||||
|
||||
class Session {
|
||||
public:
|
||||
Session(utils::ThreadPool* tp, SessionProtocol proto);
|
||||
|
||||
private:
|
||||
Socket::IOCP iocp_;
|
||||
utils::ThreadPool* tp_;
|
||||
};
|
||||
} // namespace happytanuki
|
||||
27
include/socket/address.h
Normal file
27
include/socket/address.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
namespace Socket {
|
||||
|
||||
struct Address {
|
||||
Address();
|
||||
Address(int type, gsl::czstring presentationAddr, std::uint16_t port);
|
||||
|
||||
void zeroFill();
|
||||
void set(int type, gsl::czstring presentationAddr, std::uint16_t port);
|
||||
void set(int type, in_addr_t addr, std::uint16_t port);
|
||||
void set(int type, in_addr addr, std::uint16_t port);
|
||||
void set(int type, in6_addr addr, std::uint16_t port);
|
||||
void setType(int type);
|
||||
|
||||
operator std::string();
|
||||
std::uint16_t getPort();
|
||||
|
||||
union {
|
||||
struct sockaddr addr;
|
||||
struct sockaddr_in addr_in;
|
||||
struct sockaddr_in6 addr_in6;
|
||||
};
|
||||
socklen_t length;
|
||||
};
|
||||
|
||||
} // namespace Chattr
|
||||
376
include/socket/iocp.h
Normal file
376
include/socket/iocp.h
Normal file
@@ -0,0 +1,376 @@
|
||||
#pragma once
|
||||
#include <functional>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
|
||||
#include "socket/tcp_socket.h"
|
||||
#include "socket/wsa_manager.h"
|
||||
#include "utils/thread_pool.h"
|
||||
|
||||
#ifdef __linux__
|
||||
|
||||
typedef struct _OVERLAPPED {
|
||||
char dummy;
|
||||
} OVERLAPPED;
|
||||
|
||||
typedef struct __WSABUF {
|
||||
std::uint32_t len;
|
||||
char* buf;
|
||||
} WSABUF;
|
||||
|
||||
#endif
|
||||
|
||||
namespace Socket {
|
||||
|
||||
class IOCP;
|
||||
|
||||
enum class IOCPEVENT { QUIT, READ, WRITE };
|
||||
|
||||
struct IOCPPASSINDATA {
|
||||
OVERLAPPED overlapped;
|
||||
IOCPEVENT event;
|
||||
std::shared_ptr<UDPSocket> socket;
|
||||
std::uint32_t recvbytes;
|
||||
std::uint32_t sendbytes;
|
||||
std::uint32_t transferredbytes;
|
||||
WSABUF wsabuf;
|
||||
std::uint32_t bufsize;
|
||||
IOCP* IOCPInstance;
|
||||
#ifdef __linux__
|
||||
std::shared_ptr<std::queue<IOCPPASSINDATA*>> sendQueue;
|
||||
#endif
|
||||
|
||||
IOCPPASSINDATA(std::uint32_t bufsize) {
|
||||
std::memset(&overlapped, 0, sizeof(overlapped));
|
||||
event = IOCPEVENT::QUIT;
|
||||
socket = nullptr;
|
||||
recvbytes = 0;
|
||||
sendbytes = 0;
|
||||
transferredbytes = 0;
|
||||
this->bufsize = bufsize;
|
||||
IOCPInstance = nullptr;
|
||||
|
||||
wsabuf.buf = new char[bufsize];
|
||||
}
|
||||
IOCPPASSINDATA(const IOCPPASSINDATA& other)
|
||||
: event(other.event),
|
||||
socket(other.socket),
|
||||
transferredbytes(other.transferredbytes),
|
||||
bufsize(other.bufsize),
|
||||
IOCPInstance(other.IOCPInstance)
|
||||
#ifdef __linux__
|
||||
,
|
||||
sendQueue(other.sendQueue)
|
||||
#endif
|
||||
{
|
||||
std::memset(&overlapped, 0, sizeof(overlapped));
|
||||
recvbytes = 0;
|
||||
sendbytes = 0;
|
||||
wsabuf.buf = new char[bufsize];
|
||||
std::memcpy(wsabuf.buf, other.wsabuf.buf, other.wsabuf.len);
|
||||
}
|
||||
|
||||
~IOCPPASSINDATA() {
|
||||
if (wsabuf.buf != nullptr) delete wsabuf.buf;
|
||||
}
|
||||
|
||||
IOCPPASSINDATA& operator=(const IOCPPASSINDATA& other) {
|
||||
if (this != &other) {
|
||||
std::memset(&overlapped, 0, sizeof(overlapped));
|
||||
event = other.event;
|
||||
socket = other.socket;
|
||||
recvbytes = 0;
|
||||
sendbytes = 0;
|
||||
transferredbytes = other.transferredbytes;
|
||||
bufsize = other.bufsize;
|
||||
IOCPInstance = other.IOCPInstance;
|
||||
#ifdef __linux__
|
||||
sendQueue = other.sendQueue;
|
||||
#endif
|
||||
wsabuf.buf = new char[bufsize];
|
||||
std::memcpy(wsabuf.buf, other.wsabuf.buf, other.wsabuf.len);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
class IOCP {
|
||||
public:
|
||||
#ifdef _WIN32
|
||||
static void iocpWather(
|
||||
utils::ThreadPool* threadPool, HANDLE completionPort_,
|
||||
std::function<void(utils::ThreadPool*, IOCPPASSINDATA*)> callback) {
|
||||
IOCPPASSINDATA* data;
|
||||
SOCKET sock;
|
||||
DWORD cbTransfrred;
|
||||
int retVal = GetQueuedCompletionStatus(completionPort_, &cbTransfrred,
|
||||
(PULONG_PTR)&sock,
|
||||
(LPOVERLAPPED*)&data, INFINITE);
|
||||
if (retVal == 0 || cbTransfrred == 0) {
|
||||
data->event = IOCPEVENT::QUIT;
|
||||
spdlog::debug("Disconnected. [{}]",
|
||||
(std::string)(data->socket->remoteAddr));
|
||||
} else {
|
||||
data->transferredbytes = cbTransfrred;
|
||||
}
|
||||
threadPool->enqueueJob(callback, data);
|
||||
threadPool->enqueueJob(iocpWather, completionPort_, callback);
|
||||
};
|
||||
#elif __linux__
|
||||
static void socketReader(
|
||||
ThreadPool* threadPool, epoll_event event, int epollfd,
|
||||
std::function<void(ThreadPool*, IOCPPASSINDATA*)> callback) {
|
||||
pthread_t tid = pthread_self();
|
||||
|
||||
if (event.data.ptr == nullptr) {
|
||||
spdlog::error("invalid call on {}", tid);
|
||||
return;
|
||||
}
|
||||
|
||||
IOCPPASSINDATA* rootIocpData = (IOCPPASSINDATA*)event.data.ptr;
|
||||
|
||||
std::lock_guard<std::mutex> lock(rootIocpData->socket->readMutex);
|
||||
while (true) {
|
||||
char peekBuffer[1];
|
||||
int rc = rootIocpData->socket->recv(peekBuffer, 1, MSG_PEEK);
|
||||
if (rc > 0)
|
||||
;
|
||||
else if (rc == 0) {
|
||||
rootIocpData->event = IOCPEVENT::QUIT;
|
||||
spdlog::debug("Disconnected. [{}]",
|
||||
(std::string)(rootIocpData->socket->remoteAddr));
|
||||
::epoll_ctl(epollfd, EPOLL_CTL_DEL, rootIocpData->socket->sock, NULL);
|
||||
threadPool->enqueueJob(callback, rootIocpData);
|
||||
// delete rootIocpData;
|
||||
return;
|
||||
} else {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
spdlog::trace("No data to read on {}", tid);
|
||||
return;
|
||||
} else {
|
||||
rootIocpData->event = IOCPEVENT::QUIT;
|
||||
spdlog::debug("Disconnected. [{}]",
|
||||
(std::string)(rootIocpData->socket->remoteAddr));
|
||||
::epoll_ctl(epollfd, EPOLL_CTL_DEL, rootIocpData->socket->sock, NULL);
|
||||
threadPool->enqueueJob(callback, rootIocpData);
|
||||
// delete rootIocpData;
|
||||
return;
|
||||
}
|
||||
}
|
||||
Chattr::IOCPPASSINDATA* ptr = new Chattr::IOCPPASSINDATA(*rootIocpData);
|
||||
ptr->wsabuf.len = 1500;
|
||||
|
||||
int redSize = 0;
|
||||
int headerSize = 8;
|
||||
int totalRedSize = 0;
|
||||
|
||||
while (totalRedSize < headerSize) {
|
||||
redSize = ptr->socket->recv(ptr->buf + totalRedSize,
|
||||
headerSize - totalRedSize, 0);
|
||||
|
||||
if (redSize == SOCKET_ERROR) {
|
||||
ptr->event = IOCPEVENT::QUIT;
|
||||
spdlog::debug("Disconnected. [{}]",
|
||||
(std::string)(ptr->socket->remoteAddr));
|
||||
::epoll_ctl(epollfd, EPOLL_CTL_DEL, ptr->socket->sock, NULL);
|
||||
threadPool->enqueueJob(callback, ptr);
|
||||
// delete ptr;
|
||||
return;
|
||||
} else if (redSize == 0) {
|
||||
ptr->event = IOCPEVENT::QUIT;
|
||||
spdlog::debug("Disconnected. [{}]",
|
||||
(std::string)(ptr->socket->remoteAddr));
|
||||
::epoll_ctl(epollfd, EPOLL_CTL_DEL, ptr->socket->sock, NULL);
|
||||
threadPool->enqueueJob(callback, ptr);
|
||||
// delete ptr;
|
||||
return;
|
||||
}
|
||||
totalRedSize += redSize;
|
||||
}
|
||||
|
||||
Packet packet;
|
||||
::memcpy(packet.serialized, ptr->buf, headerSize);
|
||||
|
||||
redSize = 0;
|
||||
int dataLength = ntohs(packet.__data.packetLength);
|
||||
|
||||
while (totalRedSize < dataLength + headerSize) {
|
||||
redSize = ptr->socket->recv(ptr->buf + totalRedSize,
|
||||
dataLength + headerSize - totalRedSize, 0);
|
||||
|
||||
if (redSize == SOCKET_ERROR) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
spdlog::trace("No data to read on {}", tid);
|
||||
return;
|
||||
}
|
||||
ptr->event = IOCPEVENT::QUIT;
|
||||
spdlog::debug("Disconnected. [{}]",
|
||||
(std::string)(ptr->socket->remoteAddr));
|
||||
::epoll_ctl(epollfd, EPOLL_CTL_DEL, ptr->socket->sock, NULL);
|
||||
threadPool->enqueueJob(callback, ptr);
|
||||
// delete ptr;
|
||||
return;
|
||||
} else if (redSize == 0) {
|
||||
ptr->event = IOCPEVENT::QUIT;
|
||||
spdlog::debug("Disconnected. [{}]",
|
||||
(std::string)(ptr->socket->remoteAddr));
|
||||
::epoll_ctl(epollfd, EPOLL_CTL_DEL, ptr->socket->sock, NULL);
|
||||
threadPool->enqueueJob(callback, ptr);
|
||||
// delete ptr;
|
||||
return;
|
||||
}
|
||||
totalRedSize += redSize;
|
||||
}
|
||||
ptr->transferredbytes = totalRedSize;
|
||||
threadPool->enqueueJob(callback, ptr);
|
||||
}
|
||||
};
|
||||
static void socketWriter(
|
||||
ThreadPool* threadPool, epoll_event event, int epollfd,
|
||||
std::function<void(ThreadPool*, IOCPPASSINDATA*)> callback) {
|
||||
pthread_t tid = pthread_self();
|
||||
|
||||
if (event.data.ptr == nullptr) {
|
||||
spdlog::error("invalid call on {}", tid);
|
||||
return;
|
||||
}
|
||||
|
||||
IOCPPASSINDATA* rootIocpData = (IOCPPASSINDATA*)event.data.ptr;
|
||||
|
||||
std::lock_guard<std::mutex> lock(rootIocpData->socket->writeMutex);
|
||||
while (!rootIocpData->sendQueue->empty()) {
|
||||
IOCPPASSINDATA* data = rootIocpData->sendQueue->front();
|
||||
rootIocpData->sendQueue->pop();
|
||||
|
||||
if (data == nullptr) {
|
||||
spdlog::error("invalid call on {}", tid);
|
||||
break;
|
||||
}
|
||||
|
||||
int packetSize = data->wsabuf.len;
|
||||
int totalSentSize = 0;
|
||||
int sentSize = 0;
|
||||
|
||||
spdlog::trace("Sending to: [{}]", (std::string)data->socket->remoteAddr);
|
||||
|
||||
while (totalSentSize < packetSize) {
|
||||
sentSize = data->socket->send(data->buf + totalSentSize,
|
||||
packetSize - totalSentSize, 0);
|
||||
|
||||
if (sentSize == SOCKET_ERROR) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
spdlog::warn("buffer full");
|
||||
continue;
|
||||
}
|
||||
data->event = IOCPEVENT::QUIT;
|
||||
spdlog::debug("Disconnected. [{}]",
|
||||
(std::string)(data->socket->remoteAddr));
|
||||
::epoll_ctl(epollfd, EPOLL_CTL_DEL, data->socket->sock, NULL);
|
||||
threadPool->enqueueJob(callback, data);
|
||||
// delete data;
|
||||
return;
|
||||
}
|
||||
totalSentSize += sentSize;
|
||||
}
|
||||
data->transferredbytes = totalSentSize;
|
||||
threadPool->enqueueJob(callback, data);
|
||||
}
|
||||
};
|
||||
static void iocpWatcher(
|
||||
ThreadPool* threadPool, int epollfd, int epollDetroyerFd,
|
||||
std::function<void(ThreadPool*, IOCPPASSINDATA*)> callback) {
|
||||
struct epoll_event events[FD_SETSIZE];
|
||||
pthread_t tid = pthread_self();
|
||||
|
||||
int nready = ::epoll_wait(epollfd, events, FD_SETSIZE, -1);
|
||||
|
||||
for (int i = 0; i < nready; i++) {
|
||||
struct epoll_event current_event = events[i];
|
||||
|
||||
if (current_event.events & EPOLLIN) {
|
||||
if (current_event.data.fd == epollDetroyerFd) {
|
||||
return;
|
||||
}
|
||||
std::function<void(ThreadPool*, IOCPPASSINDATA*)> task(callback);
|
||||
threadPool->enqueueJob(socketReader, current_event, epollfd, task);
|
||||
} else if (current_event.events & EPOLLOUT) {
|
||||
std::function<void(ThreadPool*, IOCPPASSINDATA*)> task(callback);
|
||||
threadPool->enqueueJob(socketWriter, current_event, epollfd, task);
|
||||
}
|
||||
if (--nready <= 0) break;
|
||||
}
|
||||
threadPool->enqueueJob(iocpWatcher, epollfd, epollDetroyerFd, callback);
|
||||
};
|
||||
#endif
|
||||
|
||||
IOCP();
|
||||
~IOCP();
|
||||
|
||||
template <typename _Callable>
|
||||
void init(utils::ThreadPool* __IOCPThread, _Callable&& callback) {
|
||||
IOCPThread_ = __IOCPThread;
|
||||
#ifdef _WIN32
|
||||
completionPort_ =
|
||||
::CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
|
||||
if (completionPort_ == NULL) {
|
||||
spdlog::critical("CreateIoCompletionPort()");
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
#elif __linux__
|
||||
epollfd_ = ::epoll_create(1);
|
||||
epollDetroyerFd = ::eventfd(0, EFD_NONBLOCK);
|
||||
struct epoll_event ev;
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.fd = epollDetroyerFd;
|
||||
::epoll_ctl(epollfd_, EPOLL_CTL_ADD, epollDetroyerFd, &ev);
|
||||
#endif
|
||||
auto boundFunc = [callback = std::move(callback)](
|
||||
utils::ThreadPool* __IOCPThread,
|
||||
IOCPPASSINDATA* data) mutable {
|
||||
callback(__IOCPThread, data);
|
||||
};
|
||||
|
||||
#ifdef _WIN32
|
||||
int tCount = __IOCPThread->threadCount;
|
||||
|
||||
spdlog::info("Resizing threadpool size to: {}", tCount * 2);
|
||||
|
||||
__IOCPThread->respawnWorker(tCount * 2);
|
||||
|
||||
for (int i = 0; i < tCount; i++) {
|
||||
std::function<void(utils::ThreadPool*, IOCPPASSINDATA*)> task(boundFunc);
|
||||
__IOCPThread->enqueueJob(iocpWather, completionPort_, task);
|
||||
}
|
||||
#elif __linux__
|
||||
__IOCPThread->respawnWorker(__IOCPThread->threadCount + 1);
|
||||
spdlog::info("Spawning 1 Epoll Waiter...");
|
||||
__IOCPThread->enqueueJob(iocpWatcher, epollfd_, epollDetroyerFd, boundFunc);
|
||||
#endif
|
||||
}
|
||||
|
||||
void destruct();
|
||||
|
||||
void registerSocket(IOCPPASSINDATA* data);
|
||||
|
||||
int recv(IOCPPASSINDATA* data, int bufferCount);
|
||||
int send(IOCPPASSINDATA* data, int bufferCount, int __flags);
|
||||
|
||||
#ifdef __linux__
|
||||
int epollDetroyerFd = -1;
|
||||
#endif
|
||||
|
||||
private:
|
||||
struct WSAManager* wsaManager = WSAManager::GetInstance();
|
||||
utils::ThreadPool* IOCPThread_;
|
||||
|
||||
#ifdef _WIN32
|
||||
HANDLE completionPort_ = INVALID_HANDLE_VALUE;
|
||||
#elif __linux__
|
||||
int epollfd_ = -1;
|
||||
std::unordered_map<std::shared_ptr<TCPSocket>, std::mutex> writeMutex;
|
||||
std::unordered_map<std::shared_ptr<TCPSocket>, std::queue<IOCPPASSINDATA*>>
|
||||
writeBuffer;
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace Socket
|
||||
18
include/socket/tcp_socket.h
Normal file
18
include/socket/tcp_socket.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#include "udp_socket.h"
|
||||
|
||||
namespace Socket {
|
||||
|
||||
class TCPSocket : public UDPSocket {
|
||||
public:
|
||||
using UDPSocket::UDPSocket;
|
||||
using UDPSocket::init;
|
||||
int init(int domain);
|
||||
int listen(int __n);
|
||||
void accept(TCPSocket& newSock, Address& addr);
|
||||
int connect(Socket::Address& serveraddr);
|
||||
int recv(void *__restrict __buf, size_t __n, int __flags);
|
||||
int send(const void *__buf, size_t __n, int __flags);
|
||||
};
|
||||
|
||||
}
|
||||
45
include/socket/udp_socket.h
Normal file
45
include/socket/udp_socket.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
#include "address.h"
|
||||
|
||||
namespace Socket {
|
||||
|
||||
struct Address;
|
||||
|
||||
class UDPSocket {
|
||||
public:
|
||||
UDPSocket() = default;
|
||||
UDPSocket(int domain, int type, int protocol);
|
||||
~UDPSocket();
|
||||
|
||||
int init(int domain, int type, int protocol);
|
||||
void destruct();
|
||||
|
||||
operator SOCKET();
|
||||
void set(const SOCKET __sock, int __domain);
|
||||
int setsockopt(int level, int optname, const char* optval, int optlen);
|
||||
|
||||
int bind(Address __addr);
|
||||
|
||||
int recvfrom(void* __restrict __buf, size_t __n, int __flags,
|
||||
struct Address& __addr);
|
||||
int sendto(const void* __buf, size_t __n, int __flags, struct Address __addr);
|
||||
|
||||
UDPSocket(const UDPSocket&);
|
||||
UDPSocket(UDPSocket&&) noexcept;
|
||||
UDPSocket& operator=(const UDPSocket&);
|
||||
UDPSocket& operator=(UDPSocket&&) noexcept;
|
||||
|
||||
struct Address bindAddr = {};
|
||||
struct Address remoteAddr = {};
|
||||
|
||||
int domain = 0;
|
||||
SOCKET sock = INVALID_SOCKET;
|
||||
|
||||
std::mutex readMutex;
|
||||
std::mutex writeMutex;
|
||||
|
||||
protected:
|
||||
bool valid_ = false;
|
||||
};
|
||||
|
||||
} // namespace Socket
|
||||
20
include/socket/wsa_manager.h
Normal file
20
include/socket/wsa_manager.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
namespace Socket {
|
||||
|
||||
struct WSAManager {
|
||||
public:
|
||||
static WSAManager* GetInstance() {
|
||||
static WSAManager instance;
|
||||
return &instance;
|
||||
}
|
||||
|
||||
WSAManager(const WSAManager&) = delete;
|
||||
WSAManager& operator=(const WSAManager&) = delete;
|
||||
|
||||
private:
|
||||
WSAManager();
|
||||
~WSAManager();
|
||||
};
|
||||
|
||||
}
|
||||
18
include/transport/transport.h
Normal file
18
include/transport/transport.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "utils/thread_pool.h"
|
||||
|
||||
namespace happytanuki {
|
||||
|
||||
class Transport {
|
||||
public:
|
||||
Transport(utils::ThreadPool* tp);
|
||||
|
||||
void Send();
|
||||
void Recv();
|
||||
|
||||
private:
|
||||
utils::ThreadPool* tp_;
|
||||
};
|
||||
|
||||
} // namespace happytanuki
|
||||
9
include/utils/log.h
Normal file
9
include/utils/log.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
namespace utils {
|
||||
|
||||
void setDefaultLogger(spdlog::level::level_enum logLevel,
|
||||
gsl::czstring logFileName, std::uint32_t logFileSize,
|
||||
std::uint32_t logFileCount);
|
||||
|
||||
} // namespace utils
|
||||
32
include/utils/snowflake.h
Normal file
32
include/utils/snowflake.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
|
||||
namespace utils {
|
||||
|
||||
struct Snowflake {
|
||||
union {
|
||||
struct {
|
||||
std::uint64_t timestamp : 42;
|
||||
std::uint64_t instance : 10;
|
||||
std::uint64_t sequence : 12;
|
||||
};
|
||||
std::uint64_t snowflake;
|
||||
};
|
||||
|
||||
bool operator==(const Snowflake& other) const {
|
||||
return snowflake == other.snowflake;
|
||||
}
|
||||
};
|
||||
|
||||
Snowflake GenerateID();
|
||||
|
||||
} // namespace Chattr
|
||||
|
||||
namespace std {
|
||||
template <>
|
||||
struct hash<utils::Snowflake> {
|
||||
std::size_t operator()(const utils::Snowflake& k) const {
|
||||
return std::hash<std::uint64_t>{}(k.snowflake);
|
||||
}
|
||||
};
|
||||
} // namespace std
|
||||
78
include/utils/thread_pool.h
Normal file
78
include/utils/thread_pool.h
Normal file
@@ -0,0 +1,78 @@
|
||||
#pragma once
|
||||
#include <condition_variable>
|
||||
#include <functional>
|
||||
#include <future>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
|
||||
namespace utils {
|
||||
|
||||
class ThreadPool {
|
||||
public:
|
||||
ThreadPool();
|
||||
ThreadPool(std::uint32_t numThreads);
|
||||
~ThreadPool();
|
||||
|
||||
void init(std::uint32_t numThreads);
|
||||
void terminate();
|
||||
|
||||
void respawnWorker(std::uint32_t numThreads);
|
||||
|
||||
template <typename _Callable, typename... _Args>
|
||||
requires(
|
||||
!std::is_void_v<std::invoke_result_t<_Callable, ThreadPool*, _Args...>>)
|
||||
int enqueueJob(_Callable&& __job,
|
||||
std::invoke_result_t<_Callable, _Args...>& retVal,
|
||||
_Args&&... __args) {
|
||||
if (terminate_) {
|
||||
spdlog::error("Cannot run jobs on threads that terminating...");
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(jobQueueMutex);
|
||||
auto boundFunc = [this, &retVal, __job,
|
||||
... __args = std::move(__args)]() mutable {
|
||||
retVal = __job(this, std::move(__args)...);
|
||||
};
|
||||
auto task = std::packaged_task<void()>(std::move(boundFunc));
|
||||
jobs_.push(std::move(task));
|
||||
jobQueueCV_.notify_one();
|
||||
|
||||
return 0;
|
||||
}
|
||||
template <typename _Callable, typename... _Args>
|
||||
requires std::is_void_v<
|
||||
std::invoke_result_t<_Callable, ThreadPool*, _Args...>>
|
||||
int enqueueJob(_Callable&& __job, _Args&&... __args) {
|
||||
if (terminate_) {
|
||||
spdlog::error("Cannot run jobs on threads that terminating...");
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(jobQueueMutex);
|
||||
auto boundFunc = [this, __job, ... __args = std::move(__args)]() mutable {
|
||||
__job(this, std::move(__args)...);
|
||||
};
|
||||
auto task = std::packaged_task<void()>(std::move(boundFunc));
|
||||
jobs_.push(std::move(task));
|
||||
jobQueueCV_.notify_one();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int threadCount = 0;
|
||||
|
||||
private:
|
||||
void* Worker();
|
||||
|
||||
std::condition_variable jobQueueCV_;
|
||||
std::mutex jobQueueMutex;
|
||||
std::queue<std::packaged_task<void()>> jobs_;
|
||||
std::vector<std::thread> workers_;
|
||||
bool terminate_ = false;
|
||||
};
|
||||
|
||||
} // namespace Chattr
|
||||
Reference in New Issue
Block a user