1 /*
2  * Geario - A cross-platform abstraction library with asynchronous I/O.
3  *
4  * Copyright (C) 2021-2022 Kerisy.com
5  *
6  * Website: https://www.kerisy.com
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module geario.event.timer.IOCP;
13 
14 // dfmt off
15 version (HAVE_IOCP) : 
16 // dfmt on
17 
18 import geario.event.selector.Selector;
19 import geario.event.timer.Common;
20 import geario.Functions;
21 import geario.net.channel.Types;
22 
23 import core.time;
24 
25 /**
26 */
27 class AbstractTimer : TimerChannelBase {
28     this(Selector loop) {
29         super(loop);
30         setFlag(ChannelFlag.Read, true);
31         _timer = new GearioWheelTimer();
32         _timer.timeout = &onTimerTimeout;
33         _readBuffer = new UintObject();
34     }
35 
36     bool readTimer(scope SimpleActionHandler read) {
37         this.ClearError();
38         this._readBuffer.data = 1;
39         if (read)
40             read(this._readBuffer);
41         return false;
42     }
43 
44     private void onTimerTimeout(Object) {
45         _timer.rest(wheelSize);
46         this.OnRead();
47     }
48 
49     override void Stop() {
50         _timer.Stop();
51         super.Stop();
52     }
53 
54     bool setTimerOut() {
55         if (_interval > 0) {
56             _interval = _interval > 20 ? _interval : 20;
57             auto size = _interval / CustomTimerMinTimeOut;
58             const auto superfluous = _interval % CustomTimerMinTimeOut;
59             size += superfluous > CustomTimer_Next_TimeOut ? 1 : 0;
60             size = size > 0 ? size : 1;
61             _wheelSize = cast(uint) size;
62             _circle = _wheelSize / CustomTimerWheelSize;
63             return true;
64         }
65         return false;
66     }
67 
68     @property GearioWheelTimer timer() {
69         return _timer;
70     }
71 
72     UintObject _readBuffer;
73 
74     private GearioWheelTimer _timer;
75 }