-
Notifications
You must be signed in to change notification settings - Fork 0
update examples #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
update examples #13
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| add_executable(echo-server echo-server.cpp) | ||
| target_link_libraries(echo-server PRIVATE condy uring) | ||
|
|
||
| add_executable(fast-cp fast-cp.cpp) | ||
| target_link_libraries(fast-cp PRIVATE condy uring) | ||
| add_executable(link-cp link-cp.cpp) | ||
| target_link_libraries(link-cp PRIVATE condy uring) | ||
|
|
||
| add_executable(custom-allocator custom-allocator.cpp) | ||
| target_link_libraries(custom-allocator PRIVATE condy uring) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,92 +7,95 @@ | |||||||||||||||||||||||||||||||||||||||
| #include <sys/socket.h> | ||||||||||||||||||||||||||||||||||||||||
| #include <unistd.h> | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| void prepare_address(const std::string &host, uint16_t port, | ||||||||||||||||||||||||||||||||||||||||
| sockaddr_in &addr) { | ||||||||||||||||||||||||||||||||||||||||
| memset(&addr, 0, sizeof(addr)); | ||||||||||||||||||||||||||||||||||||||||
| addr.sin_family = AF_INET; | ||||||||||||||||||||||||||||||||||||||||
| addr.sin_port = htons(port); | ||||||||||||||||||||||||||||||||||||||||
| inet_pton(AF_INET, host.c_str(), &addr.sin_addr); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| constexpr size_t BACKLOG = 128; | ||||||||||||||||||||||||||||||||||||||||
| constexpr size_t MAX_CONNECTIONS = 1024; | ||||||||||||||||||||||||||||||||||||||||
| constexpr size_t MAX_MESSAGE_LEN = 2048; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| condy::Coro<> handle_client(int client_fd) { | ||||||||||||||||||||||||||||||||||||||||
| constexpr size_t BUFFER_SIZE = 1024; | ||||||||||||||||||||||||||||||||||||||||
| char buffer[BUFFER_SIZE]; | ||||||||||||||||||||||||||||||||||||||||
| condy::Coro<void> session(int client_fd) { | ||||||||||||||||||||||||||||||||||||||||
| char buffer[MAX_MESSAGE_LEN]; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| while (true) { | ||||||||||||||||||||||||||||||||||||||||
| int n = co_await condy::async_read( | ||||||||||||||||||||||||||||||||||||||||
| client_fd, condy::buffer(buffer, BUFFER_SIZE), 0); | ||||||||||||||||||||||||||||||||||||||||
| int n = co_await condy::async_recv(condy::fixed(client_fd), | ||||||||||||||||||||||||||||||||||||||||
| condy::buffer(buffer), 0); | ||||||||||||||||||||||||||||||||||||||||
| if (n <= 0) { | ||||||||||||||||||||||||||||||||||||||||
| if (n < 0) { | ||||||||||||||||||||||||||||||||||||||||
| std::fprintf(stderr, "Read error: %s\n", std::strerror(-n)); | ||||||||||||||||||||||||||||||||||||||||
| std::fprintf(stderr, "Read error: %d\n", n); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| n = co_await condy::async_write(client_fd, condy::buffer(buffer, n), 0); | ||||||||||||||||||||||||||||||||||||||||
| n = co_await condy::async_send(condy::fixed(client_fd), | ||||||||||||||||||||||||||||||||||||||||
| condy::buffer(buffer, n), 0); | ||||||||||||||||||||||||||||||||||||||||
| if (n < 0) { | ||||||||||||||||||||||||||||||||||||||||
| std::fprintf(stderr, "Write error: %s\n", std::strerror(-n)); | ||||||||||||||||||||||||||||||||||||||||
| std::fprintf(stderr, "Write error: %d\n", n); | ||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| co_await condy::async_close(client_fd); | ||||||||||||||||||||||||||||||||||||||||
| std::printf("Connection closed, fd:%d\n", client_fd); | ||||||||||||||||||||||||||||||||||||||||
| co_await condy::async_close(condy::fixed(client_fd)); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| condy::Coro<> co_main(const std::string &host, uint16_t port) { | ||||||||||||||||||||||||||||||||||||||||
| condy::Coro<int> co_main(int server_fd) { | ||||||||||||||||||||||||||||||||||||||||
| while (true) { | ||||||||||||||||||||||||||||||||||||||||
| sockaddr_in client_addr; | ||||||||||||||||||||||||||||||||||||||||
| socklen_t client_len = sizeof(client_addr); | ||||||||||||||||||||||||||||||||||||||||
| int client_fd = co_await condy::async_accept_direct( | ||||||||||||||||||||||||||||||||||||||||
| server_fd, (struct sockaddr *)&client_addr, &client_len, 0, | ||||||||||||||||||||||||||||||||||||||||
| CONDY_FILE_INDEX_ALLOC); | ||||||||||||||||||||||||||||||||||||||||
| if (client_fd < 0) { | ||||||||||||||||||||||||||||||||||||||||
| std::fprintf(stderr, "Failed to accept connection: %d\n", | ||||||||||||||||||||||||||||||||||||||||
| client_fd); | ||||||||||||||||||||||||||||||||||||||||
| co_return 1; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| condy::co_spawn(session(client_fd)).detach(); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| void prepare_address(const std::string &host, uint16_t port, | ||||||||||||||||||||||||||||||||||||||||
| sockaddr_in &addr) { | ||||||||||||||||||||||||||||||||||||||||
| memset(&addr, 0, sizeof(addr)); | ||||||||||||||||||||||||||||||||||||||||
| addr.sin_family = AF_INET; | ||||||||||||||||||||||||||||||||||||||||
| addr.sin_port = htons(port); | ||||||||||||||||||||||||||||||||||||||||
| inet_pton(AF_INET, host.c_str(), &addr.sin_addr); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| int main(int argc, char **argv) noexcept(false) { | ||||||||||||||||||||||||||||||||||||||||
| if (argc < 3) { | ||||||||||||||||||||||||||||||||||||||||
| std::fprintf(stderr, "Usage: %s <host> <port>\n", argv[0]); | ||||||||||||||||||||||||||||||||||||||||
| return 1; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| std::string host = argv[1]; | ||||||||||||||||||||||||||||||||||||||||
| uint16_t port = static_cast<uint16_t>(std::stoi(argv[2])); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
| uint16_t port = static_cast<uint16_t>(std::stoi(argv[2])); | |
| uint16_t port; | |
| try { | |
| std::size_t pos = 0; | |
| int parsed = std::stoi(argv[2], &pos); | |
| // Ensure the whole argument was parsed and the value is within valid port range. | |
| if (argv[2][pos] != '\0' || parsed < 0 || parsed > 65535) { | |
| std::fprintf(stderr, | |
| "Invalid port '%s'. Please provide a number between 0 and 65535.\n", | |
| argv[2]); | |
| return 1; | |
| } | |
| port = static_cast<uint16_t>(parsed); | |
| } catch (const std::exception &) { | |
| std::fprintf(stderr, | |
| "Invalid port '%s'. Please provide a number between 0 and 65535.\n", | |
| argv[2]); | |
| return 1; | |
| } |
Copilot
AI
Jan 13, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The server_fd socket is never closed before the program exits. If condy::sync_wait returns normally or if an error occurs in co_main, the socket file descriptor will leak. Consider closing the socket after sync_wait returns, or use RAII (e.g., a unique_ptr with a custom deleter) to ensure proper cleanup.
| return condy::sync_wait(runtime, co_main(server_fd)); | |
| int result = condy::sync_wait(runtime, co_main(server_fd)); | |
| close(server_fd); | |
| return result; |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
co_mainfunction has an infinite loop that never returns under normal circumstances. This means the return value of 1 on line 48 will only be reached ifasync_accept_directfails. However, the function is declared to returnint, but the infinite loop prevents reaching a normal return path. Consider either documenting that this is intentional behavior (the server runs indefinitely), or implement a shutdown mechanism.