stream: add read/write_all() methods to stream
This commit is contained in:
@@ -54,8 +54,6 @@ struct private_stream_t {
|
|||||||
* Data for write-ready callback
|
* Data for write-ready callback
|
||||||
*/
|
*/
|
||||||
void *write_data;
|
void *write_data;
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
METHOD(stream_t, read_, ssize_t,
|
METHOD(stream_t, read_, ssize_t,
|
||||||
@@ -86,6 +84,29 @@ METHOD(stream_t, read_, ssize_t,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
METHOD(stream_t, read_all, bool,
|
||||||
|
private_stream_t *this, void *buf, size_t len)
|
||||||
|
{
|
||||||
|
ssize_t ret;
|
||||||
|
|
||||||
|
while (len)
|
||||||
|
{
|
||||||
|
ret = read_(this, buf, len, TRUE);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
if (ret == 0)
|
||||||
|
{
|
||||||
|
errno = ECONNRESET;
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
len -= ret;
|
||||||
|
buf += ret;
|
||||||
|
}
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
METHOD(stream_t, write_, ssize_t,
|
METHOD(stream_t, write_, ssize_t,
|
||||||
private_stream_t *this, void *buf, size_t len, bool block)
|
private_stream_t *this, void *buf, size_t len, bool block)
|
||||||
{
|
{
|
||||||
@@ -114,6 +135,29 @@ METHOD(stream_t, write_, ssize_t,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
METHOD(stream_t, write_all, bool,
|
||||||
|
private_stream_t *this, void *buf, size_t len)
|
||||||
|
{
|
||||||
|
ssize_t ret;
|
||||||
|
|
||||||
|
while (len)
|
||||||
|
{
|
||||||
|
ret = write_(this, buf, len, TRUE);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
if (ret == 0)
|
||||||
|
{
|
||||||
|
errno = ECONNRESET;
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
len -= ret;
|
||||||
|
buf += ret;
|
||||||
|
}
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a registered watcher
|
* Remove a registered watcher
|
||||||
*/
|
*/
|
||||||
@@ -236,8 +280,10 @@ stream_t *stream_create_from_fd(int fd)
|
|||||||
INIT(this,
|
INIT(this,
|
||||||
.public = {
|
.public = {
|
||||||
.read = _read_,
|
.read = _read_,
|
||||||
|
.read_all = _read_all,
|
||||||
.on_read = _on_read,
|
.on_read = _on_read,
|
||||||
.write = _write_,
|
.write = _write_,
|
||||||
|
.write_all = _write_all,
|
||||||
.on_write = _on_write,
|
.on_write = _on_write,
|
||||||
.get_file = _get_file,
|
.get_file = _get_file,
|
||||||
.destroy = _destroy,
|
.destroy = _destroy,
|
||||||
|
|||||||
@@ -71,6 +71,19 @@ struct stream_t {
|
|||||||
*/
|
*/
|
||||||
ssize_t (*read)(stream_t *this, void *buf, size_t len, bool block);
|
ssize_t (*read)(stream_t *this, void *buf, size_t len, bool block);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read data from the stream, avoiding short reads.
|
||||||
|
*
|
||||||
|
* This call is always blocking, and reads until len has been read
|
||||||
|
* completely. If the connection is closed before enough bytes could be
|
||||||
|
* returned, errno is set to ECONNRESET.
|
||||||
|
*
|
||||||
|
* @param buf data buffer to read into
|
||||||
|
* @param len number of bytes to read
|
||||||
|
* @return TRUE if len bytes read, FALSE on error
|
||||||
|
*/
|
||||||
|
bool (*read_all)(stream_t *this, void *buf, size_t len);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register a callback to invoke when stream has data to read.
|
* Register a callback to invoke when stream has data to read.
|
||||||
*
|
*
|
||||||
@@ -92,6 +105,18 @@ struct stream_t {
|
|||||||
*/
|
*/
|
||||||
ssize_t (*write)(stream_t *this, void *buf, size_t len, bool block);
|
ssize_t (*write)(stream_t *this, void *buf, size_t len, bool block);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write data to the stream, avoiding short writes.
|
||||||
|
*
|
||||||
|
* This call is always blocking, and writes until len bytes has been
|
||||||
|
* written.
|
||||||
|
*
|
||||||
|
* @param buf data buffer to write
|
||||||
|
* @param len number of bytes to write
|
||||||
|
* @return TRUE if len bytes written, FALSE on error
|
||||||
|
*/
|
||||||
|
bool (*write_all)(stream_t *this, void *buf, size_t len);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register a callback to invoke when a write would not block.
|
* Register a callback to invoke when a write would not block.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user