This commit is contained in:
aozhiwei 2022-12-13 10:25:32 +08:00
parent 358c62df57
commit e4a9fab0ef
5 changed files with 184 additions and 70 deletions

View File

@ -265,20 +265,21 @@ namespace a8
AsyncTcpClient* client = (AsyncTcpClient*)param.sender.GetUserData();
client->DoAsyncConnect();
client->connect_timer_attacher.ClearTimerList();
context->xtimer.AddDeadLineTimerAndAttach(
param.param1,
a8::XParams()
.SetSender(client),
[] (const a8::XParams& param)
{
AsyncTcpClient* client = (AsyncTcpClient*)param.sender.GetUserData();
client->DoAsyncClose();
if (client->on_error) {
client->on_error(client, 111);
}
},
&client->connect_timer_attacher.timer_list_
);
context->xtimer.AddDeadLineTimer
(
param.param1,
a8::Args({}),
[client] (int event, const a8::Args& args, const a8::Args* user_args)
{
if (a8::TIMER_EXEC_EVENT == event) {
client->DoAsyncClose();
if (client->on_error) {
client->on_error(client, 111);
}
}
},
&client->connect_timer_attacher
);
}
void IoLoop::_IMAsyncClose(IoLoopThreadContext* context, a8::XParams& param)

View File

@ -40,12 +40,14 @@ namespace a8
void XTimerAttacher::ClearTimerList()
{
#if 0
struct list_head* pos = nullptr;
struct list_head* n = nullptr;
list_for_each_safe(pos, n, &timer_list_){
xtimer_list* tmp_timer = xtimer->GetTimerByAttach(pos);
xtimer->DeleteTimer(tmp_timer, is_destory_);
}
#endif
}
}

View File

@ -19,6 +19,7 @@ namespace a8
const int INVALID_FD = -1;
const int INVALID_SOCKET = -1;
const int INVALID_SOCKET_HANDLE = 0;
};
#endif

View File

@ -41,9 +41,8 @@ struct xtimer_list {
int fixed_timer_execute_times;
struct xtvec_base *base;
a8::XTimerFunc timer_func;
a8::XTimerAfterFunc timer_after_func;
a8::XParams param;
a8::TimerCbProc cb;
a8::Args args;
};
struct XTimerDestoryHandleNode
@ -104,8 +103,8 @@ static inline int DetachTimer(struct xtimer_list *timer)
}
static inline void InitTimerList(xtvec_base* base, xtimer_list* timer, int timer_type,
long long expires, int expire_time, a8::XParams& param,
a8::XTimerFunc timer_func, a8::XTimerAfterFunc timer_after_func)
long long expires, int expire_time,
a8::Args args, a8::TimerCbProc cb)
{
INIT_LIST_HEAD(&timer->destory_handle_list);
INIT_LIST_HEAD(&timer->entry);
@ -115,9 +114,8 @@ static inline void InitTimerList(xtvec_base* base, xtimer_list* timer, int timer
timer->expire_time = expire_time;
timer->fixed_timer_execute_times = 0;
timer->base = base;
timer->timer_func = timer_func;
timer->timer_after_func = timer_after_func;
timer->param = param;
timer->cb = std::move(cb);
timer->args = std::move(args);
}
namespace a8
@ -158,12 +156,25 @@ namespace a8
context_ = context;
gc_time_ = gc_time;
cache_timer_num_ = cache_timer_num;
#if 1
base_->timer_tick = get_tick_count_func_(context_);
#endif
AddRepeatTimer(gc_time_,
a8::XParams().SetSender(this),
&XTimer::GC_XTimerFunc);
AddRepeatTimer
(gc_time_,
a8::Args({}),
[this] (int event, const a8::Args& args, const a8::Args* user_args)
{
if (a8::TIMER_EXEC_EVENT == event) {
int i = 0;
while (!list_empty(&base_->free_timer) &&
base_->free_timer_num > cache_timer_num_ && i < 1000) {
xtimer_list* timer = list_first_entry(&base_->free_timer, struct xtimer_list, entry);
list_del_init(&timer->entry);
delete timer;
base_->free_timer_num--;
++i;
}
}
});
}
void XTimer::Update()
@ -173,6 +184,98 @@ namespace a8
}
}
#if 1
void XTimer::AddDeadLineTimer(
int expire_time,
a8::Args args,
a8::TimerCbProc timer_cb,
a8::XTimerAttacher* attacher
)
{
}
std::weak_ptr<XTimerPtr> XTimer::AddDeadLineTimerEx(
int expire_time,
a8::Args args,
a8::TimerCbProc timer_cb,
a8::XTimerAttacher* attacher
)
{
}
void XTimer::AddRepeatTimer(
int expire_time,
a8::Args args,
a8::TimerCbProc timer_cb,
a8::XTimerAttacher* attacher)
{
}
std::weak_ptr<XTimerPtr> XTimer::AddRepeatTimerEx(
int expire_time,
a8::Args args,
a8::TimerCbProc timer_cb,
a8::XTimerAttacher* attacher)
{
}
void XTimer::FireUserEvent(std::weak_ptr<XTimerPtr>& timer_ptr, a8::Args& args)
{
}
void XTimer::ModifyTimer(std::weak_ptr<XTimerPtr>& timer_ptr, int expire_time)
{
}
void XTimer::DeleteTimer(std::weak_ptr<XTimerPtr>& timer_ptr)
{
}
long long XTimer::GetRemainTime(std::weak_ptr<XTimerPtr>& timer_ptr)
{
}
void XTimer::DestoryTimer(std::weak_ptr<XTimerPtr>& timer_ptr)
{
}
xtimer_list* XTimer::GetTimerByAttach(list_head* attach_entry)
{
}
xtimer_list* XTimer::GetRunningTimer()
{
}
XTimerDestoryHandle* XTimer::AddTimerDestoryHandle(std::weak_ptr<XTimerPtr>& timer_ptr,
std::function<void(xtimer_list*)> cb)
{
}
void XTimer::RemoveTimerDestoryHandle(XTimerDestoryHandle* handle)
{
}
void XTimer::DeleteRunningTimer()
{
}
#else
xtimer_list* XTimer::AddDeadLineTimer(int expire_time, a8::XParams param, a8::XTimerFunc timer_func,
a8::XTimerAfterFunc timer_after_func)
{
@ -428,21 +531,6 @@ namespace a8
free_timers(&base_->free_timer);
}
void XTimer::GC_XTimerFunc(const a8::XParams& param)
{
XTimer* timer = (XTimer*)param.sender.GetUserData();
int i = 0;
while (!list_empty(&timer->base_->free_timer) &&
timer->base_->free_timer_num > timer->cache_timer_num_ && i < 1000) {
xtimer_list* timer1 = list_first_entry(&timer->base_->free_timer, struct xtimer_list, entry);
list_del_init(&timer1->entry);
delete timer1;
timer->base_->free_timer_num--;
i++;
}
}
XTimerDestoryHandle* XTimer::AddTimerDestoryHandle(xtimer_list* timer,
std::function<void(xtimer_list*)> cb)
{
@ -482,5 +570,6 @@ namespace a8
DeleteTimer(ptr.lock()->timer);
}
}
#endif
}

View File

@ -1,5 +1,5 @@
/*
* 仿linux2.6
* linux2.6
*/
#ifndef A8_XTIMER_H
#define A8_XTIMER_H
@ -8,8 +8,14 @@ struct xtimer_list;
struct xtvec_base;
namespace a8
{
typedef void (*XTimerFunc)(const a8::XParams& param);
typedef void (*XTimerAfterFunc)(const a8::XParams& param, bool is_destory);
const int TIMER_EXEC_EVENT = 1;
const int TIMER_DELETE_EVNET = 2;
const int TIMER_DESTORY_EVNET = 3;
const int TIMER_USER_EVNET = 66;
typedef std::function<void(int, const a8::Args&, const a8::Args*)> TimerCbProc;
typedef long long (*XGetTickCountFunc)(void*);
typedef struct list_head XTimerDestoryHandle;
struct XTimerPtr;
@ -25,48 +31,63 @@ namespace a8
void Update();
//添加截止时间定时器(一次性定时器)
xtimer_list* AddDeadLineTimer(int expire_time, a8::XParams param, a8::XTimerFunc timer_func,
a8::XTimerAfterFunc timer_after_func = nullptr);
xtimer_list* AddDeadLineTimerAndAttach(int expire_time, a8::XParams,
a8::XTimerFunc timer_func,
list_head* attach_list,
a8::XTimerAfterFunc timer_after_func = nullptr);
void AddDeadLineTimer(
int expire_time,
a8::Args args,
a8::TimerCbProc timer_cb,
a8::XTimerAttacher* attacher = nullptr
);
std::weak_ptr<XTimerPtr> AddDeadLineTimerEx(
int expire_time,
a8::Args args,
a8::TimerCbProc timer_cb,
a8::XTimerAttacher* attacher = nullptr
);
//添加重复执行定时器(周期性定时器)
xtimer_list* AddRepeatTimer(int expire_time, a8::XParams param, a8::XTimerFunc timer_func,
a8::XTimerAfterFunc timer_after_func = nullptr);
xtimer_list* AddRepeatTimerAndAttach(int expire_time, a8::XParams param, a8::XTimerFunc timer_func,
list_head* attach_list,
a8::XTimerAfterFunc timer_after_func = nullptr);
void AddRepeatTimer(
int expire_time,
a8::Args args,
a8::TimerCbProc timer_cb,
a8::XTimerAttacher* attacher = nullptr);
std::weak_ptr<XTimerPtr> AddRepeatTimerEx(
int expire_time,
a8::Args args,
a8::TimerCbProc timer_cb,
a8::XTimerAttacher* attacher = nullptr);
//发送用户事件
void FireUserEvent(std::weak_ptr<XTimerPtr>& timer_ptr, a8::Args& args);
//修改定时器参数
void ModifyTimer(xtimer_list* timer, int expire_time);
void ModifyTimer(std::weak_ptr<XTimerPtr>& ptr, int expire_time);
void ModifyTimer(std::weak_ptr<XTimerPtr>& timer_ptr, int expire_time);
//删除定时器
void DeleteTimer(xtimer_list* timer, bool is_destory = false);
void DeleteTimer(std::weak_ptr<XTimerPtr>& timer_ptr);
//获取定时器剩余时间
long long GetRemainTime(std::weak_ptr<XTimerPtr>& timer_ptr);
//删除当前运行中定时器
void DeleteRunningTimer();
protected:
//销毁定时器
void DestoryTimer(std::weak_ptr<XTimerPtr>& timer_ptr);
//通过关联的list_head获取定时器对象
xtimer_list* GetTimerByAttach(list_head* attach_entry);
//获取定时器关联参数
a8::XParams* MutableParams(xtimer_list* timer);
a8::XParams* MutableParams(std::weak_ptr<XTimerPtr>& ptr);
//获取定时器剩余时间
long long GetRemainTime(xtimer_list* timer);
//获取当前正在运行的定时器
xtimer_list* GetRunningTimer();
//添加定时器销毁事件
XTimerDestoryHandle* AddTimerDestoryHandle(xtimer_list* timer,
XTimerDestoryHandle* AddTimerDestoryHandle(std::weak_ptr<XTimerPtr>& timer_ptr,
std::function<void(xtimer_list*)> cb);
//移除定时器销毁事件
void RemoveTimerDestoryHandle(XTimerDestoryHandle* handle);
//获取定时器弱引用
std::weak_ptr<XTimerPtr> GetTimerPtr(xtimer_list* timer);
//删除弱引用指向的定时器
void DeleteTimer(std::weak_ptr<XTimerPtr>& ptr);
private:
void UpdateTimer();
xtimer_list* NewTimerList();
void AddToFreeList(xtimer_list* timer);
void Clear();
static void GC_XTimerFunc(const a8::XParams& param);
private:
xtvec_base* base_ = nullptr;