Skip to content

Commit

Permalink
Get rid of protocol_exception
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenCWills authored and AJenbo committed Nov 18, 2023
1 parent ab8aef4 commit a23119a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 35 deletions.
8 changes: 0 additions & 8 deletions Source/dvlnet/abstract_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ namespace devilution::net {
using buffer_t = std::vector<unsigned char>;
using provider_t = unsigned long;

class dvlnet_exception : public std::exception {
public:
const char *what() const throw() override
{
return "Network error";
}
};

class abstract_net {
public:
virtual int create(std::string addrstr) = 0;
Expand Down
40 changes: 31 additions & 9 deletions Source/dvlnet/base_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class base_protocol : public base {
tl::expected<void, PacketError> recv_ingame(packet &pkt, endpoint_t sender);
bool is_recognized(endpoint_t sender);

bool wait_network();
tl::expected<bool, PacketError> wait_network();
bool wait_firstpeer();
tl::expected<void, PacketError> wait_join();
};
Expand All @@ -80,15 +80,18 @@ plr_t base_protocol<P>::get_master()
}

template <class P>
bool base_protocol<P>::wait_network()
tl::expected<bool, PacketError> base_protocol<P>::wait_network()
{
// wait for ZeroTier for 5 seconds
for (auto i = 0; i < 500; ++i) {
if (proto.network_online())
break;
tl::expected<bool, PacketError> status = proto.network_online();
if (!status.has_value())
return status;
if (*status)
return true;
SDL_Delay(10);
}
return proto.network_online();
return false;
}

template <class P>
Expand Down Expand Up @@ -118,7 +121,12 @@ bool base_protocol<P>::wait_firstpeer()
template <class P>
bool base_protocol<P>::send_info_request()
{
if (!proto.network_online())
tl::expected<bool, PacketError> status = proto.network_online();
if (!status.has_value()) {
LogError("network_online: {}", status.error().what());
return false;
}
if (!*status)
return false;
tl::expected<std::unique_ptr<packet>, PacketError> pkt
= pktfty->make_packet<PT_INFO_REQUEST>(PLR_BROADCAST, PLR_MASTER);
Expand Down Expand Up @@ -158,7 +166,12 @@ int base_protocol<P>::create(std::string addrstr)
gamename = addrstr;
isGameHost_ = true;

if (wait_network()) {
tl::expected<bool, PacketError> isReady = wait_network();
if (!isReady.has_value()) {
LogError("wait_network: {}", isReady.error().what());
return -1;
}
if (*isReady) {
plr_self = 0;
if (tl::expected<void, PacketError> result = Connect(plr_self);
!result.has_value()) {
Expand All @@ -175,7 +188,13 @@ int base_protocol<P>::join(std::string addrstr)
gamename = addrstr;
isGameHost_ = false;

if (wait_network()) {
tl::expected<bool, PacketError> isReady = wait_network();
if (!isReady.has_value()) {
const std::string_view message = isReady.error().what();
SDL_SetError("wait_join: %.*s", static_cast<int>(message.size()), message.data());
return -1;
}
if (*isReady) {
if (wait_firstpeer()) {
tl::expected<void, PacketError> result = wait_join();
if (!result.has_value()) {
Expand Down Expand Up @@ -413,7 +432,10 @@ tl::expected<void, PacketError> base_protocol<P>::recv_ingame(packet &pkt, endpo
tl::expected<const buffer_t *, PacketError> pktInfo = pkt.Info();
if (!pktInfo.has_value())
return tl::make_unexpected(pktInfo.error());
peer.endpoint.unserialize(**pktInfo);
if (tl::expected<void, PacketError> result = peer.endpoint.unserialize(**pktInfo);
!result.has_value()) {
return result;
}
peer.sendQueue = std::make_unique<std::deque<packet>>();
if (tl::expected<void, PacketError> result = Connect(*newPlayer);
!result.has_value()) {
Expand Down
19 changes: 12 additions & 7 deletions Source/dvlnet/protocol_zt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void protocol_zt::set_reuseaddr(int fd)
lwip_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes));
}

bool protocol_zt::network_online()
tl::expected<bool, PacketError> protocol_zt::network_online()
{
if (!zerotier_network_ready())
return false;
Expand All @@ -65,7 +65,7 @@ bool protocol_zt::network_online()
if (ret < 0) {
Log("lwip, (udp) bind: {}", strerror(errno));
SDL_SetError("lwip, (udp) bind: %s", strerror(errno));
throw protocol_exception();
return tl::make_unexpected(ProtocolError());
}
set_nonblock(fd_udp);
}
Expand All @@ -76,13 +76,13 @@ bool protocol_zt::network_online()
if (r1 < 0) {
Log("lwip, (tcp) bind: {}", strerror(errno));
SDL_SetError("lwip, (udp) bind: %s", strerror(errno));
throw protocol_exception();
return tl::make_unexpected(ProtocolError());
}
auto r2 = lwip_listen(fd_tcp, 10);
if (r2 < 0) {
Log("lwip, listen: {}", strerror(errno));
SDL_SetError("lwip, listen: %s", strerror(errno));
throw protocol_exception();
return tl::make_unexpected(ProtocolError());
}
set_nonblock(fd_tcp);
set_nodelay(fd_tcp);
Expand Down Expand Up @@ -117,7 +117,7 @@ bool protocol_zt::send_oob_mc(const buffer_t &data) const
return send_oob(mc, data);
}

bool protocol_zt::send_queued_peer(const endpoint &peer)
tl::expected<bool, PacketError> protocol_zt::send_queued_peer(const endpoint &peer)
{
if (peer_list[peer].fd == -1) {
peer_list[peer].fd = lwip_socket(AF_INET6, SOCK_STREAM, 0);
Expand Down Expand Up @@ -146,7 +146,7 @@ bool protocol_zt::send_queued_peer(const endpoint &peer)
if (decltype(len)(r) == len) {
peer_list[peer].send_queue.pop_front();
} else {
throw protocol_exception();
return tl::make_unexpected(ProtocolError());
}
}
return true;
Expand All @@ -168,7 +168,12 @@ bool protocol_zt::recv_peer(const endpoint &peer)
bool protocol_zt::send_queued_all()
{
for (const auto &[endpoint, _] : peer_list) {
if (!send_queued_peer(endpoint)) {
tl::expected<bool, PacketError> result = send_queued_peer(endpoint);
if (!result.has_value()) {
LogError("send_queued_peer: {}", result.error().what());
continue;
}
if (!*result) {
// handle error?
}
}
Expand Down
21 changes: 10 additions & 11 deletions Source/dvlnet/protocol_zt.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@
#include <string>

#include "dvlnet/frame_queue.h"
#include "dvlnet/packet.h"

namespace devilution {
namespace net {

class protocol_exception : public std::exception {
public:
const char *what() const throw() override
{
return "Protocol error";
}
};
inline PacketError ProtocolError()
{
return PacketError("Protocol error");
}

class protocol_zt {
public:
Expand Down Expand Up @@ -55,11 +53,12 @@ class protocol_zt {
return buffer_t(addr.begin(), addr.end());
}

void unserialize(const buffer_t &buf)
tl::expected<void, PacketError> unserialize(const buffer_t &buf)
{
if (buf.size() != 16)
throw protocol_exception();
return tl::make_unexpected(ProtocolError());
std::copy(buf.begin(), buf.end(), addr.begin());
return {};
}

void from_string(const std::string &str);
Expand All @@ -73,7 +72,7 @@ class protocol_zt {
bool send_oob_mc(const buffer_t &data) const;
bool recv(endpoint &peer, buffer_t &data);
bool get_disconnected(endpoint &peer);
bool network_online();
tl::expected<bool, PacketError> network_online();
bool is_peer_connected(endpoint &peer);
bool is_peer_relayed(const endpoint &peer) const;
static std::string make_default_gamename();
Expand Down Expand Up @@ -102,7 +101,7 @@ class protocol_zt {
static void set_nodelay(int fd);
static void set_reuseaddr(int fd);

bool send_queued_peer(const endpoint &peer);
tl::expected<bool, PacketError> send_queued_peer(const endpoint &peer);
bool recv_peer(const endpoint &peer);
bool send_queued_all();
bool recv_from_peers();
Expand Down

0 comments on commit a23119a

Please sign in to comment.