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>
23 using namespace lldb_dap
;
25 StreamDescriptor::StreamDescriptor() = default;
27 StreamDescriptor::StreamDescriptor(StreamDescriptor
&&other
) {
28 *this = std::move(other
);
31 StreamDescriptor::~StreamDescriptor() {
37 ::closesocket(m_socket
);
45 StreamDescriptor
&StreamDescriptor::operator=(StreamDescriptor
&&other
) {
46 m_close
= other
.m_close
;
47 other
.m_close
= false;
48 m_is_socket
= other
.m_is_socket
;
50 m_socket
= other
.m_socket
;
56 StreamDescriptor
StreamDescriptor::from_socket(SOCKET s
, bool close
) {
58 sd
.m_is_socket
= true;
64 StreamDescriptor
StreamDescriptor::from_file(int fd
, bool close
) {
66 sd
.m_is_socket
= false;
72 bool OutputStream::write_full(llvm::StringRef str
) {
73 while (!str
.empty()) {
74 int bytes_written
= 0;
75 if (descriptor
.m_is_socket
)
76 bytes_written
= ::send(descriptor
.m_socket
, str
.data(), str
.size(), 0);
78 bytes_written
= ::write(descriptor
.m_fd
, str
.data(), str
.size());
80 if (bytes_written
< 0) {
81 if (errno
== EINTR
|| errno
== EAGAIN
)
85 str
= str
.drop_front(bytes_written
);
91 bool InputStream::read_full(std::ofstream
*log
, size_t length
,
99 if (descriptor
.m_is_socket
)
100 bytes_read
= ::recv(descriptor
.m_socket
, ptr
, length
, 0);
102 bytes_read
= ::read(descriptor
.m_fd
, ptr
, length
);
104 if (bytes_read
== 0) {
106 *log
<< "End of file (EOF) reading from input file.\n";
109 if (bytes_read
< 0) {
112 if (descriptor
.m_is_socket
)
113 reason
= WSAGetLastError();
118 if (reason
== EINTR
|| reason
== EAGAIN
)
123 *log
<< "Error " << reason
<< " reading from input file.\n";
127 assert(bytes_read
>= 0 && (size_t)bytes_read
<= length
);
129 length
-= bytes_read
;
135 bool InputStream::read_line(std::ofstream
*log
, std::string
&line
) {
138 if (!read_full(log
, 1, line
))
141 if (llvm::StringRef(line
).endswith("\r\n"))
144 line
.erase(line
.size() - 2);
148 bool InputStream::read_expected(std::ofstream
*log
, llvm::StringRef expected
) {
150 if (!read_full(log
, expected
.size(), result
))
152 if (expected
!= result
) {
154 *log
<< "Warning: Expected '" << expected
.str() << "', got '" << result