summaryrefslogtreecommitdiff
path: root/network.cpp
blob: 72001d4ea8b247014e4796c55810305984627ed4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// network.cpp - written and placed in the public domain by Wei Dai

#include "pch.h"
#include "network.h"

NAMESPACE_BEGIN(CryptoPP)

unsigned int NonblockingSource::PumpMessages2(unsigned int &messageCount, bool blocking)
{
	if (messageCount == 0)
		return 0;

	unsigned long byteCount = ULONG_MAX;
	messageCount = 0;
	RETURN_IF_NONZERO(Pump2(byteCount, blocking));
	if (!m_messageEndSent && SourceExhausted())
	{
		RETURN_IF_NONZERO(AttachedTransformation()->Put2(NULL, 0, GetAutoSignalPropagation(), true));
		m_messageEndSent = true;
		messageCount = 1;
	}
	return 0;
}

bool NonblockingSink::IsolatedFlush(bool hardFlush, bool blocking)
{
	TimedFlush(blocking ? INFINITE_TIME : 0);
	return hardFlush && !!GetCurrentBufferSize();
}

// *************************************************************

#ifdef HIGHRES_TIMER_AVAILABLE

NetworkSource::NetworkSource(BufferedTransformation *attachment)
	: NonblockingSource(attachment), m_buf(1024*4), m_bufSize(0), m_state(NORMAL)
{
}

unsigned int NetworkSource::GeneralPump2(unsigned long &byteCount, bool blockingOutput, unsigned long maxTime, bool checkDelimiter, byte delimiter)
{
	NetworkReceiver &receiver = AccessReceiver();

	unsigned long maxSize = byteCount;
	byteCount = 0;
	bool forever = maxTime == INFINITE_TIME;
	Timer timer(Timer::MILLISECONDS, forever);
	unsigned long timeout;
	BufferedTransformation *t = AttachedTransformation();

	if (m_state == OUTPUT_BLOCKED)
		goto DoOutput;

	while (true)
	{
		if (m_state == WAITING_FOR_RESULT)
		{
			if (receiver.MustWaitForResult())
			{
				timeout = SaturatingSubtract(maxTime, timer.ElapsedTime());
				if (!receiver.Wait(timeout))
					break;
			}

			unsigned int recvResult = receiver.GetReceiveResult();
//			assert(recvResult > 0 || receiver.EofReceived());
			m_bufSize += recvResult;
			m_state = NORMAL;
		}

		if (m_bufSize == 0)
		{
			if (receiver.EofReceived())
				break;
		}
		else
		{
			m_putSize = STDMIN((unsigned long)m_bufSize, maxSize - byteCount);
			if (checkDelimiter)
				m_putSize = std::find(m_buf.begin(), m_buf+m_putSize, delimiter) - m_buf;

DoOutput:
			unsigned int result = t->PutModifiable2(m_buf, m_putSize, 0, forever || blockingOutput);
			if (result)
			{
				timeout = SaturatingSubtract(maxTime, timer.ElapsedTime());
				if (t->Wait(timeout))
					goto DoOutput;
				else
				{
					m_state = OUTPUT_BLOCKED;
					return result;
				}
			}
			m_state = NORMAL;

			byteCount += m_putSize;
			m_bufSize -= m_putSize;
			if (m_bufSize > 0)
			{
				memmove(m_buf, m_buf+m_putSize, m_bufSize);
				if (checkDelimiter && m_buf[0] == delimiter)
					break;
			}
		}

		if (byteCount == maxSize)
			break;

		unsigned long elapsed = timer.ElapsedTime();
		if (elapsed > maxTime)
			break;	// once time limit is reached, return even if there is more data waiting

		if (receiver.MustWaitToReceive())
		{
			if (!receiver.Wait(maxTime - elapsed))
				break;
		}

		receiver.Receive(m_buf+m_bufSize, m_buf.size()-m_bufSize);
		m_state = WAITING_FOR_RESULT;
	}

	return 0;
}

// *************************************************************

unsigned int NetworkSink::Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking)
{
	if (m_blockedBytes)
	{
		assert(length >= m_blockedBytes);
		inString += length - m_blockedBytes;
		length = m_blockedBytes;
	}
	m_buffer.LazyPut(inString, length);

	unsigned int targetSize = messageEnd ? 0 : m_maxBufferSize;
	TimedFlush(blocking ? INFINITE_TIME : 0, m_autoFlush ? 0 : targetSize);

	if (m_buffer.CurrentSize() > targetSize)
	{
		assert(!blocking);
		m_blockedBytes = STDMIN(m_buffer.CurrentSize() - targetSize, (unsigned long)length);
		m_buffer.UndoLazyPut(m_blockedBytes);
		m_buffer.FinalizeLazyPut();
		return STDMAX(m_blockedBytes, 1U);
	}
	m_blockedBytes = 0;

	if (messageEnd)
		AccessSender().SendEof();
	return 0;
}

unsigned int NetworkSink::TimedFlush(unsigned long maxTime, unsigned int targetSize)
{
	if (m_buffer.IsEmpty())
		return 0;

	NetworkSender &sender = AccessSender();

	bool forever = maxTime == INFINITE_TIME;
	Timer timer(Timer::MILLISECONDS, forever);
	unsigned long timeout;
	unsigned int totalFlushSize = 0;

	while (true)
	{
		if (m_needSendResult)
		{
			if (sender.MustWaitForResult())
			{
				timeout = SaturatingSubtract(maxTime, timer.ElapsedTime());
				if (!sender.Wait(timeout))
					break;
			}

			unsigned int sendResult = sender.GetSendResult();
			m_buffer.Skip(sendResult);
			totalFlushSize += sendResult;
			m_needSendResult = false;

			if (m_buffer.CurrentSize() <= targetSize)
				break;
		}

		unsigned long elapsed = timer.ElapsedTime();
		if (elapsed > maxTime)
			break;	// once time limit is reached, return even if there is more data waiting

		if (sender.MustWaitToSend())
		{
			if (!sender.Wait(maxTime - elapsed))
				break;
		}

		unsigned int contiguousSize = 0;
		const byte *block = m_buffer.Spy(contiguousSize);

		sender.Send(block, contiguousSize);
		m_needSendResult = true;
	}

	return totalFlushSize;
}

#endif	// #ifdef HIGHRES_TIMER_AVAILABLE

NAMESPACE_END