Crypto++  5.6.3
Free C++ class library of cryptographic schemes
winpipes.h
1 #ifndef CRYPTOPP_WINPIPES_H
2 #define CRYPTOPP_WINPIPES_H
3 
4 #ifdef WINDOWS_PIPES_AVAILABLE
5 
6 #include "cryptlib.h"
7 #include "network.h"
8 #include "queue.h"
9 #include <winsock2.h>
10 
11 NAMESPACE_BEGIN(CryptoPP)
12 
13 //! Windows Handle
14 class WindowsHandle
15 {
16 public:
17  WindowsHandle(HANDLE h = INVALID_HANDLE_VALUE, bool own=false);
18  WindowsHandle(const WindowsHandle &h) : m_h(h.m_h), m_own(false) {}
19  virtual ~WindowsHandle();
20 
21  bool GetOwnership() const {return m_own;}
22  void SetOwnership(bool own) {m_own = own;}
23 
24  operator HANDLE() const {return m_h;}
25  HANDLE GetHandle() const {return m_h;}
26  bool HandleValid() const;
27  void AttachHandle(HANDLE h, bool own=false);
28  HANDLE DetachHandle();
29  void CloseHandle();
30 
31 protected:
32  virtual void HandleChanged() {}
33 
34  HANDLE m_h;
35  bool m_own;
36 };
37 
38 //! Windows Pipe
39 class WindowsPipe
40 {
41 public:
42  class Err : public OS_Error
43  {
44  public:
45  Err(HANDLE h, const std::string& operation, int error);
46  HANDLE GetHandle() const {return m_h;}
47 
48  private:
49  HANDLE m_h;
50  };
51 
52 protected:
53  virtual HANDLE GetHandle() const =0;
54  virtual void HandleError(const char *operation) const;
55  void CheckAndHandleError(const char *operation, BOOL result) const
56  {assert(result==TRUE || result==FALSE); if (!result) HandleError(operation);}
57 };
58 
59 //! pipe-based implementation of NetworkReceiver
60 class WindowsPipeReceiver : public WindowsPipe, public NetworkReceiver
61 {
62 public:
63  WindowsPipeReceiver();
64 
65  bool MustWaitForResult() {return true;}
66  bool Receive(byte* buf, size_t bufLen);
67  unsigned int GetReceiveResult();
68  bool EofReceived() const {return m_eofReceived;}
69 
70  HANDLE GetHandle() const {return m_event;}
71  unsigned int GetMaxWaitObjectCount() const {return 1;}
72  void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack);
73 
74 private:
75  WindowsHandle m_event;
76  OVERLAPPED m_overlapped;
77  bool m_resultPending;
78  DWORD m_lastResult;
79  bool m_eofReceived;
80 };
81 
82 //! pipe-based implementation of NetworkSender
83 class WindowsPipeSender : public WindowsPipe, public NetworkSender
84 {
85 public:
86  WindowsPipeSender();
87 
88  bool MustWaitForResult() {return true;}
89  void Send(const byte* buf, size_t bufLen);
90  unsigned int GetSendResult();
91  bool MustWaitForEof() { return false; }
92  void SendEof() {}
93 
94  HANDLE GetHandle() const {return m_event;}
95  unsigned int GetMaxWaitObjectCount() const {return 1;}
96  void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack);
97 
98 private:
99  WindowsHandle m_event;
100  OVERLAPPED m_overlapped;
101  bool m_resultPending;
102  DWORD m_lastResult;
103 };
104 
105 //! Windows Pipe Source
106 class WindowsPipeSource : public WindowsHandle, public NetworkSource, public WindowsPipeReceiver
107 {
108 public:
109  WindowsPipeSource(HANDLE h=INVALID_HANDLE_VALUE, bool pumpAll=false, BufferedTransformation *attachment=NULL)
110  : WindowsHandle(h), NetworkSource(attachment)
111  {
112  if (pumpAll)
113  PumpAll();
114  }
115 
118 
119 private:
120  HANDLE GetHandle() const {return WindowsHandle::GetHandle();}
121  NetworkReceiver & AccessReceiver() {return *this;}
122 };
123 
124 //! Windows Pipe Sink
125 class WindowsPipeSink : public WindowsHandle, public NetworkSink, public WindowsPipeSender
126 {
127 public:
128  WindowsPipeSink(HANDLE h=INVALID_HANDLE_VALUE, unsigned int maxBufferSize=0, unsigned int autoFlushBound=16*1024)
129  : WindowsHandle(h), NetworkSink(maxBufferSize, autoFlushBound) {}
130 
133 
134 private:
135  HANDLE GetHandle() const {return WindowsHandle::GetHandle();}
136  NetworkSender & AccessSender() {return *this;}
137 };
138 
139 NAMESPACE_END
140 
141 #endif // WINDOWS_PIPES_AVAILABLE
142 
143 #endif
Network Source.
Definition: network.h:165
container of wait objects
Definition: wait.h:151
The operating system reported an error.
Definition: cryptlib.h:217
void PumpAll()
Pump all data to attached transformation.
Definition: filters.h:1157
Abstract base classes that provide a uniform interface to this library.
void GetWaitObjects(WaitObjectContainer &container, CallStack const &callStack)
Retrieves waitable objects.
Definition: network.cpp:245
Interface for buffered transformations.
Definition: cryptlib.h:1342
unsigned int GetMaxWaitObjectCount() const
Retrieves the maximum number of waitable objects.
Definition: network.cpp:404
Classes for an unlimited queue to store bytes.
virtual void GetWaitObjects(WaitObjectContainer &container, CallStack const &callStack)=0
Retrieves waitable objects.
void GetWaitObjects(WaitObjectContainer &container, CallStack const &callStack)
Retrieves waitable objects.
Definition: network.cpp:409
unsigned int GetMaxWaitObjectCount() const
Retrieves the maximum number of waitable objects.
Definition: network.cpp:238
virtual bool Receive(byte *buf, size_t bufLen)=0
receive data from network source, returns whether result is immediately available ...
Crypto++ library namespace.
Network Receiver.
Definition: network.h:94
virtual unsigned int GetMaxWaitObjectCount() const =0
Maximum number of wait objects that this object can return.
Network Sink.
Definition: network.h:188
Network Sender.
Definition: network.h:152