All Projects → codepr → ev

codepr / ev

Licence: BSD-2-Clause License
Lightweight event-loop library based on multiplexing IO

Programming Languages

c
50402 projects - #5 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to ev

epump
ePump是一个基于I/O事件通知、非阻塞通信、多路复用、多线程等机制开发的事件驱动模型的 C 语言应用开发框架,利用该框架可以很容易地开发出高性能、大并发连接的服务器程序。
Stars: ✭ 26 (+73.33%)
Mutual labels:  select, event-driven, epoll
Zeus
A high performance, cross-platform Internet Communication Engine. Developed with native socket API. Aim at handling millions of concurrent connections.
Stars: ✭ 30 (+100%)
Mutual labels:  select, event-driven, epoll
libapenetwork
Fast cross-platform async network library
Stars: ✭ 17 (+13.33%)
Mutual labels:  event-driven, kqueue, epoll
netpoll
Package netpoll implements a network poller based on epoll/kqueue.
Stars: ✭ 38 (+153.33%)
Mutual labels:  event-driven, kqueue, epoll
xm2cloud term
powerful webssh that developed with django, channels, xterm,ioloop
Stars: ✭ 17 (+13.33%)
Mutual labels:  select, kqueue, epoll
Gnet
🚀 gnet is a high-performance, lightweight, non-blocking, event-driven networking framework written in pure Go./ gnet 是一个高性能、轻量级、非阻塞的事件驱动 Go 网络框架。
Stars: ✭ 5,736 (+38140%)
Mutual labels:  event-driven, kqueue, epoll
toyhttpd
I/O 模型练手代码,分别使用阻塞式 I/O、select、poll 和 epoll 和 Java NIO 实现了简单的 HTTP Server
Stars: ✭ 43 (+186.67%)
Mutual labels:  select, poll, epoll
Libfiber
The high performance coroutine library for Linux/FreeBSD/MacOS/Windows, supporting select/poll/epoll/kqueue/iocp/windows GUI
Stars: ✭ 519 (+3360%)
Mutual labels:  select, epoll
fastsocket
⚠️⚠️⚠️ DEPRECATED
Stars: ✭ 14 (-6.67%)
Mutual labels:  kqueue, epoll
WebServer
High-performance multi-threaded tcp network server in c++11
Stars: ✭ 58 (+286.67%)
Mutual labels:  epoll, eventloop
hev-task-system
A simple, lightweight multi-task system (coroutines) for Unix (Linux/BSD/macOS)
Stars: ✭ 41 (+173.33%)
Mutual labels:  kqueue, epoll
Mongols
C++ high performance networking with TCP/UDP/RESP/HTTP/WebSocket protocols
Stars: ✭ 250 (+1566.67%)
Mutual labels:  tcp-server, epoll
Eomaia
一个基于reactor模式的Linux/C++网络库,支持one loop per thread机制。
Stars: ✭ 159 (+960%)
Mutual labels:  tcp-server, epoll
async
⏱ Promises and reactive-streams in Swift built for high-performance and scalability.
Stars: ✭ 35 (+133.33%)
Mutual labels:  kqueue, epoll
Mgx
🌈 A high performance network framework written in c++ (support tcp and http)
Stars: ✭ 15 (+0%)
Mutual labels:  tcp-server, epoll
Evpp
A modern C++ network library for developing high performance network services in TCP/UDP/HTTP protocols.
Stars: ✭ 2,850 (+18900%)
Mutual labels:  tcp-server, nonblocking
llb
Dead simple event-driven load-balancer
Stars: ✭ 27 (+80%)
Mutual labels:  event-driven, epoll
tunnel
一款单线程、轻量级和高性能的内网穿透程序,支持TCP流量转发(支持所有TCP上层协议,包括HTTP,SSH等),支持多客户端同时连接
Stars: ✭ 39 (+160%)
Mutual labels:  select, epoll
Libuev
Lightweight event loop library for Linux epoll() family APIs
Stars: ✭ 170 (+1033.33%)
Mutual labels:  event-driven, epoll
Message Io
Event-driven message library for building network applications easy and fast.
Stars: ✭ 321 (+2040%)
Mutual labels:  tcp-server, event-driven

EV

Light event-loop library loosely inspired by the excellent libuv, in a single small (< 700 sloc) header, based on the common IO multiplexing implementations available, epoll on linux, kqueue on BSD-like and OSX, poll/select as a fallback, dependencies-free. A common usage of the library is to craft event-driven TCP servers, ev_tcp.h exposes a set of APIs to fulfill this purpose in a simple manner.

TLS is supported as well through OpenSSL, and source have to be compiled adding a -DHAVE_OPENSSL=1 to enable it. Of course it requires libssl-dev installed on the host machine to work.

In conclusion the library is composed of 2 distinct modules

  • ev.h a generic eventloop for I/O bound concurrency on a single-thread:
    • Based on the best multiplexing IO implementation available on the host, supporting epoll/poll/select on linux and kqueue on BSD
    • All IO operations are done in a non-blocking way
    • Support for time based repeated tasks
  • ev_tcp.h exposes a set of APIs to simply create an event-driven TCP server using ev.h as the main engine:
    • TCP/UNIX socket connections
    • Basic TLS support through OpenSSL
    • Callback oriented design

To adopt these libraries it's required to define a value just before inclusion in one file only in the project:

#define EV_SOURCE
#include "ev.h"

Or in case of ev_tcp.h

#define EV_SOURCE
#define EV_TCP_SOURCE
#include "ev_tcp.h"

Running examples

A simple event-driven echo server

$ make echo-server

Write periodically on the screen ping and pong on different frequencies, referred as cron tasks

$ make ping-pong

Helper APIs

Lightweight event-driven hello world TCP server

#include <stdio.h>
#include <stdlib.h>
#define EV_SOURCE      // add before ev_tcp
#define EV_TCP_SOURCE  // add before ev_tcp
#include "../ev_tcp.h"

#define HOST    "127.0.0.1"
#define PORT    5959
#define BACKLOG 128

static void on_close(ev_tcp_handle *client, int err) {
    (void) client;
    if (err == EV_TCP_SUCCESS)
        printf("Connection closed\n");
    else
        printf("Connection closed: %s\n", ev_tcp_err(err));
    free(client);
}

static void on_write(ev_tcp_handle *client) {
    (void) client;
    printf("Written response\n");
}

static void on_data(ev_tcp_handle *client) {
    printf("Received %li bytes\n", client->buffer.size);
    if (strncmp(client->buffer.buf, "quit", 4) == 0)
        ev_tcp_close_handle(client);
    else
        // Enqueue a write of the buffer content for the next loop cycle
        ev_tcp_enqueue_write(client);
        // If want to respond on the same loop cycle
        // ev_tcp_write(client);
}

static void on_connection(ev_tcp_handle *server) {
    ev_tcp_handle *client = malloc(sizeof(*client));
    if (!client) {
        fprintf(stderr, "On connection failed: Out of memory");
        exit(EXIT_FAILURE);
    }
    int err = ev_tcp_server_accept(server, client, on_data, on_write);
    if (err < 0)
        free(client);
    else
        ev_tcp_handle_set_on_close(client, on_close);
}

int main(void) {

    ev_context *ctx = ev_get_ev_context();
    ev_tcp_server server;
    int err = 0;
    if ((err = ev_tcp_server_init(&server, ctx, 128)) < 0) {
        fprintf(stderr, "ev_tcp_server_init failed: %s", ev_tcp_err(err));
        exit(EXIT_FAILURE);
    }
    // To set TLS using OpenSSL
    // struct ev_tls_options tls_opt = {
    //     .ca = CA,
    //     .cert = CERT,
    //     .key = KEY
    // };
    // tls_opt.protocols = EV_TLSv1_2|EV_TLSv1_3;
    // ev_tcp_server_set_tls(&server, &tls_opt);
    int err = ev_tcp_server_listen(&server, HOST, PORT, on_connection);
    if (err < 0)
        exit(EXIT_FAILURE);
    // Blocking call
    ev_tcp_server_run(&server);
    // This could be registered to a SIGINT|SIGTERM signal notification
    // to stop the server with Ctrl+C
    ev_tcp_server_stop(&server);

    return 0;
}

Take a look to examples/ directory for more snippets.

Roadmap

  • (Re)Move server abstraction on generic ev_tcp_handle, add client
  • UDP helper APIs
  • Improve error handling
  • Documentation
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].