Creates a socket host object.
This patch moves the logic of many common socket operations into
its own class lldb_private::Socket. It then modifies the
ConnectionFileDescriptor class, and a few users of that class,
to use this new Socket class instead of hardcoding socket logic
directly.
Finally, this patch creates a common interface called IOObject for
any objects that support reading and writing, so that endpoints
such as sockets and files can be treated the same.
Differential Revision: http://reviews.llvm.org/D4641
Reviewed by: Todd Fiala, Greg Clayton
llvm-svn: 214984
diff --git a/lldb/source/Host/common/File.cpp b/lldb/source/Host/common/File.cpp
index 500b03c..50513af 100644
--- a/lldb/source/Host/common/File.cpp
+++ b/lldb/source/Host/common/File.cpp
@@ -24,6 +24,7 @@
#include "lldb/Core/DataBufferHeap.h"
#include "lldb/Core/Error.h"
+#include "lldb/Core/Log.h"
#include "lldb/Host/Config.h"
#include "lldb/Host/FileSpec.h"
@@ -77,11 +78,11 @@
FILE * File::kInvalidStream = NULL;
File::File(const char *path, uint32_t options, uint32_t permissions) :
+ IOObject(eFDTypeFile, false),
m_descriptor (kInvalidDescriptor),
m_stream (kInvalidStream),
m_options (),
m_own_stream (false),
- m_own_descriptor (false),
m_is_interactive (eLazyBoolCalculate),
m_is_real_terminal (eLazyBoolCalculate)
{
@@ -91,11 +92,11 @@
File::File (const FileSpec& filespec,
uint32_t options,
uint32_t permissions) :
+ IOObject(eFDTypeFile, false),
m_descriptor (kInvalidDescriptor),
m_stream (kInvalidStream),
m_options (0),
m_own_stream (false),
- m_own_descriptor (false),
m_is_interactive (eLazyBoolCalculate),
m_is_real_terminal (eLazyBoolCalculate)
@@ -107,11 +108,11 @@
}
File::File (const File &rhs) :
+ IOObject(eFDTypeFile, false),
m_descriptor (kInvalidDescriptor),
m_stream (kInvalidStream),
m_options (0),
m_own_stream (false),
- m_own_descriptor (false),
m_is_interactive (eLazyBoolCalculate),
m_is_real_terminal (eLazyBoolCalculate)
{
@@ -148,13 +149,20 @@
return kInvalidDescriptor;
}
+IOObject::WaitableHandle
+File::GetWaitableHandle()
+{
+ return m_descriptor;
+}
+
+
void
File::SetDescriptor (int fd, bool transfer_ownership)
{
if (IsValid())
Close();
m_descriptor = fd;
- m_own_descriptor = transfer_ownership;
+ m_should_close_fd = transfer_ownership;
}
@@ -168,7 +176,7 @@
const char *mode = GetStreamOpenModeFromOptions (m_options);
if (mode)
{
- if (!m_own_descriptor)
+ if (!m_should_close_fd)
{
// We must duplicate the file descriptor if we don't own it because
// when you call fdopen, the stream will own the fd
@@ -177,7 +185,7 @@
#else
m_descriptor = ::fcntl(GetDescriptor(), F_DUPFD);
#endif
- m_own_descriptor = true;
+ m_should_close_fd = true;
}
do
@@ -191,7 +199,7 @@
if (m_stream)
{
m_own_stream = true;
- m_own_descriptor = false;
+ m_should_close_fd = false;
}
}
}
@@ -228,7 +236,7 @@
else
{
m_options = rhs.m_options;
- m_own_descriptor = true;
+ m_should_close_fd = true;
}
}
else
@@ -307,7 +315,7 @@
error.SetErrorToErrno();
else
{
- m_own_descriptor = true;
+ m_should_close_fd = true;
m_options = options;
}
@@ -371,7 +379,7 @@
error.SetErrorToErrno();
}
- if (DescriptorIsValid() && m_own_descriptor)
+ if (DescriptorIsValid() && m_should_close_fd)
{
if (::close (m_descriptor) != 0)
error.SetErrorToErrno();
@@ -380,7 +388,7 @@
m_stream = kInvalidStream;
m_options = 0;
m_own_stream = false;
- m_own_descriptor = false;
+ m_should_close_fd = false;
m_is_interactive = eLazyBoolCalculate;
m_is_real_terminal = eLazyBoolCalculate;
return error;
@@ -669,6 +677,7 @@
num_bytes = 0;
error.SetErrorString("invalid file handle");
}
+
return error;
}