|
7 | 7 | //===----------------------------------------------------------------------===// |
8 | 8 |
|
9 | 9 | #include "IOStream.h" |
| 10 | +#include "lldb/Utility/IOObject.h" |
| 11 | +#include "lldb/Utility/Status.h" |
10 | 12 | #include <fstream> |
11 | 13 | #include <string> |
12 | 14 |
|
13 | | -#if defined(_WIN32) |
14 | | -#include <io.h> |
15 | | -#else |
16 | | -#include <netinet/in.h> |
17 | | -#include <sys/socket.h> |
18 | | -#include <unistd.h> |
19 | | -#endif |
20 | | - |
21 | 15 | using namespace lldb_dap; |
22 | 16 |
|
23 | | -StreamDescriptor::StreamDescriptor() = default; |
24 | | - |
25 | | -StreamDescriptor::StreamDescriptor(StreamDescriptor &&other) { |
26 | | - *this = std::move(other); |
27 | | -} |
28 | | - |
29 | | -StreamDescriptor::~StreamDescriptor() { |
30 | | - if (!m_close) |
31 | | - return; |
32 | | - |
33 | | - if (m_is_socket) |
34 | | -#if defined(_WIN32) |
35 | | - ::closesocket(m_socket); |
36 | | -#else |
37 | | - ::close(m_socket); |
38 | | -#endif |
39 | | - else |
40 | | - ::close(m_fd); |
41 | | -} |
42 | | - |
43 | | -StreamDescriptor &StreamDescriptor::operator=(StreamDescriptor &&other) { |
44 | | - m_close = other.m_close; |
45 | | - other.m_close = false; |
46 | | - m_is_socket = other.m_is_socket; |
47 | | - if (m_is_socket) |
48 | | - m_socket = other.m_socket; |
49 | | - else |
50 | | - m_fd = other.m_fd; |
51 | | - return *this; |
52 | | -} |
53 | | - |
54 | | -StreamDescriptor StreamDescriptor::from_socket(SOCKET s, bool close) { |
55 | | - StreamDescriptor sd; |
56 | | - sd.m_is_socket = true; |
57 | | - sd.m_socket = s; |
58 | | - sd.m_close = close; |
59 | | - return sd; |
60 | | -} |
61 | | - |
62 | | -StreamDescriptor StreamDescriptor::from_file(int fd, bool close) { |
63 | | - StreamDescriptor sd; |
64 | | - sd.m_is_socket = false; |
65 | | - sd.m_fd = fd; |
66 | | - sd.m_close = close; |
67 | | - return sd; |
68 | | -} |
69 | | - |
70 | 17 | bool OutputStream::write_full(llvm::StringRef str) { |
71 | | - while (!str.empty()) { |
72 | | - int bytes_written = 0; |
73 | | - if (descriptor.m_is_socket) |
74 | | - bytes_written = ::send(descriptor.m_socket, str.data(), str.size(), 0); |
75 | | - else |
76 | | - bytes_written = ::write(descriptor.m_fd, str.data(), str.size()); |
77 | | - |
78 | | - if (bytes_written < 0) { |
79 | | - if (errno == EINTR || errno == EAGAIN) |
80 | | - continue; |
81 | | - return false; |
82 | | - } |
83 | | - str = str.drop_front(bytes_written); |
84 | | - } |
| 18 | + if (!descriptor) |
| 19 | + return false; |
85 | 20 |
|
86 | | - return true; |
| 21 | + size_t num_bytes = str.size(); |
| 22 | + auto status = descriptor->Write(str.data(), num_bytes); |
| 23 | + return status.Success(); |
87 | 24 | } |
88 | 25 |
|
89 | 26 | bool InputStream::read_full(std::ofstream *log, size_t length, |
90 | 27 | std::string &text) { |
| 28 | + if (!descriptor) |
| 29 | + return false; |
| 30 | + |
91 | 31 | std::string data; |
92 | 32 | data.resize(length); |
93 | 33 |
|
94 | | - char *ptr = &data[0]; |
95 | | - while (length != 0) { |
96 | | - int bytes_read = 0; |
97 | | - if (descriptor.m_is_socket) |
98 | | - bytes_read = ::recv(descriptor.m_socket, ptr, length, 0); |
99 | | - else |
100 | | - bytes_read = ::read(descriptor.m_fd, ptr, length); |
101 | | - |
102 | | - if (bytes_read == 0) { |
103 | | - if (log) |
104 | | - *log << "End of file (EOF) reading from input file.\n"; |
105 | | - return false; |
106 | | - } |
107 | | - if (bytes_read < 0) { |
108 | | - int reason = 0; |
109 | | -#if defined(_WIN32) |
110 | | - if (descriptor.m_is_socket) |
111 | | - reason = WSAGetLastError(); |
112 | | - else |
113 | | - reason = errno; |
114 | | -#else |
115 | | - reason = errno; |
116 | | - if (reason == EINTR || reason == EAGAIN) |
117 | | - continue; |
118 | | -#endif |
119 | | - |
120 | | - if (log) |
121 | | - *log << "Error " << reason << " reading from input file.\n"; |
122 | | - return false; |
123 | | - } |
| 34 | + auto status = descriptor->Read(data.data(), length); |
| 35 | + if (status.Fail()) |
| 36 | + return false; |
124 | 37 |
|
125 | | - assert(bytes_read >= 0 && (size_t)bytes_read <= length); |
126 | | - ptr += bytes_read; |
127 | | - length -= bytes_read; |
128 | | - } |
129 | 38 | text += data; |
130 | 39 | return true; |
131 | 40 | } |
|
0 commit comments