6 Star 1 Fork 0

wudizhanshen66/花生酱

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
timer.c 2.39 KB
一键复制 编辑 原始数据 按行查看 历史
wudizhanshen66 提交于 2024-05-14 16:52 . 3期所有,4期超时断开
#include "timer.h"
Time_Queue* time_queue_init(){
Time_Queue* p = (Time_Queue*)calloc(1,sizeof(Time_Queue));
return p;
}
void time_queue_destroy(Time_Queue* queue){
for(int i=0; i<TIME_NUM; i++){
if(queue->slot[i].size==0)
continue;
else{
struct node* p_node = queue->slot[i].head;
while(p_node!=NULL){
struct node* temp = p_node;
p_node = p_node->next;
free(temp);
}
}
}
free(queue);
}
void move_next(Time_Queue* queue){
queue->current_index ++;
queue->current_index %= TIME_NUM;
int index = queue->current_index;
if(queue->slot[index].size>0){
struct slot* p_slot = &queue->slot[index];
struct node* p_node = p_slot->head;
struct node* p_temp = NULL;
while(p_node!=NULL){
p_temp = p_node;
p_node = p_node->next;
delUser(p_temp->peerfd, p_temp->uid);
free(p_temp);
}
p_slot->head = NULL;
p_slot->size = 0;
}
}
void update_node(Time_Queue* queue, int uid, int peerfd){
int index = queue->current_index;
struct slot* p_slot = queue->slot;
for(int i=0; i<TIME_NUM; i++){
struct node* p_node = (p_slot+i)->head;
struct node* p_after = NULL;
while(p_node!=NULL){
if(p_node->peerfd == peerfd || p_node->uid == uid){
if(p_after==NULL){ // 第一个结点
p_slot[i].head = p_node->next;
}else{ // 非第一个结点
p_after->next = p_node->next;
}
p_slot[i].size--;
p_slot = queue->slot+index; // p_node 指向当前结点
p_slot->size ++;
p_node->next = p_slot->head;
p_slot->head = p_node;
return ;
}
p_after = p_node;
p_node = p_node->next;
}
}
fprintf(stderr, "The peerfd %d and uid %d is no find int time_queue\n",peerfd,uid);
}
void insert_node(Time_Queue* queue, int uid, int peerfd){
int index = queue->current_index;
struct slot* p_slot = queue->slot+index;
struct node* p_node = (struct node*)malloc(sizeof(p_node));
p_node->peerfd = peerfd;
p_node->uid = uid;
p_node->next = p_slot->head;
p_slot->head = p_node;
p_slot->size ++;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wudizhanshen66/peanut.git
git@gitee.com:wudizhanshen66/peanut.git
wudizhanshen66
peanut
花生酱
master

搜索帮助