Skip to content

Commit 3d1fbc6

Browse files
committed
Replace chain of UDP pbufs with a single pbuf before sending (#1009)
Packets up to 1492 bytes may be sent this way. Also reduced pbuf_unit_size to 128 bytes.
1 parent c4436d8 commit 3d1fbc6

File tree

1 file changed

+23
-35
lines changed

1 file changed

+23
-35
lines changed

libraries/ESP8266WiFi/src/include/UdpContext.h

+23-35
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/*
1+
/*
22
UdpContext.h - UDP connection handling on top of lwIP
33
44
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
55
This file is part of the esp8266 core for Arduino environment.
6-
6+
77
This library is free software; you can redistribute it and/or
88
modify it under the terms of the GNU Lesser General Public
99
License as published by the Free Software Foundation; either
@@ -206,10 +206,10 @@ class UdpContext
206206
size_t max_size = _rx_buf->len - _rx_buf_offset;
207207
size = (size < max_size) ? size : max_size;
208208
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);
211211
_consume(size);
212-
212+
213213
return size;
214214
}
215215

@@ -236,7 +236,7 @@ class UdpContext
236236
{
237237
_reserve(_tx_buf_offset + size);
238238
}
239-
239+
240240
size_t left_to_copy = size;
241241
while(left_to_copy)
242242
{
@@ -249,7 +249,7 @@ class UdpContext
249249
continue;
250250
}
251251
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);
253253
_tx_buf_offset += will_copy;
254254
left_to_copy -= will_copy;
255255
data += will_copy;
@@ -259,18 +259,20 @@ class UdpContext
259259

260260
void send(ip_addr_t* addr = 0, uint16_t port = 0)
261261
{
262-
size_t orig_size = _tx_buf_head->tot_len;
263-
264262
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;
273270
}
271+
pbuf_free(_tx_buf_head);
272+
_tx_buf_head = 0;
273+
_tx_buf_cur = 0;
274+
_tx_buf_offset = 0;
275+
274276

275277
if (!addr) {
276278
addr = &_dest_addr;
@@ -282,30 +284,16 @@ class UdpContext
282284
_pcb->ttl = _multicast_ttl;
283285
}
284286

285-
udp_sendto(_pcb, _tx_buf_head, addr, port);
286-
287+
udp_sendto(_pcb, tx_copy, addr, port);
287288
_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);
302290
}
303291

304292
private:
305293

306294
void _reserve(size_t size)
307295
{
308-
const size_t pbuf_unit_size = 512;
296+
const size_t pbuf_unit_size = 128;
309297
if (!_tx_buf_head)
310298
{
311299
_tx_buf_head = pbuf_alloc(PBUF_TRANSPORT, pbuf_unit_size, PBUF_RAM);
@@ -357,7 +345,7 @@ class UdpContext
357345
}
358346

359347

360-
static void _s_recv(void *arg,
348+
static void _s_recv(void *arg,
361349
udp_pcb *upcb, pbuf *p,
362350
ip_addr_t *addr, u16_t port)
363351
{

0 commit comments

Comments
 (0)