Browse Source

Prefer ASIO functions that take error codes

pull/6627/head
staphen 3 years ago committed by Anders Jenbo
parent
commit
d4b33685a2
  1. 37
      Source/dvlnet/tcp_client.cpp
  2. 5
      Source/dvlnet/tcp_server.cpp

37
Source/dvlnet/tcp_client.cpp

@ -17,14 +17,9 @@ namespace devilution::net {
int tcp_client::create(std::string addrstr) int tcp_client::create(std::string addrstr)
{ {
try { auto port = *sgOptions.Network.port;
auto port = *sgOptions.Network.port; local_server = std::make_unique<tcp_server>(ioc, addrstr, port, *pktfty);
local_server = std::make_unique<tcp_server>(ioc, addrstr, port, *pktfty); return join(local_server->LocalhostSelf());
return join(local_server->LocalhostSelf());
} catch (std::system_error &e) {
SDL_SetError("%s", e.what());
return -1;
}
} }
int tcp_client::join(std::string addrstr) int tcp_client::join(std::string addrstr)
@ -32,16 +27,26 @@ int tcp_client::join(std::string addrstr)
constexpr int MsSleep = 10; constexpr int MsSleep = 10;
constexpr int NoSleep = 250; constexpr int NoSleep = 250;
try { std::string port = StrCat(*sgOptions.Network.port);
std::stringstream port;
port << *sgOptions.Network.port; asio::error_code errorCode;
asio::connect(sock, resolver.resolve(addrstr, port.str())); asio::ip::basic_resolver_results<asio::ip::tcp> range = resolver.resolve(addrstr, port, errorCode);
asio::ip::tcp::no_delay option(true); if (errorCode) {
sock.set_option(option); SDL_SetError("%s", errorCode.message().c_str());
} catch (std::exception &e) { return -1;
SDL_SetError("%s", e.what()); }
asio::connect(sock, range, errorCode);
if (errorCode) {
SDL_SetError("%s", errorCode.message().c_str());
return -1; return -1;
} }
asio::ip::tcp::no_delay option(true);
sock.set_option(option, errorCode);
if (errorCode)
LogError("Client error setting socket option: {}", errorCode.message());
StartReceive(); StartReceive();
{ {
cookie_self = packet_out::GenerateCookie(); cookie_self = packet_out::GenerateCookie();

5
Source/dvlnet/tcp_server.cpp

@ -208,8 +208,11 @@ void tcp_server::HandleAccept(const scc &con, const asio::error_code &ec)
if (NextFree() == PLR_BROADCAST) { if (NextFree() == PLR_BROADCAST) {
DropConnection(con); DropConnection(con);
} else { } else {
asio::error_code errorCode;
asio::ip::tcp::no_delay option(true); asio::ip::tcp::no_delay option(true);
con->socket.set_option(option); con->socket.set_option(option, errorCode);
if (errorCode)
LogError("Server error setting socket option: {}", errorCode.message());
con->timeout = timeout_connect; con->timeout = timeout_connect;
StartReceive(con); StartReceive(con);
StartTimeout(con); StartTimeout(con);

Loading…
Cancel
Save