1 /*
2  * Hunt - A refined core library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.event.timer.IOCP;
13 
14 // dfmt off
15 version (HAVE_IOCP) : 
16 // dfmt on
17 
18 import hunt.event.selector.Selector;
19 import hunt.event.timer.Common;
20 import hunt.Functions;
21 import hunt.io.channel.Common;
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 HuntWheelTimer();
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 HuntWheelTimer timer() {
69         return _timer;
70     }
71 
72     UintObject _readBuffer;
73 
74     private HuntWheelTimer _timer;
75 }