A http/websocket server framework on linux.

Overview

🌟 Unixtar

✨Unique Star✨

中文版传送门

The framework is a Web-Server on unix based system.

Without using any third-party libraries, the framework writes from unix system calls and standard C library functions.

The framework adopts the model of Epoll + NetThreads + WorkerThreads: model-overview

The framework focuses on the following tasks:

  • The transport and IP layers use UNIX domain socket directly, with each network thread working with an Epoll object and multiple worker threads to provide concurrency.
  • Http server. Responsible for short connection requests. Framework completes Http protocol Serialization and Parsing. The Http body can be serialized by Protobuf. Framework Provides overload protection capability.
  • WebSocket server. Responsible for long connection requests, providing the ability to actively push messages to self or/and other connections. Framework completes WebSocket protocol handshake, Packing, Parsing, wave.
  • Reverse proxy. Provides the ability to forward requests to service nodes and load balancing. You can choose among several load balancing strategies.
  • Coroutine. Coroutine switching, register set saving and restoring, stack frames saving and restoring are all implemented by AT&T assembly, Api still implementation in progress.
  • The code of the transport layer module is completely independent of the specific application layer protocol, you can easily add your own application layer protocols by inheriting the ApplicationPacket class.
  • You can easily add network interfaces by inheriting the NetSceneBase class.
  • You can use the built-in thread pool singleton to complete your own asynchronous tasks, which can be: immediate, immediate with serialized tag, periodic, or delayed task.

Workflow when processing HTTP short link requests: workflow

Build (unix)

$ git clone --recursive https://github.com/xingyuuchen/unixtar.git framework
$ cd framework/script
$ bash autogen.sh   # do this if you want to use ProtoBuf.
$ bash cmake.sh -d   # -d: build will run as a daemon process, logs redirected to file using linux rsyslog, instead of stdout.

Example Usage

Http short-link

You are only responsible to write NetScene to get your business logic done.

Each network interface is represented by a class(NetScene_xxx). They all inherit indirectly from NetSceneBase. You can treat it as a Servlet in Java.

If you use ProtoBuf to serialize your data, inherit from NetSceneProtoBuf and use the POST request.

Else, to customize your network communication protocol, inherit from NetSceneCustom. GET and POST are supported.

After defining your network interface classes and implement your business logic, please register your class to the framework: NetSceneDispatcher::Instance()::RegisterNetScene();.

You can custom some configuration by editing webserverconf.yml. You can custom:

  • Port on which the process is listening.
  • Number of threads handling network events.
  • Number of threads handling business logic.
  • Maximum number of connections.
  • Maximum business backlog (exceeding this threshold is considered overloaded).
  • Reverse proxy server information, the period during which heartbeat packets are sent.

After configuration, call WebServer::Instance().Serve();, and the service just gets started!

Hope you enjoy :)

(); NetSceneDispatcher::Instance().RegisterNetScene(); NetSceneDispatcher::Instance().RegisterNetScene(); WebServer::Instance().Serve(); LogI("Webserver Down") return 0; } ">
#include "log.h"
#include "webserver.h"
#include "netscenedispatcher.h"
#ifdef DAEMON
#include "daemon.h"
#endif

int main(int ac, char **argv) {
#ifdef DAEMON
    if (unixtar::Daemonize() < 0) {
        printf("daemonize failed\n");
        return 0;
    }
#endif
    
    logger::OpenLog(argv[0]);

    WebServer::Instance().Config();
    
    LogI("Launching Server...")
    
    // NetScene_YourBusiness must inherit from NetSceneBase, which is your
    // predefined network interface (e.g. A specific Http url route)
    // See class NetSceneGetIndexPage below for detail.
    NetSceneDispatcher::Instance().RegisterNetScene();
    NetSceneDispatcher::Instance().RegisterNetScene();
    NetSceneDispatcher::Instance().RegisterNetScene();
    
    WebServer::Instance().Serve();
    
    LogI("Webserver Down")
    return 0;
}

Here is an example of a network interface class:

class NetSceneGetIndexPage : public NetSceneCustom { public: NetSceneGetIndexPage(); // The unique NetScene type. int GetType() override; // New instance of your NetScene here. NetSceneBase *NewInstance() override; // Your business logic here. int DoSceneImpl(const std::string &_in_buffer) override; // Http body pointer. void *Data() override; // How long your http body is. size_t Length() override; // Http url route. const char *Route() override; // Http Content-Type. const char *ContentType() override; private: char resp_[128] {0, }; static const char *const kUrlRoute; static const char *const kRespFormat; static std::mutex mutex_; }; ">
#pragma once
#include "netscenecustom.h"
#include <mutex>

class NetSceneGetIndexPage : public NetSceneCustom {
  public:
    NetSceneGetIndexPage();

    // The unique NetScene type.
    int GetType() override;

    // New instance of your NetScene here.
    NetSceneBase *NewInstance() override;

    // Your business logic here.
    int DoSceneImpl(const std::string &_in_buffer) override;
    
    // Http body pointer.
    void *Data() override;

    // How long your http body is.
    size_t Length() override;

    // Http url route.
    const char *Route() override;

    // Http Content-Type.
    const char *ContentType() override;

  private:
    char                        resp_[128] {0, };
    static const char *const    kUrlRoute;
    static const char *const    kRespFormat;
    static std::mutex           mutex_;
    
};
lock(mutex_); // count up visitors. snprintf(resp_, sizeof(resp_), kRespFormat, ++visit_times_since_last_boot_); return 0; } const char *NetSceneGetIndexPage::ContentType() { return http::HeaderField::kTextPlain; } void *NetSceneGetIndexPage::Data() { return resp_; } size_t NetSceneGetIndexPage::Length() { return strlen(resp_); } const char *NetSceneGetIndexPage::Route() { return kUrlRoute; } ">
#include "netscene_getindexpage.h"
#include "constantsprotocol.h"
#include "log.h"
#include "http/headerfield.h"

const char *const NetSceneGetIndexPage::kUrlRoute = "/";

const char *const NetSceneGetIndexPage::kRespFormat =
        "If you see this, the server is running normally, %d visits since last boot.";

std::mutex NetSceneGetIndexPage::mutex_;

NetSceneGetIndexPage::NetSceneGetIndexPage() : NetSceneCustom() {}

int NetSceneGetIndexPage::GetType() { return 0; }

NetSceneBase *NetSceneGetIndexPage::NewInstance() { return new NetSceneGetIndexPage(); }

int NetSceneGetIndexPage::DoSceneImpl(const std::string &_in_buffer) {
    static int visit_times_since_last_boot_ = 0;
    std::unique_lock lock(mutex_);
    // count up visitors.
    snprintf(resp_, sizeof(resp_), kRespFormat, ++visit_times_since_last_boot_);
    return 0;
}

const char *NetSceneGetIndexPage::ContentType() { return http::HeaderField::kTextPlain; }

void *NetSceneGetIndexPage::Data() { return resp_; }

size_t NetSceneGetIndexPage::Length() { return strlen(resp_); }

const char *NetSceneGetIndexPage::Route() { return kUrlRoute; }

Note: It is highly recommended to use ProtoBuf. Some predefined protobuf .proto files is in /protos/, you can run:

$ cd framework/script
$ bash autogen.sh

to generate protobuf c++ files, see NetSceneHelloSvr.cc for instance.

WebSocket long-link

Although Http protocol can maintain long links, it does not provide the server with the ability to actively push messages to the client. Use WebSocket to fill this gap.

Example Project

Plant-Recognition-Server is a web-server project, under the hood it is unixtar provides basic http network capability.

Reverse Proxy

You can launch a reverse proxy by commands below:

$ cd framework/script
$ bash launchproxy.sh

Reverse proxy do such things:

  • Forward. Forward Http packet to web servers who truly handles request, then pass back Http response.
  • Load Balance. You can chose among three different rules: Poll, By weight, IP Hash.
  • Registry Center. Receive heartbeats from all web server nodes and maintain states for them.

Configure your reverse proxy by editing reverseproxy/proxyserverconf.yml. You can custom:

  • Port that reverse proxy server listens on.
  • Load balancing strategy.
  • All web server nodes available to forward Http request.
  • Maximum connections at the same time.
  • Number of threads who handles network events.

License

If you use this software for your research, a footnote with the link to this repository is appreciated: github.com/xingyuuchen/unixtar.git.

Or for citation with BibTeX:

@misc{2021unixtar,
  title={unixtar: A Http/WebSocket Server Framework on Linux.},
  author={Xingyu Chen},
  howpublished={\url{https://github.com/xingyuuchen/unixtar.git}},
  year={2021}
}
Owner
xingyuuchen
An undergraduate student in Xi'an Jiaotong University
xingyuuchen
Drogon: A C++14/17 based HTTP web application framework running on Linux/macOS/Unix/Windows

English | 简体中文 | 繁體中文 Overview Drogon is a C++14/17-based HTTP application framework. Drogon can be used to easily build various types of web applicat

An Tao 7.7k Aug 4, 2022
Experimental, scalable, high performance HTTP server

Lwan Web Server Lwan is a high-performance & scalable web server. The project web site contains more details. Build status OS Arch Release Debug Stati

Leandro A. F. Pereira 5.6k Aug 3, 2022
Embeddable Event-based Asynchronous Message/HTTP Server library for C/C++

libasyncd Embeddable Event-based Asynchronous Message/HTTP Server library for C/C++. What is libasyncd? Libasyncd is an embeddable event-driven asynch

Seungyoung 165 May 25, 2022
A high performance, middleware oriented C++14 http web framework please use matt-42/lithium instead

A high performance, middleware oriented C++14 http web framework please use matt-42/lithium instead

Matthieu Garrigues 1.7k Aug 2, 2022
Pistache is a modern and elegant HTTP and REST framework for C++

Pistache is a modern and elegant HTTP and REST framework for C++. It is entirely written in pure-C++17* and provides a clear and pleasant API.

null 2.7k Aug 7, 2022
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.

Cutelyst - The Qt Web Framework A Web Framework built on top of Qt, using the simple and elegant approach of Catalyst (Perl) framework. Qt's meta obje

Cutelyst 779 Aug 3, 2022
TreeFrog Framework : High-speed C++ MVC Framework for Web Application

Small but Powerful and Efficient TreeFrog Framework is a high-speed and full-stack web application framework based on C++ and Qt, which supports HTTP

TreeFrog Framework 1.1k Aug 7, 2022
C library to create simple HTTP servers and Web Applications.

Onion http server library Travis status Coverity status Onion is a C library to create simple HTTP servers and Web Applications. master the developmen

David Moreno Montero 1.9k Aug 10, 2022
bittyhttp - A threaded HTTP library for building REST services in C.

bittyhttp - A threaded HTTP library for building REST services in C.

Colin Luoma 12 Nov 29, 2021
Embedded C/C++ web server

CivetWeb The official home of CivetWeb is https://github.com/civetweb/civetweb Continuous integration for Linux and macOS (Travis CI): Continuous inte

null 2.2k Aug 11, 2022
The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.

Welcome! The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design

Microsoft 7k Aug 8, 2022
A C++11 RESTful web server library

Served Overview Served is a C++ library for building high performance RESTful web servers. Served builds upon Boost.ASIO to provide a simple API for d

Meltwater 693 Aug 5, 2022
A lightweight Bedorck Dedicated Server Plugin Loader

LiteLoader 简体中文 A lightweight Bedorck Dedicated Server Plugin Loader Based on BedrockX Install Download LiteLoader from Releases or Actions, unzip it

null 420 Aug 6, 2022
cserv is an event-driven and non-blocking web server

cserv is an event-driven and non-blocking web server. It ideally has one worker process per cpu or processor core, and each one is capable of handling thousands of incoming network connections per worker. There is no need to create new threads or processes for each connection.

null 41 Jul 2, 2022
Tntnet is a web application server for web applications written in C++.

Tntnet is a web application server for web applications written in C++.

Tommi Mäkitalo 72 Aug 1, 2022
Crow is very fast and easy to use C++ micro web framework (inspired by Python Flask)

Crow is C++ microframework for web. (inspired by Python Flask) #include "crow.h" int main() { crow::SimpleApp app; CROW_ROUTE(app, "/")([]()

Jaeseung Ha 6.9k Aug 13, 2022
Your high performance web application C framework

facil.io is a C micro-framework for web applications. facil.io includes: A fast HTTP/1.1 and Websocket static file + application server. Support for c

Bo 1.6k Aug 2, 2022
🌱Light and powerful C++ web framework for highly scalable and resource-efficient web application. It's zero-dependency and easy-portable.

Oat++ News Hey, meet the new oatpp version 1.2.5! See the changelog for details. Check out the new oatpp ORM - read more here. Oat++ is a modern Web F

Oat++ 5.6k Aug 11, 2022