summaryrefslogtreecommitdiffstats
path: root/quanta/components/debugger/dbgp/dbgpnetwork.cpp
blob: 7730960dc38ea3385c47b8a4884c726440ff7ea5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * Copyright (C) 2005 by Linus McCabe, <Linus@McCabe.nu>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "dbgpnetwork.h"

#include <kserversocket.h>
#include <kstreamsocket.h>
#include <kdebug.h>
#include <tdelocale.h>

DBGpNetwork::DBGpNetwork()
: TQObject()
{
  m_socket = NULL;
  m_server = NULL;
  m_datalen = -1;
  m_transaction_id = 0;
}

DBGpNetwork::~DBGpNetwork()
{
}

void DBGpNetwork::sessionStart(bool useproxy, const TQString& server, const TQString & service)
{

  kdDebug(24002) << k_funcinfo << ", m_server: " << m_server << ", m_socket" << m_socket << endl;
  m_useproxy = useproxy;

  if(m_useproxy)
  {
    if(m_socket)
    {
//       m_socket->setBufferSize(-1);
      connect(m_socket, TQT_SIGNAL(gotError(int)), this, TQT_SLOT(slotError(int)));
      connect(m_socket, TQT_SIGNAL(connected(const KResolverEntry &)), this, TQT_SLOT(slotConnected(const KNetwork::KResolverEntry &)));
      connect(m_socket, TQT_SIGNAL(closed()), this, TQT_SLOT(slotConnectionClosed()));
      connect(m_socket, TQT_SIGNAL(readyRead()), this, TQT_SLOT(slotReadyRead()));
      connect(m_socket, TQT_SIGNAL(destroyed()), this, TQT_SLOT(slotSocketDestroyed()));
      m_socket->connect();
      emit active(false);
      kdDebug(24002) << k_funcinfo << ", proxy:" << server << ", " << service << endl;
    }
  }
  else
  {
    if(!m_server)
    {
      m_server = new KNetwork::TDEServerSocket(service);

      m_server->setAddressReuseable(true);
      connect(m_server, TQT_SIGNAL(readyAccept()), this, TQT_SLOT(slotReadyAccept()));
      connect(m_server, TQT_SIGNAL(gotError(int)), this, TQT_SLOT(slotError(int)));

      if(m_server->listen())
      {
        emit active(true);
        emit networkError(i18n("Listening on port %1").arg(service), true);
      }
      else
      {
        delete m_server;
        m_server = NULL;
        emit active(false);
        emit networkError(i18n("Unable to listen on port %1").arg(service), true);
      }
    }
  }
}


void DBGpNetwork::sessionEnd()
{
  // Close socket
  if(m_socket)
  {
    m_socket->flush();
    disconnect(m_socket, TQT_SIGNAL(closed()), this, TQT_SLOT(slotConnectionClosed()));
    if (m_socket)
      m_socket->close();
    delete m_socket;
    m_socket = 0L;
  }

  // Close the server
  if(m_server)
  {
    m_server->close();
    delete m_server;
    m_server = NULL;
  }

  // Fake a connection closed signal
  slotConnectionClosed();
  emit active(false);
}


// Socket errors
void DBGpNetwork::slotError(int)
{
  kdDebug(24002) << k_funcinfo << ", m_server: " << m_server << ", m_socket" << m_socket << endl;
  if(m_socket)
  {
    kdDebug(24002) << k_funcinfo << ", " << m_socket->TDESocketBase::errorString() << endl;
    if(m_socket->error() == KNetwork::TDESocketBase::RemotelyDisconnected)
    {
       slotConnectionClosed();
       emit networkError(i18n("Disconnected from remote host"), true);
      return;
    }

    if(m_socket->error())
    {      
      emit networkError(m_socket->TDESocketBase::errorString(), true);
    }
  }

  if(m_server && m_server->error())
  {
    kdDebug(24002) << k_funcinfo << ", " << m_server->errorString() << endl;
    emit networkError(m_server->errorString(), true);
  }
}

// slotReadyAccept
void DBGpNetwork::slotReadyAccept()
{

  kdDebug(24002) << k_funcinfo << ", m_server: " << m_server << ", m_socket" << m_socket << endl;
  if(!m_socket)
  {
    disconnect(m_server, TQT_SIGNAL(readyAccept()), this, TQT_SLOT(slotReadyAccept()));

    m_socket = (KNetwork::KStreamSocket *)m_server->accept(); // TDESocketServer returns a KStreamSocket (!)
    if(m_socket)
    {
      kdDebug(24002) << k_funcinfo << ", ready" << ", m_socket" << m_socket << endl;
      m_socket->enableRead(true);
      m_socket->setAddressReuseable(true);
//       m_socket->setSocketFlags(KExtendedSocket::inetSocket |  KExtendedSocket::inputBufferedSocket);
//       m_socket->setBufferSize(-1);
      connect(m_socket, TQT_SIGNAL(gotError(int)), this, TQT_SLOT(slotError(int)));
      connect(m_socket, TQT_SIGNAL(connected(const KResolverEntry &)), this, TQT_SLOT(slotConnected(const KResolverEntry &)));
      connect(m_socket, TQT_SIGNAL(closed()), this, TQT_SLOT(slotConnectionClosed()));
      connect(m_socket, TQT_SIGNAL(readyRead()), this, TQT_SLOT(slotReadyRead()));
      connected();
    }
    else
    {
      kdDebug(24002) << k_funcinfo << ", " << m_server->errorString() << endl;
    }
  }

}

// Connection established
void DBGpNetwork::slotConnected(const KResolverEntry &)
{
  connected();
}

bool DBGpNetwork::isConnected()
{
  return  m_socket != NULL && m_socket->state() == KNetwork::KClientSocketBase::Connected;
}

bool DBGpNetwork::isActive()
{
  return
       (m_socket != NULL && m_socket->state() == KNetwork::KClientSocketBase::Connected) 
    || (m_server != NULL);
}

void DBGpNetwork::connected()
{

  kdDebug(24002) << k_funcinfo << endl;
  emit connected(true);
//   debuggerInterface()->enableAction("debug_disconnect", true);
//   debuggerInterface()->enableAction("debug_request", false);
}

// Connectio closed
void DBGpNetwork::slotConnectionClosed()
{
  kdDebug(24002) << k_funcinfo << ", m_server: " << m_server << ", m_socket" << m_socket << endl;

  // Check if we have more data to read
  slotReadyRead();
//   kdDebug(24002) << k_funcinfo << "buffer: " << m_buffer << endl;

  if(m_socket)
  {
    m_socket->flush();
    m_socket->close();
    delete m_socket;
    m_socket = NULL;
  }

  if(m_server)
    connect(m_server, TQT_SIGNAL(readyAccept()), this, TQT_SLOT(slotReadyAccept()));

  // Disable all session related actions and enable connection action
  emit connected(false);
  emit active(m_server != NULL);

}


//called when m_socket is destroyed either by deleting it or if XDebug disconnects from the client
void DBGpNetwork::slotSocketDestroyed()
{
  kdDebug(24002) << k_funcinfo << " , m_server: " << m_server << ", m_socket" << m_socket << endl;

  m_socket = NULL; //m_socket is already wrong, without this the app would crash on the next m_socket->close() or delete m_socket call.
  slotConnectionClosed();
}


// Data from socket
void DBGpNetwork::slotReadyRead()
{

  // Data from dbgp  
  while(m_socket && (m_socket->bytesAvailable() > 0 || m_fifo.length() >= (unsigned long)m_datalen))
  {
    int bytes;
    TQString data;

    if(m_socket && m_socket->bytesAvailable() > 0 )
    {
      // Read all available bytes from socket and append them to the buffer
      bytes = m_socket->bytesAvailable();
      char* buffer = new char[bytes];
      m_socket->readBlock(buffer, bytes);

      // Put it in the fifo buffer
      m_fifo.append(buffer, bytes);

      delete[] buffer;
    }

    while(1)
    {
      // If datalen == -1, we didnt read the size yet, otherwise we're reading data.
      if(m_datalen == -1)
      {
        bytes = m_fifo.find('\0');
        if(bytes < 0)
          break;

        data = m_fifo.retrieve();
        m_datalen = data.toLong();

      }
      if(m_datalen != -1 && (long)m_fifo.length() >= m_datalen + 1)
      {
        data = m_fifo.retrieve();
        m_datalen = -1;
        emit command(data);
      }
      else
        break;
    }
  }
}

long DBGpNetwork::sendCommand(const TQString & command)
{
  return sendCommand(command, "");
}

long DBGpNetwork::sendCommand(const TQString & command, const TQString & arguments)
{
  if(!isConnected())
    return false;

  m_transaction_id++;
  TQString commandline = command + TQString(" -i %1").arg(m_transaction_id) + (!arguments.isEmpty() ? " " : "") + arguments;

  kdDebug(24002) << k_funcinfo << ", sending: " << commandline << endl;

  m_socket->writeBlock(commandline.latin1(), commandline.length() + 1); // Send string + NULL termination

  return m_transaction_id;
}

long DBGpNetwork::sendCommand( const TQString & command, const TQString & arguments, const TQString & data )
{
  TQByteArrayFifo buffer;
  buffer.append(data.ascii(), data.length());
  return sendCommand(command, arguments + " -- " + buffer.base64Encoded());
}

// #include "dbgpnetwork.moc"

#include "dbgpnetwork.moc"