Crypto++
winpipes.h
1 #ifndef CRYPTOPP_WINPIPES_H
2 #define CRYPTOPP_WINPIPES_H
3 
4 #include "config.h"
5 
6 #ifdef WINDOWS_PIPES_AVAILABLE
7 
8 #include "network.h"
9 #include "queue.h"
10 #include <winsock2.h>
11 
12 NAMESPACE_BEGIN(CryptoPP)
13 
14 //! Windows Handle
16 {
17 public:
18  WindowsHandle(HANDLE h = INVALID_HANDLE_VALUE, bool own=false);
19  WindowsHandle(const WindowsHandle &h) : m_h(h.m_h), m_own(false) {}
20  virtual ~WindowsHandle();
21 
22  bool GetOwnership() const {return m_own;}
23  void SetOwnership(bool own) {m_own = own;}
24 
25  operator HANDLE() {return m_h;}
26  HANDLE GetHandle() const {return m_h;}
27  bool HandleValid() const;
28  void AttachHandle(HANDLE h, bool own=false);
29  HANDLE DetachHandle();
30  void CloseHandle();
31 
32 protected:
33  virtual void HandleChanged() {}
34 
35  HANDLE m_h;
36  bool m_own;
37 };
38 
39 //! Windows Pipe
41 {
42 public:
43  class Err : public OS_Error
44  {
45  public:
46  Err(HANDLE h, const std::string& operation, int error);
47  HANDLE GetHandle() const {return m_h;}
48 
49  private:
50  HANDLE m_h;
51  };
52 
53 protected:
54  virtual HANDLE GetHandle() const =0;
55  virtual void HandleError(const char *operation) const;
56  void CheckAndHandleError(const char *operation, BOOL result) const
57  {assert(result==TRUE || result==FALSE); if (!result) HandleError(operation);}
58 };
59 
60 //! pipe-based implementation of NetworkReceiver
62 {
63 public:
65 
66  bool MustWaitForResult() {return true;}
67  bool Receive(byte* buf, size_t bufLen);
68  unsigned int GetReceiveResult();
69  bool EofReceived() const {return m_eofReceived;}
70 
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
84 {
85 public:
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  unsigned int GetMaxWaitObjectCount() const {return 1;}
95  void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack);
96 
97 private:
98  WindowsHandle m_event;
99  OVERLAPPED m_overlapped;
100  bool m_resultPending;
101  DWORD m_lastResult;
102 };
103 
104 //! Windows Pipe Source
106 {
107 public:
108  WindowsPipeSource(HANDLE h=INVALID_HANDLE_VALUE, bool pumpAll=false, BufferedTransformation *attachment=NULL)
109  : WindowsHandle(h), NetworkSource(attachment)
110  {
111  if (pumpAll)
112  PumpAll();
113  }
114 
116  NetworkSource::GetWaitObjects;
117 
118 private:
119  HANDLE GetHandle() const {return WindowsHandle::GetHandle();}
120  NetworkReceiver & AccessReceiver() {return *this;}
121 };
122 
123 //! Windows Pipe Sink
125 {
126 public:
127  WindowsPipeSink(HANDLE h=INVALID_HANDLE_VALUE, unsigned int maxBufferSize=0, unsigned int autoFlushBound=16*1024)
128  : WindowsHandle(h), NetworkSink(maxBufferSize, autoFlushBound) {}
129 
131  NetworkSink::GetWaitObjects;
132 
133 private:
134  HANDLE GetHandle() const {return WindowsHandle::GetHandle();}
135  NetworkSender & AccessSender() {return *this;}
136 };
137 
138 NAMESPACE_END
139 
140 #endif
141 
142 #endif