-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_debug.c
205 lines (161 loc) · 4.77 KB
/
server_debug.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
// server.c
// This is a udp server of gateway. Receive GPS information and Send Command to
// handring.
//
// Compile: g++ event_test.c -o event_test -levent -L/usr/local/lib -Wl,-rpath=/usr/local/lib
// Execture: ./event_test
//
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#define UDP_PORT 7212
#include <event.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <event2/event.h>
int g_count = 0;
struct timeval stTv;
static void discard_cb(int severity, const char *msg)
{
/* This callback does nothing. */
}
static FILE *logfile = NULL;
static void write_to_file_cb(int severity, const char *msg)
{
const char *s;
// if (!logfile)
// return;
switch (severity) {
case _EVENT_LOG_DEBUG: s = "debug"; break;
case _EVENT_LOG_MSG: s = "msg"; break;
case _EVENT_LOG_WARN: s = "warn"; break;
case _EVENT_LOG_ERR: s = "error"; break;
default: s = "?"; break; /* never reached */
}
fprintf(logfile, "[%s] %s\n", s, msg);
}
/* Turn off all logging from Libevent. */
void suppress_logging(void)
{
event_set_log_callback(discard_cb);
}
/* Redirect all Libevent log messages to the C stdio file 'f'. */
void set_logfile()
{
logfile = stderr;
event_set_log_callback(write_to_file_cb);
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// TIMER FUNCTION
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void timer_function(int x, short int y, void *pargs)
{
printf("Get %d Message in a 5 sec \n",g_count);
g_count = 0 ;
event_add((struct event*)pargs,&stTv);
}
void dump_proc(char * aReqBuffer,int nByte)
{
int i ;
int count = 0;
for(i = 0; i < nByte; i++)
{
fprintf(stderr, "0x%x ",*(aReqBuffer + i));
if (count == 9)
{
count = 0;
fprintf(stderr,"\n");
}else{
count ++;
}
}
fprintf(stderr, "\n",*(aReqBuffer + i));
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Event Function
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void func_for_eve1(int x, short int y, void *pargs)
{
unsigned int unFromAddrLen;
int nByte = 0;
char aReqBuffer[512];
struct sockaddr_in stFromAddr;
unFromAddrLen = sizeof(stFromAddr);
if ((nByte = recvfrom(x, aReqBuffer, sizeof(aReqBuffer), 0,
(struct sockaddr *)&stFromAddr, &unFromAddrLen)) == -1)
{
printf("error occured while receivingn");
}
//write_to_file_cb(_EVENT_LOG_DEBUG,aReqBuffer);
dump_proc( aReqBuffer, nByte)
dump_proc (aReqBuffer,nByte) ;
if (( sendto(x, aReqBuffer, nByte, 0,
(struct sockaddr *)&stFromAddr, unFromAddrLen)) == -1)
{
printf("error occured while receivingn");
}
//printf("Function called buffer is %sn",aReqBuffer);
g_count++;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// MAIN
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
int main(int argc, char **argv)
{
struct event_base *base ;
struct event g_Timer;
struct event g_eve;
int udpsock_fd;
struct sockaddr_in stAddr;
//FILE *fp = fopen ("./debuglog","w");
set_logfile();
event_enable_debug_mode();
base = event_init();
if ((udpsock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
printf("ERROR - unable to create socket:n");
exit(-1);
}
//Start : Set flags in non-blocking mode
int nReqFlags = fcntl(udpsock_fd, F_GETFL, 0);
if (nReqFlags< 0)
{
printf("ERROR - cannot set socket options");
}
if (fcntl(udpsock_fd, F_SETFL, nReqFlags | O_NONBLOCK) < 0)
{
printf("ERROR - cannot set socket options");
}
// End: Set flags in non-blocking mode
memset(&stAddr, 0, sizeof(struct sockaddr_in));
//stAddr.sin_addr.s_addr = inet_addr("192.168.64.1555552");
stAddr.sin_addr.s_addr = INADDR_ANY; //listening on local ip
stAddr.sin_port = htons(UDP_PORT);
stAddr.sin_family = AF_INET;
int nOptVal = 1;
if (setsockopt(udpsock_fd, SOL_SOCKET, SO_REUSEADDR,
(const void *)&nOptVal, sizeof(nOptVal)))
{
printf("ERROR - socketOptions: Error at Setsockopt");
}
if (bind(udpsock_fd, (struct sockaddr *)&stAddr, sizeof(stAddr)) != 0)
{
printf("Error: Unable to bind the default IP n");
exit(-1);
}
event_set(&g_eve, udpsock_fd, EV_READ | EV_PERSIST, func_for_eve1, &g_eve);
event_add(&g_eve, NULL);
/////////////TIMER START///////////////////////////////////
stTv.tv_sec = 5;
stTv.tv_usec = 0;
// event_set(&g_Timer, -1, EV_TIMEOUT , timer_function, &g_Timer);
// event_add(&g_Timer, &stTv);
////////////TIMER END/////////////////////////////////////
event_base_dispatch(base);
return 0;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^