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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
/*
* NetworkTransacrion.cpp
*
* Created on: 23 Dec 2016
* Author: David
*/
#include "NetworkTransaction.h"
#include "Network.h"
#include "RepRap.h"
#include "Socket.h"
#include <cstdarg>
//***************************************************************************************************
// NetworkTransaction class
NetworkTransaction *NetworkTransaction::freelist = nullptr;
NetworkTransaction::NetworkTransaction(NetworkTransaction *n)
: next(n), fileBuffer(nullptr), sendBuffer(nullptr), sendStack(new OutputStack()), fileBeingSent(nullptr), status(TransactionStatus::released)
{
}
void NetworkTransaction::Set(Socket *skt, TransactionStatus s)
{
cs = skt;
status = s;
closeRequested = false;
}
bool NetworkTransaction::HasMoreDataToRead() const
{
return cs != nullptr && cs->HasMoreDataToRead();
}
bool NetworkTransaction::IsConnected() const
{
return cs != nullptr && cs->IsConnected();
}
bool NetworkTransaction::IsSending() const
{
return status == TransactionStatus::sending || status == TransactionStatus::finished;
}
bool NetworkTransaction::CanWrite() const
{
return status != TransactionStatus::released && cs != nullptr && cs->CanWrite();
}
// Read one char from the NetworkTransaction
bool NetworkTransaction::Read(char& b)
{
if (cs == nullptr)
{
b = 0;
return false;
}
return cs->ReadChar(b);
}
// Read data from the NetworkTransaction and return true on success
bool NetworkTransaction::ReadBuffer(const char *&buffer, size_t &len)
{
return cs != NoConnection && cs->ReadBuffer(buffer, len);
}
void NetworkTransaction::Write(char b)
{
if (CanWrite())
{
if (sendBuffer == nullptr && !OutputBuffer::Allocate(sendBuffer))
{
// Should never get here
return;
}
sendBuffer->cat(b);
}
}
void NetworkTransaction::Write(const char* s)
{
if (CanWrite())
{
if (sendBuffer == nullptr && !OutputBuffer::Allocate(sendBuffer))
{
// Should never get here
return;
}
sendBuffer->cat(s);
}
}
void NetworkTransaction::Write(StringRef ref)
{
Write(ref.Pointer(), ref.strlen());
}
void NetworkTransaction::Write(const char* s, size_t len)
{
if (CanWrite())
{
if (sendBuffer == nullptr && !OutputBuffer::Allocate(sendBuffer))
{
// Should never get here
return;
}
sendBuffer->cat(s, len);
}
}
void NetworkTransaction::Write(OutputBuffer *buffer)
{
if (CanWrite())
{
// Note we use an individual stack here, because we don't want to link different
// OutputBuffers for different destinations together...
sendStack->Push(buffer);
}
else
{
// Don't keep buffers we can't send...
OutputBuffer::ReleaseAll(buffer);
}
}
void NetworkTransaction::Write(OutputStack *stack)
{
if (stack != nullptr)
{
if (CanWrite())
{
sendStack->Append(stack);
}
else
{
stack->ReleaseAll();
}
}
}
void NetworkTransaction::Printf(const char* fmt, ...)
{
if (CanWrite() && (sendBuffer != nullptr || OutputBuffer::Allocate(sendBuffer)))
{
va_list p;
va_start(p, fmt);
sendBuffer->vprintf(fmt, p);
va_end(p);
}
}
void NetworkTransaction::SetFileToWrite(FileStore *file)
{
if (CanWrite())
{
fileBeingSent = file;
}
else if (file != nullptr)
{
// debugPrintf("Want to write file but can't write\n");
file->Close();
}
}
// Get some data to send up to the specified length, which must be nonzero.
// A null return means there is nothing left to send or we couldn't allocate a buffer.
// If there was nothing left to send then the status is changed to 'finished'.
const uint8_t *NetworkTransaction::GetDataToSend(size_t& length)
{
// Discard any empty output buffers
while (sendBuffer != nullptr && sendBuffer->BytesLeft() == 0)
{
sendBuffer = OutputBuffer::Release(sendBuffer);
if (sendBuffer == nullptr)
{
sendBuffer = sendStack->Pop();
}
}
// If we have an output buffer, send it
if (sendBuffer != nullptr)
{
if (length > sendBuffer->BytesLeft())
{
length = sendBuffer->BytesLeft();
}
return reinterpret_cast<const uint8_t*>(sendBuffer->Read(length));
}
// If we have a file to send, send it
if (fileBeingSent != nullptr && fileBuffer == nullptr)
{
fileBuffer = NetworkBuffer::Allocate();
if (fileBuffer == nullptr)
{
return nullptr; // no buffer available
}
}
// If we have a file buffer here, we must be in the process of sending a file
if (fileBuffer != nullptr)
{
if (fileBuffer->IsEmpty() && fileBeingSent != nullptr)
{
const int bytesRead = fileBuffer->ReadFromFile(fileBeingSent);
if (bytesRead != (int)NetworkBuffer::bufferSize)
{
fileBeingSent->Close();
fileBeingSent = nullptr;
}
}
if (!fileBuffer->IsEmpty())
{
if (length > fileBuffer->Remaining())
{
length = fileBuffer->Remaining();
}
return fileBuffer->TakeData(length);
}
fileBuffer->Release();
fileBuffer = nullptr;
}
status = TransactionStatus::finished;
return nullptr;
}
// This is called by the Webserver to send output data to a client. If keepConnectionAlive is set to false,
// the current connection will be terminated once everything has been sent.
void NetworkTransaction::Commit(bool keepConnectionAlive)
{
closeAfterSending = !keepConnectionAlive;
if (sendBuffer == nullptr)
{
sendBuffer = sendStack->Pop();
}
status = TransactionStatus::sending;
}
// Call this to perform some networking tasks while processing deferred requests,
// and to move this transaction and all transactions that are associated with its
// connection to the end of readyTransactions. There are three ways to do this:
//
// 1) DeferOnly: Do not modify any of the processed data and don't send an ACK.
// This will ensure that zero-window packets are sent back to the client
// 2) ResetData: Reset the read pointers and acknowledge that the data has been processed
// 3) DiscardData: Free the processed data, acknowledge it and append this transaction as
// an empty item again without payload
//
void NetworkTransaction::Defer(DeferralMode mode)
{
if (mode == DeferralMode::DiscardData)
{
cs->DiscardReceivedData(); // discard the incoming data, because we don't need to process it any more
}
status = TransactionStatus::deferred;
reprap.GetNetwork()->Defer(this);
}
// This method should be called if we don't want to send data to the client and if we
// don't want to interfere with the connection state. May also be called from ISR!
void NetworkTransaction::Discard()
{
status = TransactionStatus::finished;
cs->ReleaseTransaction();
}
uint32_t NetworkTransaction::GetRemoteIP() const
{
return (cs != nullptr) ? cs->GetRemoteIP() : 0;
}
Port NetworkTransaction::GetRemotePort() const
{
return (cs != nullptr) ? cs->GetRemotePort() : 0;
}
Port NetworkTransaction::GetLocalPort() const
{
return (cs != nullptr) ? cs->GetLocalPort() : 0;
}
// Create the specified number of buffers and put them on the freelist
/*static*/ void NetworkTransaction::AllocateTransactions(unsigned int number)
{
if (freelist == nullptr)
{
while (number != 0)
{
freelist = new NetworkTransaction(freelist);
--number;
}
}
}
// Allocate a buffer from the freelist
/*static*/ NetworkTransaction *NetworkTransaction::Allocate()
{
NetworkTransaction * const ret = freelist;
if (ret != nullptr)
{
freelist = ret->next;
ret->next = nullptr;
ret->fileBuffer = nullptr;
ret->fileBeingSent = nullptr;
}
return ret;
}
// Free resources and recycle this buffer
NetworkTransaction *NetworkTransaction::Release()
{
if (fileBuffer != nullptr)
{
fileBuffer->Release();
fileBuffer = nullptr;
}
if (fileBeingSent != nullptr)
{
fileBeingSent->Close();
fileBeingSent = nullptr;
}
OutputBuffer::ReleaseAll(sendBuffer);
sendStack->ReleaseAll();
NetworkTransaction *ret = next;
next = freelist;
freelist = this;
return ret;
}
// End
|