Fix the wqueue header, and update example

This commit is contained in:
Nils O. Selåsdal
2015-11-12 18:05:41 +01:00
parent 74701edf31
commit 6a2ecdf2f4
2 changed files with 24 additions and 13 deletions
+3 -3
View File
@@ -8,7 +8,7 @@ enum UC_WQ_RESULT {
/** INdicates the MBuf was fully processed and can be freed*/
UC_WQ_DONE = 1,
/** Indicates the MBuf was not fully processed, and will not be freed */
UC_WQ_UC_PARTIAL = 0,
UC_WQ_PARTIAL = 0,
/** Indicats an error and further processing of the wqueue should not be done*/
UC_WQ_ABORT = -1
};
@@ -38,7 +38,7 @@ enum UC_WQ_RESULT {
* for this event.
*
*/
typedef UC_WQ_RESULT (*wqueue_write_cb)(struct IOMux *mux, struct IOMuxFD *fd, struct MBuf *mbuf);
typedef enum UC_WQ_RESULT (*wqueue_write_cb)(struct IOMux *mux, struct IOMuxFD *fd, struct MBuf *mbuf);
/**
* Callback when read events are indicated.
@@ -49,7 +49,7 @@ typedef UC_WQ_RESULT (*wqueue_write_cb)(struct IOMux *mux, struct IOMuxFD *fd, s
* @param mux associated IOMux
* @param the fd of the current wqueue
*/
typedef UC_WQ_RESULT (*wqueue_read_cb)(struct IOMux *mux, struct IOMuxFD *fd);
typedef enum UC_WQ_RESULT (*wqueue_read_cb)(struct IOMux *mux, struct IOMuxFD *fd);
/** Represents a queue of data to write.
*/
+20 -9
View File
@@ -6,6 +6,7 @@
#include <ucore/utils.h>
#include <ucore/iomux.h>
#include <ucore/wqueue.h>
#include <ucore/fd_utils.h>
int stdout_read(struct IOMux *mux, struct IOMuxFD *fd)
@@ -14,20 +15,26 @@ int stdout_read(struct IOMux *mux, struct IOMuxFD *fd)
}
void stdin_read_cb(struct IOMux *mux, struct IOMuxFD *fd, unsigned int event)
{
struct MBuf *mbuf = uc_mbuf_new(512);
struct MBuf *mbuf = uc_mbuf_new(65);
uint8_t *buf = uc_mbuf_head(mbuf);
struct UCWQueue *wqueue = fd->cookie_ptr;
ssize_t len = read(fd->fd, buf, sizeof buf);
ssize_t len = read(fd->fd, buf, 65);
printf("Read %d bytes\n", len);
if (len <= 0) {
uc_mbuf_free(mbuf);
iomux_unregister_fd(mux, fd);
uc_wqueue_clear(wqueue);
iomux_unregister_fd(mux, &wqueue->fd);
return;
}
uc_mbuf_put(mbuf, len);
struct UCWQueue *wqueue = fd->cookie_ptr;
uc_wqueue_enqueue(wqueue, mbuf);
struct MBuf *mbuf2 = uc_mbuf_copy_data(mbuf);
uc_wqueue_enqueue(wqueue, mbuf2);
}
struct IOMuxFD stdin_fd = {
@@ -48,28 +55,32 @@ int stdout_write(struct IOMux *mux, struct IOMuxFD *fd, struct MBuf *mbuf)
wlen = write(fd->fd, data, len);
if (wlen < 0) {
if (errno != EWOULDBLOCK) {
perror("write");
return -1;
}
return UC_WQ_ABORT;
}
if (wlen == len) {
return 1;
return UC_WQ_DONE;
}
return 0;
return UC_WQ_PARTIAL;
}
struct UCWQueue stdout_queue;
int main(int argc, char *argv[])
{
struct IOMux *mux = iomux_create(IOMUX_TYPE_DEFAULT);
//struct IOMux *mux = iomux_create(IOMUX_TYPE_SELECT);
struct IOMux *mux = iomux_create(IOMUX_TYPE_EPOLL);
uc_wqueue_init(&stdout_queue, mux);
stdin_fd.cookie_ptr = &stdout_queue;
stdout_queue.fd.fd = 1;
stdout_queue.fd.what = MUX_EV_READ;
stdout_queue.write_cb = stdout_write;
stdout_queue.read_cb = stdout_read;
// stdout_queue.read_cb = stdout_read; //we should not get read events here.
uc_set_nonblocking(1);
iomux_register_fd(mux, &stdin_fd);
iomux_register_fd(mux, &stdout_queue.fd);