• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

embedded2016/server-framework: Asynchronous server framework in modern C

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

embedded2016/server-framework

开源软件地址:

https://github.com/embedded2016/server-framework

开源编程语言:

C 99.4%

开源软件介绍:

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.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap