Skip to content

mallocobject/NetX

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

███╗   ██╗███████╗████████╗██╗  ██╗
████╗  ██║██╔════╝╚══██╔══╝╚██╗██╔╝
██╔██╗ ██║█████╗     ██║    ╚███╔╝ 
██║╚██╗██║██╔══╝     ██║    ██╔██╗ 
██║ ╚████║███████╗   ██║   ██║  ██║
╚═╝  ╚═══╝╚══════╝   ╚═╝   ╚═╝  ╚═╝
  

C++20 Platform CMake Developing

NETX - High Performance Network Framework


Build

Use CMake to generate and build the executable in Release mode:

cmake -B build -DCMAKE_BUILD_TYPE=Release && cmake --build build

HTTP Server Setup Example

#include "netx/http/server.hpp"

using namespace netx::http;
using namespace netx::net;
using namespace netx::async;
using namespace std::chrono_literals;

int main()
{
	HttpServer::server()
		.listen("127.0.0.1", 8080)
		.route(
            "GET", "/",
            [](const HttpRequest& req, HttpResponse* res, Stream* stream) -> Task<>
            {
                res->status(200)
                .content_type("text/html")
                .keep_alive(
                    req.header("connection") != "close" &&
                    !(req.version == "HTTP/1.0" &&
                    req.header("connection") != "keep-alive"))
                .body("<h1>Hello Netx</h1>");

                co_await stream->write(res->to_formatted_string());
            })
        .timeout(3s)
		.loop(8)
		.start();
}

Settings

  • .listen("127.0.0.1", 8080) binds the HTTP server to the local address and port.
  • .route("GET", "/", ...) registers a handler for GET /.
  • .timeout(3s) closes or times out idle connections after 3s.
  • .loop(8) starts 8 worker event loops for request processing.
  • .start() launches the server.

Logging

You can configure logging with environment variables before starting the executable.

ELOG_PATH=/absolute/log/dir ELOG_LEVEL=INFO ./build/test/test1
  • ELOG_PATH={dir} sets the log output directory. The directory must already exist.
  • ELOG_LEVEL={TRACE, DEBUG, INFO, WARN, ERROR, FATAL} filters terminal log output only.
  • Async file logging is still written in full and is not affected by ELOG_LEVEL.

About

Async Networking eXtended

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors