-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.h
88 lines (73 loc) · 1.96 KB
/
util.h
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
#ifndef __UTIL_H__
#define __UTIL_H__
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <errno.h>
class Util
{
public:
/*
* @para port_num : the port number will be listened
* @return : -1 if failed; otherwise return listening socket id
*/
static int create_socket(char* ip, int port_no);
/*
* @ param fd : the socket file descriptor will be made unblocking
* @ return : -1 if failed; otherwise return 0
*/
static int make_socket_unblocking(int sockfd);
/*
* @param pev
a pointer to an epoll_event struct
* @param fd
a file descriptor that will be listened
* @param events
a bit set that represents event types
*/
static void create_event(epoll_event* pev, int fd, uint32_t events);
/*
*@return
0 if success
-1 if fail
* */
static int update_event(int epoll_fd, int sock_fd, uint32_t events);
/*
*glue s1 and s2 into new_str
*@param s1 in
this variable have to end with '\0'
*@param s2 in
this variable have to end with '\0'
*@param delimiter in
it is used as a delimiter between s1 and s2
*@param new_str out
new_str must have enough space to hold s1+delimiter+s2
this variable will be end with '\0'
*@return
assume this function is always success
*/
static void str_glue(const char* s1, const char* s2, const char delimiter, char* new_str);
/*
*split old_str into s1 and s2
*@param old_str in
it must be end with '\0', and must contain one delimiter character
*@param delimiter in
used to spliting
*@param s1 out
it will be end with '\0', it must have enough space
*@param s2 out
it will be end with '\0', it must have enough space
*@assume
this function always success
*/
static void str_split(const char* old_str, const char delimiter, char* s1, char* s2);
static void err_quit(const char* msg);
static void err_sys(const char* msg);
};
#endif