-
Notifications
You must be signed in to change notification settings - Fork 9
/
events.c
64 lines (61 loc) · 1.03 KB
/
events.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
/**
* @filename: events.c
*
* @author: QinYUN575
*
* @create date: 2019/11/3
*
*
*
*/
#include <stdio.h>
#include <string.h>
#include "queue.h"
#include "event.h"
#include "events.h"
/**
* 事件接收
*
*/
int receive_event(Queue *events, const Event *event)
{
Event *new_event;
if ((new_event = (Event *)malloc(sizeof(Event))) == 0)
{
return -1;
}
memcpy(new_event, event, sizeof(Event));
if (queue_enqueue(events, event) != 0)
{
return -1;
}
return 0;
}
/**
* 事件处理
*
*/
int process_event(Queue *events, int (*dispatch)(Event *event))
{
Event *event;
if (queue_size(events) == 0)
{
return -1;
}
else
{
if (queue_dequeue(events, (void **)&event) == 0)
{
return -1;
}
else
{
if (dispatch != NULL)
{
dispatch(event);
}
free(event);
}
}
return 0;
}