~zooko/cryptopp/trunk

1 by weidai
Initial revision
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"
64 by weidai
sync with private branch
10
#include <winsock2.h>
1 by weidai
Initial revision
11
12
NAMESPACE_BEGIN(CryptoPP)
13
14
//! Windows Handle
15
class WindowsHandle
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
40
class WindowsPipe
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
173 by weidai
fix documentation, fix PanamaMAC, fix algorithm names
60
//! pipe-based implementation of NetworkReceiver
1 by weidai
Initial revision
61
class WindowsPipeReceiver : public WindowsPipe, public NetworkReceiver
62
{
63
public:
64
	WindowsPipeReceiver();
65
66
	bool MustWaitForResult() {return true;}
184 by weidai
port to MSVC .NET 2005 beta 2
67
	bool Receive(byte* buf, size_t bufLen);
1 by weidai
Initial revision
68
	unsigned int GetReceiveResult();
69
	bool EofReceived() const {return m_eofReceived;}
70
71
	unsigned int GetMaxWaitObjectCount() const {return 1;}
204 by weidai
merge in changes by denis bider and fix compile on gcc 3.4.4 and MSVC 6
72
	void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack);
1 by weidai
Initial revision
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
173 by weidai
fix documentation, fix PanamaMAC, fix algorithm names
82
//! pipe-based implementation of NetworkSender
1 by weidai
Initial revision
83
class WindowsPipeSender : public WindowsPipe, public NetworkSender
84
{
85
public:
86
	WindowsPipeSender();
87
88
	bool MustWaitForResult() {return true;}
184 by weidai
port to MSVC .NET 2005 beta 2
89
	void Send(const byte* buf, size_t bufLen);
1 by weidai
Initial revision
90
	unsigned int GetSendResult();
204 by weidai
merge in changes by denis bider and fix compile on gcc 3.4.4 and MSVC 6
91
	bool MustWaitForEof() { return false; }
1 by weidai
Initial revision
92
	void SendEof() {}
93
94
	unsigned int GetMaxWaitObjectCount() const {return 1;}
204 by weidai
merge in changes by denis bider and fix compile on gcc 3.4.4 and MSVC 6
95
	void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack);
1 by weidai
Initial revision
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
105
class WindowsPipeSource : public WindowsHandle, public NetworkSource, public WindowsPipeReceiver
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
115
	NetworkSource::GetMaxWaitObjectCount;
116
	NetworkSource::GetWaitObjects;
117
118
private:
119
	HANDLE GetHandle() const {return WindowsHandle::GetHandle();}
120
	NetworkReceiver & AccessReceiver() {return *this;}
121
};
122
123
//! Windows Pipe Sink
124
class WindowsPipeSink : public WindowsHandle, public NetworkSink, public WindowsPipeSender
125
{
126
public:
69 by weidai
sync with private branch
127
	WindowsPipeSink(HANDLE h=INVALID_HANDLE_VALUE, unsigned int maxBufferSize=0, unsigned int autoFlushBound=16*1024)
128
		: WindowsHandle(h), NetworkSink(maxBufferSize, autoFlushBound) {}
1 by weidai
Initial revision
129
130
	NetworkSink::GetMaxWaitObjectCount;
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