tcp/udp 추가, todo:iocp openssl 지원하도록 업데이트하기
This commit is contained in:
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