-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nixd/librpc: use exceptions for IO operations
- Loading branch information
Showing
4 changed files
with
50 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <string> | ||
#include <cstddef> | ||
|
||
#include <sys/types.h> | ||
|
||
namespace nixd::rpc { | ||
|
||
/// \brief Read N bytes from FD into Buf, block until N bytes are read. | ||
/// \returns the number of bytes read, or -1 on error. (errno is set) | ||
long readBytes(int FD, void *Buf, std::size_t N); | ||
size_t readBytes(int FD, void *Buf, std::size_t N); | ||
|
||
} // namespace nixd::rpc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
#include "nixd/rpc/IO.h" | ||
|
||
#include <system_error> | ||
|
||
#include <unistd.h> | ||
|
||
namespace nixd::rpc { | ||
|
||
long readBytes(int FD, void *Buf, std::size_t N) { | ||
std::size_t Read = 0; | ||
size_t readBytes(int FD, void *Buf, std::size_t N) { | ||
size_t Read = 0; | ||
while (Read < N) { | ||
auto BytesRead = read(FD, static_cast<char *>(Buf) + Read, N - Read); | ||
size_t BytesRead = read(FD, static_cast<char *>(Buf) + Read, N - Read); | ||
if (BytesRead <= 0) | ||
return BytesRead; | ||
throw std::system_error(std::make_error_code(std::errc(errno))); | ||
Read += BytesRead; | ||
} | ||
return static_cast<long>(Read); | ||
return Read; | ||
} | ||
|
||
} // namespace nixd::rpc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters