mirror of https://github.com/ARMmbed/mbed-os.git
Make TCPSocket send all data when blocking
Previously, send() was somewhat soft - it only ever made one send call to the underlying stack, so it would typically take as much data as would fit in the buffer, and only block if it was unable to write anything. This is not the intent of a POSIX socket/filehandle write. It should try to send everything if blocking, and only send less if interrupted by a signal: - If the O_NONBLOCK flag is clear, write() shall block the calling thread until the data can be accepted. - If the O_NONBLOCK flag is set, write() shall not block the thread. If some data can be written without blocking the thread, write() shall write what it can and return the number of bytes written. Otherwise, it shall return -1 and set errno to [EAGAIN]. This "send all" behaviour is of slightly limited usefulness in POSIX, as you still usually have to worry about the interruption possibility: - If write() is interrupted by a signal before it writes any data, it shall return -1 with errno set to [EINTR]. - If write() is interrupted by a signal after it successfully writes some data, it shall return the number of bytes written. But as mbed OS does not have the possibility of signal interruption, if we strengthen send to write everything, we can make applications' lives easier - they can just do "send(large amount)" confident that it will all go in one call (if no errors). So, rework to make multiple sends to the underlying stack, blocking as necessary, until all data is written. This change does not apply to recv(), which is correct in only blocking until some data is available: - If O_NONBLOCK is set, read() shall return -1 and set errno to [EAGAIN]. - If O_NONBLOCK is clear, read() shall block the calling thread until some data becomes available. - The use of the O_NONBLOCK flag has no effect if there is some data available.pull/5466/head
parent
a519b8449b
commit
67b97d39c4
|
@ -105,7 +105,9 @@ nsapi_error_t TCPSocket::connect(const char *host, uint16_t port)
|
|||
nsapi_size_or_error_t TCPSocket::send(const void *data, nsapi_size_t size)
|
||||
{
|
||||
_lock.lock();
|
||||
const uint8_t *data_ptr = static_cast<const uint8_t *>(data);
|
||||
nsapi_size_or_error_t ret;
|
||||
nsapi_size_t written = 0;
|
||||
|
||||
// If this assert is hit then there are two threads
|
||||
// performing a send at the same time which is undefined
|
||||
|
@ -113,6 +115,9 @@ nsapi_size_or_error_t TCPSocket::send(const void *data, nsapi_size_t size)
|
|||
MBED_ASSERT(!_write_in_progress);
|
||||
_write_in_progress = true;
|
||||
|
||||
// Unlike recv, we should write the whole thing if blocking. POSIX only
|
||||
// allows partial as a side-effect of signal handling; it normally tries to
|
||||
// write everything if blocking. Without signals we can always write all.
|
||||
while (true) {
|
||||
if (!_socket) {
|
||||
ret = NSAPI_ERROR_NO_SOCKET;
|
||||
|
@ -120,10 +125,16 @@ nsapi_size_or_error_t TCPSocket::send(const void *data, nsapi_size_t size)
|
|||
}
|
||||
|
||||
_pending = 0;
|
||||
ret = _stack->socket_send(_socket, data, size);
|
||||
if ((_timeout == 0) || (ret != NSAPI_ERROR_WOULD_BLOCK)) {
|
||||
ret = _stack->socket_send(_socket, data_ptr + written, size - written);
|
||||
if (ret >= 0) {
|
||||
written += ret;
|
||||
if (written >= size) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (_timeout == 0) {
|
||||
break;
|
||||
} else {
|
||||
} else if (ret == NSAPI_ERROR_WOULD_BLOCK) {
|
||||
uint32_t flag;
|
||||
|
||||
// Release lock before blocking so other threads
|
||||
|
@ -134,15 +145,22 @@ nsapi_size_or_error_t TCPSocket::send(const void *data, nsapi_size_t size)
|
|||
|
||||
if (flag & osFlagsError) {
|
||||
// Timeout break
|
||||
ret = NSAPI_ERROR_WOULD_BLOCK;
|
||||
break;
|
||||
}
|
||||
} else if (ret < 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_write_in_progress = false;
|
||||
_lock.unlock();
|
||||
return ret;
|
||||
if (ret <= 0 && ret != NSAPI_ERROR_WOULD_BLOCK) {
|
||||
return ret;
|
||||
} else if (written == 0) {
|
||||
return NSAPI_ERROR_WOULD_BLOCK;
|
||||
} else {
|
||||
return written;
|
||||
}
|
||||
}
|
||||
|
||||
nsapi_size_or_error_t TCPSocket::recv(void *data, nsapi_size_t size)
|
||||
|
|
|
@ -88,9 +88,9 @@ public:
|
|||
* The socket must be connected to a remote host. Returns the number of
|
||||
* bytes sent from the buffer.
|
||||
*
|
||||
* By default, send blocks until data is sent. If socket is set to
|
||||
* non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
|
||||
* immediately.
|
||||
* By default, send blocks until all data is sent. If socket is set to
|
||||
* non-blocking or times out, a partial amount can be written.
|
||||
* NSAPI_ERROR_WOULD_BLOCK is returned if no data was written.
|
||||
*
|
||||
* @param data Buffer of data to send to the host
|
||||
* @param size Size of the buffer in bytes
|
||||
|
@ -104,9 +104,9 @@ public:
|
|||
* The socket must be connected to a remote host. Returns the number of
|
||||
* bytes received into the buffer.
|
||||
*
|
||||
* By default, recv blocks until data is sent. If socket is set to
|
||||
* non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
|
||||
* immediately.
|
||||
* By default, recv blocks until some data is received. If socket is set to
|
||||
* non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to
|
||||
* indicate no data.
|
||||
*
|
||||
* @param data Destination buffer for data received from the host
|
||||
* @param size Size of the buffer in bytes
|
||||
|
|
Loading…
Reference in New Issue