-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrmc_proto_test_common.c
145 lines (107 loc) · 3.52 KB
/
rmc_proto_test_common.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (C) 2018, Jaguar Land Rover
// This program is licensed under the terms and conditions of the
// Mozilla Public License, version 2.0. The full text of the
// Mozilla Public License is at https://www.mozilla.org/MPL/2.0/
//
// Author: Magnus Feuer ([email protected])
#include "rmc_proto_test_common.h"
#include "rmc_log.h"
void _test(char* fmt_string, int major, int minor, int error)
{
if (!error)
return;
printf(fmt_string, major, minor, strerror(error));
exit(255);
}
void poll_add(user_data_t user_data,
int descriptor,
rmc_index_t index,
rmc_poll_action_t action)
{
int epollfd = user_data.i32;
struct epoll_event ev = {
.data.u32 = (uint32_t) index,
.events = 0 // EPOLLONESHOT
};
if (action & RMC_POLLREAD)
ev.events |= EPOLLIN;
if (action & RMC_POLLWRITE)
ev.events |= EPOLLOUT;
RMC_LOG_INDEX_COMMENT(index,
"poll_add(%d)%s%s%s",
descriptor,
((action & RMC_POLLREAD)?" read":""),
((action & RMC_POLLWRITE)?" write":""),
(!(action & (RMC_POLLREAD | RMC_POLLWRITE)))?" [none]":"");
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, descriptor, &ev) == -1) {
RMC_LOG_INDEX_FATAL(index, "epoll_ctl(add)");
exit(255);
}
}
void poll_modify(user_data_t user_data,
int descriptor,
rmc_index_t index,
rmc_poll_action_t old_action,
rmc_poll_action_t new_action)
{
int epollfd = user_data.i32;
struct epoll_event ev = {
.data.u32 = (uint32_t) index,
.events = 0 // EPOLLONESHOT
};
if (old_action == new_action)
return ;
if (new_action & RMC_POLLREAD)
ev.events |= EPOLLIN;
if (new_action & RMC_POLLWRITE)
ev.events |= EPOLLOUT;
RMC_LOG_INDEX_DEBUG(index, "poll_modify(%d)%s%s%s",
descriptor,
((new_action & RMC_POLLREAD)?" read":""),
((new_action & RMC_POLLWRITE)?" write":""),
(!(new_action & (RMC_POLLREAD | RMC_POLLWRITE)))?" [none]":"");
if (epoll_ctl(epollfd, EPOLL_CTL_MOD, descriptor, &ev) == -1) {
RMC_LOG_INDEX_FATAL(index, "epoll_ctl(modify): %s", strerror(errno));
exit(255);
}
}
void poll_remove(user_data_t user_data,
int descriptor,
rmc_index_t index)
{
int epollfd = user_data.i32;
if (epoll_ctl(epollfd, EPOLL_CTL_DEL, descriptor, 0) == -1) {
RMC_LOG_INDEX_WARNING(index, "epoll_ctl(delete): %s", strerror(errno));
return;
}
RMC_LOG_INDEX_COMMENT(index, "poll_remove()");
}
char* _op_res_string(uint8_t res)
{
switch(res) {
case RMC_ERROR:
return "error";
case RMC_READ_MULTICAST:
return "read multicast";
case RMC_READ_MULTICAST_LOOPBACK:
return "multicast loopback";
case RMC_READ_MULTICAST_NEW:
return "new multicast";
case RMC_READ_MULTICAST_NOT_READY:
return "multicast not ready";
case RMC_READ_TCP:
return "read tcp";
case RMC_READ_ACCEPT:
return "accept";
case RMC_READ_DISCONNECT:
return "disconnect";
case RMC_WRITE_MULTICAST:
return "write multicast";
case RMC_COMPLETE_CONNECTION:
return "complete connection";
case RMC_WRITE_TCP:
return "tcp write";
default:
return "[unknown]";
}
}