root/trunk/whisperlib/net/base/timeouter.h

Revision 7, 3.1 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Authors: Catalin Popescu
31
32 #ifndef __NET_BASE_TIMEOUTER_H__
33 #define __NET_BASE_TIMEOUTER_H__
34
35 #include <map>
36 #include <whisperlib/net/base/selector.h>
37 #include <whisperlib/common/base/types.h>
38 #include <whisperlib/common/base/log.h>
39 #include <whisperlib/common/base/callback.h>
40
41 namespace net {
42
43 class Timeouter {
44  public:
45   typedef Callback1<int64> TimeoutCallback;
46
47   Timeouter(Selector* selector, TimeoutCallback* callback)
48       : selector_(selector),
49         callback_(callback),
50         timeout_closures_() {
51     CHECK(callback->is_permanent());
52   }
53   ~Timeouter() {
54     UnsetAllTimeouts();
55     delete callback_;
56   }
57
58   // Registers (or reregisters) a timeout call in timeout_in_ms ms from
59   // this moment with the given timeout_id.
60   // On timeout we call Connection::HandleTimeoutEvent, which in turn calls
61   // the virtual HandleTimeout - override that to
62   void SetTimeout(int64 timeout_id, int64 timeout_in_ms);
63   // Clears a previously set timeout callback. Returns true if indeed a timeout
64   // callback was set for that particular timeout_id
65   bool UnsetTimeout(int64 timeout_id);
66
67   // Clears all timeouts (normally on close / destructor)
68   void UnsetAllTimeouts();
69
70  private:
71   void TimeoutFunction(int64 timeout_id) {
72     // First clear the timeout that we got from the map of timeouts !
73     CHECK(timeout_closures_.erase(timeout_id));
74     callback_->Run(timeout_id);
75   }
76
77   Selector* const selector_;
78   TimeoutCallback* const callback_;
79
80   // Timeout callbacks - use them w/ SetTimeout/Unset
81   typedef map<int64, Closure*> ClosureMap;
82   ClosureMap timeout_closures_;
83 };
84 }
85
86 #endif  // __NET_BASE_TIMEOUTER_H__
Note: See TracBrowser for help on using the browser.