Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
Sonicadvance1 committed Jan 13, 2025
1 parent 95ee828 commit facec98
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 40 deletions.
79 changes: 43 additions & 36 deletions Source/Tools/LinuxEmulation/LinuxSyscalls/GdbServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,12 @@ fextl::string GdbServer::ReadPacket() {
break;
case '}': // escape char
{
auto escaped = CommsStream.get();
Utils::NetStream::ReturnGet escaped;

do {
escaped = CommsStream.get();
} while (!escaped.HasData() && !escaped.HasHangup());

if (escaped.HasData()) {
packet.push_back(escaped.GetData() ^ 0x20);
} else {
Expand All @@ -214,7 +219,7 @@ fextl::string GdbServer::ReadPacket() {
case '#': // end of packet
{
char hexString[3] = {0, 0, 0};
CommsStream.read(hexString, 2);
CommsStream.read(hexString, 2, true);
int expected_checksum = std::strtoul(hexString, nullptr, 16);

if (calculateChecksum(packet) == expected_checksum) {
Expand Down Expand Up @@ -1395,45 +1400,47 @@ void GdbServer::GdbServerLoop() {

HandledPacketType response {};

// Outer server loop. Handles packet start, ACK/NAK and break
Utils::NetStream::ReturnGet c;
while (!CoreShuttingDown.load() && (c = CommsStream.get()).HasData()) {
switch (c.GetData()) {
case '$': {
auto packet = ReadPacket();
response = ProcessPacket(packet);
SendPacketPair(response);
if (response.TypeResponse == HandledPacketType::TYPE_UNKNOWN) {
LogMan::Msg::DFmt("Unknown packet {}", packet);
while (!CoreShuttingDown.load()) {
// Outer server loop. Handles packet start, ACK/NAK and break
Utils::NetStream::ReturnGet c;
while ((c = CommsStream.get()).HasData()) {
switch (c.GetData()) {
case '$': {
auto packet = ReadPacket();
response = ProcessPacket(packet);
SendPacketPair(response);
if (response.TypeResponse == HandledPacketType::TYPE_UNKNOWN) {
LogMan::Msg::DFmt("Unknown packet {}", packet);
}
break;
}
break;
}
case '+':
// ACK, do nothing.
break;
case '-':
// NAK, Resend requested
{
std::lock_guard lk(sendMutex);
SendPacket(response.Response);
case '+':
// ACK, do nothing.
break;
case '-':
// NAK, Resend requested
{
std::lock_guard lk(sendMutex);
SendPacket(response.Response);
}
break;
case '\x03': { // ASCII EOT
SyscallHandler->TM.Pause();
fextl::string str = fextl::fmt::format("T02thread:{:02x};", getpid());
if (LibraryMapChanged) {
// If libraries have changed then let gdb know
str += "library:1;";
}
SendPacketPair({std::move(str), HandledPacketType::TYPE_ACK});
break;
}
break;
case '\x03': { // ASCII EOT
SyscallHandler->TM.Pause();
fextl::string str = fextl::fmt::format("T02thread:{:02x};", getpid());
if (LibraryMapChanged) {
// If libraries have changed then let gdb know
str += "library:1;";
default: LogMan::Msg::DFmt("GdbServer: Unexpected byte {} ({:02x})", c.GetData(), c.GetData());
}
SendPacketPair({std::move(str), HandledPacketType::TYPE_ACK});
break;
}
default: LogMan::Msg::DFmt("GdbServer: Unexpected byte {} ({:02x})", c.GetData(), c.GetData());
}
}

if (c.HasHangup()) {
break;
if (c.HasHangup()) {
break;
}
}

{
Expand Down
6 changes: 3 additions & 3 deletions Source/Tools/LinuxEmulation/LinuxSyscalls/NetStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ NetStream::ReturnGet NetStream::get() {
.fd = socketfd, .events = POLLIN, .revents = 0,
};

auto Result = ppoll(&pfd, 1, nullptr, nullptr);
auto Result = poll(&pfd, 1, -1);
if (Result > 0) {
if (pfd.revents & POLLHUP) {
return NetStream::ReturnGet {true};
Expand All @@ -45,14 +45,14 @@ NetStream::ReturnGet NetStream::get() {
return NetStream::ReturnGet {false};
}

size_t NetStream::read(char* buf, size_t size) {
size_t NetStream::read(char* buf, size_t size, bool ContinueOnInterrupt) {
size_t Read {};
while (Read < size) {
auto Result = get();
if (Result.HasData()) {
buf[Read] = Result.GetData();
++Read;
} else {
} else if ((!Result.HasData() && !ContinueOnInterrupt) || Result.HasHangup()) {
return Read;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Tools/LinuxEmulation/LinuxSyscalls/NetStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class NetStream final {
}
};
ReturnGet get();
size_t read(char* buf, size_t size);
size_t read(char* buf, size_t size, bool ContinueOnInterrupt);

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

0 comments on commit facec98

Please sign in to comment.