1 //===-- IOStream.cpp --------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
14 #include <netinet/in.h>
15 #include <sys/socket.h>
22 using namespace lldb_dap
;
24 StreamDescriptor::StreamDescriptor() = default;
26 StreamDescriptor::StreamDescriptor(StreamDescriptor
&&other
) {
27 *this = std::move(other
);
30 StreamDescriptor::~StreamDescriptor() {
36 ::closesocket(m_socket
);
44 StreamDescriptor
&StreamDescriptor::operator=(StreamDescriptor
&&other
) {
45 m_close
= other
.m_close
;
46 other
.m_close
= false;
47 m_is_socket
= other
.m_is_socket
;
49 m_socket
= other
.m_socket
;
55 StreamDescriptor
StreamDescriptor::from_socket(SOCKET s
, bool close
) {
57 sd
.m_is_socket
= true;
63 StreamDescriptor
StreamDescriptor::from_file(int fd
, bool close
) {
65 sd
.m_is_socket
= false;
71 bool OutputStream::write_full(llvm::StringRef str
) {
72 while (!str
.empty()) {
73 int bytes_written
= 0;
74 if (descriptor
.m_is_socket
)
75 bytes_written
= ::send(descriptor
.m_socket
, str
.data(), str
.size(), 0);
77 bytes_written
= ::write(descriptor
.m_fd
, str
.data(), str
.size());
79 if (bytes_written
< 0) {
80 if (errno
== EINTR
|| errno
== EAGAIN
)
84 str
= str
.drop_front(bytes_written
);
90 bool InputStream::read_full(std::ofstream
*log
, size_t length
,
98 if (descriptor
.m_is_socket
)
99 bytes_read
= ::recv(descriptor
.m_socket
, ptr
, length
, 0);
101 bytes_read
= ::read(descriptor
.m_fd
, ptr
, length
);
103 if (bytes_read
== 0) {
105 *log
<< "End of file (EOF) reading from input file.\n";
108 if (bytes_read
< 0) {
111 if (descriptor
.m_is_socket
)
112 reason
= WSAGetLastError();
117 if (reason
== EINTR
|| reason
== EAGAIN
)
122 *log
<< "Error " << reason
<< " reading from input file.\n";
126 assert(bytes_read
>= 0 && (size_t)bytes_read
<= length
);
128 length
-= bytes_read
;
134 bool InputStream::read_line(std::ofstream
*log
, std::string
&line
) {
137 if (!read_full(log
, 1, line
))
140 if (llvm::StringRef(line
).ends_with("\r\n"))
143 line
.erase(line
.size() - 2);
147 bool InputStream::read_expected(std::ofstream
*log
, llvm::StringRef expected
) {
149 if (!read_full(log
, expected
.size(), result
))
151 if (expected
!= result
) {
153 *log
<< "Warning: Expected '" << expected
.str() << "', got '" << result