stream: allow async read/write callback to destroy the stream explicitly

This commit is contained in:
Martin Willi
2013-07-18 16:00:29 +02:00
parent c9d1742b5d
commit 4701929266
2 changed files with 15 additions and 10 deletions
+11 -6
View File
@@ -175,21 +175,26 @@ static void remove_watcher(private_stream_t *this)
static bool watch(private_stream_t *this, int fd, watcher_event_t event) static bool watch(private_stream_t *this, int fd, watcher_event_t event)
{ {
bool keep = FALSE; bool keep = FALSE;
stream_cb_t cb;
switch (event) switch (event)
{ {
case WATCHER_READ: case WATCHER_READ:
keep = this->read_cb(this->read_data, &this->public); cb = this->read_cb;
if (!keep) this->read_cb = NULL;
keep = cb(this->read_data, &this->public);
if (keep)
{ {
this->read_cb = NULL; this->read_cb = cb;
} }
break; break;
case WATCHER_WRITE: case WATCHER_WRITE:
keep = this->write_cb(this->write_data, &this->public); cb = this->write_cb;
if (!keep) this->write_cb = NULL;
keep = cb(this->write_data, &this->public);
if (keep)
{ {
this->write_cb = NULL; this->write_cb = cb;
} }
break; break;
case WATCHER_EXCEPT: case WATCHER_EXCEPT:
@@ -39,9 +39,9 @@ typedef stream_t*(*stream_constructor_t)(char *uri);
/** /**
* Callback function prototype, called when stream is ready. * Callback function prototype, called when stream is ready.
* *
* It is not allowed to destroy the stream during the callback, this would * It is allowed to destroy the stream during the callback, but only if it has
* deadlock. Instead, return FALSE to destroy the stream. It is not allowed * no other active on_read()/on_write() callback and returns FALSE. It is not
* to call on_read()/on_write() during this callback. * allowed to to call on_read()/on_write/() during the callback.
* *
* As select() may return even if a read()/write() would actually block, it is * As select() may return even if a read()/write() would actually block, it is
* recommended to use the non-blocking calls and handle return values * recommended to use the non-blocking calls and handle return values
@@ -49,7 +49,7 @@ typedef stream_t*(*stream_constructor_t)(char *uri);
* *
* @param data data passed during callback registration * @param data data passed during callback registration
* @param stream associated stream * @param stream associated stream
* @return FALSE to destroy the stream * @return FALSE unregisters the invoked callback, TRUE keeps it
*/ */
typedef bool (*stream_cb_t)(void *data, stream_t *stream); typedef bool (*stream_cb_t)(void *data, stream_t *stream);