forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathClientContext.h
327 lines (272 loc) · 7.12 KB
/
ClientContext.h
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
/*
ClientContext.h - emulation of TCP connection handling on top of lwIP
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef CLIENTCONTEXT_H
#define CLIENTCONTEXT_H
class ClientContext;
class WiFiClient;
extern "C" void esp_yield();
extern "C" void esp_schedule();
#include <assert.h>
bool getDefaultPrivateGlobalSyncValue ();
typedef void (*discard_cb_t)(void*, ClientContext*);
class ClientContext
{
public:
ClientContext(tcp_pcb* pcb, discard_cb_t discard_cb, void* discard_cb_arg) :
_discard_cb(discard_cb), _discard_cb_arg(discard_cb_arg), _refcnt(0), _next(0),
_sync(::getDefaultPrivateGlobalSyncValue()), _sock(-1)
{
(void)pcb;
}
ClientContext (int sock) :
_discard_cb(nullptr), _discard_cb_arg(nullptr), _refcnt(0), _next(nullptr),
_sync(::getDefaultPrivateGlobalSyncValue()), _sock(sock)
{
}
err_t abort()
{
if (_sock >= 0)
{
::close(_sock);
mockverbose("socket %d closed\n", _sock);
}
_sock = -1;
return ERR_ABRT;
}
err_t close()
{
abort();
return ERR_OK;
}
~ClientContext()
{
abort();
}
ClientContext* next() const
{
return _next;
}
ClientContext* next(ClientContext* new_next)
{
_next = new_next;
return _next;
}
void ref()
{
++_refcnt;
DEBUGV(":ref %d\r\n", _refcnt);
}
void unref()
{
DEBUGV(":ur %d\r\n", _refcnt);
if(--_refcnt == 0) {
discard_received();
close();
if (_discard_cb)
_discard_cb(_discard_cb_arg, this);
DEBUGV(":del\r\n");
delete this;
}
}
int connect(const ip_addr_t* addr, uint16_t port)
{
return mockConnect(addr->addr, _sock, port);
}
size_t availableForWrite()
{
// XXXFIXME be smarter
return 512;
}
void setNoDelay(bool nodelay)
{
mockverbose("TODO setNoDelay(%d)\n", (int)nodelay);
}
bool getNoDelay() const
{
mockverbose("TODO getNoDelay()\n");
return false;
}
void setTimeout(int timeout_ms)
{
_timeout_ms = timeout_ms;
}
int getTimeout() const
{
return _timeout_ms;
}
uint32_t getRemoteAddress() const
{
mockverbose("TODO getRemoteAddress()\n");
return 0;
}
uint16_t getRemotePort() const
{
mockverbose("TODO getRemotePort()\n");
return 0;
}
uint32_t getLocalAddress() const
{
mockverbose("TODO getLocalAddress()\n");
return 0;
}
uint16_t getLocalPort() const
{
mockverbose("TODO getLocalPort()\n");
return 0;
}
size_t getSize()
{
if (_sock < 0)
return 0;
if (_inbufsize)
return _inbufsize;
ssize_t ret = mockFillInBuf(_sock, _inbuf, _inbufsize);
if (ret < 0)
{
abort();
return 0;
}
return ret;
}
int read()
{
char c;
return read(&c, 1)? (unsigned char)c: -1;
}
size_t read (char* dst, size_t size)
{
ssize_t ret = mockRead(_sock, dst, size, 0, _inbuf, _inbufsize);
if (ret < 0)
{
abort();
return 0;
}
return ret;
}
int peek()
{
char c;
return peekBytes(&c, 1)? c: -1;
}
size_t peekBytes(char *dst, size_t size)
{
ssize_t ret = mockPeekBytes(_sock, dst, size, _timeout_ms, _inbuf, _inbufsize);
if (ret < 0)
{
abort();
return 0;
}
return ret;
}
void discard_received()
{
mockverbose("TODO: ClientContext::discard_received()\n");
}
bool wait_until_acked(int max_wait_ms = WIFICLIENT_MAX_FLUSH_WAIT_MS)
{
(void)max_wait_ms;
return true;
}
uint8_t state()
{
(void)getSize(); // read on socket to force detect closed peer
return _sock >= 0? ESTABLISHED: CLOSED;
}
size_t write(const char* data, size_t size)
{
ssize_t ret = mockWrite(_sock, (const uint8_t*)data, size, _timeout_ms);
if (ret < 0)
{
abort();
return 0;
}
return ret;
}
void keepAlive (uint16_t idle_sec = TCP_DEFAULT_KEEPALIVE_IDLE_SEC, uint16_t intv_sec = TCP_DEFAULT_KEEPALIVE_INTERVAL_SEC, uint8_t count = TCP_DEFAULT_KEEPALIVE_COUNT)
{
(void) idle_sec;
(void) intv_sec;
(void) count;
mockverbose("TODO ClientContext::keepAlive()\n");
}
bool isKeepAliveEnabled () const
{
mockverbose("TODO ClientContext::isKeepAliveEnabled()\n");
return false;
}
uint16_t getKeepAliveIdle () const
{
mockverbose("TODO ClientContext::getKeepAliveIdle()\n");
return 0;
}
uint16_t getKeepAliveInterval () const
{
mockverbose("TODO ClientContext::getKeepAliveInternal()\n");
return 0;
}
uint8_t getKeepAliveCount () const
{
mockverbose("TODO ClientContext::getKeepAliveCount()\n");
return 0;
}
bool getSync () const
{
mockverbose("TODO ClientContext::getSync()\n");
return _sync;
}
void setSync (bool sync)
{
mockverbose("TODO ClientContext::setSync()\n");
_sync = sync;
}
// return a pointer to available data buffer (size = peekAvailable())
// semantic forbids any kind of read() before calling peekConsume()
const char* peekBuffer ()
{
return _inbuf;
}
// return number of byte accessible by peekBuffer()
size_t peekAvailable ()
{
ssize_t ret = mockPeekBytes(_sock, nullptr, 0, 0, _inbuf, _inbufsize);
if (ret < 0)
{
abort();
return 0;
}
return _inbufsize;
}
// consume bytes after use (see peekBuffer)
void peekConsume (size_t consume)
{
assert(consume <= _inbufsize);
memmove(_inbuf, _inbuf + consume, _inbufsize - consume);
_inbufsize -= consume;
}
private:
discard_cb_t _discard_cb = nullptr;
void* _discard_cb_arg = nullptr;
int8_t _refcnt;
ClientContext* _next;
bool _sync;
// MOCK
int _sock = -1;
int _timeout_ms = 5000;
char _inbuf [CCBUFSIZE];
size_t _inbufsize = 0;
};
#endif //CLIENTCONTEXT_H