[IOMultiplexerV2] alpha
[NextIRCd.git] / src / IOHandler / IOHandler.c
1 /* IOHandler.c - IOMultiplexer v2
2  * Copyright (C) 2014  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17 #define _IOHandler_internals
18 #include "IOInternal.h"
19 #include "IOHandler.h"
20
21 #include "IOLog.h"
22 #include "IOGarbageCollector.h"
23 #include "IOTimer.h"
24 #include "IODNSLookup.h"
25 #include "IOSockets.h"
26
27 /* compat */
28 #include "compat/utime.h"
29
30 #define IOHANDLER_STATE_INITIALIZED  0x0001
31 #define IOHANDLER_STATE_RUNNING      0x0002
32
33 static int iohandler_state = 0;
34
35
36 void iohandler_init() {
37         if((iohandler_state & IOHANDLER_STATE_INITIALIZED)) 
38                 return;
39         
40         iolog_init();
41         iogc_init();
42         
43         _init_timers();
44         _init_iodns();
45         _init_sockets();
46         
47         iohandler_state |= IOHANDLER_STATE_INITIALIZED;
48 }
49
50 void iohandler_stop() {
51         iohandler_state &= ~IOHANDLER_STATE_RUNNING;
52 }
53
54 static void iohandler_loop() {
55         while(iohandler_state & IOHANDLER_STATE_RUNNING) { // endless loop
56                 // iohandler calls
57                 iogc_exec();
58                 iodns_poll();
59                 iosocket_loop(IOHANDLER_LOOP_MAXTIME);
60                 
61         }
62 }
63
64 void iohandler_run() {
65         iohandler_state |= IOHANDLER_STATE_RUNNING;
66         
67         iohandler_loop();
68 }
69