-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg_event.c
104 lines (84 loc) · 1.57 KB
/
tg_event.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "tg_event.h"
#include <stdlib.h>
#include "tg_log.h"
#define TAG "EVENT"
static struct TGEvent * pHead=NULL;
static struct TGEvent * pTail=NULL;
static int queueCount=0;
// push new event to queue
// 0 client quit
// 1 client request
// Note: the event doesn't copy pData
// only point to this.
void pushEventToQueue(int type,void * pData)
{
struct TGEvent * pEv=(struct TGEvent *) malloc(sizeof(struct TGEvent));
memset(pEv,0,sizeof(struct TGEvent));
//TODO generate toke
pEv->token=-1;
pEv->type=type;
pEv->data=pData;
if(pHead==NULL)
{
pHead=pEv;
pTail=pEv;
}
else
{
pTail->pNext=pEv;
pTail=pEv;
}
pTail->pNext=NULL;
//record queue elements counts
queueCount++;
}
// returned head of event queue
// event object must be free after use
struct TGEvent * head()
{
struct TGEvent * pEv=pHead;
if(pHead!=NULL)
{
pHead=pHead->pNext;
// check if head is null
// it means current queue only exist one element
// we shoule clean pTail
if(pHead==NULL)
{
pTail=NULL;
}
queueCount--;
}
if(pEv!=NULL)
{
logD(TAG," event[%d type:%d parameters:%s]",(long)pEv,pEv->type,(char *)pEv->data);
}
return pEv;
}
// return current queue size
int getQueueSize()
{
return queueCount;
}
// cancel all cancel all events
void cancelAllEvent()
{
struct TGEvent * pEv=NULL;
while((pEv=head())!=NULL)
{
freeTGEvent(pEv);
}
}
// free struct Event memory
// Attribute pData of struct TGEvent as char * free
void freeTGEvent(struct TGEvent * pEv)
{
if(pEv!=NULL)
{
if(pEv->data!=NULL)
{
free((char *)pEv->data);
}
free(pEv);
}
}