Implement write queue for iomux
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <ucore/utils.h>
|
||||
#include <ucore/utils.h>
|
||||
#include <ucore/iomux.h>
|
||||
#include <ucore/wqueue.h>
|
||||
|
||||
|
||||
int stdout_read(struct IOMux *mux, struct IOMuxFD *fd)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
void stdin_read_cb(struct IOMux *mux, struct IOMuxFD *fd, unsigned int event)
|
||||
{
|
||||
struct MBuf *mbuf = uc_mbuf_new(512);
|
||||
uint8_t *buf = uc_mbuf_head(mbuf);
|
||||
|
||||
ssize_t len = read(fd->fd, buf, sizeof buf);
|
||||
|
||||
printf("Read %d bytes\n", len);
|
||||
if (len <= 0) {
|
||||
uc_mbuf_free(mbuf);
|
||||
return;
|
||||
}
|
||||
uc_mbuf_put(mbuf, len);
|
||||
|
||||
struct UCWQueue *wqueue = fd->cookie_ptr;
|
||||
uc_wqueue_enqueue(wqueue, mbuf);
|
||||
}
|
||||
|
||||
struct IOMuxFD stdin_fd = {
|
||||
.fd = 0,
|
||||
.what = MUX_EV_READ,
|
||||
.callback = stdin_read_cb,
|
||||
|
||||
};
|
||||
|
||||
int stdout_write(struct IOMux *mux, struct IOMuxFD *fd, struct MBuf *mbuf)
|
||||
{
|
||||
uint32_t len;
|
||||
ssize_t wlen;
|
||||
uint8_t *data;
|
||||
|
||||
len = uc_mbuf_len(mbuf);
|
||||
data = uc_mbuf_pull(mbuf, len);
|
||||
wlen = write(fd->fd, data, len);
|
||||
|
||||
if (wlen < 0) {
|
||||
perror("write");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (wlen == len) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct UCWQueue stdout_queue;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct IOMux *mux = iomux_create(IOMUX_TYPE_DEFAULT);
|
||||
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;
|
||||
|
||||
iomux_register_fd(mux, &stdin_fd);
|
||||
iomux_register_fd(mux, &stdout_queue.fd);
|
||||
iomux_run(mux);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user