Skip to content

Commit

Permalink
Netstream: Use a std::variant
Browse files Browse the repository at this point in the history
  • Loading branch information
Sonicadvance1 committed Jan 10, 2025
1 parent de5cdae commit 95ee828
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
22 changes: 11 additions & 11 deletions Source/Tools/LinuxEmulation/LinuxSyscalls/GdbServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ fextl::string GdbServer::ReadPacket() {
// where any $ or # in the packet body are escaped ('}' followed by the char XORed with 0x20)
// The checksum is a single unsigned byte sum of the data, hex encoded.

std::optional<Utils::NetStream::ReturnGet> c;
while ((c = CommsStream.get()).has_value() && !c->Hangup) {
switch (c->data) {
Utils::NetStream::ReturnGet c;
while ((c = CommsStream.get()).HasData()) {
switch (c.GetData()) {
case '$': // start of packet
if (packet.size() != 0) {
LogMan::Msg::EFmt("Dropping unexpected data: \"{}\"", packet);
Expand All @@ -204,8 +204,8 @@ fextl::string GdbServer::ReadPacket() {
case '}': // escape char
{
auto escaped = CommsStream.get();
if (escaped.has_value() && !escaped->Hangup) {
packet.push_back(escaped->data ^ 0x20);
if (escaped.HasData()) {
packet.push_back(escaped.GetData() ^ 0x20);
} else {
LogMan::Msg::EFmt("Received Invalid escape char: ${}", packet);
}
Expand All @@ -224,7 +224,7 @@ fextl::string GdbServer::ReadPacket() {
}
break;
}
default: packet.push_back(c->data); break;
default: packet.push_back(c.GetData()); break;
}
}

Expand Down Expand Up @@ -1396,9 +1396,9 @@ void GdbServer::GdbServerLoop() {
HandledPacketType response {};

// Outer server loop. Handles packet start, ACK/NAK and break
std::optional<Utils::NetStream::ReturnGet> c;
while (!CoreShuttingDown.load() && (c = CommsStream.get()).has_value() && !c->Hangup) {
switch (c->data) {
Utils::NetStream::ReturnGet c;
while (!CoreShuttingDown.load() && (c = CommsStream.get()).HasData()) {
switch (c.GetData()) {
case '$': {
auto packet = ReadPacket();
response = ProcessPacket(packet);
Expand Down Expand Up @@ -1428,11 +1428,11 @@ void GdbServer::GdbServerLoop() {
SendPacketPair({std::move(str), HandledPacketType::TYPE_ACK});
break;
}
default: LogMan::Msg::DFmt("GdbServer: Unexpected byte {} ({:02x})", c->data, c->data);
default: LogMan::Msg::DFmt("GdbServer: Unexpected byte {} ({:02x})", c.GetData(), c.GetData());
}
}

if (c.has_value() && c->Hangup) {
if (c.HasHangup()) {
break;
}

Expand Down
14 changes: 7 additions & 7 deletions Source/Tools/LinuxEmulation/LinuxSyscalls/NetStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
#include <unistd.h>

namespace FEX::Utils {
std::optional<NetStream::ReturnGet> NetStream::get() {
NetStream::ReturnGet NetStream::get() {
if (read_offset != receive_buffer.size() && read_offset != receive_offset) {
auto Result = receive_buffer.at(read_offset);
++read_offset;
return ReturnGet {Result, false};
return NetStream::ReturnGet {Result};
}

if (read_offset == receive_buffer.size()) {
Expand All @@ -29,7 +29,7 @@ std::optional<NetStream::ReturnGet> NetStream::get() {
auto Result = ppoll(&pfd, 1, nullptr, nullptr);
if (Result > 0) {
if (pfd.revents & POLLHUP) {
return ReturnGet {'\0', true};
return NetStream::ReturnGet {true};
}

const auto remaining_size = receive_buffer.size() - receive_offset;
Expand All @@ -38,19 +38,19 @@ std::optional<NetStream::ReturnGet> NetStream::get() {
receive_offset += Result;
auto Result = receive_buffer.at(read_offset);
++read_offset;
return ReturnGet {Result, false};
return NetStream::ReturnGet {Result};
}
}

return std::nullopt;
return NetStream::ReturnGet {false};
}

size_t NetStream::read(char* buf, size_t size) {
size_t Read {};
while (Read < size) {
auto Result = get();
if (Result.has_value() && !Result->Hangup) {
buf[Read] = Result->data;
if (Result.HasData()) {
buf[Read] = Result.GetData();
++Read;
} else {
return Read;
Expand Down
16 changes: 12 additions & 4 deletions Source/Tools/LinuxEmulation/LinuxSyscalls/NetStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <FEXCore/fextl/vector.h>

#include <optional>
#include <variant>

namespace FEX::Utils {
class NetStream final {
Expand All @@ -25,11 +26,18 @@ class NetStream final {
return socketfd != -1;
}

struct ReturnGet {
char data;
bool Hangup;
struct ReturnGet final : public std::variant<char, bool> {
bool HasHangup() const {
return std::holds_alternative<bool>(*this) && std::get<bool>(*this);
}
bool HasData() const {
return std::holds_alternative<char>(*this);
}
char GetData() const {
return std::get<char>(*this);
}
};
std::optional<ReturnGet> get();
ReturnGet get();
size_t read(char* buf, size_t size);

bool SendPacket(const fextl::string& packet);
Expand Down

0 comments on commit 95ee828

Please sign in to comment.