{"id":18409291,"url":"https://github.com/sdtelectronics/tev","last_synced_at":"2025-04-12T21:51:29.605Z","repository":{"id":105561328,"uuid":"447979063","full_name":"SdtElectronics/TeV","owner":"SdtElectronics","description":"Thin wrapper for boost.asio. Stateful services made easy.","archived":false,"fork":false,"pushed_at":"2022-05-02T14:31:58.000Z","size":53,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-16T05:16:04.774Z","etag":null,"topics":["asio","cpp11","tcp"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SdtElectronics.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-01-14T13:35:35.000Z","updated_at":"2022-01-26T11:43:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"2f0fe20a-df63-466a-b06d-3d1addb88d57","html_url":"https://github.com/SdtElectronics/TeV","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SdtElectronics%2FTeV","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SdtElectronics%2FTeV/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SdtElectronics%2FTeV/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SdtElectronics%2FTeV/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SdtElectronics","download_url":"https://codeload.github.com/SdtElectronics/TeV/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248637832,"owners_count":21137538,"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":["asio","cpp11","tcp"],"created_at":"2024-11-06T03:24:29.490Z","updated_at":"2025-04-12T21:51:29.582Z","avatar_url":"https://github.com/SdtElectronics.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TeV\n![build workflow](https://github.com/SdtElectronics/TeV/actions/workflows/build.yml/badge.svg)\n\nThin wrapper of boost.asio for creating applications based on asynchronous TCP sockets.\n\n* Based on asio library and therefore gained the same cross-platform ability\n* C++ 11 compatible\n* Deliberately avoid using `std::function\u003c\u003e` to reduce overhead of callbacks\n* Presents extremely simple interfaces supporting two styles of session definition:\n\u003ctable\u003e\n\u003ctr\u003e \n\u003ctd\u003e\nSession Defined with Class\n\u003c/td\u003e\n\u003ctd\u003e\nSession Defined with Functions\n\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n\u003ctd\u003e\n\u003cpre lang=\"C++\"\u003e\nclass Server: public Session{\n  public:\n    virtual void onConnect     (TCPsession\u0026 session)override{\n        doRead(session);\n    }\n\n    virtual void onDisconnect  (TCPsession\u0026 session)override{\n        std::cout \u003c\u003c session.getRemoteIP() \n           \u003c\u003c ':' \u003c\u003c session.getRemotePort() \n           \u003c\u003c \" disconnected\" \u003c\u003c std::endl;\n    }\n\n    virtual void onError(TCPsession\u0026 session, \n                         const std::string\u0026 err, \n                         ErrorType)override{\n        std::cout \u003c\u003c err \u003c\u003c std::endl;\n    }\n\n    virtual ~TimeoutServer(){}\n\n    private:\n    void doRead(TCPsession\u0026 session){\n        session.asyncRead('\\0', \n        [](TCPsession\u0026 session_){\n            std::cout \u003c\u003c session_.getMessage() \u003c\u003c std::endl;\n            doRead(session_);\n        });\n    }\n};\n\nint main(){\n    asio::io_context io_context;\n    unsigned short port = 1701;\n    Service\u003c\u003e srv(port);\n    srv.start([]{return std::make_shared\u003cServer\u003e();});\n    Worker::run();\n}\n\u003c/pre\u003e\n\u003c/td\u003e\n\u003ctd\u003e\n\u003cpre lang=\"C++\"\u003e\nvoid doRead(TCPsession\u0026 session){\n    session.asyncRead('\\0', \n    [](TCPsession\u0026 session_){\n        std::cout \u003c\u003c session_.getMessage() \u003c\u003c std::endl;\n        doRead(session_);\n    });\n}\n\nint main(){\n    asio::io_context io_context;\n    \n    auto session = SessionBuilder\u003c\u003e().onConnect([](TCPsession\u0026 session){\n        doRead(session);\n    }).onDisconnect([](TCPsession\u0026 session){\n        std::cout \u003c\u003c session.getRemoteIP() \n           \u003c\u003c ':' \u003c\u003c session.getRemotePort() \n           \u003c\u003c \" disconnected\" \u003c\u003c std::endl;\n    }).onError([](TCPsession\u0026 session, const std::string\u0026 err, ErrorType){\n        std::cout \u003c\u003c err \u003c\u003c std::endl;\n    }).buildFactory();\n\n    unsigned short port = 1701;\n    Service\u003c\u003e srv(port);\n    srv.start(session);\n    Worker::run();\n}\n\u003c/pre\u003e\n\u003c/td\u003e\n\u003c/tr\u003e\n\u003c/table\u003e\n\n## Examples\n* Echo client and server, messages terminated with `'\\0'`: [echoNulTer](examples/echoNulTer)\n* Echo client and server, messages with header indicating the length: [echoHeader](examples/echoHeader)\n* Echo client and server, session defined by chain of callbacks: [builder](examples/builder)\n* Timeout server: [timeout](examples/timeout)\n\n## Integration\n### As a cmake subproject\n\n### As a header-only library\n\n## References\n### TcpSession\n`TCPsession` encapsulated operations to be applied on the socket, including read, write, close, stat etc. This class should not be instantiated, but is passed to user-defined callbacks by reference.\n#### Member functions:\n* #### `void asyncRead(std::size_t length, CB\u0026\u0026 callback)`\n\n    Read `length` bytes of data from socket, and invoke `callback` upon finish. The signature of `CB` should be `void (TCPsession\u0026)`\n\n* #### `void asyncRead(char token, CB\u0026\u0026 callback)`\n\n    Read data from socket until `token` is met, then invoke `callback`. The signature of `CB` should be `void (TCPsession\u0026)`\n\n* #### `void asyncRead(const char* token, CB\u0026\u0026 callback)`\n\n    Read data from socket until `token` is met, then invoke `callback`. The signature of `CB` should be `void (TCPsession\u0026)`\n\n* #### `void asyncRead(CF\u0026\u0026 completionCondition, CB\u0026\u0026 callback)`\n\n    Read data from socket until `completionCondition` returns 0, then invoke `callback`. The signature of `CB` should be `void (TCPsession\u0026)`\n\n* #### `void asyncWrite(const std::string\u0026 message, CB\u0026\u0026 callback)`\n\n    Write `message` to socket, and invoke `callback` upon finish. The signature of `CB` should be `void (TCPsession\u0026)`. `message` won't be copied, so this reference must stay valid until `callback` is called.\n\n* #### `void asyncWrite(SharedBuffer bufs, CB\u0026\u0026 callback)`\n\n    Write content in `bufs` to socket, and invoke `callback` upon finish. The signature of `CB` should be `void (TCPsession\u0026)`. `SharedBuffer` will manage underlying memory, so no special care need to be paid on lifetime.\n\n* #### `Timer defer(const duration\u003cRep, Period\u003e\u0026 timeout, CB\u0026\u0026 callback)`\n\n    Invoke `callback` after `timeout`. The signature of `CB` should be `void (const std::error_code\u0026)`.\n\n* #### `Timer expire(const duration\u003cRep, Period\u003e\u0026 timeout)`\n\n    Close session after `timeout`\n\n* #### `Timer expire(const duration\u003cRep, Period\u003e\u0026 timeout, CB\u0026\u0026 callback)`\n\n    Invoke `callback` and close session after `timeout`\n\n* #### `void close()`\n\n    Close the session gracefully\n\n* #### `std::string getMessage()`\n\n    Get the received message after `asyncRead()`\n\n* #### `std::string getMessage(std::size_t length)`\n\n    Read `length` bytes of the received message after `asyncRead()`\n\n* #### `std::string getRemoteIP()`\n\n    Get the IP address of the remote host\n\n* #### `unsigned short getRemotePort()`\n\n    Get the port of the remote host\n\n### TcpService\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsdtelectronics%2Ftev","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsdtelectronics%2Ftev","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsdtelectronics%2Ftev/lists"}