Fix the wqueue header, and update example
This commit is contained in:
@@ -8,7 +8,7 @@ enum UC_WQ_RESULT {
|
|||||||
/** INdicates the MBuf was fully processed and can be freed*/
|
/** INdicates the MBuf was fully processed and can be freed*/
|
||||||
UC_WQ_DONE = 1,
|
UC_WQ_DONE = 1,
|
||||||
/** Indicates the MBuf was not fully processed, and will not be freed */
|
/** 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*/
|
/** Indicats an error and further processing of the wqueue should not be done*/
|
||||||
UC_WQ_ABORT = -1
|
UC_WQ_ABORT = -1
|
||||||
};
|
};
|
||||||
@@ -38,7 +38,7 @@ enum UC_WQ_RESULT {
|
|||||||
* for this event.
|
* 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.
|
* 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 mux associated IOMux
|
||||||
* @param the fd of the current wqueue
|
* @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.
|
/** Represents a queue of data to write.
|
||||||
*/
|
*/
|
||||||
|
|||||||
+21
-10
@@ -6,6 +6,7 @@
|
|||||||
#include <ucore/utils.h>
|
#include <ucore/utils.h>
|
||||||
#include <ucore/iomux.h>
|
#include <ucore/iomux.h>
|
||||||
#include <ucore/wqueue.h>
|
#include <ucore/wqueue.h>
|
||||||
|
#include <ucore/fd_utils.h>
|
||||||
|
|
||||||
|
|
||||||
int stdout_read(struct IOMux *mux, struct IOMuxFD *fd)
|
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)
|
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);
|
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);
|
printf("Read %d bytes\n", len);
|
||||||
if (len <= 0) {
|
if (len <= 0) {
|
||||||
uc_mbuf_free(mbuf);
|
uc_mbuf_free(mbuf);
|
||||||
|
iomux_unregister_fd(mux, fd);
|
||||||
|
uc_wqueue_clear(wqueue);
|
||||||
|
iomux_unregister_fd(mux, &wqueue->fd);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
uc_mbuf_put(mbuf, len);
|
uc_mbuf_put(mbuf, len);
|
||||||
|
|
||||||
struct UCWQueue *wqueue = fd->cookie_ptr;
|
|
||||||
uc_wqueue_enqueue(wqueue, mbuf);
|
uc_wqueue_enqueue(wqueue, mbuf);
|
||||||
|
struct MBuf *mbuf2 = uc_mbuf_copy_data(mbuf);
|
||||||
|
uc_wqueue_enqueue(wqueue, mbuf2);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct IOMuxFD stdin_fd = {
|
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);
|
wlen = write(fd->fd, data, len);
|
||||||
|
|
||||||
if (wlen < 0) {
|
if (wlen < 0) {
|
||||||
perror("write");
|
if (errno != EWOULDBLOCK) {
|
||||||
return -1;
|
perror("write");
|
||||||
|
}
|
||||||
|
return UC_WQ_ABORT;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (wlen == len) {
|
if (wlen == len) {
|
||||||
return 1;
|
return UC_WQ_DONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return UC_WQ_PARTIAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct UCWQueue stdout_queue;
|
struct UCWQueue stdout_queue;
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
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);
|
uc_wqueue_init(&stdout_queue, mux);
|
||||||
stdin_fd.cookie_ptr = &stdout_queue;
|
stdin_fd.cookie_ptr = &stdout_queue;
|
||||||
stdout_queue.fd.fd = 1;
|
stdout_queue.fd.fd = 1;
|
||||||
stdout_queue.fd.what = MUX_EV_READ;
|
|
||||||
stdout_queue.write_cb = stdout_write;
|
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, &stdin_fd);
|
||||||
iomux_register_fd(mux, &stdout_queue.fd);
|
iomux_register_fd(mux, &stdout_queue.fd);
|
||||||
|
|||||||
Reference in New Issue
Block a user