forked from JohnHau/mis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcur_udp_kbd_tim.c
executable file
·424 lines (345 loc) · 9.62 KB
/
cur_udp_kbd_tim.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curses.h>
#include <unistd.h>
#include <aio.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#define PORT 8900
#define MAXLINE 1024
#define LOG_FILE "game.log"
#define DIALOG_WIDTH 40
#define DIALOG_HEIGHT 20
#define CONTENT_WIDTH (DIALOG_WIDTH - 2)
void set_colors()
{
// these don't actually have anything to do with binary, so we are free to use "normal" numbers
init_pair(1, COLOR_RED, COLOR_BLACK); // red on black
init_pair(2, COLOR_GREEN, COLOR_BLACK); // green on black
init_pair(3, COLOR_YELLOW, COLOR_BLACK); // yellow on black
init_pair(4, COLOR_BLUE, COLOR_BLACK); // blue on black
init_pair(5, COLOR_MAGENTA, COLOR_BLACK); // magenta on black
init_pair(6, COLOR_CYAN, COLOR_BLACK); // cyan on black
init_pair(7, COLOR_WHITE, COLOR_BLACK); // white on black
}
void end_curses()
{
endwin();
}
// a simple log function, works similar to printf
void glog(const char *format, ... )
{
va_list args; // argument list
static FILE *logfile = NULL; // file pointer to the logfile
char *fformat; // the modified format of the string which will be written to the logfile
int length; // length of the format
if(!(format == NULL && logfile == NULL)) {
// open the logfile if not already opened
if(logfile == NULL) {
logfile = fopen(LOG_FILE, "w");
// if that doesn't work exit with an error message
if(logfile == NULL) {
fprintf(stderr, "Cannot open logfile %s\n", LOG_FILE);
exit(EXIT_FAILURE);
}
}
// if NULL is given as format, close the opened file
if(format != NULL) {
// increase length by 2 (for \n\0
length = strlen(format) + 2;
// allocate memory
fformat = malloc(sizeof(char) * length);
// copy the format over
strncpy(fformat, format, length - 2);
// append \n\0
fformat[length - 2] = '\n';
fformat[length - 1] = '\0';
// get the rest of the arguments
va_start(args, format);
// use vfprintf() to
vfprintf(logfile, fformat, args);
// forces the logmessage to be written into the file right now
fflush(logfile);
va_end(args);
// free the allocated memory for the format string
free(fformat);
}
else
{
// close the logfile
fclose(logfile);
}
}
}
// create a basic dialog with title which looks like this:
//
// +--------------------------------------+
// +---------------- TITLE ---------------+
// +--------------------------------------+
// | |
// | |
// | |
// | |
// | |
// | |
// | |
// | |
// | |
// | |
// | |
// | |
// | |
// | |
// | |
// | |
// +--------------------------------------+
//
WINDOW *create_dialog_window(const char *title) {
WINDOW *win;
int sx, sy, i, u, startpos;
// get the screen size
getmaxyx(stdscr, sy, sx);
// create a little window in the center of the screen with a border and a size of 40x20
win = newwin(DIALOG_HEIGHT, DIALOG_WIDTH, sy / 2 - DIALOG_HEIGHT / 2, sx / 2 - DIALOG_WIDTH / 2);
// create the border
wborder(win, '|', '|', '-', '-', '+', '+', '+', '+');
// fill the first two lines with "-" and "+" on the border
for(u = 1; u <= 2; u++) {
for(i = 0; i < DIALOG_WIDTH; i++) {
mvwprintw(win, u, i, "%c", i == 0 || i == DIALOG_WIDTH - 1 ? '+' : '-');
}
}
// print the title in the middle of the dialog
startpos = DIALOG_WIDTH / 2 - strlen(title) / 2;
// print a space before the title
mvwprintw(win, 1, startpos - 1, " ");
// print the title itself
mvwprintw(win, 1, startpos, "%s", title);
// print a space after the title
mvwprintw(win, 1, startpos + strlen(title), " ");
return win;
}
// creates a dialog with menu entries, you can press numbers to select a menu entry and close the dialog
int create_numbered_dialog(const char *title, const char *contents, int lines) {
WINDOW *win = create_dialog_window(title);
int i, ch, number = 0;
// insert menu entries into the dialog
for(i = 0; i < lines; i++) {
mvwprintw(win, i + 3, 1, &contents[i * CONTENT_WIDTH], i + 1);
}
// display the dialog
wrefresh(win);
// wait for some input
while((ch = wgetch(win))) {
// error? begin again.
//if(ch == ERR) continue;
// select the first menu entry if enter is pressed
if(ch == '\n') ch = '1';
// a number pressed?
if(ch >= '0' && ch <= '9') {
number = ch - '0';
// prevent from handling numbers which are > than the number of menu entries
if(ch - '0' <= lines) {
// get out of the loop
break;
}
}
}
// delete the window
delwin(win);
// return the number pressed
return number;
}
// displays the main menu
int display_menu()
{
// the contents of the menu
// %i will be replaced with the number of the menu entry
char menu[][CONTENT_WIDTH] =
{
"%i) Start the game",
"%i) Highscores",
"%i) Controls",
"%i) Help",
"%i) Clear Highscores",
"%i) Exit"
};
// create a numbered dialog
return create_numbered_dialog("MENU", (char *)menu, 6);
}
void main_menu()
{
int selected_menu_entry;
do {
selected_menu_entry = display_menu();
if(selected_menu_entry == 1) {
// run the game
//run();
} else if(selected_menu_entry == 2) {
// display the highscores
//show_highscores();
} else if(selected_menu_entry == 3) {
// display a dialog which explains the controls of the game
//display_controls();
} else if(selected_menu_entry == 4) {
// display a dialog which explains the elements of the game
//display_help();
} else if(selected_menu_entry == 5) {
// clear highscores
#if 0
if(clear_score_dialog() == 1)
{
clear_highscore();
}
#endif
}
// leave if the menu entry "exit" is chosen
} while(selected_menu_entry != 6);
}
#if 0
void enable_kbd_signals(void)
{
int fd_flags;
fcntl(0,F_SETOWN,getpid());
fd_flags = fcntl(0,F_GETFL);
fcntl(0,F_SETFL,(fd_flags|O_ASYNC));
//fcntl(0,F_SETFL,(fd_flags|O_RSYNC));
}
#endif
struct aiocb kbcbuf;
void setup_aio_buffer(void)
{
static char input[1];
kbcbuf.aio_fildes = 0;
kbcbuf.aio_buf = input;
kbcbuf.aio_nbytes = 1;
kbcbuf.aio_offset = 0;
kbcbuf.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
kbcbuf.aio_sigevent.sigev_signo = SIGIO;
}
void on_input(int signum)
{
#if 0
int c = getch();
move(15,15);
addch(c);
#endif
int c;
char *cp = (char*)kbcbuf.aio_buf;/*cast to char*/
if(aio_error(&kbcbuf) != 0)
{
perror("reading failed");
}
else
{
if(aio_return(&kbcbuf) == 1)
{
c=*cp;
move(15,15);
addch(c);
refresh();
if(c == 'q')
{
endwin();
printf("that is it\n");
exit(EXIT_SUCCESS);
}
}
aio_read(&kbcbuf);
}
}
void init_curses()
{
#if _BSD_SOURCE || _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
// by default, ncurses stops the execution of the program for
// 1 second if escape is pressed. Disable this behaviour.
setenv("ESCDELAY", "0", 1);
#endif
initscr();
// get more control over the input
cbreak();
// getch() returns ERR if no input is present and doesn't wait
//nodelay(stdscr, TRUE);
// don't echo the inserted keys to the screen
noecho();
// colors!
start_color();
set_colors();
// also grab keys like F1 etc.
keypad(stdscr, TRUE);
}
int main(int argc, char* argv[])
{
int sockfd;
char hname[128]={0};
char buffer[MAXLINE];
char *hello = "Hello from server";
struct sockaddr_in servaddr, cliaddr;
struct hostent *hent=NULL;
gethostname(hname,sizeof(hname));
hent = gethostbyname(hname);
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
{
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
memset(&cliaddr, 0, sizeof(cliaddr));
// Filling server information
servaddr.sin_family = AF_INET; // IPv4
//servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);
//servaddr.sin_port = PORT;
// Bind the socket with the server address
if ( bind(sockfd, (const struct sockaddr *)&servaddr,
sizeof(servaddr)) < 0 )
{
perror("bind failed");
exit(EXIT_FAILURE);
}
int len, n;
len = sizeof(cliaddr); //len is value/resuslt
WINDOW *xw;
void on_input(int);
signal(SIGIO,on_input);
//enable_kbd_signals();
setup_aio_buffer();
aio_read(&kbcbuf);
init_curses();
while(1)
{
//pause();
move(20,25);
//printf("waiting on port %d\n",PORT);
printw("waiting on port %d\n",PORT);
refresh();
#if 0
n = recvfrom(sockfd, (char *)buffer, MAXLINE,
MSG_WAITALL, ( struct sockaddr *) &cliaddr,
&len);
#endif
n = recvfrom(sockfd, (char *)buffer, MAXLINE,
0, ( struct sockaddr *) &cliaddr,
&len);
buffer[n] = '\0';
//printf("Client : %s\n", buffer);
move(25,25);
printw("Client : %s\n", buffer);
refresh();
//sleep(5);
}
// main_menu();
// xw=create_dialog_window("hello");
// wrefresh(xw);
// refresh();
// sleep(4);
end_curses();
}