Add wait_for_read_send/recv
authorVolker Lendecke <vl@samba.org>
Tue, 26 Jul 2011 13:06:44 +0000 (15:06 +0200)
committerMichael Adam <obnox@samba.org>
Tue, 11 Oct 2011 13:20:57 +0000 (15:20 +0200)
Wait for readability of a socket as a tevent_req

lib/async_req/async_sock.c
lib/async_req/async_sock.h

index 2c90b6dd17b0dc0a506666e1ea380a45da6893f5..43de837e2c35c94989c308ab5eae0f5e3b964f6c 100644 (file)
@@ -666,3 +666,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;
+}
index 8d98886e2bb216cbf8bf1f126da602d283ccd004..1910643ef72ca441840da84b63e821c97750a200 100644 (file)
@@ -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