@@ -109,62 +109,94 @@ class Stream: public Print {
109
109
virtual String readString ();
110
110
String readStringUntil (char terminator);
111
111
112
- // ::read(buf, len): conflicting returned type:
112
+ // ////////////////// extension: readNow (is ::read() with unified signature)
113
+ // (supposed to be internally used, and ephemeral)
114
+ //
115
+ // about ::read(buf, len): conflicting returned type:
113
116
// - `int` in arduino's Client::
114
117
// - `size_t` in esp8266 API (HardwareSerial::, FS::)
115
118
// - not existent in arduino's Stream::
116
119
// changing every read()/write() `size_t` return type to `int` will be a breaking change
117
120
// => adding int ::readNow(buf, len) for now (following official `int Client::read(buf, len))`
118
121
//
119
122
// int ::readNow(buf, len)
120
- // read at most len bytes, returns effectively transfered bytes (can be less than len)
121
- // immediate return when no more data are available (no timeout)
122
- virtual int readNow (char * buffer, size_t len);
123
- virtual int readNow (uint8_t * buffer, size_t len) final { return readNow ((char *)buffer, len); }
123
+ // read at most len bytes, returns effectively transfered bytes (can be less than ' len' )
124
+ // with no timeout: immediate return when no more data are available
125
+ virtual int readNow (char * buffer, size_t len);
126
+ virtual int readNow (uint8_t * buffer, size_t len) final { return readNow ((char *)buffer, len); }
124
127
125
- // ////////////////// extensions: direct access to input buffer
128
+ // ////////////////// extension: direct access to input buffer
129
+ // for providing, when possible, a pointer to available data for read
126
130
127
- // inform user and ::to() on effective buffered peek API implementation
131
+ // informs user and ::to() on effective buffered peek API implementation
132
+ // by default: not available
128
133
virtual bool peekBufferAPI () const { return false ; }
129
134
130
- // return number of byte accessible by peekBuffer()
135
+ // returns number of byte accessible by peekBuffer()
131
136
virtual size_t availableForPeek () { return 0 ; }
132
137
133
- // return a pointer to available data buffer (size = availableForPeek())
134
- // semantic forbids any kind of read()
135
- // after calling peekBuffer()
136
- // and before calling peekConsume()
138
+ // returns a pointer to available data buffer (size = availableForPeek())
139
+ // semantic forbids any kind of :: read()
140
+ // - after calling peekBuffer()
141
+ // - and before calling peekConsume()
137
142
virtual const char * peekBuffer () { return nullptr ; }
138
143
139
- // consume bytes after peekBuffer() use
144
+ // consumes bytes after peekBuffer() use
145
+ // (then ::read() is allowed)
140
146
virtual void peekConsume (size_t consume) { (void )consume; }
141
147
142
148
// by default read timeout is possible (incoming data from network,serial..)
143
- // (children can override to false (like String))
149
+ // children can override to false (like String::)
150
+ // (outputTimeoutPossible() is defined in Print::)
144
151
virtual bool inputTimeoutPossible () const { return true ; }
145
152
146
153
// ////////////////// extensions: Stream streams
147
-
148
154
// Stream::to()
149
- // transfer from `Stream::` to `Print::` at most maxlen bytes and return number of transfered bytes
150
- // (uses 1-copy peekBuffer API when available, or transfer through a 2-copies local stack space)
151
- // - maxLen<0 will transfer until input starvation or saturated output
152
- // - timeout_ms==TimeoutMs::neverExpires: use getTimeout() (when 0: take what's available and immediate return)
153
- // - readUntilChar: setting anything in 0..255 will stop transfer when this char is read *and copied too*.
155
+ //
156
+ // Stream::to() uses 1-copy transfers when peekBuffer API is
157
+ // available, or makes a regular transfer through a local temporary
158
+ // stack buffer.
159
+ //
160
+ // By default "source->to(&dest)" transfers everything until
161
+ // available (read or write) gets to 0 and a timeout occurs.
162
+ //
163
+ // "source->to(&dest, maxLen)" is like above but also returns when
164
+ // maxLen bytes are transferred.
165
+ //
166
+ // More generally ::to() will transfer as much as possible until:
167
+ // - maxLen (>=0) bytes are transferred (without timeout),
168
+ // - or readUntilChar (>=0) is reached (without timeout),
169
+ // - or available for read or write gets to zero (after timeout).
170
+ //
171
+ // Timeout value is by default thisStream->getTimeout() but it can
172
+ // be set to "alwaysExpired" (=0) or any value within oneShotMs
173
+ // allowed range.
174
+ //
175
+ // Return value:
176
+ // >0: the number of transfered bytes
177
+ // 0: nothing has been transfered, getLastTo() may contain an error reason
178
+ //
179
+ // Notes:
180
+ // - readUntilChar is copied and counted
181
+ // - for efficiency: Stream classes should implement peekAPI when possible
182
+ // - for efficiency: Stream classes should implement {input,output}TimeoutPossible()
183
+
184
+ using oneShotMs = esp8266::polledTimeout::oneShotFastMs;
185
+
154
186
virtual size_t to (Print* to,
155
- const ssize_t len = -1 ,
156
- esp8266::polledTimeout::oneShotFastMs:: timeType timeout = esp8266::polledTimeout::oneShotFastMs ::neverExpires /* =>getTimeout() */ ,
187
+ const ssize_t maxLen = -1 ,
188
+ oneShotMs:: timeType timeout = oneShotMs ::neverExpires /* =>getTimeout() */ ,
157
189
int readUntilChar = -1 ) final ;
158
- enum
190
+ typedef enum
159
191
{
160
- TOSTREAM_SUCCESS = 0 ,
161
- TOSTREAM_TIMED_OUT ,
162
- TOSTREAM_READ_ERROR ,
163
- TOSTREAM_WRITE_ERROR ,
164
- TOSTREAM_SHORT ,
165
- } _last_to = TOSTREAM_SUCCESS ;
166
-
167
- decltype (_last_to) getLastTo () const { return _last_to ; }
192
+ STREAMTO_SUCCESS = 0 ,
193
+ STREAMTO_TIMED_OUT ,
194
+ STREAMTO_READ_ERROR ,
195
+ STREAMTO_WRITE_ERROR ,
196
+ STREAMTO_SHORT ,
197
+ } toReport_e ;
198
+
199
+ toReport_e getLastTo () /* const*/ { return (toReport_e) getWriteError () ; }
168
200
169
201
// ////////////////// end of extensions
170
202
0 commit comments