1
- /*
1
+ /*
2
2
UdpContext.h - UDP connection handling on top of lwIP
3
3
4
4
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
5
5
This file is part of the esp8266 core for Arduino environment.
6
-
6
+
7
7
This library is free software; you can redistribute it and/or
8
8
modify it under the terms of the GNU Lesser General Public
9
9
License as published by the Free Software Foundation; either
@@ -206,10 +206,10 @@ class UdpContext
206
206
size_t max_size = _rx_buf->len - _rx_buf_offset;
207
207
size = (size < max_size) ? size : max_size;
208
208
DEBUGV (" :urd %d, %d, %d\r\n " , size, _rx_buf->len , _rx_buf_offset);
209
-
210
- os_memcpy (dst, reinterpret_cast <char *>(_rx_buf->payload ) + _rx_buf_offset, size);
209
+
210
+ memcpy (dst, reinterpret_cast <char *>(_rx_buf->payload ) + _rx_buf_offset, size);
211
211
_consume (size);
212
-
212
+
213
213
return size;
214
214
}
215
215
@@ -236,7 +236,7 @@ class UdpContext
236
236
{
237
237
_reserve (_tx_buf_offset + size);
238
238
}
239
-
239
+
240
240
size_t left_to_copy = size;
241
241
while (left_to_copy)
242
242
{
@@ -249,7 +249,7 @@ class UdpContext
249
249
continue ;
250
250
}
251
251
size_t will_copy = (left_to_copy < free_cur) ? left_to_copy : free_cur;
252
- os_memcpy (reinterpret_cast <char *>(_tx_buf_cur->payload ) + used_cur, data, will_copy);
252
+ memcpy (reinterpret_cast <char *>(_tx_buf_cur->payload ) + used_cur, data, will_copy);
253
253
_tx_buf_offset += will_copy;
254
254
left_to_copy -= will_copy;
255
255
data += will_copy;
@@ -259,18 +259,20 @@ class UdpContext
259
259
260
260
void send (ip_addr_t * addr = 0 , uint16_t port = 0 )
261
261
{
262
- size_t orig_size = _tx_buf_head->tot_len ;
263
-
264
262
size_t data_size = _tx_buf_offset;
265
- size_t size_adjustment = orig_size - data_size;
266
- for (pbuf* p = _tx_buf_head; p; p = p->next )
267
- {
268
- p->tot_len -= size_adjustment;
269
- if (!p->next )
270
- {
271
- p->len = p->tot_len ;
272
- }
263
+ pbuf* tx_copy = pbuf_alloc (PBUF_TRANSPORT, data_size, PBUF_RAM);
264
+ uint8_t * dst = reinterpret_cast <uint8_t *>(tx_copy->payload );
265
+ for (pbuf* p = _tx_buf_head; p; p = p->next ) {
266
+ size_t will_copy = (data_size < p->len ) ? data_size : p->len ;
267
+ memcpy (dst, p->payload , will_copy);
268
+ dst += will_copy;
269
+ data_size -= will_copy;
273
270
}
271
+ pbuf_free (_tx_buf_head);
272
+ _tx_buf_head = 0 ;
273
+ _tx_buf_cur = 0 ;
274
+ _tx_buf_offset = 0 ;
275
+
274
276
275
277
if (!addr) {
276
278
addr = &_dest_addr;
@@ -282,30 +284,16 @@ class UdpContext
282
284
_pcb->ttl = _multicast_ttl;
283
285
}
284
286
285
- udp_sendto (_pcb, _tx_buf_head, addr, port);
286
-
287
+ udp_sendto (_pcb, tx_copy, addr, port);
287
288
_pcb->ttl = old_ttl;
288
-
289
- for (pbuf* p = _tx_buf_head; p; p = p->next )
290
- {
291
- p->tot_len += size_adjustment;
292
- if (!p->next )
293
- {
294
- p->len = p->tot_len ;
295
- }
296
- }
297
-
298
- pbuf_free (_tx_buf_head);
299
- _tx_buf_head = 0 ;
300
- _tx_buf_cur = 0 ;
301
- _tx_buf_offset = 0 ;
289
+ pbuf_free (tx_copy);
302
290
}
303
291
304
292
private:
305
293
306
294
void _reserve (size_t size)
307
295
{
308
- const size_t pbuf_unit_size = 512 ;
296
+ const size_t pbuf_unit_size = 128 ;
309
297
if (!_tx_buf_head)
310
298
{
311
299
_tx_buf_head = pbuf_alloc (PBUF_TRANSPORT, pbuf_unit_size, PBUF_RAM);
@@ -357,7 +345,7 @@ class UdpContext
357
345
}
358
346
359
347
360
- static void _s_recv (void *arg,
348
+ static void _s_recv (void *arg,
361
349
udp_pcb *upcb, pbuf *p,
362
350
ip_addr_t *addr, u16_t port)
363
351
{
0 commit comments