92 lines
2.0 KiB
C
92 lines
2.0 KiB
C
#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>
|
|
#include <ucore/fd_utils.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(65);
|
|
uint8_t *buf = uc_mbuf_head(mbuf);
|
|
struct UCWQueue *wqueue = fd->cookie_ptr;
|
|
|
|
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);
|
|
|
|
uc_wqueue_enqueue(wqueue, mbuf);
|
|
struct MBuf *mbuf2 = uc_mbuf_copy_data(mbuf);
|
|
uc_wqueue_enqueue(wqueue, mbuf2);
|
|
}
|
|
|
|
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) {
|
|
if (errno != EWOULDBLOCK) {
|
|
perror("write");
|
|
}
|
|
return UC_WQ_ABORT;
|
|
}
|
|
|
|
if (wlen == len) {
|
|
return UC_WQ_DONE;
|
|
}
|
|
|
|
return UC_WQ_PARTIAL;
|
|
}
|
|
|
|
struct UCWQueue stdout_queue;
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
//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.write_cb = stdout_write;
|
|
// 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);
|
|
iomux_run(mux);
|
|
|
|
return 0;
|
|
}
|
|
|