summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorVolker Lendecke <vl@samba.org>2011-07-26 15:06:44 +0200
committerVolker Lendecke <vlendec@samba.org>2011-07-28 17:42:22 +0200
commit428196799028d80a5fe3b09392cb7015049c5397 (patch)
tree1e64c15664da4203576571bbfb1bbb76c9b12ff3 /lib
parente84c7a2e26d206f38bcb94d4d1b6c854cdd4094c (diff)
downloadsamba-428196799028d80a5fe3b09392cb7015049c5397.tar.gz
samba-428196799028d80a5fe3b09392cb7015049c5397.tar.bz2
samba-428196799028d80a5fe3b09392cb7015049c5397.zip
Add wait_for_read_send/recv
Wait for readability of a socket as a tevent_req
Diffstat (limited to 'lib')
-rw-r--r--lib/async_req/async_sock.c55
-rw-r--r--lib/async_req/async_sock.h5
2 files changed, 60 insertions, 0 deletions
diff --git a/lib/async_req/async_sock.c b/lib/async_req/async_sock.c
index dfb1a1cdbd..811cf8d675 100644
--- a/lib/async_req/async_sock.c
+++ b/lib/async_req/async_sock.c
@@ -667,3 +667,58 @@ ssize_t read_packet_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
*pbuf = talloc_move(mem_ctx, &state->buf);
return talloc_get_size(*pbuf);
}
+
+struct wait_for_read_state {
+ struct tevent_req *req;
+ struct tevent_fd *fde;
+};
+
+static void wait_for_read_done(struct tevent_context *ev,
+ struct tevent_fd *fde,
+ uint16_t flags,
+ void *private_data);
+
+struct tevent_req *wait_for_read_send(TALLOC_CTX *mem_ctx,
+ struct tevent_context *ev,
+ int fd)
+{
+ struct tevent_req *req;
+ struct wait_for_read_state *state;
+
+ req = tevent_req_create(mem_ctx, &state, struct wait_for_read_state);
+ if (req == NULL) {
+ return NULL;
+ }
+ state->req = req;
+ state->fde = tevent_add_fd(ev, state, fd, TEVENT_FD_READ,
+ wait_for_read_done, state);
+ if (tevent_req_nomem(state->fde, req)) {
+ return tevent_req_post(req, ev);
+ }
+ return req;
+}
+
+static void wait_for_read_done(struct tevent_context *ev,
+ struct tevent_fd *fde,
+ uint16_t flags,
+ void *private_data)
+{
+ struct wait_for_read_state *state = talloc_get_type_abort(
+ private_data, struct wait_for_read_state);
+
+ if (flags & TEVENT_FD_READ) {
+ TALLOC_FREE(state->fde);
+ tevent_req_done(state->req);
+ }
+}
+
+bool wait_for_read_recv(struct tevent_req *req, int *perr)
+{
+ int err;
+
+ if (tevent_req_is_unix_error(req, &err)) {
+ *perr = err;
+ return false;
+ }
+ return true;
+}
diff --git a/lib/async_req/async_sock.h b/lib/async_req/async_sock.h
index 8d98886e2b..1910643ef7 100644
--- a/lib/async_req/async_sock.h
+++ b/lib/async_req/async_sock.h
@@ -61,4 +61,9 @@ struct tevent_req *read_packet_send(TALLOC_CTX *mem_ctx,
ssize_t read_packet_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
uint8_t **pbuf, int *perrno);
+struct tevent_req *wait_for_read_send(TALLOC_CTX *mem_ctx,
+ struct tevent_context *ev,
+ int fd);
+bool wait_for_read_recv(struct tevent_req *req, int *perr);
+
#endif