forked from llvm-mirror/lldb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@214984 91177308-0d34-0410-b5e6-96231b3b80d8
- Loading branch information
Zachary Turner
committed
Aug 6, 2014
1 parent
db3cb6a
commit d5832f8
Showing
16 changed files
with
1,078 additions
and
1,006 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//===-- IOObject.h ----------------------------------------------*- C++ -*-===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef liblldb_Host_Common_IOObject_h_ | ||
#define liblldb_Host_Common_IOObject_h_ | ||
|
||
#include <stdarg.h> | ||
#include <stdio.h> | ||
#include <sys/types.h> | ||
|
||
#include "lldb/lldb-private.h" | ||
|
||
namespace lldb_private { | ||
|
||
class IOObject | ||
{ | ||
public: | ||
typedef enum | ||
{ | ||
eFDTypeFile, // Other FD requiring read/write | ||
eFDTypeSocket, // Socket requiring send/recv | ||
} FDType; | ||
|
||
// TODO: On Windows this should be a HANDLE, and wait should use | ||
// WaitForMultipleObjects | ||
typedef int WaitableHandle; | ||
static const WaitableHandle kInvalidHandleValue; | ||
|
||
IOObject(FDType type, bool should_close) | ||
: m_fd_type(type) | ||
, m_should_close_fd(should_close) | ||
{ | ||
} | ||
virtual ~IOObject() {} | ||
|
||
virtual Error Read (void *buf, size_t &num_bytes) = 0; | ||
virtual Error Write (const void *buf, size_t &num_bytes) = 0; | ||
virtual bool IsValid() const = 0; | ||
virtual Error Close() = 0; | ||
|
||
FDType GetFdType() const { return m_fd_type; } | ||
|
||
virtual WaitableHandle GetWaitableHandle() = 0; | ||
|
||
protected: | ||
FDType m_fd_type; | ||
bool m_should_close_fd; // True if this class should close the file descriptor when it goes away. | ||
|
||
private: | ||
DISALLOW_COPY_AND_ASSIGN (IOObject); | ||
}; | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
//===-- Socket.h ------------------------------------------------*- C++ -*-===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef liblldb_Host_Socket_h_ | ||
#define liblldb_Host_Socket_h_ | ||
|
||
#include <string> | ||
|
||
#include "lldb/lldb-private.h" | ||
|
||
#include "lldb/Core/Error.h" | ||
#include "lldb/Host/IOObject.h" | ||
#include "lldb/Host/Predicate.h" | ||
#include "lldb/Host/SocketAddress.h" | ||
|
||
#ifdef _WIN32 | ||
#include "lldb/Host/windows/windows.h" | ||
#include <winsock2.h> | ||
#include <ws2tcpip.h> | ||
#endif | ||
|
||
namespace llvm | ||
{ | ||
class StringRef; | ||
} | ||
|
||
namespace lldb_private { | ||
|
||
#if defined(_MSC_VER) | ||
typedef SOCKET NativeSocket; | ||
#else | ||
typedef int NativeSocket; | ||
#endif | ||
|
||
class Socket : public IOObject | ||
{ | ||
public: | ||
typedef enum | ||
{ | ||
ProtocolTcp, | ||
ProtocolUdp, | ||
ProtocolUnixDomain | ||
} SocketProtocol; | ||
|
||
static const NativeSocket kInvalidSocketValue; | ||
|
||
Socket(NativeSocket socket, SocketProtocol protocol, bool should_close); | ||
~Socket(); | ||
|
||
// Initialize a Tcp Socket object in listening mode. listen and accept are implemented | ||
// separately because the caller may wish to manipulate or query the socket after it is | ||
// initialized, but before entering a blocking accept. | ||
static Error TcpListen(llvm::StringRef host_and_port, Socket *&socket, Predicate<uint16_t>* predicate); | ||
static Error TcpConnect(llvm::StringRef host_and_port, Socket *&socket); | ||
static Error UdpConnect(llvm::StringRef host_and_port, Socket *&send_socket, Socket *&recv_socket); | ||
static Error UnixDomainConnect(llvm::StringRef host_and_port, Socket *&socket); | ||
static Error UnixDomainAccept(llvm::StringRef host_and_port, Socket *&socket); | ||
|
||
// Blocks on a listening socket until a connection is received. This method assumes that | ||
// |this->m_socket| is a listening socket, created via either TcpListen() or via the native | ||
// constructor that takes a NativeSocket, which itself was created via a call to |listen()| | ||
Error BlockingAccept(llvm::StringRef host_and_port, Socket *&socket); | ||
|
||
int GetOption (int level, int option_name, int &option_value); | ||
int SetOption (int level, int option_name, int option_value); | ||
|
||
static uint16_t GetPortNumber(const NativeSocket& socket); | ||
uint16_t GetPortNumber () const; | ||
|
||
NativeSocket GetNativeSocket () const { return m_socket; } | ||
SocketProtocol GetSocketProtocol() const { return m_protocol; } | ||
|
||
virtual Error Read (void *buf, size_t &num_bytes); | ||
virtual Error Write (const void *buf, size_t &num_bytes); | ||
|
||
virtual Error PreDisconnect(); | ||
virtual Error Close(); | ||
|
||
virtual bool IsValid() const { return m_socket != kInvalidSocketValue; } | ||
virtual WaitableHandle GetWaitableHandle(); | ||
|
||
protected: | ||
static bool | ||
DecodeHostAndPort (llvm::StringRef host_and_port, | ||
std::string &host_str, | ||
std::string &port_str, | ||
int32_t& port, | ||
Error *error_ptr); | ||
|
||
|
||
SocketProtocol m_protocol; | ||
NativeSocket m_socket; | ||
SocketAddress m_udp_send_sockaddr; // Send address used for UDP connections. | ||
}; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.