@@ -22,53 +22,54 @@ class BufferStrategy : public DataStrategy
22
22
23
23
size_t write (ClientContext& ctx) override
24
24
{
25
- write_some (ctx);
25
+ _write_some (ctx);
26
26
while (!_done) {
27
27
esp_yield ();
28
28
}
29
29
return _written;
30
30
}
31
31
32
- void write_some (ClientContext& ctx)
33
- {
34
- size_t can_write = ctx.getSendBufferSize ();
35
- size_t will_write = (can_write < _size) ? can_write : _size;
36
- if (!ctx.write (_buf, will_write)) {
37
- end ();
38
- return ;
39
- }
40
- _buf += will_write;
41
- _size -= will_write;
42
- _queued += will_write;
43
- }
44
-
45
32
void on_sent (ClientContext& ctx, size_t size) override
46
33
{
47
34
if (_size > 0 ) {
48
- write_some (ctx);
35
+ _write_some (ctx);
49
36
}
50
37
_queued -= size;
51
38
_written += size;
52
39
if (_queued == 0 ) {
53
- end ();
40
+ _end ();
54
41
}
55
42
}
56
43
57
44
void on_error (ClientContext& ctx) override
58
45
{
59
46
if (_queued > 0 ) {
60
- end ();
47
+ _end ();
61
48
}
62
49
}
63
50
64
51
void on_poll (ClientContext& ctx) override
65
52
{
66
53
if (millis () > _timeout && _queued > 0 ) {
67
- end ();
54
+ _end ();
55
+ }
56
+ }
57
+
58
+ protected:
59
+ void _write_some (ClientContext& ctx)
60
+ {
61
+ size_t can_write = ctx.getSendBufferSize ();
62
+ size_t will_write = (can_write < _size) ? can_write : _size;
63
+ if (!ctx.write (_buf, will_write)) {
64
+ _end ();
65
+ return ;
68
66
}
67
+ _buf += will_write;
68
+ _size -= will_write;
69
+ _queued += will_write;
69
70
}
70
71
71
- void end ()
72
+ void _end ()
72
73
{
73
74
if (!_done) {
74
75
_done = true ;
@@ -168,40 +169,17 @@ class ChunkedStrategy : public DataStrategy
168
169
169
170
size_t write (ClientContext& ctx) override
170
171
{
171
- write_some (ctx);
172
+ _write_some (ctx);
172
173
while (!_done) {
173
174
esp_yield ();
174
175
}
175
176
return _written;
176
177
}
177
178
178
- void write_some (ClientContext& ctx)
179
- {
180
- size_t can_write = ctx.getSendBufferSize ();
181
- size_t will_write = (can_write < _size) ? can_write : _size;
182
-
183
- BufferLink* new_buf = new BufferLink (will_write, _buffers_tail);
184
- if (!_buffers_head) {
185
- _buffers_head = new_buf;
186
- }
187
- _buffers_tail = new_buf;
188
- size_t cb = _source.readBytes ((char *) new_buf->data (), will_write);
189
- if (cb < will_write) {
190
- end ();
191
- return ;
192
- }
193
- if (!ctx.write (new_buf->data (), will_write)) {
194
- end ();
195
- return ;
196
- }
197
- _size -= will_write;
198
- _last_write_time = millis ();
199
- }
200
-
201
179
void on_sent (ClientContext& ctx, size_t size) override
202
180
{
203
181
if (_size > 0 ) {
204
- write_some (ctx);
182
+ _write_some (ctx);
205
183
}
206
184
auto size_to_remove = size;
207
185
while (size_to_remove) {
@@ -219,14 +197,14 @@ class ChunkedStrategy : public DataStrategy
219
197
}
220
198
_written += size;
221
199
if (_buffers_head == nullptr ) {
222
- end ();
200
+ _end ();
223
201
}
224
202
}
225
203
226
204
void on_error (ClientContext& ctx) override
227
205
{
228
206
if (_buffers_head != nullptr ) {
229
- end ();
207
+ _end ();
230
208
}
231
209
}
232
210
@@ -235,11 +213,34 @@ class ChunkedStrategy : public DataStrategy
235
213
if (_last_write_time != 0 &&
236
214
millis () - _last_write_time > _timeout &&
237
215
_buffers_head != nullptr ) {
238
- end ();
216
+ _end ();
239
217
}
240
218
}
241
219
242
- void end ()
220
+ protected:
221
+ void _write_some (ClientContext& ctx)
222
+ {
223
+ size_t can_write = ctx.getSendBufferSize ();
224
+ size_t will_write = (can_write < _size) ? can_write : _size;
225
+ BufferLink* new_buf = new BufferLink (will_write, _buffers_tail);
226
+ if (!_buffers_head) {
227
+ _buffers_head = new_buf;
228
+ }
229
+ _buffers_tail = new_buf;
230
+ size_t cb = _source.readBytes ((char *) new_buf->data (), will_write);
231
+ if (cb < will_write) {
232
+ _end ();
233
+ return ;
234
+ }
235
+ if (!ctx.write (new_buf->data (), will_write)) {
236
+ _end ();
237
+ return ;
238
+ }
239
+ _size -= will_write;
240
+ _last_write_time = millis ();
241
+ }
242
+
243
+ void _end ()
243
244
{
244
245
if (!_done) {
245
246
_done = true ;
0 commit comments