00001 #ifndef CRYPTOPP_WINPIPES_H
00002 #define CRYPTOPP_WINPIPES_H
00003
00004 #ifdef WINDOWS_PIPES_AVAILABLE
00005
00006 #include "cryptlib.h"
00007 #include "network.h"
00008 #include "queue.h"
00009 #include <winsock2.h>
00010
00011 NAMESPACE_BEGIN(CryptoPP)
00012
00013
00014 class WindowsHandle
00015 {
00016 public:
00017 WindowsHandle(HANDLE h = INVALID_HANDLE_VALUE, bool own=false);
00018 WindowsHandle(const WindowsHandle &h) : m_h(h.m_h), m_own(false) {}
00019 virtual ~WindowsHandle();
00020
00021 bool GetOwnership() const {return m_own;}
00022 void SetOwnership(bool own) {m_own = own;}
00023
00024 operator HANDLE() const {return m_h;}
00025 HANDLE GetHandle() const {return m_h;}
00026 bool HandleValid() const;
00027 void AttachHandle(HANDLE h, bool own=false);
00028 HANDLE DetachHandle();
00029 void CloseHandle();
00030
00031 protected:
00032 virtual void HandleChanged() {}
00033
00034 HANDLE m_h;
00035 bool m_own;
00036 };
00037
00038
00039 class WindowsPipe
00040 {
00041 public:
00042 class Err : public OS_Error
00043 {
00044 public:
00045 Err(HANDLE h, const std::string& operation, int error);
00046 HANDLE GetHandle() const {return m_h;}
00047
00048 private:
00049 HANDLE m_h;
00050 };
00051
00052 protected:
00053 virtual HANDLE GetHandle() const =0;
00054 virtual void HandleError(const char *operation) const;
00055 void CheckAndHandleError(const char *operation, BOOL result) const
00056 {assert(result==TRUE || result==FALSE); if (!result) HandleError(operation);}
00057 };
00058
00059
00060 class WindowsPipeReceiver : public WindowsPipe, public NetworkReceiver
00061 {
00062 public:
00063 WindowsPipeReceiver();
00064
00065 bool MustWaitForResult() {return true;}
00066 bool Receive(byte* buf, size_t bufLen);
00067 unsigned int GetReceiveResult();
00068 bool EofReceived() const {return m_eofReceived;}
00069
00070 HANDLE GetHandle() const {return m_event;}
00071 unsigned int GetMaxWaitObjectCount() const {return 1;}
00072 void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack);
00073
00074 private:
00075 WindowsHandle m_event;
00076 OVERLAPPED m_overlapped;
00077 bool m_resultPending;
00078 DWORD m_lastResult;
00079 bool m_eofReceived;
00080 };
00081
00082
00083 class WindowsPipeSender : public WindowsPipe, public NetworkSender
00084 {
00085 public:
00086 WindowsPipeSender();
00087
00088 bool MustWaitForResult() {return true;}
00089 void Send(const byte* buf, size_t bufLen);
00090 unsigned int GetSendResult();
00091 bool MustWaitForEof() { return false; }
00092 void SendEof() {}
00093
00094 HANDLE GetHandle() const {return m_event;}
00095 unsigned int GetMaxWaitObjectCount() const {return 1;}
00096 void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack);
00097
00098 private:
00099 WindowsHandle m_event;
00100 OVERLAPPED m_overlapped;
00101 bool m_resultPending;
00102 DWORD m_lastResult;
00103 };
00104
00105
00106 class WindowsPipeSource : public WindowsHandle, public NetworkSource, public WindowsPipeReceiver
00107 {
00108 public:
00109 WindowsPipeSource(HANDLE h=INVALID_HANDLE_VALUE, bool pumpAll=false, BufferedTransformation *attachment=NULL)
00110 : WindowsHandle(h), NetworkSource(attachment)
00111 {
00112 if (pumpAll)
00113 PumpAll();
00114 }
00115
00116 using NetworkSource::GetMaxWaitObjectCount;
00117 using NetworkSource::GetWaitObjects;
00118
00119 private:
00120 HANDLE GetHandle() const {return WindowsHandle::GetHandle();}
00121 NetworkReceiver & AccessReceiver() {return *this;}
00122 };
00123
00124
00125 class WindowsPipeSink : public WindowsHandle, public NetworkSink, public WindowsPipeSender
00126 {
00127 public:
00128 WindowsPipeSink(HANDLE h=INVALID_HANDLE_VALUE, unsigned int maxBufferSize=0, unsigned int autoFlushBound=16*1024)
00129 : WindowsHandle(h), NetworkSink(maxBufferSize, autoFlushBound) {}
00130
00131 using NetworkSink::GetMaxWaitObjectCount;
00132 using NetworkSink::GetWaitObjects;
00133
00134 private:
00135 HANDLE GetHandle() const {return WindowsHandle::GetHandle();}
00136 NetworkSender & AccessSender() {return *this;}
00137 };
00138
00139 NAMESPACE_END
00140
00141 #endif // WINDOWS_PIPES_AVAILABLE
00142
00143 #endif