1 /*
2  * Kiss - A refined core library for D programming language.
3  *
4  * Copyright (C) 2015-2018  Shanghai Putao Technology Co., Ltd
5  *
6  * Developer: HuntLabs.cn
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11  
12 module kiss.event.timer.iocp;
13 
14 // dfmt off
15 version (Windows) : 
16 // dfmt on
17 
18 
19 import kiss.event.core;
20 import kiss.event.timer.common;
21 
22 import core.time;
23 import std.datetime;
24 import std.exception;
25 import kiss.logger;
26 
27 
28 /**
29 */
30 class AbstractTimer : TimerChannelBase
31 {
32     this(Selector loop)
33     {
34         super(loop);
35         setFlag(WatchFlag.Read, true);
36         _timer = new KissWheelTimer();
37         _timer.timeout = &onTimerTimeout;
38         _readBuffer = new UintObject();
39     }
40 
41     bool readTimer(scope ReadCallBack read)
42     {
43         this.clearError();
44         this._readBuffer.data = 1;
45         if (read)
46             read(this._readBuffer);
47         return false;
48     }
49 
50     // override void start(bool immediately = false, bool once = false)
51     // {
52     //     this.setTimerOut();
53     //     super.start(immediately, once);
54     // }
55 
56     private void onTimerTimeout(Object) 
57     {        
58         _timer.rest(wheelSize);
59         this.onRead();
60     }
61 
62     override void stop()
63     {
64         _timer.stop();
65         super.stop();
66     }
67 
68     
69     bool setTimerOut()
70     {
71         if (_interval > 0)
72         {
73             _interval = _interval > 20 ? _interval : 20;
74             auto size = _interval / CustomTimerMinTimeOut;
75             const auto superfluous = _interval % CustomTimerMinTimeOut;
76             size += superfluous > CustomTimer_Next_TimeOut ? 1 : 0;
77             size = size > 0 ? size : 1;
78             _wheelSize = cast(uint) size;
79             _circle = _wheelSize / CustomTimerWheelSize;
80             return true;
81         }
82         return false;
83     }
84 
85 
86     @property KissWheelTimer timer() { return _timer; }
87     // mixin OverrideErro;
88 
89     UintObject _readBuffer;
90 
91     private KissWheelTimer _timer;
92 }