This commit is contained in:
aozhiwei 2022-12-17 11:05:39 +08:00
parent 02e8212d0b
commit 3fca06a9bc
6 changed files with 0 additions and 361 deletions

View File

@ -1,110 +0,0 @@
#pragma once
#include <algorithm>
namespace a8
{
template <class T>
class CommonRank
{
private:
std::vector<T*> list_;
std::set<T*> set_;
std::function<bool (const T*, const T*)> cmp_func_;
size_t max_num_ = 0;
public:
CommonRank() {}
CommonRank(std::function<bool (const T*, const T*)> cmp_func, size_t max_num)
{
cmp_func_ = cmp_func;
max_num_ = max_num;
}
size_t Size()
{
return list_.size();
}
const std::vector<T*>& GetList()
{
return list_;
}
void Sort()
{
std::sort(list_.begin(), list_.end(), cmp_func_);
}
void Update(T* val)
{
if (Size() < max_num_) {
if (!Exists(val)) {
list_.push_back(val);
set_.insert(val);
Sort();
}
} else {
if (cmp_func_(val, list_[list_.size() - 1])) {
if (!Exists(val)) {
set_.erase(list_[list_.size() - 1]);
list_[list_.size() - 1] = val;
set_.insert(val);
Sort();
}
}
}
}
bool Exists(T* val)
{
return set_.find(val) != set_.end();
}
void Remove(T* val)
{
if (!Exists(val)) {
return;
}
{
auto itr = std::find(list_.begin(), list_.end(), val);
if (itr != list_.end()) {
list_.erase(itr);
}
}
set_.erase(val);
}
};
template <typename T>
a8::CommonRank<T>* ForceCreateCommonRankList(std::map<long long, a8::CommonRank<T>>& rank_list,
long long key,
std::function<bool (const T*, const T*)> cmp_func,
size_t max_num)
{
auto itr = rank_list.find(key);
if (itr != rank_list.end()) {
return &itr->second;
}
rank_list[key] = a8::CommonRank<T>(cmp_func, max_num);
return &rank_list[key];
}
template <typename T>
static a8::CommonRank<T>* ForceCreateCommonRankList(std::map<std::string, a8::CommonRank<T>>& rank_list,
const std::string& key,
std::function<bool (const T*, const T*)> cmp_func,
size_t max_num)
{
auto itr = rank_list.find(key);
if (itr != rank_list.end()) {
return &itr->second;
}
rank_list[key] = a8::CommonRank<T>(cmp_func, max_num);
return &rank_list[key];
}
}

View File

@ -1,13 +0,0 @@
#pragma once
namespace a8
{
class EpollEventHandler
{
public:
virtual void SetEpollFd(int epoll_fd) = 0;
virtual void DoSend() = 0;
virtual void DoRecv() = 0;
virtual void DoError() = 0;
};
}

View File

@ -1,79 +0,0 @@
#include <unistd.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <a8/a8.h>
#include <a8/eventfd.h>
namespace a8
{
EventFD::EventFD()
{
fd_ = ::eventfd(0, 0);
if (fd_ == a8::INVALID_FD) {
abort();
}
}
EventFD::~EventFD()
{
UnInit();
}
void EventFD::Init(void* context)
{
context_ = context;
}
void EventFD::UnInit()
{
if (fd_ != a8::INVALID_FD) {
struct epoll_event ev;
::epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, fd_, &ev);
::close(fd_);
fd_ = a8::INVALID_FD;
}
}
void EventFD::Write(unsigned long long value)
{
::eventfd_write(fd_, value);
}
void EventFD::SetEpollFd(int epoll_fd)
{
struct epoll_event ev;
ev.data.fd = fd_;
ev.events = EPOLLIN | EPOLLET;
ev.data.ptr = this;
int ret = ::epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd_, &ev);
if (ret != 0) {
abort();
}
epoll_fd_ = epoll_fd;
}
void EventFD::DoRecv()
{
eventfd_t value = 0;
::eventfd_read(fd_, &value);
if (OnEvent) {
OnEvent(context_, value);
}
}
void EventFD::DoSend()
{
abort();
}
void EventFD::DoError()
{
abort();
}
}

View File

@ -1,28 +0,0 @@
#pragma once
#include <a8/epolleventhandler.h>
namespace a8
{
class EventFD : public EpollEventHandler
{
public:
std::function<void (void*, unsigned long long)> OnEvent;
EventFD();
virtual ~EventFD();
void Init(void* context);
void UnInit();
void Write(unsigned long long value);
virtual void SetEpollFd(int epoll_fd) override;
virtual void DoRecv() override;
virtual void DoSend() override;
virtual void DoError() override;
private:
int epoll_fd_ = a8::INVALID_FD;
void* context_ = nullptr;
int fd_ = a8::INVALID_FD;
};
}

View File

@ -1,102 +0,0 @@
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <a8/a8.h>
#include <a8/timerfd.h>
namespace a8
{
TimerFD::TimerFD()
{
fd_ = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
if (fd_ == a8::INVALID_FD) {
abort();
}
}
TimerFD::~TimerFD()
{
UnInit();
}
void TimerFD::Init(void* context)
{
context_ = context;
}
void TimerFD::UnInit()
{
if (fd_ != a8::INVALID_FD) {
struct epoll_event ev;
::epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, fd_, &ev);
::close(fd_);
fd_ = a8::INVALID_FD;
}
}
void TimerFD::Start(long long ms)
{
itimerspec new_value;
new_value.it_interval.tv_sec = ms / 1000;
new_value.it_interval.tv_nsec = (ms % 1000) * 1000 * 1000;
new_value.it_value.tv_sec = ms / 1000;
new_value.it_value.tv_nsec = (ms % 1000) * 1000 * 1000;
if (timerfd_settime(fd_, 0, &new_value, nullptr) != 0) {
abort();
}
}
void TimerFD::Stop()
{
itimerspec new_value;
new_value.it_interval.tv_sec = 0;
new_value.it_interval.tv_nsec = 0;
new_value.it_value.tv_sec = 0;
new_value.it_value.tv_nsec = 0;
if (timerfd_settime(fd_, 0, &new_value, nullptr) != 0) {
abort();
}
}
void TimerFD::SetEpollFd(int epoll_fd)
{
struct epoll_event ev;
ev.data.fd = fd_;
ev.events = EPOLLIN | EPOLLET;
ev.data.ptr = this;
int ret = ::epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd_, &ev);
if (ret != 0) {
abort();
}
epoll_fd_ = epoll_fd;
}
void TimerFD::DoRecv()
{
unsigned long long value = 0;
::read(fd_, &value, sizeof(value));
if (OnTimer) {
OnTimer(context_, value);
}
}
void TimerFD::DoSend()
{
abort();
}
void TimerFD::DoError()
{
abort();
}
}

View File

@ -1,29 +0,0 @@
#pragma once
#include <a8/epolleventhandler.h>
namespace a8
{
class TimerFD : public EpollEventHandler
{
public:
std::function<void (void*, unsigned long long)> OnTimer;
TimerFD();
virtual ~TimerFD();
void Init(void* context);
void UnInit();
void Start(long long ms);
void Stop();
virtual void SetEpollFd(int epoll_fd) override;
virtual void DoRecv() override;
virtual void DoSend() override;
virtual void DoError() override;
private:
int epoll_fd_ = a8::INVALID_FD;
void* context_ = nullptr;
int fd_ = a8::INVALID_FD;
};
}