Crafter.Network/README.md
2025-11-03 17:52:45 +01:00

3.6 KiB

Crafter.Network

A cross-platform C++ networking library providing TCP and HTTP client/server functionality with modern C++ features.

Overview

Crafter.Network is a comprehensive networking library designed for modern C++ applications. It provides both TCP and HTTP networking capabilities with support for synchronous and asynchronous operations, making it suitable for a wide range of networking tasks.

Features

  • TCP Networking: Client and server implementations for TCP connections
  • HTTP Support: Full HTTP client and server implementations with routing capabilities
  • Asynchronous Operations: Thread pool-based async operations for improved performance
  • Cross-Platform: Built for Unix-like systems with socket-based networking
  • Modern C++: Uses C++ modules, STL containers, and modern C++ features

Architecture

The library follows a modular design using C++20 modules:

Core Modules

  • Crafter.Network: Main module that exports all components
  • Crafter.Network:ClientTCP: TCP client implementation
  • Crafter.Network:ListenerTCP: TCP server implementation
  • Crafter.Network:ClientHTTP: HTTP client implementation
  • Crafter.Network:ListenerHTTP: HTTP server implementation
  • Crafter.Network:HTTP: HTTP protocol utilities and data structures

Components

TCP Components

ClientTCP

// Create a TCP client
Crafter::ClientTCP client("localhost", 8080);
client.Send("Hello World", 11);

// Receive data
std::vector<char> data = client.RecieveSync();

ListenerTCP

// Create a TCP listener
auto callback = [](Crafter::ClientTCP* client) {
    // Handle new connection
};
Crafter::ListenerTCP listener(8080, callback);
listener.ListenSyncSync(); // Synchronous listening

HTTP Components

ClientHTTP

// Create an HTTP client
Crafter::ClientHTTP client("httpbin.org", 80);

// Send HTTP request
std::string request = Crafter::CreateRequestHTTP("GET", "/get", "httpbin.org");
Crafter::HTTPResponse response = client.Send(request);

ListenerHTTP

// Create an HTTP listener with routes
std::unordered_map<std::string, std::function<std::string(const Crafter::HTTPRequest&)>> routes;
routes["/hello"] = [](const Crafter::HTTPRequest& req) {
    return Crafter::CreateResponseHTTP("200 OK", "Hello World!");
};

Crafter::ListenerHTTP listener(8080, routes);
listener.Listen();

Build Configuration

The project uses a configuration system with multiple build targets:

  • base: Core interfaces only
  • lib: Static library build with dependencies
  • lib-debug: Debug static library build
  • lib-shared: Shared library build with dependencies

Testing

The library includes comprehensive tests covering:

  • Compilation verification
  • HTTP receive functionality
  • HTTP send functionality
  • HTTP send/receive operations
  • Keep-alive HTTP operations
  • Large HTTP data transfers

Dependencies

  • Crafter.Thread: Thread pool management for asynchronous operations

Usage Example

#include <Crafter.Network>
#include <iostream>

int main() {
    // Simple HTTP client example
    Crafter::ClientHTTP client("httpbin.org", 80);
    
    auto request = Crafter::CreateRequestHTTP("GET", "/get", "httpbin.org");
    auto response = client.Send(request);
    
    std::cout << "Status: " << response.status << std::endl;
    std::cout << "Body: " << response.body << std::endl;
    
    return 0;
}

License

This library is licensed under the GNU Lesser General Public License version 3.0. See LICENSE for more information.

Copyright (C) 2025 Catcrafts® Catcrafts.net