1 //===-- GDBRemoteTestUtils.cpp ----------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "GDBRemoteTestUtils.h" 11 12 #include "lldb/Host/common/TCPSocket.h" 13 #include "lldb/Host/posix/ConnectionFileDescriptorPosix.h" 14 15 #include <future> 16 17 namespace lldb_private { 18 namespace process_gdb_remote { 19 20 void GDBRemoteTest::SetUpTestCase() { 21 #if defined(_MSC_VER) 22 WSADATA data; 23 ::WSAStartup(MAKEWORD(2, 2), &data); 24 #endif 25 } 26 27 void GDBRemoteTest::TearDownTestCase() { 28 #if defined(_MSC_VER) 29 ::WSACleanup(); 30 #endif 31 } 32 33 void Connect(GDBRemoteCommunication &client, GDBRemoteCommunication &server) { 34 bool child_processes_inherit = false; 35 Error error; 36 TCPSocket listen_socket(child_processes_inherit, error); 37 ASSERT_FALSE(error.Fail()); 38 error = listen_socket.Listen("127.0.0.1:0", 5); 39 ASSERT_FALSE(error.Fail()); 40 41 Socket *accept_socket; 42 std::future<Error> accept_error = std::async(std::launch::async, [&] { 43 return listen_socket.Accept("127.0.0.1:0", child_processes_inherit, 44 accept_socket); 45 }); 46 47 char connect_remote_address[64]; 48 snprintf(connect_remote_address, sizeof(connect_remote_address), 49 "connect://localhost:%u", listen_socket.GetLocalPortNumber()); 50 51 std::unique_ptr<ConnectionFileDescriptor> conn_ap( 52 new ConnectionFileDescriptor()); 53 ASSERT_EQ(conn_ap->Connect(connect_remote_address, nullptr), 54 lldb::eConnectionStatusSuccess); 55 56 client.SetConnection(conn_ap.release()); 57 ASSERT_TRUE(accept_error.get().Success()); 58 server.SetConnection(new ConnectionFileDescriptor(accept_socket)); 59 } 60 61 } // namespace process_gdb_remote 62 } // namespace lldb_private 63