1 //===-- SBCommunication.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 "lldb/API/SBCommunication.h"
11 #include "lldb/API/SBBroadcaster.h"
12 #include "lldb/Core/Communication.h"
13 #include "lldb/Core/ConnectionFileDescriptor.h"
14 
15 using namespace lldb;
16 using namespace lldb_private;
17 
18 
19 
20 SBCommunication::SBCommunication() :
21     m_lldb_object (NULL),
22     m_lldb_object_owned (false)
23 {
24 }
25 
26 SBCommunication::SBCommunication(const char * broadcaster_name) :
27     m_lldb_object (new Communication (broadcaster_name)),
28     m_lldb_object_owned (true)
29 {
30 }
31 
32 SBCommunication::~SBCommunication()
33 {
34     if (m_lldb_object && m_lldb_object_owned)
35         delete m_lldb_object;
36     m_lldb_object = NULL;
37     m_lldb_object_owned = false;
38 }
39 
40 ConnectionStatus
41 SBCommunication::CheckIfBytesAvailable ()
42 {
43     if (m_lldb_object)
44         return m_lldb_object->BytesAvailable (0, NULL);
45     return eConnectionStatusNoConnection;
46 }
47 
48 ConnectionStatus
49 SBCommunication::WaitForBytesAvailableInfinite ()
50 {
51     if (m_lldb_object)
52         return m_lldb_object->BytesAvailable (UINT32_MAX, NULL);
53     return eConnectionStatusNoConnection;
54 }
55 
56 ConnectionStatus
57 SBCommunication::WaitForBytesAvailableWithTimeout (uint32_t timeout_usec)
58 {
59     if (m_lldb_object)
60         return m_lldb_object->BytesAvailable (timeout_usec, NULL);
61     return eConnectionStatusNoConnection;
62 }
63 
64 ConnectionStatus
65 SBCommunication::Connect (const char *url)
66 {
67     if (m_lldb_object)
68     {
69         if (!m_lldb_object->HasConnection ())
70             m_lldb_object->SetConnection (new ConnectionFileDescriptor());
71         return m_lldb_object->Connect (url, NULL);
72     }
73     return eConnectionStatusNoConnection;
74 }
75 
76 ConnectionStatus
77 SBCommunication::AdoptFileDesriptor (int fd, bool owns_fd)
78 {
79     if (m_lldb_object)
80     {
81         if (m_lldb_object->HasConnection ())
82         {
83             if (m_lldb_object->IsConnected())
84                 m_lldb_object->Disconnect ();
85         }
86         m_lldb_object->SetConnection (new ConnectionFileDescriptor (fd, owns_fd));
87         if (m_lldb_object->IsConnected())
88             return eConnectionStatusSuccess;
89         else
90             return eConnectionStatusLostConnection;
91     }
92     return eConnectionStatusNoConnection;
93 }
94 
95 
96 ConnectionStatus
97 SBCommunication::Disconnect ()
98 {
99     if (m_lldb_object)
100         return m_lldb_object->Disconnect ();
101     return eConnectionStatusNoConnection;
102 }
103 
104 bool
105 SBCommunication::IsConnected () const
106 {
107     if (m_lldb_object)
108         return m_lldb_object->IsConnected ();
109     return false;
110 }
111 
112 size_t
113 SBCommunication::Read (void *dst, size_t dst_len, uint32_t timeout_usec, ConnectionStatus &status)
114 {
115     if (m_lldb_object)
116         return m_lldb_object->Read (dst, dst_len, timeout_usec, status, NULL);
117     status = eConnectionStatusNoConnection;
118     return 0;
119 }
120 
121 
122 size_t
123 SBCommunication::Write (const void *src, size_t src_len, ConnectionStatus &status)
124 {
125     if (m_lldb_object)
126         return m_lldb_object->Write (src, src_len, status, NULL);
127     status = eConnectionStatusNoConnection;
128     return 0;
129 }
130 
131 bool
132 SBCommunication::ReadThreadStart ()
133 {
134     if (m_lldb_object)
135         return m_lldb_object->StartReadThread ();
136     return false;
137 }
138 
139 
140 bool
141 SBCommunication::ReadThreadStop ()
142 {
143     if (m_lldb_object)
144         return m_lldb_object->StopReadThread ();
145     return false;
146 }
147 
148 bool
149 SBCommunication::ReadThreadIsRunning ()
150 {
151     if (m_lldb_object)
152         return m_lldb_object->ReadThreadIsRunning ();
153     return false;
154 }
155 
156 bool
157 SBCommunication::SetReadThreadBytesReceivedCallback
158 (
159     ReadThreadBytesReceived callback,
160     void *callback_baton
161 )
162 {
163     if (m_lldb_object)
164     {
165         m_lldb_object->SetReadThreadBytesReceivedCallback (callback, callback_baton);
166         return true;
167     }
168     return false;
169 }
170 
171 SBBroadcaster
172 SBCommunication::GetBroadcaster ()
173 {
174     SBBroadcaster broadcaster (m_lldb_object, false);
175     return broadcaster;
176 }
177 
178 
179 //
180 //void
181 //SBCommunication::CreateIfNeeded ()
182 //{
183 //    if (m_lldb_object == NULL)
184 //    {
185 //        static uint32_t g_broadcaster_num;
186 //        char broadcaster_name[256];
187 //        ::snprintf (name, broadcaster_name, "%p SBCommunication", this);
188 //        m_lldb_object = new Communication (broadcaster_name);
189 //        m_lldb_object_owned = true;
190 //    }
191 //    assert (m_lldb_object);
192 //}
193 //
194 //
195