Crypto++  5.6.3
Free C++ class library of cryptographic schemes
wait.h
1 #ifndef CRYPTOPP_WAIT_H
2 #define CRYPTOPP_WAIT_H
3 
4 #include "config.h"
5 
6 #ifdef SOCKETS_AVAILABLE
7 
8 #include "misc.h"
9 #include "cryptlib.h"
10 #include <vector>
11 
12 #ifdef USE_WINDOWS_STYLE_SOCKETS
13 #include <winsock2.h>
14 #else
15 #include <sys/types.h>
16 #endif
17 
18 #if defined(__ANDROID__)
19 #include <sys/select.h>
20 #endif
21 
22 #include "hrtimer.h"
23 
24 NAMESPACE_BEGIN(CryptoPP)
25 
26 class Tracer
27 {
28 public:
29  Tracer(unsigned int level) : m_level(level) {}
30  virtual ~Tracer() {}
31 
32 protected:
33  //! Override this in your most-derived tracer to do the actual tracing.
34  virtual void Trace(unsigned int n, std::string const& s) = 0;
35 
36  /*! By default, tracers will decide which trace messages to trace according to a trace level
37  mechanism. If your most-derived tracer uses a different mechanism, override this to
38  return false. If this method returns false, the default TraceXxxx(void) methods will all
39  return 0 and must be overridden explicitly by your tracer for trace messages you want. */
40  virtual bool UsingDefaults() const { return true; }
41 
42 protected:
43  unsigned int m_level;
44 
45  void TraceIf(unsigned int n, std::string const&s)
46  { if (n) Trace(n, s); }
47 
48  /*! Returns nr if, according to the default log settings mechanism (using log levels),
49  the message should be traced. Returns 0 if the default trace level mechanism is not
50  in use, or if it is in use but the event should not be traced. Provided as a utility
51  method for easier and shorter coding of default TraceXxxx(void) implementations. */
52  unsigned int Tracing(unsigned int nr, unsigned int minLevel) const
53  { return (UsingDefaults() && m_level >= minLevel) ? nr : 0; }
54 };
55 
56 // Your Tracer-derived class should inherit as virtual public from Tracer or another
57 // Tracer-derived class, and should pass the log level in its constructor. You can use the
58 // following methods to begin and end your Tracer definition.
59 
60 // This constructor macro initializes Tracer directly even if not derived directly from it;
61 // this is intended, virtual base classes are always initialized by the most derived class.
62 #define CRYPTOPP_TRACER_CONSTRUCTOR(DERIVED) \
63  public: DERIVED(unsigned int level = 0) : Tracer(level) {}
64 
65 #define CRYPTOPP_BEGIN_TRACER_CLASS_1(DERIVED, BASE1) \
66  class DERIVED : virtual public BASE1, public NotCopyable { CRYPTOPP_TRACER_CONSTRUCTOR(DERIVED)
67 
68 #define CRYPTOPP_BEGIN_TRACER_CLASS_2(DERIVED, BASE1, BASE2) \
69  class DERIVED : virtual public BASE1, virtual public BASE2, public NotCopyable { CRYPTOPP_TRACER_CONSTRUCTOR(DERIVED)
70 
71 #define CRYPTOPP_END_TRACER_CLASS };
72 
73 // In your Tracer-derived class, you should define a globally unique event number for each
74 // new event defined. This can be done using the following macros.
75 
76 #define CRYPTOPP_BEGIN_TRACER_EVENTS(UNIQUENR) enum { EVENTBASE = UNIQUENR,
77 #define CRYPTOPP_TRACER_EVENT(EVENTNAME) EventNr_##EVENTNAME,
78 #define CRYPTOPP_END_TRACER_EVENTS };
79 
80 // In your own Tracer-derived class, you must define two methods per new trace event type:
81 // - unsigned int TraceXxxx() const
82 // Your default implementation of this method should return the event number if according
83 // to the default trace level system the event should be traced, or 0 if it should not.
84 // - void TraceXxxx(string const& s)
85 // This method should call TraceIf(TraceXxxx(), s); to do the tracing.
86 // For your convenience, a macro to define these two types of methods are defined below.
87 // If you use this macro, you should also use the TRACER_EVENTS macros above to associate
88 // event names with numbers.
89 
90 #define CRYPTOPP_TRACER_EVENT_METHODS(EVENTNAME, LOGLEVEL) \
91  virtual unsigned int Trace##EVENTNAME() const { return Tracing(EventNr_##EVENTNAME, LOGLEVEL); } \
92  virtual void Trace##EVENTNAME(std::string const& s) { TraceIf(Trace##EVENTNAME(), s); }
93 
94 
95 /*! A simple unidirectional linked list with m_prev == 0 to indicate the final entry.
96  The aim of this implementation is to provide a very lightweight and practical
97  tracing mechanism with a low performance impact. Functions and methods supporting
98  this call-stack mechanism would take a parameter of the form "CallStack const& callStack",
99  and would pass this parameter to subsequent functions they call using the construct:
100 
101  SubFunc(arg1, arg2, CallStack("my func at place such and such", &callStack));
102 
103  The advantage of this approach is that it is easy to use and should be very efficient,
104  involving no allocation from the heap, just a linked list of stack objects containing
105  pointers to static ASCIIZ strings (or possibly additional but simple data if derived). */
107 {
108 public:
109  CallStack(char const* i, CallStack const* p) : m_info(i), m_prev(p) {}
110  CallStack const* Prev() const { return m_prev; }
111  virtual std::string Format() const;
112 
113 protected:
114  char const* m_info;
115  CallStack const* m_prev;
116 };
117 
118 /*! An extended CallStack entry type with an additional numeric parameter. */
120 {
121 public:
122  CallStackWithNr(char const* i, word32 n, CallStack const* p) : CallStack(i, p), m_nr(n) {}
123  std::string Format() const;
124 
125 protected:
126  word32 m_nr;
127 };
128 
129 /*! An extended CallStack entry type with an additional string parameter. */
131 {
132 public:
133  CallStackWithStr(char const* i, char const* z, CallStack const* p) : CallStack(i, p), m_z(z) {}
134  std::string Format() const;
135 
136 protected:
137  char const* m_z;
138 };
139 
140 // Thanks to Maximilian Zamorsky for help with http://connect.microsoft.com/VisualStudio/feedback/details/1570496/
141 CRYPTOPP_BEGIN_TRACER_CLASS_1(WaitObjectsTracer, Tracer)
142  CRYPTOPP_BEGIN_TRACER_EVENTS(0x48752841)
143  CRYPTOPP_TRACER_EVENT(NoWaitLoop)
144  CRYPTOPP_END_TRACER_EVENTS
145  CRYPTOPP_TRACER_EVENT_METHODS(NoWaitLoop, 1)
146 CRYPTOPP_END_TRACER_CLASS
147 
148 struct WaitingThreadData;
149 
150 //! container of wait objects
152 {
153 public:
154  //! exception thrown by WaitObjectContainer
155  class Err : public Exception
156  {
157  public:
158  Err(const std::string& s) : Exception(IO_ERROR, s) {}
159  };
160 
161  static unsigned int MaxWaitObjects();
162 
164 
165  void Clear();
166  void SetNoWait(CallStack const& callStack);
167  void ScheduleEvent(double milliseconds, CallStack const& callStack);
168  // returns false if timed out
169  bool Wait(unsigned long milliseconds);
170 
171 #ifdef USE_WINDOWS_STYLE_SOCKETS
172 # ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
173  virtual ~WaitObjectContainer();
174 # else
176 #endif
177  void AddHandle(HANDLE handle, CallStack const& callStack);
178 #else
179  void AddReadFd(int fd, CallStack const& callStack);
180  void AddWriteFd(int fd, CallStack const& callStack);
181 #endif
182 
183 private:
184  WaitObjectsTracer* m_tracer;
185 
186 #ifdef USE_WINDOWS_STYLE_SOCKETS
187  void CreateThreads(unsigned int count);
188  std::vector<HANDLE> m_handles;
189  std::vector<WaitingThreadData *> m_threads;
190  HANDLE m_startWaiting;
191  HANDLE m_stopWaiting;
192 #else
193  fd_set m_readfds, m_writefds;
194  int m_maxFd;
195 #endif
196  bool m_noWait;
197  double m_firstEventTime;
198  Timer m_eventTimer;
199 
200 #ifdef USE_WINDOWS_STYLE_SOCKETS
201  typedef size_t LastResultType;
202 #else
203  typedef int LastResultType;
204 #endif
205  enum { LASTRESULT_NOWAIT = -1, LASTRESULT_SCHEDULED = -2, LASTRESULT_TIMEOUT = -3 };
206  LastResultType m_lastResult;
207  unsigned int m_sameResultCount;
208  Timer m_noWaitTimer;
209  void SetLastResult(LastResultType result);
210  void DetectNoWait(LastResultType result, CallStack const& callStack);
211 };
212 
213 NAMESPACE_END
214 
215 #endif
216 
217 #endif
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:139
container of wait objects
Definition: wait.h:151
high resolution timer
Definition: hrtimer.h:53
Utility functions for the Crypto++ library.
Abstract base classes that provide a uniform interface to this library.
Library configuration file.
Definition: wait.h:26
exception thrown by WaitObjectContainer
Definition: wait.h:155
Crypto++ library namespace.
Ensures an object is not copyable.
Definition: misc.h:189