8c2952457
김태훈
응용 프로그램 추가
|
1
2
3
|
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@author 오재경 freefrug@falinux.com FALinux.Co.,Ltd.
@brief poll 을 관리한다.
@modify 2009-05-07 (오재경) 파일하나만을 폴로 돌리는 함수를 추가
2009-05-20 (오재경) tag 로써 poll_obj 객체를 얻는 함수 추가
2009-10-09 (오재경) poll_do_loop() 함수의 재귀호출 회수를 제한하도록 수정
2010-01-04 (오재경) poll_obj_t 구조체에서 poll_ndx 멤버변수 제거
poll_delete() 함수에서 tlist_delete() 함수대신 tlist_remove()함수로 수정
2010-03-19 (오재경) poll_obj_t 구조체에서 on_disconnect 멤버 추가
tcp 일 경우 사용됨
2010-08-18 (장길석) mingw와 함께 사용할 수 있는 코드 추가
2014-09-03 (김민수) tty가 아닌 stdin이 POLL_IN으로 등록될 경우 항상 readable하기 때문에
CPU 점유율이 100%가 되는 문제가 있어서
stdin이 tty가 아닐 경우 이벤트를 등록하지 않는다.
|
8c2952457
김태훈
응용 프로그램 추가
|
18
19
20
21
22
23
24
25
26
|
@todo
@bug
@remark
@warning
*/
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
27
28
|
|
8c2952457
김태훈
응용 프로그램 추가
|
29
30
|
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
31
|
#define EMBEDDED_LINUX
|
8c2952457
김태훈
응용 프로그램 추가
|
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
|
#ifdef MS_WIN32
#undef EMBEDDED_LINUX
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef EMBEDDED_LINUX
#include <sys/socket.h>
#include <sys/poll.h>
#else
#include <windows.h>
#include <winsock2.h>
#endif
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#include <tlist.h>
#include <pollmng.h>
char desc_pollmng[] = "falinux pollmng ver 0.2.4";
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
71
|
|
8c2952457
김태훈
응용 프로그램 추가
|
72
73
|
static struct pollfd poll_array[POLL_MAX_COUNT];
static tlist *poll_list = NULL;
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
74
75
76
77
78
|
static int cnt_recursive = 0;
static int is_need_rebuild = TRUE;
static int is_loop_break = FALSE;
|
8c2952457
김태훈
응용 프로그램 추가
|
79
80
81
82
83
84
85
86
87
88
89
|
#ifdef MS_WIN32
typedef unsigned long int nfds_t;
static int poll( struct pollfd *a_fds, nfds_t a_nfds, int a_timeout){
struct pollfd *p_fds;
FD_SET rset;
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
90
|
|
8c2952457
김태훈
응용 프로그램 추가
|
91
92
93
94
95
|
int cnt_rs = 0;
int cnt_sock = 0;
int is_socket_exists = FALSE;
int ndx;
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
96
|
|
8c2952457
김태훈
응용 프로그램 추가
|
97
98
99
100
101
102
103
104
105
106
|
p_fds = a_fds;
for ( ndx = 0; ndx < a_nfds; ndx++){
p_fds->revents = 0;
if ( p_fds->is_serial){
DWORD dwBytesRead, dwErrorFlags;
COMSTAT comstat;
ClearCommError( p_fds->fd, &dwErrorFlags, &comstat);
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
107
|
dwBytesRead = comstat.cbInQue;
|
8c2952457
김태훈
응용 프로그램 추가
|
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
|
if ( 0 < dwBytesRead){
p_fds->revents = POLLIN;
cnt_rs++;
} else {
p_fds->revents = 0;
}
} else {
is_socket_exists = TRUE;
}
p_fds++;
}
if ( !is_socket_exists){
usleep( 1000);
return cnt_rs;
}
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
136
|
|
8c2952457
김태훈
응용 프로그램 추가
|
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
p_fds = a_fds;
FD_ZERO( &rset);
for( ndx = 0; ndx < a_nfds; ndx++ ){
if ( !p_fds->is_serial){
if ( -1 != *(SOCKET *)p_fds->fd ){
FD_SET( *(SOCKET *)p_fds->fd, &rset );
}
}
p_fds++;
}
struct timeval sttTimeout;
sttTimeout.tv_sec = a_timeout / 1000;
sttTimeout.tv_usec = ( a_timeout % 1000 ) * 1000;
cnt_sock = select( 0, &rset, NULL, NULL, &sttTimeout );
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
156
|
if( 0 > cnt_sock ){
|
8c2952457
김태훈
응용 프로그램 추가
|
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
return -1;
}
else {
p_fds = a_fds;
cnt_sock = 0;
for ( ndx = 0; ndx < a_nfds; ndx++){
if ( !p_fds->is_serial){
if ( FD_ISSET( *(SOCKET *)p_fds->fd, &rset)){
p_fds->revents = POLLIN;
cnt_sock++;
}
}
p_fds++;
}
}
return cnt_rs + cnt_sock;
}
#endif
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
179
|
|
8c2952457
김태훈
응용 프로그램 추가
|
180
181
182
183
184
185
|
@remark
*
void poll_init( void )
{
poll_list = tlist_create();
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
186
187
|
is_need_rebuild = TRUE;
is_loop_break = FALSE;
|
8c2952457
김태훈
응용 프로그램 추가
|
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
#ifdef MS_WIN32
WSADATA wsaData;
if ( 0 != WSAStartup( MAKEWORD( 2, 2), &wsaData)){
perror( "WSAStartup() error!!" );
exit( 1);
}
#endif
}
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
202
|
|
8c2952457
김태훈
응용 프로그램 추가
|
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
|
@remark
*
void poll_exit( void )
{
poll_obj_t *obj;
int ndx;
for ( ndx = 0; ndx < poll_list->fcount ; ndx++ )
{
obj = tlist_get( poll_list, ndx );
if ( 0 <= obj->fd )
{
#ifdef EMBEDDED_LINUX
close( obj->fd );
#else
CloseHandle( obj->fd);
#endif
}
free( (void *)obj );
}
tlist_free( poll_list );
}
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
232
233
|
|
8c2952457
김태훈
응용 프로그램 추가
|
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
|
*
void poll_rebuild( void ){
poll_obj_t *obj;
int ndx;
memset( poll_array, 0, sizeof(poll_array) );
for (ndx= 0; ndx < poll_list->fcount; ndx++)
{
int events;
obj = (poll_obj_t *)tlist_get( poll_list, ndx );
if ( obj->fd == fileno(stdin) && isatty(fileno(stdin)) == 0 )
{
continue;
}
events = 0;
if ( obj->on_poll_in ) events |= POLLIN;
if ( obj->on_poll_out ) events |= POLLOUT;
if ( obj->on_poll_err ) events |= POLLERR;
if ( obj->on_poll_hup ) events |= POLLHUP;
poll_array[ndx].fd = obj->fd;
poll_array[ndx].events = events;
#ifdef MS_WIN32
poll_array[ndx].is_serial = obj->is_serial;
#endif
}
is_loop_break = TRUE;
is_need_rebuild = FALSE;
}
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
274
275
276
277
278
|
|
8c2952457
김태훈
응용 프로그램 추가
|
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
|
*
poll_obj_t *poll_add( fd_t fd )
{
poll_obj_t *obj;
int idx;
if ( NULL == poll_list )
{
printf( "error need to call poll_init()
" );
return NULL;
}
obj = (poll_obj_t *)malloc( sizeof(poll_obj_t) );
memset( (void *)obj, 0, sizeof(poll_obj_t) );
obj->fd = fd;
obj->type = STYP_FILE;
obj->tag = 0;
obj->on_poll_in = NULL;
obj->on_poll_out = NULL;
obj->on_poll_err = NULL;
obj->on_poll_hup = NULL;
obj->on_timeout = NULL;
obj->on_disconnect = NULL;
obj->priv = NULL;
obj->user = NULL;
#ifdef MS_WIN32
obj->is_serial = FALSE;
#endif
idx = tlist_add( poll_list, (void *)obj );
if ( 0 <= idx ) {
is_need_rebuild = TRUE;
return obj;
}
else {
free( (void *)obj );
return NULL;
}
}
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
324
325
326
|
|
8c2952457
김태훈
응용 프로그램 추가
|
327
328
329
330
331
332
333
334
335
336
337
|
*
poll_obj_t *poll_get_obj( int idx )
{
if ( ( 0 <= idx ) && ( idx < poll_list->fcount ) )
{
return (poll_obj_t *)tlist_get( poll_list, idx );
}
return NULL;
}
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
338
339
340
|
|
8c2952457
김태훈
응용 프로그램 추가
|
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
*
void *poll_get_priv( int idx )
{
poll_obj_t *obj;
if ( ( 0 <= idx ) && ( idx < poll_list->fcount ) )
{
obj = (poll_obj_t *)tlist_get( poll_list, idx );
return obj->priv;
}
return NULL;
}
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
356
357
|
|
8c2952457
김태훈
응용 프로그램 추가
|
358
359
360
361
362
363
364
365
|
*
int poll_count( void )
{
return poll_list->fcount;
}
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
366
367
368
|
|
8c2952457
김태훈
응용 프로그램 추가
|
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
*
poll_obj_t *poll_obj_byfd( fd_t fd )
{
poll_obj_t *obj;
int idx;
for( idx=0; idx<poll_list->fcount; idx++ )
{
obj = tlist_get( poll_list, idx );
if ( obj->fd == fd )
{
return obj;
}
}
return NULL;
}
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
387
388
389
390
|
|
8c2952457
김태훈
응용 프로그램 추가
|
391
392
393
394
395
396
397
|
*
void poll_delete( poll_obj_t *obj )
{
fd_t fd;
fd = obj->fd;
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
398
|
free( (void *)obj );
|
8c2952457
김태훈
응용 프로그램 추가
|
399
|
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
400
|
|
8c2952457
김태훈
응용 프로그램 추가
|
401
402
403
404
405
406
407
408
409
410
411
412
|
#ifdef EMBEDDED_LINUX
close( fd );
#else
CloseHandle( fd );
#endif
tlist_remove( poll_list, obj );
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
413
|
poll_rebuild();
|
8c2952457
김태훈
응용 프로그램 추가
|
414
415
|
}
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
416
417
418
419
|
|
8c2952457
김태훈
응용 프로그램 추가
|
420
421
422
423
424
425
426
427
428
429
430
431
|
*
void poll_delete_byfd( fd_t fd )
{
poll_obj_t *obj;
obj = poll_obj_byfd( fd );
if ( obj )
{
poll_delete( obj );
}
}
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
432
433
434
435
436
437
|
|
8c2952457
김태훈
응용 프로그램 추가
|
438
439
440
441
442
443
|
*
int poll_do_loop( int time_out )
{
int fd_cnt, event_cnt;
int ndx, rtnval = POLL_EVENTED;
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
444
|
|
8c2952457
김태훈
응용 프로그램 추가
|
445
446
447
448
449
450
451
452
|
if ( POLL_RECURSIVE_COUNT < cnt_recursive )
{
printf( "fatal error : poll_do_loop() recursive limit
" );
return POLL_RECURSIVE_LIMIT_ERR;
}
cnt_recursive++;
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
453
|
if ( is_need_rebuild ){
|
8c2952457
김태훈
응용 프로그램 추가
|
454
455
456
|
poll_rebuild();
}
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
457
|
|
8c2952457
김태훈
응용 프로그램 추가
|
458
459
460
461
462
463
464
465
466
|
fd_cnt = poll_list->fcount;
event_cnt = poll( (struct pollfd *)&poll_array, fd_cnt, time_out );
if ( 0 > event_cnt )
{
rtnval = POLL_ASYNC_ERR;
goto lable_poll_do_loop_end;
}
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
467
|
|
8c2952457
김태훈
응용 프로그램 추가
|
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
|
if ( 0 == event_cnt )
{
for ( ndx=0; ndx < fd_cnt; ndx++ )
{
poll_obj_t *obj;
obj = tlist_get( poll_list, ndx );
if ( obj->on_timeout )
{
obj->on_timeout( obj );
}
}
rtnval = POLL_TIME_OUT;
goto lable_poll_do_loop_end;
}
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
484
485
|
is_loop_break = FALSE;
|
8c2952457
김태훈
응용 프로그램 추가
|
486
487
488
489
|
for ( ndx=0; ndx<fd_cnt; ndx++ )
{
poll_obj_t *obj;
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
490
|
|
8c2952457
김태훈
응용 프로그램 추가
|
491
492
493
494
495
496
497
498
499
500
|
if( poll_array[ndx].revents & POLLIN )
{
obj = tlist_get( poll_list, ndx );
if ( ( obj ) && ( obj->on_poll_in ) )
{
obj->on_poll_in( obj );
event_cnt --;
}
}
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
501
|
|
8c2952457
김태훈
응용 프로그램 추가
|
502
503
504
505
506
507
508
509
510
511
|
if( poll_array[ndx].revents & POLLOUT )
{
obj = tlist_get( poll_list, ndx );
if ( ( obj ) && ( obj->on_poll_out ) )
{
obj->on_poll_out( obj );
event_cnt --;
}
}
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
512
|
|
8c2952457
김태훈
응용 프로그램 추가
|
513
514
515
516
517
518
519
520
521
522
|
if( poll_array[ndx].revents & POLLHUP )
{
obj = tlist_get( poll_list, ndx );
if ( ( obj ) && ( obj->on_poll_hup ) )
{
obj->on_poll_hup( obj );
event_cnt --;
}
}
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
523
|
|
8c2952457
김태훈
응용 프로그램 추가
|
524
525
526
527
528
529
530
531
532
533
534
|
if( poll_array[ndx].revents & POLLERR )
{
obj = tlist_get( poll_list, ndx );
if ( ( obj ) && ( obj->on_poll_err ) )
{
obj->on_poll_err( obj );
event_cnt --;
}
}
if ( 0 >= event_cnt ) break;
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
535
|
if ( is_loop_break ) break;
|
8c2952457
김태훈
응용 프로그램 추가
|
536
537
538
539
540
541
542
543
544
545
546
|
}
lable_poll_do_loop_end:
cnt_recursive --;
return rtnval;
}
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
547
548
549
550
551
552
553
|
|
8c2952457
김태훈
응용 프로그램 추가
|
554
555
556
557
558
559
560
561
562
563
|
*
int poll_do_one( fd_t fd, int event, int time_out )
{
int event_cnt;
struct pollfd poll_one[4];
poll_one[0].fd = fd;
poll_one[0].revents = 0;
poll_one[0].events = event;
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
564
|
|
8c2952457
김태훈
응용 프로그램 추가
|
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
|
event_cnt = poll( (struct pollfd *)&poll_one, 1, time_out );
if ( 0 > event_cnt )
{
return POLL_ASYNC_ERR;
}
if ( 0 == event_cnt )
{
return POLL_TIME_OUT;
}
return POLL_EVENTED;
}
|
3061c73f6
김태훈
인코딩 변경 EUC-KR -> ...
|
580
581
582
|
|
8c2952457
김태훈
응용 프로그램 추가
|
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
|
*
poll_obj_t *poll_obj_bytag( int tag )
{
poll_obj_t *obj;
int idx;
for( idx=0; idx<poll_list->fcount; idx++ )
{
obj = tlist_get( poll_list, idx );
if ( obj->tag == tag )
{
return obj;
}
}
return NULL;
}
|