Skip to content

Commit

Permalink
nixd/librpc: handle EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Mar 13, 2024
1 parent b5a9b05 commit 65621fe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion nixd/librpc/include/nixd/rpc/IO.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
namespace nixd::rpc {

/// \brief Read N bytes from FD into Buf, block until N bytes are read.
size_t readBytes(int FD, void *Buf, std::size_t N);
[[nodiscard]] size_t readBytes(int FD, void *Buf, std::size_t N);

} // namespace nixd::rpc
12 changes: 10 additions & 2 deletions nixd/librpc/src/Transport.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "nixd/rpc/Transport.h"
#include "nixd/rpc/IO.h"

#include <stdexcept>
#include <system_error>
#include <vector>

Expand All @@ -20,17 +21,24 @@ void send(int OutboundFD, std::string_view Msg) {

std::vector<char> recv(int InboundFD) {
MessageSizeT Size;
readBytes(InboundFD, &Size, sizeof(Size));
if (readBytes(InboundFD, &Size, sizeof(MessageSizeT)) <
sizeof(MessageSizeT)) {
return {};
}

// Collect a message
std::vector<char> Buf(Size);
readBytes(InboundFD, Buf.data(), Size);
if (readBytes(InboundFD, Buf.data(), Size) < Size) {
throw std::runtime_error("in-complete inbound size");
}
return Buf;
}

int Transport::run() {
for (;;) {
std::vector<char> Buf = recv();
if (Buf.empty())
break;
handleInbound(Buf);
}
return 0;
Expand Down

0 comments on commit 65621fe

Please sign in to comment.