Skip to content

Commit 2ac8d24

Browse files
committed
Reduce the C++ standard to make it more portable
1 parent 75c6446 commit 2ac8d24

3 files changed

Lines changed: 33 additions & 20 deletions

File tree

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
CXXFLAGS=-std=c++20 -Wall -Wextra
2+
3+
all: echo_server timers_example
4+
15
echo_server: echo_server.cpp
2-
g++ -std=c++23 -o echo_server echo_server.cpp
6+
g++ $(CXXFLAGS) -o echo_server echo_server.cpp
37

48
timers_example: timers_example.cpp
5-
g++ -std=c++23 -o timers_example timers_example.cpp
9+
g++ $(CXXFLAGS) -o timers_example timers_example.cpp

echo_server.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
#include <print>
1+
#include <cstdio>
22
#include <memory>
33
#include <asio.hpp>
44

55
using asio::ip::tcp;
6-
using std::println;
76

8-
const size_t port = 6969;
7+
const size_t port = 6970;
98
asio::io_context io;
109
asio::ip::tcp::acceptor server(io, tcp::endpoint(tcp::v4(), port));
1110

@@ -19,22 +18,32 @@ class Request {
1918
asio::streambuf buffer;
2019
};
2120

22-
void handle_request(const std::error_code& ec, tcp::socket socket) {
21+
void handle_request(const std::error_code& error, tcp::socket socket) {
2322
server.async_accept(io, &handle_request); // The Loop
2423

24+
if (error) {
25+
printf("Error while accepting: %s\n", error.message().c_str());
26+
return;
27+
}
28+
2529
auto request = std::make_shared<Request>(std::move(socket));
2630

27-
auto ep = request->socket.remote_endpoint();
28-
println("Accepted connection {}", ep.address().to_string());
31+
printf("Accepted connection\n");
2932

3033
asio::async_read_until(request->socket, request->buffer, '\n',
3134
[request](const auto &error, size_t bytes_transferred) {
32-
auto ep = request->socket.remote_endpoint();
33-
println("Received {} bytes from {}", bytes_transferred, ep.address().to_string());
35+
if (error) {
36+
printf("Error while reading: %s\n", error.message().c_str());
37+
return;
38+
}
39+
printf("Received %zu bytes\n", bytes_transferred);
3440
asio::async_write(request->socket, request->buffer,
3541
[request](const auto &error, size_t bytes_transferred) {
36-
auto ep = request->socket.remote_endpoint();
37-
println("Sent {} bytes to {}", bytes_transferred, ep.address().to_string());
42+
if (error) {
43+
printf("Error while writing: %s\n", error.message().c_str());
44+
return;
45+
}
46+
printf("Sent %zu bytes\n", bytes_transferred);
3847
});
3948
});
4049
}
@@ -43,14 +52,14 @@ int main()
4352
{
4453
size_t seconds = 5;
4554
asio::steady_timer t(io, asio::chrono::seconds(seconds));
46-
println("Waiting for {} seconds", seconds);
55+
printf("Waiting for %zu seconds\n", seconds);
4756
t.async_wait([seconds](const std::error_code &e) {
48-
println("------------------------------");
49-
println("Done waiting {} seconds. Error: {}", seconds, e.message());
50-
println("------------------------------");
57+
printf("------------------------------\n");
58+
printf("Done waiting %zu seconds. Error: %s\n", seconds, e.message().c_str());
59+
printf("------------------------------\n");
5160
});
5261

53-
println("Listening too 127.0.0.1:{}", port);
62+
printf("Listening too 127.0.0.1:%zu\n", port);
5463
server.async_accept(io, &handle_request);
5564
io.run();
5665

timers_example.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <print>
1+
#include <cstdio>
22
#include <system_error>
33
#include <vector>
44

@@ -12,9 +12,9 @@ int main()
1212

1313
for (size_t i = 0; i < 5; ++i) {
1414
asio::steady_timer t(io, asio::chrono::seconds(i));
15-
std::println("Waiting for {} seconds", i);
15+
printf("Waiting for %zu seconds\n", i);
1616
t.async_wait([i](const std::error_code &e) {
17-
std::println("Done waiting {} seconds! Error code: {}", i, e.message());
17+
printf("Done waiting %zu seconds! Error: %s", i, e.message().c_str());
1818
});
1919
timers.push_back(std::move(t));
2020
}

0 commit comments

Comments
 (0)