Skip to content

Commit 23d79ed

Browse files
update error messages
1 parent b29f4be commit 23d79ed

File tree

4 files changed

+38
-55
lines changed

4 files changed

+38
-55
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
cmake_minimum_required(VERSION 3.22.0)
22

3+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
4+
35
project(boost-tcp-server-client LANGUAGES CXX)
46

57
add_subdirectory(tests)

TcpClient.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,20 @@ void TcpClient::connect(const boost::asio::ip::tcp::endpoint &endpoint) {
1919
auto socket = std::make_shared<boost::asio::ip::tcp::socket>(m_ioContext);
2020
socket->async_connect(endpoint, [this, socket](const auto &error) {
2121
if (error) {
22-
std::cerr << "TcpClient::connect() found error: " + error.message()
23-
<< std::endl;
22+
std::cerr << "TcpClient::connect() error: " + error.message() + ".\n";
2423
m_observer.onDisconnected();
2524
return;
2625
}
2726
m_connection = TcpConnection::create(std::move(*socket), *this);
2827
m_connection->startReading();
29-
std::cout << "TCP Client was connected" << std::endl;
28+
std::cout << "TCPClient connected.\n";
3029
m_observer.onConnected();
3130
});
3231
}
3332

3433
void TcpClient::send(const char *data, size_t size) {
3534
if (!m_connection) {
36-
std::cerr << "TcpClient::send() found error: no connection" << std::endl;
35+
std::cerr << "TcpClient::send() error: no connection.\n";
3736
return;
3837
}
3938
m_connection->send(data, size);
@@ -53,7 +52,7 @@ void TcpClient::onReceived([[maybe_unused]] int connectionId, const char *data,
5352
void TcpClient::onConnectionClosed([[maybe_unused]] int connectionId) {
5453
if (m_connection) {
5554
m_connection.reset();
56-
std::cout << "TCP Client was disconnected" << std::endl;
55+
std::cout << "TCPClient disconnected.\n";
5756
m_observer.onDisconnected();
5857
}
5958
}

TcpConnection.cpp

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace {
66
constexpr auto f_maxPackageSize{1024};
7-
} // namespace
7+
} // namespace
88

99
void TcpConnection::Observer::onReceived([[maybe_unused]] int connectionId,
1010
[[maybe_unused]] const char *data,
@@ -15,17 +15,13 @@ void TcpConnection::Observer::onConnectionClosed(
1515

1616
TcpConnection::TcpConnection(boost::asio::ip::tcp::socket &&socket,
1717
Observer &observer, int id)
18-
: m_socket{std::move(socket)},
19-
m_readBuffer{},
20-
m_writeBuffer{},
21-
m_writeBufferMutex{},
22-
m_observer{observer},
23-
m_isReading{false},
24-
m_isWritting{false},
25-
m_id{id} {}
18+
: m_socket{std::move(socket)}, m_readBuffer{}, m_writeBuffer{},
19+
m_writeBufferMutex{}, m_observer{observer}, m_isReading{false},
20+
m_isWritting{false}, m_id{id} {}
2621

27-
std::shared_ptr<TcpConnection> TcpConnection::create(
28-
boost::asio::ip::tcp::socket &&socket, Observer &observer, int id) {
22+
std::shared_ptr<TcpConnection>
23+
TcpConnection::create(boost::asio::ip::tcp::socket &&socket, Observer &observer,
24+
int id) {
2925
return std::shared_ptr<TcpConnection>(
3026
new TcpConnection{std::move(socket), observer, id});
3127
}
@@ -50,9 +46,8 @@ void TcpConnection::close() {
5046
m_socket.cancel();
5147
m_socket.close();
5248
} catch (const std::exception &e) {
53-
std::cerr << "TcpConnection::Close() caught exception: " +
54-
static_cast<std::string>(e.what())
55-
<< std::endl;
49+
std::cerr << "TcpConnection::close() exception: " +
50+
static_cast<std::string>(e.what()) + ".\n";
5651
return;
5752
}
5853
m_observer.onConnectionClosed(m_id);
@@ -62,20 +57,19 @@ void TcpConnection::doRead() {
6257
m_isReading = true;
6358
auto buffers = m_readBuffer.prepare(f_maxPackageSize);
6459
auto self = shared_from_this();
65-
m_socket.async_read_some(
66-
buffers, [this, self](const auto &error, auto bytesTransferred) {
67-
if (error) {
68-
std::cerr << "TcpConnection::doRead() found error: " + error.message()
69-
<< std::endl;
70-
return close();
71-
}
72-
m_readBuffer.commit(bytesTransferred);
73-
m_observer.onReceived(
74-
m_id, static_cast<const char *>(m_readBuffer.data().data()),
75-
bytesTransferred);
76-
m_readBuffer.consume(bytesTransferred);
77-
doRead();
78-
});
60+
m_socket.async_read_some(buffers, [this, self](const auto &error,
61+
auto bytesTransferred) {
62+
if (error) {
63+
std::cerr << "TcpConnection::doRead() error: " + error.message() + ".\n";
64+
return close();
65+
}
66+
m_readBuffer.commit(bytesTransferred);
67+
m_observer.onReceived(m_id,
68+
static_cast<const char *>(m_readBuffer.data().data()),
69+
bytesTransferred);
70+
m_readBuffer.consume(bytesTransferred);
71+
doRead();
72+
});
7973
}
8074

8175
void TcpConnection::doWrite() {
@@ -85,8 +79,7 @@ void TcpConnection::doWrite() {
8579
const auto &error,
8680
auto bytesTransferred) {
8781
if (error) {
88-
std::cerr << "TcpConnection::doWrite() found error: " + error.message()
89-
<< std::endl;
82+
std::cerr << "TcpConnection::doWrite() error: " + error.message() + ".\n";
9083
return close();
9184
}
9285
std::lock_guard<std::mutex> guard{m_writeBufferMutex};

TcpServer.cpp

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,8 @@ void TcpServer::Observer::onConnectionClosed(
1414
[[maybe_unused]] int connectionId) {}
1515

1616
TcpServer::TcpServer(boost::asio::io_context &ioContext, Observer &observer)
17-
: m_acceptor{ioContext},
18-
m_connections{},
19-
m_observer{observer},
20-
m_connectionCount{0},
21-
m_isAccepting{false},
22-
m_isClosing{false} {}
17+
: m_acceptor{ioContext}, m_connections{}, m_observer{observer},
18+
m_connectionCount{0}, m_isAccepting{false}, m_isClosing{false} {}
2319

2420
bool TcpServer::listen(const boost::asio::ip::tcp &protocol, uint16_t port) {
2521
try {
@@ -28,9 +24,8 @@ bool TcpServer::listen(const boost::asio::ip::tcp &protocol, uint16_t port) {
2824
m_acceptor.bind({protocol, port});
2925
m_acceptor.listen(boost::asio::socket_base::max_connections);
3026
} catch (const std::exception &e) {
31-
std::cerr << "TcpServer::Listen() caught exception: " +
32-
static_cast<std::string>(e.what())
33-
<< std::endl;
27+
std::cerr << "TcpServer::listen() exception: " +
28+
static_cast<std::string>(e.what()) + ".\n";
3429
return false;
3530
}
3631
return true;
@@ -44,8 +39,7 @@ void TcpServer::startAcceptingConnections() {
4439

4540
void TcpServer::send(int connectionId, const char *data, size_t size) {
4641
if (m_connections.count(connectionId) == 0) {
47-
std::cerr << "TcpServer::send() found error: connectionId not found"
48-
<< std::endl;
42+
std::cerr << "TcpServer::send() error: connection not found.\n";
4943
return;
5044
}
5145
m_connections.at(connectionId)->send(data, size);
@@ -59,25 +53,22 @@ void TcpServer::close() {
5953
}
6054
m_connections.clear();
6155
m_isClosing = false;
62-
std::cout << "TCP Server was closed" << std::endl;
56+
std::cout << "TCPServer disconnected.\n";
6357
}
6458

6559
void TcpServer::doAccept() {
6660
m_isAccepting = true;
6761
m_acceptor.async_accept([this](const auto &error, auto socket) {
6862
if (error) {
69-
std::cerr << "TcpServer::doAccept() found error: " + error.message()
70-
<< std::endl;
63+
std::cerr << "TcpServer::doAccept() error: " + error.message() + ".\n";
7164
m_isAccepting = false;
7265
return;
7366
} else {
7467
auto connection{
7568
TcpConnection::create(std::move(socket), *this, m_connectionCount)};
7669
connection->startReading();
7770
m_connections.insert({m_connectionCount, std::move(connection)});
78-
std::cout << "TCP Server accepted connection: " +
79-
std::to_string(m_connectionCount)
80-
<< std::endl;
71+
std::cout << "TCPServer accepted connection.\n";
8172
m_observer.onConnectionAccepted(m_connectionCount);
8273
m_connectionCount++;
8374
}
@@ -94,9 +85,7 @@ void TcpServer::onConnectionClosed(int connectionId) {
9485
return;
9586
}
9687
if (m_connections.erase(connectionId) > 0) {
97-
std::cout << "TCP Server removed connection: " +
98-
std::to_string(m_connectionCount)
99-
<< std::endl;
88+
std::cout << "TCPServer removed connection.\n";
10089
m_observer.onConnectionClosed(connectionId);
10190
}
10291
}

0 commit comments

Comments
 (0)