Fabcoin Core  0.16.2
P2P Digital Currency
winpipes.h
Go to the documentation of this file.
1 #ifndef CRYPTOPP_WINPIPES_H
2 #define CRYPTOPP_WINPIPES_H
3 
4 #include "config.h"
5 
6 #if !defined(NO_OS_DEPENDENCE) && defined(WINDOWS_PIPES_AVAILABLE)
7 
8 #include "cryptlib.h"
9 #include "network.h"
10 #include "queue.h"
11 #include <winsock2.h>
12 
14 
15 class WindowsHandle
17 {
18 public:
19  virtual ~WindowsHandle();
20 
21  WindowsHandle(HANDLE h = INVALID_HANDLE_VALUE, bool own=false);
22  WindowsHandle(const WindowsHandle &h) : m_h(h.m_h), m_own(false) {}
23 
24  bool GetOwnership() const {return m_own;}
25  void SetOwnership(bool own) {m_own = own;}
26 
27  operator HANDLE() const {return m_h;}
28  HANDLE GetHandle() const {return m_h;}
29  bool HandleValid() const;
30  void AttachHandle(HANDLE h, bool own=false);
31  HANDLE DetachHandle();
32  void CloseHandle();
33 
34 protected:
35  virtual void HandleChanged() {}
36 
37  HANDLE m_h;
38  bool m_own;
39 };
40 
42 class WindowsPipe
43 {
44 public:
45  class Err : public OS_Error
46  {
47  public:
48  Err(HANDLE h, const std::string& operation, int error);
49  HANDLE GetHandle() const {return m_h;}
50 
51  private:
52  HANDLE m_h;
53  };
54 
55 protected:
56  virtual HANDLE GetHandle() const =0;
57  virtual void HandleError(const char *operation) const;
58  void CheckAndHandleError(const char *operation, BOOL result) const
59  {if (!result) HandleError(operation);}
60 };
61 
63 class WindowsPipeReceiver : public WindowsPipe, public NetworkReceiver
64 {
65 public:
66  WindowsPipeReceiver();
67 
68  bool MustWaitForResult() {return true;}
69  bool Receive(byte* buf, size_t bufLen);
70  unsigned int GetReceiveResult();
71  bool EofReceived() const {return m_eofReceived;}
72 
73  HANDLE GetHandle() const {return m_event;}
74  unsigned int GetMaxWaitObjectCount() const {return 1;}
75  void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack);
76 
77 private:
78  WindowsHandle m_event;
79  OVERLAPPED m_overlapped;
80  DWORD m_lastResult;
81  bool m_resultPending;
82  bool m_eofReceived;
83 };
84 
86 class WindowsPipeSender : public WindowsPipe, public NetworkSender
87 {
88 public:
89  WindowsPipeSender();
90 
91  bool MustWaitForResult() {return true;}
92  void Send(const byte* buf, size_t bufLen);
93  unsigned int GetSendResult();
94  bool MustWaitForEof() { return false; }
95  void SendEof() {}
96 
97  HANDLE GetHandle() const {return m_event;}
98  unsigned int GetMaxWaitObjectCount() const {return 1;}
99  void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack);
100 
101 private:
102  WindowsHandle m_event;
103  OVERLAPPED m_overlapped;
104  DWORD m_lastResult;
105  bool m_resultPending;
106 };
107 
109 class WindowsPipeSource : public WindowsHandle, public NetworkSource, public WindowsPipeReceiver
110 {
111 public:
112  WindowsPipeSource(HANDLE h=INVALID_HANDLE_VALUE, bool pumpAll=false, BufferedTransformation *attachment=NULL)
113  : WindowsHandle(h), NetworkSource(attachment)
114  {
115  if (pumpAll)
116  PumpAll();
117  }
118 
119  using NetworkSource::GetMaxWaitObjectCount;
120  using NetworkSource::GetWaitObjects;
121 
122 private:
123  HANDLE GetHandle() const {return WindowsHandle::GetHandle();}
124  NetworkReceiver & AccessReceiver() {return *this;}
125 };
126 
128 class WindowsPipeSink : public WindowsHandle, public NetworkSink, public WindowsPipeSender
129 {
130 public:
131  WindowsPipeSink(HANDLE h=INVALID_HANDLE_VALUE, unsigned int maxBufferSize=0, unsigned int autoFlushBound=16*1024)
132  : WindowsHandle(h), NetworkSink(maxBufferSize, autoFlushBound) {}
133 
134  using NetworkSink::GetMaxWaitObjectCount;
135  using NetworkSink::GetWaitObjects;
136 
137 private:
138  HANDLE GetHandle() const {return WindowsHandle::GetHandle();}
139  NetworkSender & AccessSender() {return *this;}
140 };
141 
143 
144 #endif // WINDOWS_PIPES_AVAILABLE
145 
146 #endif
bool error(const char *fmt, const Args &...args)
Definition: util.h:178
uint8_t byte
Definition: Common.h:57
The operating system reported an error.
Definition: cryptlib.h:219
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
#define h(i)
Definition: sha.cpp:736
Abstract base classes that provide a uniform interface to this library.
Library configuration file.
void HandleError(const leveldb::Status &status)
Handle database error by throwing dbwrapper_error exception.
Definition: dbwrapper.cpp:199
Interface for buffered transformations.
Definition: cryptlib.h:1352
Classes for an unlimited queue to store bytes.
#define NAMESPACE_END
Definition: config.h:201