Fix the wqueue header, and update example
This commit is contained in:
+21
-10
@@ -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) {
|
||||
perror("write");
|
||||
return -1;
|
||||
if (errno != EWOULDBLOCK) {
|
||||
perror("write");
|
||||
}
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user