10
10
11
11
See file LICENSE.txt for further informations on licensing terms.
12
12
13
- Last updated July 10th, 2017
13
+ Last updated March 21st 2020
14
14
*/
15
15
16
16
#ifndef ETHERNETSERVERSTREAM_H
23
23
// #define SERIAL_DEBUG
24
24
#include " firmataDebug.h"
25
25
26
+ // If defined and set to a value higher than 1 all single bytes writes
27
+ // will be buffered until one of the following conditions is met:
28
+ // 1) write buffer full
29
+ // 2) any call to read(), available(), maintain(), peek() or flush()
30
+ // By combining the buffered bytes into a single TCP frame this feature will significantly
31
+ // reduce the network and receiver load by the factor 1/(1/20 + 1/bufferedSize).
32
+ // Buffer sizes up to 80 have been tested successfully. Note that higher buffer values
33
+ // may cause slight delays between an event and the network transmission.
34
+ #define WRITE_BUFFER_SIZE 40
35
+
36
+
26
37
class EthernetServerStream : public Stream
27
38
{
28
39
public:
@@ -39,6 +50,10 @@ class EthernetServerStream : public Stream
39
50
IPAddress localip;
40
51
uint16_t port;
41
52
bool connected;
53
+ #ifdef WRITE_BUFFER_SIZE
54
+ uint8_t writeBuffer[WRITE_BUFFER_SIZE];
55
+ uint8_t writeBufferLength;
56
+ #endif
42
57
bool maintain ();
43
58
void stop ();
44
59
@@ -58,21 +73,37 @@ EthernetServerStream::EthernetServerStream(IPAddress localip, uint16_t port)
58
73
: localip(localip),
59
74
port(port),
60
75
connected(false )
76
+ #ifdef WRITE_BUFFER_SIZE
77
+ , writeBufferLength(0 )
78
+ #endif
61
79
{
62
80
}
63
81
64
82
bool EthernetServerStream::connect_client ()
65
83
{
66
84
if ( connected )
67
85
{
68
- if ( client && client.connected () ) return true ;
86
+ if ( client && client.connected () )
87
+ {
88
+ #ifdef WRITE_BUFFER_SIZE
89
+ // send buffered bytes
90
+ if (writeBufferLength) {
91
+ client.write (writeBuffer, writeBufferLength);
92
+ writeBufferLength = 0 ;
93
+ }
94
+ #endif
95
+ return true ;
96
+ }
69
97
stop ();
70
98
}
71
99
72
100
EthernetClient newClient = server.available ();
73
101
if ( !newClient ) return false ;
74
102
client = newClient;
75
103
connected = true ;
104
+ #ifdef WRITE_BUFFER_SIZE
105
+ writeBufferLength = 0 ;
106
+ #endif
76
107
DEBUG_PRINTLN (" Connected" );
77
108
return true ;
78
109
}
@@ -126,6 +157,9 @@ EthernetServerStream::stop()
126
157
client.stop ();
127
158
}
128
159
connected = false ;
160
+ #ifdef WRITE_BUFFER_SIZE
161
+ writeBufferLength = 0 ;
162
+ #endif
129
163
}
130
164
131
165
bool
0 commit comments