• Stars
    star
    188
  • Rank 205,563 (Top 5 %)
  • Language
    C
  • License
    BSD 3-Clause "New...
  • Created over 11 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

redis's async event loop library

libae

Redis's async event library, you can use it in your projects.

supported event multiplexing model

  • epoll
  • kqueue
  • ev_port
  • select

Example

Timer

Print Hello, World on screen every 10 seconds:

int print(struct aeEventLoop *loop, long long id, void *clientData)
{
    printf("%lld - Hello, World\n", id);
    return -1;
}

int main(void)
{
    aeEventLoop *loop = aeCreateEventLoop(0);
    int i;
    for (i = 0; i < 10; i ++) {
        aeCreateTimeEvent(loop, i*1000, print, NULL, NULL);
    }
    aeMain(loop);
    aeDeleteEventLoop(loop);
    return 0;
}

Echo server

Start an echo server on 8000:

void writeToClient(aeEventLoop *loop, int fd, void *clientdata, int mask)
{
    char *buffer = clientdata;
    printf("%p\n", clientdata);
    write(fd, buffer, strlen(buffer));
    free(buffer);
    aeDeleteFileEvent(loop, fd, AE_WRITABLE);
}

void readFromClient(aeEventLoop *loop, int fd, void *clientdata, int mask)
{
    int buffer_size = 1024;
    char *buffer = malloc(sizeof(char) * buffer_size);
    bzero(buffer, buffer_size);
    int size;
    size = read(fd, buffer, buffer_size);
    aeCreateFileEvent(loop, fd, AE_WRITABLE, writeToClient, buffer);
}

void acceptTcpHandler(aeEventLoop *loop, int fd, void *clientdata, int mask)
{
    int client_port, client_fd;
    char client_ip[128];
    // create client socket
    client_fd = anetTcpAccept(NULL, fd, client_ip, 128, &client_port);
    printf("Accepted %s:%d\n", client_ip, client_port);

    // set client socket non-block
    anetNonBlock(NULL, client_fd);

    // regist on message callback
    int ret;
    ret = aeCreateFileEvent(loop, client_fd, AE_READABLE, readFromClient, NULL);
    assert(ret != AE_ERR);
}

int main()
{
    int ipfd;
    // create server socket
    ipfd = anetTcpServer(NULL, 8000, "0.0.0.0", 0);
    assert(ipfd != ANET_ERR);

    // create main event loop
    aeEventLoop *loop;
    loop = aeCreateEventLoop(1024);

    // regist socket connect callback
    int ret;
    ret = aeCreateFileEvent(loop, ipfd, AE_READABLE, acceptTcpHandler, NULL);
    assert(ret != AE_ERR);

    // start main loop
    aeMain(loop);

    // stop loop
    aeDeleteEventLoop(loop);

    return 0;
}

original document

More Repositories

1

pick

create curses based interactive selection list in the terminal
Python
722
star
2

rust-memcache

memcache client for rust
Rust
128
star
3

vox

Simple and lightweight Go web framework inspired by koa
Go
84
star
4

pic2ascii

convert pictrue to ascii code
Python
63
star
5

MySensors

Show macOS's chip tempratures and fan speeds on menu bar.
Swift
35
star
6

multicorn

Multicorn is a multi-interpreter server for Python.
Python
34
star
7

rust-ping

Rust
20
star
8

request

HTTP client for haskell, inpired by requests and http-dispatch.
Haskell
11
star
9

browsercookies

Loads cookies from your browsers
Go
10
star
10

.vim

Vim Script
10
star
11

backports.interpreters

interpreters module in python which is backported from the future
Python
9
star
12

Slide-Bottom-Bar

A slide bottom action bar on Android
Java
9
star
13

python-memcache

Experimental memcached client library for python.
Python
7
star
14

any

Go
6
star
15

qidong

Young, simple, sometimes naïve miHoYo / HoYoverse games launcher.
Python
5
star
16

pie

scheme to python-bytecode compiler
Python
5
star
17

monologue

Simple blog system based on Django.
JavaScript
4
star
18

chrysanthemum

Go
4
star
19

aisk.me

A blog for http://aisk.me runs on racket
4
star
20

rust-fire

Turn your function(s) to a command line app with one line of code.
Rust
4
star
21

meowscript

JavaScript
3
star
22

presentations

HTML
3
star
23

a-byte-of-haskell

Haskell 简明教程
Python
3
star
24

awpie

Using Python as awk alternative
Python
3
star
25

leancloud-python-todo-app

HTML
2
star
26

puck

scheme implementation in C++
C++
2
star
27

Salt-Pass

Java
2
star
28

naive

Naive is a too young too simple template engine
Python
2
star
29

Rhino-REPL

A JavaScript REPL using Rhino runs on Android
Java
2
star
30

rust-filelock

Rust
2
star
31

algorithms

C++
1
star
32

leancloud-python-test-app

Python
1
star
33

boat

lodash/underscore for go
Go
1
star
34

playbook

A ansible playbook to deploy a vps server.
1
star
35

test-pypi-package-build

Python
1
star
36

.emacs.d

My emacs config
Emacs Lisp
1
star
37

simpleflake.nim

Nim
1
star
38

dotfiles

my dot files
Vim Script
1
star
39

cloudcode-python-sdk

Python
1
star
40

rd-json-parser

A simple recursive descent parser parser
C++
1
star
41

obama

JavaScript
1
star
42

goblin

Go
1
star
43

leancloud-php-sdk

SDK for leancloud
PHP
1
star