You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Clone this respository and then use pkg.sh
to import dependencies.
$ git clone https://github.com/tidwall/evio.c
$ cd evio.c/
$ pkg.sh import
Example
Here's a simple echo server. Save the following file to echo.c
#include<stdlib.h>#include<stdio.h>#include<assert.h>#include"evio.h"voidserving(constchar**addrs, intnaddrs, void*udata) {
for (inti=0; i<naddrs; i++) {
printf("Serving at %s\n", addrs[i]);
}
}
voiderror(constchar*msg, boolfatal, void*udata) {
fprintf(stderr, "%s\n", msg);
}
int64_ttick(void*udata) {
printf("Tick\n");
// next tick in 1 second. This can be any duration in nanoseconds.return1e9;
}
voidopened(structevio_conn*conn, void*udata) {
printf("Connection opened: %s\n", evio_conn_addr(conn));
}
voidclosed(structevio_conn*conn, void*udata) {
printf("Connection closed: %s\n", evio_conn_addr(conn));
}
voiddata(structevio_conn*conn, constvoid*data, size_tlen, void*udata) {
// echo back to the connectionevio_conn_write(conn, data, len);
}
intmain() {
structevio_eventsevs= {
.serving=serving,
.error=error,
.tick=tick,
.opened=opened,
.closed=closed,
.data=data,
};
// Any number of addresses can be bound to the appliation.// Here we are listening on tcp port 9999 (ipv4 and ipv6), and at a local// unix socket file named "socket".// For ipv4 only use an ip address like tcp://127.0.0.1:9999// For ipv6 use something like tcp://[::1]:9999constchar*addrs[] = {
"tcp://localhost:9999",
"unix://socket",
};
// Run the application. This is a forever operation. evio_main(addrs, 2, evs, NULL);
}
Then build and run the server
$ cc *.c && ./a.out
And connect
$ nc localhost 9999
Multithreading
Use the evio_main_mt function to start the server using multiple threads.
// Use five threadsevio_main_mt(addrs, 2, evs, NULL, 5);
// Use ten threadsevio_main_mt(addrs, 2, evs, NULL, 10);
// Use the number of threads equal to the number of cores on the machine.evio_main_mt(addrs, 2, evs, NULL, 0);
About
A framework for building event based networking applications.