1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-29 05:21:37 +03:00

Stream::send() (#6979)

This commit is contained in:
david gauchard
2021-03-15 01:36:20 +01:00
committed by GitHub
parent 4cc1472821
commit c720c0d9e8
48 changed files with 2136 additions and 650 deletions

View File

@ -80,4 +80,3 @@ cont_t* g_pcont = NULL;
extern "C" void cont_yield(cont_t*)
{
}

View File

@ -89,7 +89,8 @@ ssize_t mockFillInBuf (int sock, char* ccinbuf, size_t& ccinbufsize)
if (ret == 0)
{
// connection closed
return -1;
// nothing is read
return 0;
}
if (ret == -1)
@ -97,16 +98,20 @@ ssize_t mockFillInBuf (int sock, char* ccinbuf, size_t& ccinbufsize)
if (errno != EAGAIN)
{
fprintf(stderr, MOCK "ClientContext::(read/peek fd=%i): filling buffer for %zd bytes: %s\n", sock, maxread, strerror(errno));
// error
return -1;
}
ret = 0;
}
ccinbufsize += ret;
return ret;
}
ssize_t mockPeekBytes (int sock, char* dst, size_t usersize, int timeout_ms, char* ccinbuf, size_t& ccinbufsize)
{
// usersize==0: peekAvailable()
if (usersize > CCBUFSIZE)
mockverbose("CCBUFSIZE(%d) should be increased by %zd bytes (-> %zd)\n", CCBUFSIZE, usersize - CCBUFSIZE, usersize);
@ -114,7 +119,7 @@ ssize_t mockPeekBytes (int sock, char* dst, size_t usersize, int timeout_ms, cha
size_t retsize = 0;
do
{
if (usersize <= ccinbufsize)
if (usersize && usersize <= ccinbufsize)
{
// data already buffered
retsize = usersize;
@ -123,7 +128,14 @@ ssize_t mockPeekBytes (int sock, char* dst, size_t usersize, int timeout_ms, cha
// check incoming data data
if (mockFillInBuf(sock, ccinbuf, ccinbufsize) < 0)
{
return -1;
}
if (usersize == 0 && ccinbufsize)
// peekAvailable
return ccinbufsize;
if (usersize <= ccinbufsize)
{
// data just received
@ -179,7 +191,7 @@ ssize_t mockWrite (int sock, const uint8_t* data, size_t size, int timeout_ms)
#endif
if (ret == -1)
{
fprintf(stderr, MOCK "ClientContext::read: write(%d): %s\n", sock, strerror(errno));
fprintf(stderr, MOCK "ClientContext::write/send(%d): %s\n", sock, strerror(errno));
return -1;
}
sent += ret;
@ -187,6 +199,8 @@ ssize_t mockWrite (int sock, const uint8_t* data, size_t size, int timeout_ms)
fprintf(stderr, MOCK "ClientContext::write: sent %d bytes (%zd / %zd)\n", ret, sent, size);
}
}
fprintf(stderr, MOCK "ClientContext::write: total sent %zd bytes\n", sent);
#ifdef DEBUG_ESP_WIFI
mockverbose(MOCK "ClientContext::write: total sent %zd bytes\n", sent);
#endif
return sent;
}

View File

@ -491,3 +491,9 @@ uart_detect_baudrate(int uart_nr)
}
};
size_t uart_peek_available (uart_t* uart) { return 0; }
const char* uart_peek_buffer (uart_t* uart) { return nullptr; }
void uart_peek_consume (uart_t* uart, size_t consume) { (void)uart; (void)consume; }

View File

@ -27,7 +27,7 @@ class WiFiClient;
extern "C" void esp_yield();
extern "C" void esp_schedule();
#include <include/DataSource.h>
#include <assert.h>
bool getDefaultPrivateGlobalSyncValue ();
@ -300,6 +300,33 @@ public:
_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;