Git Product home page Git Product logo

server-framework's Introduction

Server Framework in Modern C

It is simple to use this framework to build your own service. It is based on Protocol structure and callbacks, so that we can dynamically change protocols and support stuff such as HTTP requests.

The framework consists of the following components:

  • async: A native POSIX thread pool.
    • It uses a combination of a pipe (for wakeup signals) and mutexes (for managing the task queue).
    • Give a basic layer of protection to any server implementation.
  • reactor: Reactor pattern implementation using callbacks
    • Linux epoll system call abstraction
    • Supported events: ready to read/write, closed, reactor exit
  • protocol-server - server construction library
    • protocol-server manages everything that makes a server run, including the thread pool, process forking, accepting new connections, setting up the initial protocol for new connections, and user space socket writing buffers.

Here is a simple example:

#include "protocol-server.h"
#include <stdio.h>
#include <string.h>

void on_open(server_pt server, int sockfd) {
    printf("A connection was accepted on socket #%d.\n", sockfd);
}

void on_close(server_pt server, int sockfd) {
    printf("Socket #%d is now disconnected.\n", sockfd);
}

// simple echo implementation: the main callback
void on_data(server_pt server, int sockfd) {
    // We will assign a reading buffer on the stack
    char buff[1024];
    ssize_t incoming = 0;
    // Read everything, this is edge triggered, `on_data` won't be called
    //again until all the data was read.
    while ((incoming = Server.read(server, sockfd, buff, 1024)) > 0) {
        // since the data is stack allocated, we'll write a copy
        // otherwise, we would avoid a copy using Server.write_move
        Server.write(server, sockfd, buff, incoming);
        if (!memcmp(buff, "bye", 3)) {
            // close the connection automatically AFTER the buffer was sent.
            Server.close(server, sockfd);
        }
    }
}

int main(void) {
  /* create the echo protocol object */
  struct Protocol protocol = { .on_open = on_open,
                               .on_close = on_close,
                               .on_data = on_data,
                               .service = "echo"};
  // use the macro start_server, which will call Server.listen(settings)
  // with the settings we provide.
  start_server(.protocol = &protocol, .timeout = 10, .threads = 8);
}

Documentation

The code is heavily commented, and you can generate API documentation via Doxygen. Check the directory html for details.

Licensing

server-framework is freely redistributable under the two-clause BSD License. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

server-framework's People

Contributors

jserv avatar rniczh avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.