-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathev_loop.h
More file actions
93 lines (72 loc) · 1.88 KB
/
ev_loop.h
File metadata and controls
93 lines (72 loc) · 1.88 KB
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
/***********************************************************************
* @author simoncheng
* @data 2014.8
* @version v0.1
* this program is an encapsulation of the EPOLL API which is frequently
* used in backend systems based on linux.
* it likes a simplified libev library which only supports the events
* on fds. timer and signal will be added later.
***********************************************************************/
#ifndef _EV_LOOP_H
#define _EV_LOOP_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <unistd.h>
#include <sys/epoll.h>
#include "global.h"
//#include "min_heap.h"
#define EV_TYPE __uint32_t
/**
* use when init fd_records in muti_threads environment
*/
#define LOCK(Lock) while(!__sync_bool_compare_and_swap(&Lock, 0, 1))
#define UNLOCK(Lock) Lock = 0
enum {
EV_READ = EPOLLIN,
EV_WRITE = EPOLLOUT
};
typedef struct ev_loop_t{
int epfd;
int maxevent;
int etmodel;
//fd_record_t *fd_records;
struct epoll_event *events;
//timer
//struct ev_timer_t **heap;
void **heap;
int heap_size;
int heap_capacity;
int timer_fd;
}ev_loop_t;
typedef void* (*cb_func_t) (ev_loop_t *loop, int fd, EV_TYPE events);
typedef struct {
int active;
EV_TYPE events;
cb_func_t cb_read;
cb_func_t cb_write;
int ffd;
unsigned int write_pos;
unsigned int read_pos;
unsigned int total_len;
char buf[MAXBUFSIZE];
int http_code;
char path[256];
int keep_alive;
void* timer_ptr;
} fd_record_t;
//muti-threads share the fd_records
fd_record_t *fd_records;
ev_loop_t *ev_create_loop(int maxevent, int et);
int ev_register(ev_loop_t*loop, int fd, EV_TYPE events, cb_func_t cb);
int ev_unregister(ev_loop_t *loop, int fd);
int ev_stop(ev_loop_t *loop, int fd, EV_TYPE events);
int ev_run_loop(ev_loop_t *loop);
void ev_clear(int fd);
int tcp_server(int port);
int setnonblocking(int fd);
#ifdef __cplusplus
}
#endif
#endif