https://github.com/rouming/io
async IO library based on event loop + ZMTP (ZeroMQ) implementation
https://github.com/rouming/io
Last synced: 28 days ago
JSON representation
async IO library based on event loop + ZMTP (ZeroMQ) implementation
- Host: GitHub
- URL: https://github.com/rouming/io
- Owner: rouming
- Created: 2017-07-12T12:20:43.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-02-03T13:27:16.000Z (over 6 years ago)
- Last Synced: 2025-04-20T13:36:37.206Z (about 2 months ago)
- Language: C
- Homepage:
- Size: 39.1 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README
Awesome Lists containing this project
README
io: async IO library based on event loop
Description:
Async IO library which is based on IO read/write requests, which can
be submitted to the IO queue and if file descriptor (socket, pipe,
whatever) is ready to perform read/write - completion will be called.E.g. reads/writes from socket can be done as following:
struct io_queue q;
struct io_req req;
void *poller;
int err;io_queue_init(&q);
/* Create event loop poller */
err = poller_create(&poller);
assert(err == 0);/* Bind queue with one side of the socket */
err = io_queue_bind(&q, poller, sock);
assert(err == 0);/* Init read request, in case of completion on_read() will be called */
io_req_init(&req, &q, REQ_RD, NULL, on_read);
req.buf = (struct io_buf){
.iov[0] = {
.iov_base = NULL, /* buffer should be allocated */
.iov_len = 10 /* want to read 10 bytes */
},
.iov_num = 1, /* we have only one buffer */
.is_vari_len = false /* read exactly what is told, i.e. 10 bytes */
};/* Submit request, i.e. put it to the submission list */
err = io_queue_submit(&req);
assert(err == 0);while (1) {
struct poller_item *items[16];
int i;err = poller_wait(poller, items, ARRAY_SIZE(items), INT_MAX);
if (err == 0)
continue;
else if (err < 0) {
printf("poller_wait() failed, errno=%d\n", -err);
break;
}/* Handle events */
for (i = 0; i < err; i++) {
err = poller_do_action(poller, items[i]);
if (err)
/* We are done. */
break;
}
}io_req_deinit(&req);
io_queue_unbind(&q);
poller_destroy(poller);Exactly using the same pattern all other types of file descriptors can
be used. See io-test.c for details, where signalfd is shown as example.The nice thing about this is that any other protocol can be implemented
as a stack, e.g. zmtp-test.c accepts ZMTP (ZeroMQ) REQ sockets.Why?
o No threads.
o No C++, no Java.
o Plain C.
o Single point of wait and control.
o Event driven.Howto:
$ makethen
$ ./io-test
or
$ ./zmtp-test
Author:
Roman Pen