1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-12-18 10:22:18 +03:00

WifiClient::write refactoring (second attempt) (#2177)

* WiFiClient: use DataSource for writes

* ESP8266WebServer: delegate writing to WiFiClient

* ESP8266WebServer: set write timeout before sending content
This commit is contained in:
Ivan Grokhotkov
2016-06-23 17:47:18 +08:00
committed by GitHub
parent 5e3df08273
commit 8db4dcea42
7 changed files with 565 additions and 415 deletions

View File

@@ -35,317 +35,447 @@ typedef err_t recv_ret_t;
typedef int32_t recv_ret_t;
#endif
class ClientContext {
public:
ClientContext(tcp_pcb* pcb, discard_cb_t discard_cb, void* discard_cb_arg) :
_pcb(pcb), _rx_buf(0), _rx_buf_offset(0), _discard_cb(discard_cb), _discard_cb_arg(discard_cb_arg), _refcnt(0), _next(0), _send_waiting(false) {
tcp_setprio(pcb, TCP_PRIO_MIN);
tcp_arg(pcb, this);
tcp_recv(pcb, (tcp_recv_fn) &_s_recv);
tcp_sent(pcb, &_s_sent);
tcp_err(pcb, &_s_error);
}
#include "DataSource.h"
err_t abort(){
if(_pcb) {
DEBUGV(":abort\r\n");
tcp_arg(_pcb, NULL);
tcp_sent(_pcb, NULL);
tcp_recv(_pcb, NULL);
tcp_err(_pcb, NULL);
tcp_abort(_pcb);
_pcb = 0;
}
return ERR_ABRT;
}
class ClientContext
{
public:
ClientContext(tcp_pcb* pcb, discard_cb_t discard_cb, void* discard_cb_arg) :
_pcb(pcb), _rx_buf(0), _rx_buf_offset(0), _discard_cb(discard_cb), _discard_cb_arg(discard_cb_arg), _refcnt(0), _next(0)
{
tcp_setprio(pcb, TCP_PRIO_MIN);
tcp_arg(pcb, this);
tcp_recv(pcb, (tcp_recv_fn) &_s_recv);
tcp_sent(pcb, &_s_sent);
tcp_err(pcb, &_s_error);
tcp_poll(pcb, &_s_poll, 1);
}
err_t close(){
err_t err = ERR_OK;
if(_pcb) {
DEBUGV(":close\r\n");
tcp_arg(_pcb, NULL);
tcp_sent(_pcb, NULL);
tcp_recv(_pcb, NULL);
tcp_err(_pcb, NULL);
err = tcp_close(_pcb);
if(err != ERR_OK) {
DEBUGV(":tc err %d\r\n", err);
tcp_abort(_pcb);
err = ERR_ABRT;
}
_pcb = 0;
}
return err;
}
~ClientContext() {
}
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() {
if(this != 0) {
DEBUGV(":ur %d\r\n", _refcnt);
if(--_refcnt == 0) {
flush();
close();
if(_discard_cb)
_discard_cb(_discard_cb_arg, this);
DEBUGV(":del\r\n");
delete this;
}
}
}
void setNoDelay(bool nodelay){
if(!_pcb) return;
if(nodelay) tcp_nagle_disable(_pcb);
else tcp_nagle_enable(_pcb);
}
bool getNoDelay(){
if(!_pcb) return false;
return tcp_nagle_disabled(_pcb);
}
uint32_t getRemoteAddress() {
if(!_pcb) return 0;
return _pcb->remote_ip.addr;
}
uint16_t getRemotePort() {
if(!_pcb) return 0;
return _pcb->remote_port;
}
uint32_t getLocalAddress() {
if(!_pcb) return 0;
return _pcb->local_ip.addr;
}
uint16_t getLocalPort() {
if(!_pcb) return 0;
return _pcb->local_port;
}
size_t getSize() const {
if(!_rx_buf) return 0;
return _rx_buf->tot_len - _rx_buf_offset;
}
char read() {
if(!_rx_buf) return 0;
char c = reinterpret_cast<char*>(_rx_buf->payload)[_rx_buf_offset];
_consume(1);
return c;
}
size_t read(char* dst, size_t size) {
if(!_rx_buf) return 0;
size_t max_size = _rx_buf->tot_len - _rx_buf_offset;
size = (size < max_size) ? size : max_size;
DEBUGV(":rd %d, %d, %d\r\n", size, _rx_buf->tot_len, _rx_buf_offset);
size_t size_read = 0;
while(size) {
size_t buf_size = _rx_buf->len - _rx_buf_offset;
size_t copy_size = (size < buf_size) ? size : buf_size;
DEBUGV(":rdi %d, %d\r\n", buf_size, copy_size);
os_memcpy(dst, reinterpret_cast<char*>(_rx_buf->payload) + _rx_buf_offset, copy_size);
dst += copy_size;
_consume(copy_size);
size -= copy_size;
size_read += copy_size;
}
return size_read;
}
char peek() {
if(!_rx_buf) return 0;
return reinterpret_cast<char*>(_rx_buf->payload)[_rx_buf_offset];
}
size_t peekBytes(char *dst, size_t size) {
if(!_rx_buf) return 0;
size_t max_size = _rx_buf->tot_len - _rx_buf_offset;
size = (size < max_size) ? size : max_size;
DEBUGV(":pd %d, %d, %d\r\n", size, _rx_buf->tot_len, _rx_buf_offset);
size_t buf_size = _rx_buf->len - _rx_buf_offset;
size_t copy_size = (size < buf_size) ? size : buf_size;
DEBUGV(":rpi %d, %d\r\n", buf_size, copy_size);
os_memcpy(dst, reinterpret_cast<char*>(_rx_buf->payload) + _rx_buf_offset, copy_size);
return copy_size;
}
void flush() {
if(!_rx_buf) {
return;
}
if(_pcb) {
tcp_recved(_pcb, (size_t) _rx_buf->tot_len);
}
pbuf_free(_rx_buf);
_rx_buf = 0;
_rx_buf_offset = 0;
}
uint8_t state() const {
if(!_pcb) return CLOSED;
return _pcb->state;
}
size_t write(const char* data, size_t size) {
if(!_pcb) {
DEBUGV(":wr !_pcb\r\n");
return 0;
}
if(size == 0) {
return 0;
}
size_t room = tcp_sndbuf(_pcb);
size_t will_send = (room < size) ? room : size;
err_t err = tcp_write(_pcb, data, will_send, 0);
if(err != ERR_OK) {
DEBUGV(":wr !ERR_OK\r\n");
return 0;
}
_size_sent = will_send;
DEBUGV(":wr\r\n");
tcp_output( _pcb );
_send_waiting = true;
delay(5000); // max send timeout
_send_waiting = false;
DEBUGV(":ww\r\n");
return will_send - _size_sent;
}
private:
err_t _sent(tcp_pcb* pcb, uint16_t len) {
DEBUGV(":sent %d\r\n", len);
_size_sent -= len;
if(_size_sent == 0 && _send_waiting) esp_schedule();
return ERR_OK;
}
void _consume(size_t size) {
ptrdiff_t left = _rx_buf->len - _rx_buf_offset - size;
if(left > 0) {
_rx_buf_offset += size;
} else if(!_rx_buf->next) {
DEBUGV(":c0 %d, %d\r\n", size, _rx_buf->tot_len);
if(_pcb) tcp_recved(_pcb, _rx_buf->len);
pbuf_free(_rx_buf);
_rx_buf = 0;
_rx_buf_offset = 0;
} else {
DEBUGV(":c %d, %d, %d\r\n", size, _rx_buf->len, _rx_buf->tot_len);
auto head = _rx_buf;
_rx_buf = _rx_buf->next;
_rx_buf_offset = 0;
pbuf_ref(_rx_buf);
if(_pcb) tcp_recved(_pcb, head->len);
pbuf_free(head);
}
}
recv_ret_t _recv(tcp_pcb* pcb, pbuf* pb, err_t err) {
if(pb == 0) // connection closed
{
DEBUGV(":rcl\r\n");
if (_send_waiting) {
esp_schedule();
}
abort();
return ERR_ABRT;
}
if(_rx_buf) {
// there is some unread data
// chain the new pbuf to the existing one
DEBUGV(":rch %d, %d\r\n", _rx_buf->tot_len, pb->tot_len);
pbuf_cat(_rx_buf, pb);
} else {
DEBUGV(":rn %d\r\n", pb->tot_len);
_rx_buf = pb;
_rx_buf_offset = 0;
}
return ERR_OK;
}
void _error(err_t err) {
DEBUGV(":er %d %d %d\r\n", err, _size_sent, _send_waiting);
err_t abort()
{
if(_pcb) {
DEBUGV(":abort\r\n");
tcp_arg(_pcb, NULL);
tcp_sent(_pcb, NULL);
tcp_recv(_pcb, NULL);
tcp_err(_pcb, NULL);
_pcb = NULL;
if(_size_sent && _send_waiting) {
esp_schedule();
tcp_poll(_pcb, NULL, 0);
tcp_abort(_pcb);
_pcb = 0;
}
return ERR_ABRT;
}
err_t close()
{
err_t err = ERR_OK;
if(_pcb) {
DEBUGV(":close\r\n");
tcp_arg(_pcb, NULL);
tcp_sent(_pcb, NULL);
tcp_recv(_pcb, NULL);
tcp_err(_pcb, NULL);
tcp_poll(_pcb, NULL, 0);
err = tcp_close(_pcb);
if(err != ERR_OK) {
DEBUGV(":tc err %d\r\n", err);
tcp_abort(_pcb);
err = ERR_ABRT;
}
_pcb = 0;
}
return err;
}
~ClientContext()
{
}
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()
{
if(this != 0) {
DEBUGV(":ur %d\r\n", _refcnt);
if(--_refcnt == 0) {
flush();
close();
if(_discard_cb) {
_discard_cb(_discard_cb_arg, this);
}
DEBUGV(":del\r\n");
delete this;
}
}
}
void setNoDelay(bool nodelay)
{
if(!_pcb) {
return;
}
if(nodelay) {
tcp_nagle_disable(_pcb);
} else {
tcp_nagle_enable(_pcb);
}
}
bool getNoDelay()
{
if(!_pcb) {
return false;
}
return tcp_nagle_disabled(_pcb);
}
void setNonBlocking(bool nonblocking)
{
_noblock = nonblocking;
}
bool getNonBlocking()
{
return _noblock;
}
uint32_t getRemoteAddress()
{
if(!_pcb) {
return 0;
}
return _pcb->remote_ip.addr;
}
uint16_t getRemotePort()
{
if(!_pcb) {
return 0;
}
return _pcb->remote_port;
}
uint32_t getLocalAddress()
{
if(!_pcb) {
return 0;
}
return _pcb->local_ip.addr;
}
uint16_t getLocalPort()
{
if(!_pcb) {
return 0;
}
return _pcb->local_port;
}
size_t getSize() const
{
if(!_rx_buf) {
return 0;
}
return _rx_buf->tot_len - _rx_buf_offset;
}
char read()
{
if(!_rx_buf) {
return 0;
}
char c = reinterpret_cast<char*>(_rx_buf->payload)[_rx_buf_offset];
_consume(1);
return c;
}
size_t read(char* dst, size_t size)
{
if(!_rx_buf) {
return 0;
}
size_t max_size = _rx_buf->tot_len - _rx_buf_offset;
size = (size < max_size) ? size : max_size;
DEBUGV(":rd %d, %d, %d\r\n", size, _rx_buf->tot_len, _rx_buf_offset);
size_t size_read = 0;
while(size) {
size_t buf_size = _rx_buf->len - _rx_buf_offset;
size_t copy_size = (size < buf_size) ? size : buf_size;
DEBUGV(":rdi %d, %d\r\n", buf_size, copy_size);
os_memcpy(dst, reinterpret_cast<char*>(_rx_buf->payload) + _rx_buf_offset, copy_size);
dst += copy_size;
_consume(copy_size);
size -= copy_size;
size_read += copy_size;
}
return size_read;
}
char peek()
{
if(!_rx_buf) {
return 0;
}
return reinterpret_cast<char*>(_rx_buf->payload)[_rx_buf_offset];
}
size_t peekBytes(char *dst, size_t size)
{
if(!_rx_buf) {
return 0;
}
size_t max_size = _rx_buf->tot_len - _rx_buf_offset;
size = (size < max_size) ? size : max_size;
DEBUGV(":pd %d, %d, %d\r\n", size, _rx_buf->tot_len, _rx_buf_offset);
size_t buf_size = _rx_buf->len - _rx_buf_offset;
size_t copy_size = (size < buf_size) ? size : buf_size;
DEBUGV(":rpi %d, %d\r\n", buf_size, copy_size);
os_memcpy(dst, reinterpret_cast<char*>(_rx_buf->payload) + _rx_buf_offset, copy_size);
return copy_size;
}
void flush()
{
if(!_rx_buf) {
return;
}
if(_pcb) {
tcp_recved(_pcb, (size_t) _rx_buf->tot_len);
}
pbuf_free(_rx_buf);
_rx_buf = 0;
_rx_buf_offset = 0;
}
uint8_t state() const
{
if(!_pcb) {
return CLOSED;
}
return _pcb->state;
}
size_t write(const uint8_t* data, size_t size)
{
if (!_pcb) {
return 0;
}
return _write_from_source(new BufferDataSource(data, size));
}
size_t write(Stream& stream)
{
if (!_pcb) {
return 0;
}
return _write_from_source(new BufferedStreamDataSource<Stream>(stream, stream.available()));
}
size_t write_P(PGM_P buf, size_t size)
{
if (!_pcb) {
return 0;
}
ProgmemStream stream(buf, size);
return _write_from_source(new BufferedStreamDataSource<ProgmemStream>(stream, size));
}
protected:
void _cancel_write()
{
if (_datasource) {
delete _datasource;
_datasource = nullptr;
esp_schedule();
}
}
size_t _write_from_source(DataSource* ds)
{
assert(_datasource == nullptr);
_datasource = ds;
_written = 0;
_write_some();
while (_datasource && !_noblock) {
_send_waiting = true;
esp_yield();
}
_send_waiting = false;
return _written;
}
void _write_some()
{
if (!_datasource || !_pcb) {
return;
}
size_t left = _datasource->available();
size_t can_send = tcp_sndbuf(_pcb);
if (_pcb->snd_queuelen >= TCP_SND_QUEUELEN) {
can_send = 0;
}
size_t will_send = (can_send < left) ? can_send : left;
if (will_send) {
const uint8_t* buf = _datasource->get_buffer(will_send);
err_t err = tcp_write(_pcb, buf, will_send, TCP_WRITE_FLAG_COPY);
_datasource->release_buffer(buf, will_send);
if (err == ERR_OK) {
_written += will_send;
tcp_output(_pcb);
}
}
err_t _poll(tcp_pcb* pcb) {
return ERR_OK;
if (!_datasource->available() || _noblock) {
delete _datasource;
_datasource = nullptr;
}
}
void _write_some_from_cb()
{
_write_some();
if (!_datasource && _send_waiting) {
esp_schedule();
}
}
err_t _sent(tcp_pcb* pcb, uint16_t len)
{
DEBUGV(":sent %d\r\n", len);
_write_some_from_cb();
return ERR_OK;
}
void _consume(size_t size)
{
ptrdiff_t left = _rx_buf->len - _rx_buf_offset - size;
if(left > 0) {
_rx_buf_offset += size;
} else if(!_rx_buf->next) {
DEBUGV(":c0 %d, %d\r\n", size, _rx_buf->tot_len);
if(_pcb) {
tcp_recved(_pcb, _rx_buf->len);
}
pbuf_free(_rx_buf);
_rx_buf = 0;
_rx_buf_offset = 0;
} else {
DEBUGV(":c %d, %d, %d\r\n", size, _rx_buf->len, _rx_buf->tot_len);
auto head = _rx_buf;
_rx_buf = _rx_buf->next;
_rx_buf_offset = 0;
pbuf_ref(_rx_buf);
if(_pcb) {
tcp_recved(_pcb, head->len);
}
pbuf_free(head);
}
}
recv_ret_t _recv(tcp_pcb* pcb, pbuf* pb, err_t err)
{
if(pb == 0) { // connection closed
DEBUGV(":rcl\r\n");
_cancel_write();
abort();
return ERR_ABRT;
}
static recv_ret_t _s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, err_t err) {
return reinterpret_cast<ClientContext*>(arg)->_recv(tpcb, pb, err);
if(_rx_buf) {
DEBUGV(":rch %d, %d\r\n", _rx_buf->tot_len, pb->tot_len);
pbuf_cat(_rx_buf, pb);
} else {
DEBUGV(":rn %d\r\n", pb->tot_len);
_rx_buf = pb;
_rx_buf_offset = 0;
}
return ERR_OK;
}
static void _s_error(void *arg, err_t err) {
reinterpret_cast<ClientContext*>(arg)->_error(err);
}
void _error(err_t err)
{
DEBUGV(":er %d %08x\r\n", err, (uint32_t) _datasource);
tcp_arg(_pcb, NULL);
tcp_sent(_pcb, NULL);
tcp_recv(_pcb, NULL);
tcp_err(_pcb, NULL);
_pcb = NULL;
_cancel_write();
}
static err_t _s_poll(void *arg, struct tcp_pcb *tpcb) {
return reinterpret_cast<ClientContext*>(arg)->_poll(tpcb);
}
err_t _poll(tcp_pcb*)
{
_write_some_from_cb();
return ERR_OK;
}
static err_t _s_sent(void *arg, struct tcp_pcb *tpcb, uint16_t len) {
return reinterpret_cast<ClientContext*>(arg)->_sent(tpcb, len);
}
static recv_ret_t _s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, err_t err)
{
return reinterpret_cast<ClientContext*>(arg)->_recv(tpcb, pb, err);
}
private:
tcp_pcb* _pcb;
static void _s_error(void *arg, err_t err)
{
reinterpret_cast<ClientContext*>(arg)->_error(err);
}
pbuf* _rx_buf;
size_t _rx_buf_offset;
static err_t _s_poll(void *arg, struct tcp_pcb *tpcb)
{
return reinterpret_cast<ClientContext*>(arg)->_poll(tpcb);
}
discard_cb_t _discard_cb;
void* _discard_cb_arg;
static err_t _s_sent(void *arg, struct tcp_pcb *tpcb, uint16_t len)
{
return reinterpret_cast<ClientContext*>(arg)->_sent(tpcb, len);
}
int _refcnt;
ClientContext* _next;
private:
tcp_pcb* _pcb;
size_t _size_sent;
bool _send_waiting;
pbuf* _rx_buf;
size_t _rx_buf_offset;
discard_cb_t _discard_cb;
void* _discard_cb_arg;
int _refcnt;
ClientContext* _next;
DataSource* _datasource = nullptr;
size_t _written = 0;
bool _noblock = false;
bool _send_waiting = false;
};
#endif//CLIENTCONTEXT_H