Commit 4a655d69 authored by Florian Fischer's avatar Florian Fischer
Browse files

modernize cpp echo server

parent c95cce55
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
// Copyright © 2020 Florian Fischer // Copyright © 2020 Florian Fischer
#include <errno.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "AsyncLibrary.hpp" #include "AsyncLibrary.hpp"
#include "Common.hpp" #include "Common.hpp"
#include "Fiber.hpp" #include "Fiber.hpp"
#include "Runtime.hpp" #include "Runtime.hpp"
#include "config.h" #include "config.h"
static int port = 12345; static const int DEFAULT_PORT = 12345;
static int port = DEFAULT_PORT;
[[noreturn]] static void die(const char* msg) { [[noreturn]] static void die(const char* msg) {
perror(msg); perror(msg);
...@@ -35,6 +37,7 @@ void client_func(void* arg) { ...@@ -35,6 +37,7 @@ void client_func(void* arg) {
#endif #endif
// socket was shutdown // socket was shutdown
if (unlikely(bytes_recv == 0)) { if (unlikely(bytes_recv == 0)) {
shutdown(client_fd, SHUT_RDWR);
return; return;
} }
...@@ -49,7 +52,8 @@ void client_func(void* arg) { ...@@ -49,7 +52,8 @@ void client_func(void* arg) {
ssize_t bytes_send = 0; ssize_t bytes_send = 0;
while (bytes_send < bytes_recv) { while (bytes_send < bytes_recv) {
#ifdef USE_ASYNC_IO #ifdef USE_ASYNC_IO
ssize_t new_bytes_send = emper::io::send_and_wait(client_fd, buf, bytes_recv - bytes_send, MSG_NOSIGNAL); ssize_t new_bytes_send =
emper::io::send_and_wait(client_fd, buf, bytes_recv - bytes_send, MSG_NOSIGNAL);
#else #else
ssize_t new_bytes_send = send(client_fd, buf, bytes_recv - bytes_send, MSG_NOSIGNAL); ssize_t new_bytes_send = send(client_fd, buf, bytes_recv - bytes_send, MSG_NOSIGNAL);
#endif #endif
...@@ -122,7 +126,7 @@ static void welcome_func() { ...@@ -122,7 +126,7 @@ static void welcome_func() {
} }
} }
int main(int argc, char* argv[]) { auto main(int argc, char* argv[]) -> int {
if (argc > 2) { if (argc > 2) {
const int decimal = 10; const int decimal = 10;
port = (int)strtol(argv[1], nullptr, decimal); port = (int)strtol(argv[1], nullptr, decimal);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment