{"id":17007540,"url":"https://github.com/wkhallen/cppdtp","last_synced_at":"2025-03-22T11:47:10.102Z","repository":{"id":106424457,"uuid":"483005972","full_name":"WKHAllen/cppdtp","owner":"WKHAllen","description":"Modern cross-platform networking interfaces for C++.","archived":false,"fork":false,"pushed_at":"2025-03-13T00:47:03.000Z","size":218,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-13T01:28:45.334Z","etag":null,"topics":["cpp","network","socket","socket-client","socket-server"],"latest_commit_sha":null,"homepage":"https://wkhallen.com/dtp","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WKHAllen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-18T21:43:22.000Z","updated_at":"2025-03-13T00:47:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"c435f411-1983-409b-8cff-c4204113a0c2","html_url":"https://github.com/WKHAllen/cppdtp","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WKHAllen%2Fcppdtp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WKHAllen%2Fcppdtp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WKHAllen%2Fcppdtp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WKHAllen%2Fcppdtp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WKHAllen","download_url":"https://codeload.github.com/WKHAllen/cppdtp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244952780,"owners_count":20537472,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","network","socket","socket-client","socket-server"],"created_at":"2024-10-14T05:26:04.347Z","updated_at":"2025-03-22T11:47:10.097Z","avatar_url":"https://github.com/WKHAllen.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Data Transfer Protocol for C++\n\nModern cross-platform networking interfaces for C++.\n\n## Data Transfer Protocol\n\nThe Data Transfer Protocol (DTP) is a larger project to make ergonomic network programming available in any language.\nSee the full project [here](https://wkhallen.com/dtp/).\n\n## Creating a server\n\nA server can be built using the `Server` implementation:\n\n```c++\n#include \"cppdtp.hpp\"\n#include \u003ciostream\u003e\n\nusing namespace std;\n\n// Create a server that receives strings and returns the length of each string\nclass MyServer : cppdtp::Server\u003cint, string\u003e {\nprivate:\n    void receive(size_t client_id, string data) override {\n        // Send back the length of the string\n        send(client_id, data.length());\n    }\n\n    void connect(size_t client_id) override {\n        cout \u003c\u003c \"Client with ID \" \u003c\u003c client_id \u003c\u003c \" connected\" \u003c\u003c endl;\n    }\n\n    void disconnect(size_t client_id) override {\n        cout \u003c\u003c \"Client with ID \" \u003c\u003c client_id \u003c\u003c \" disconnected\" \u003c\u003c endl;\n    }\n\npublic:\n    MyServer() : cppdtp::Server\u003cint, string\u003e() {}\n};\n\nint main() {\n    // Start the server\n    MyServer server;\n    server.start(\"127.0.0.1\", 29275);\n\n    return 0;\n}\n```\n\n## Creating a client\n\nA client can be built using the `Client` implementation:\n\n```c++\n#include \"cppdtp.hpp\"\n#include \u003ciostream\u003e\n#include \u003cassert.h\u003e\n\nusing namespace std;\n\n// Create a client that sends a message to the server and receives the length of the message\nclass MyClient : cppdtp::Client\u003cstring, int\u003e {\nprivate:\n    string message;\n\n    void receive(int data) override {\n        // Validate the response\n        cout \u003c\u003c \"Received response from server: \" \u003c\u003c data \u003c\u003c endl;\n        assert(data == message.length());\n    }\n\n    void disconnected() override {\n        cout \u003c\u003c \"Unexpectedly disconnected from server\" \u003c\u003c endl;\n    }\n\npublic:\n    MyClient(string message_) : cppdtp::Client\u003cstring, int\u003e(), message(message_) {}\n};\n\nint main() {\n    // Connect to the server\n    string message = \"Hello, server!\";\n    MyClient client(message);\n    client.connect(\"127.0.0.1\", 29275);\n\n    // Send a message to the server\n    client.send(message);\n\n    return 0;\n}\n```\n\n## Serialization\n\nThe protocol is able to serialize and deserialize most types with ease. Custom types can be used, though for\ndeserialization purposes, they must be default constructible. For custom types that\nare [POD types](https://stackoverflow.com/questions/146452/what-are-pod-types-in-c), the default\nserialization/deserialization implementations should be sufficient. For custom types that contain pointers, contain\ndynamically sized members, or are in some other way not POD types, it will be necessary to provide a\nserialization/deserialization implementation for them. To do this, write an implementation for streaming the type\nto `cppdtp::mem_ostream` and from `cppdtp::mem_istream`. Below is an example, which is already implemented for you:\n\n```c++\ntemplate\u003ctypename T\u003e\ncppdtp::mem_ostream \u0026operator\u003c\u003c(cppdtp::mem_ostream \u0026out, const std::vector \u003cT\u003e \u0026vec) {\n    static_assert(std::is_default_constructible\u003cT\u003e::value, \"T must be default constructible\");\n\n    // Serialize a std::vector\u003cT\u003e\n\n    size_t size = vec.size();\n    out \u003c\u003c size;\n\n    for (size_t i = 0; i \u003c vec.size(); i++) {\n        out \u003c\u003c vec[i];\n    }\n\n    return out;\n}\n\ntemplate\u003ctypename T\u003e\ncppdtp::mem_istream \u0026operator\u003e\u003e(cppdtp::mem_istream \u0026in, std::vector \u003cT\u003e \u0026vec) {\n    static_assert(std::is_default_constructible\u003cT\u003e::value, \"T must be default constructible\");\n\n    // Deserialize a std::vector\u003cT\u003e\n\n    size_t size = 0;\n    in \u003e\u003e size;\n\n    for (size_t i = 0; i \u003c size; i++) {\n        T val;  // This is why `T` must be default constructible\n        in \u003e\u003e val;\n        vec.push_back(val);\n    }\n\n    return in;\n}\n```\n\nFor more information on the serialization/deserialization implementation details,\nsee [github.com/shaovoon/simplebinstream](https://github.com/shaovoon/simplebinstream).\n\n## Compilation\n\nThe protocol has a few dependencies that must be included when compiling:\n\n### Compiling on Windows\n\n- Link Winsock (`-lWs2_32`)\n- Link OpenSSL 3.0\n\n### Compiling on other platforms\n\n- Link pthread (`-lpthread`)\n- Link OpenSSL 3.0\n\nFor more information on the compilation process, see the [Makefile](Makefile).\n\n## Security\n\nInformation security comes included. Every message sent over a network interface is encrypted with AES-256. Key\nexchanges are performed using a 2048-bit RSA key-pair.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwkhallen%2Fcppdtp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwkhallen%2Fcppdtp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwkhallen%2Fcppdtp/lists"}