• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- FDRawByteChannel.h - File descriptor based byte-channel -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // File descriptor based RawByteChannel.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_ORC_RPC_FDRAWBYTECHANNEL_H
14 #define LLVM_EXECUTIONENGINE_ORC_RPC_FDRAWBYTECHANNEL_H
15 
16 #include "llvm/ExecutionEngine/Orc/RPC/RawByteChannel.h"
17 
18 #include "llvm/Support/FormatVariadic.h"
19 #include "llvm/Support/raw_ostream.h"
20 
21 #if !defined(_MSC_VER) && !defined(__MINGW32__)
22 #include <unistd.h>
23 #else
24 #include <io.h>
25 #endif
26 
27 namespace llvm {
28 namespace orc {
29 namespace rpc {
30 
31 /// RPC channel that reads from and writes from file descriptors.
32 class FDRawByteChannel final : public RawByteChannel {
33 public:
FDRawByteChannel(int InFD,int OutFD)34   FDRawByteChannel(int InFD, int OutFD) : InFD(InFD), OutFD(OutFD) {}
35 
readBytes(char * Dst,unsigned Size)36   llvm::Error readBytes(char *Dst, unsigned Size) override {
37     // dbgs() << "Reading " << Size << " bytes: [";
38     assert(Dst && "Attempt to read into null.");
39     ssize_t Completed = 0;
40     while (Completed < static_cast<ssize_t>(Size)) {
41       ssize_t Read = ::read(InFD, Dst + Completed, Size - Completed);
42       if (Read <= 0) {
43         // dbgs() << " <<<\n";
44         auto ErrNo = errno;
45         if (ErrNo == EAGAIN || ErrNo == EINTR)
46           continue;
47         else
48           return llvm::errorCodeToError(
49               std::error_code(errno, std::generic_category()));
50       }
51       // for (size_t I = 0; I != Read; ++I)
52       //  dbgs() << " " << formatv("{0:x2}", Dst[Completed + I]);
53       Completed += Read;
54     }
55     // dbgs() << " ]\n";
56     return llvm::Error::success();
57   }
58 
appendBytes(const char * Src,unsigned Size)59   llvm::Error appendBytes(const char *Src, unsigned Size) override {
60     // dbgs() << "Appending " << Size << " bytes: [";
61     assert(Src && "Attempt to append from null.");
62     ssize_t Completed = 0;
63     while (Completed < static_cast<ssize_t>(Size)) {
64       ssize_t Written = ::write(OutFD, Src + Completed, Size - Completed);
65       if (Written < 0) {
66         // dbgs() << " <<<\n";
67         auto ErrNo = errno;
68         if (ErrNo == EAGAIN || ErrNo == EINTR)
69           continue;
70         else
71           return llvm::errorCodeToError(
72               std::error_code(errno, std::generic_category()));
73       }
74       // for (size_t I = 0; I != Written; ++I)
75       //  dbgs() << " " << formatv("{0:x2}", Src[Completed + I]);
76       Completed += Written;
77     }
78     // dbgs() << " ]\n";
79     return llvm::Error::success();
80   }
81 
send()82   llvm::Error send() override { return llvm::Error::success(); }
83 
84 private:
85   int InFD, OutFD;
86 };
87 
88 } // namespace rpc
89 } // namespace orc
90 } // namespace llvm
91 
92 #endif // LLVM_EXECUTIONENGINE_ORC_RPC_FDRAWBYTECHANNEL_H
93