Ticket #5765: qt4networkcontrol.diff
File qt4networkcontrol.diff, 6.6 KB (added by , 15 years ago) |
---|
-
main.cpp
1356 1356 if (gContext->GetNumSetting("NetworkControlEnabled", 0)) 1357 1357 { 1358 1358 int networkPort = gContext->GetNumSetting("NetworkControlPort", 6545); 1359 networkControl = new NetworkControl( networkPort);1360 if (!networkControl-> ok())1359 networkControl = new NetworkControl(); 1360 if (!networkControl->listen(QHostAddress::Any,networkPort)) 1361 1361 VERBOSE(VB_IMPORTANT, 1362 1362 QString("NetworkControl failed to bind to port %1.") 1363 1363 .arg(networkPort)); -
networkcontrol.h
3 3 4 4 #include <pthread.h> 5 5 6 #include <q 3serversocket.h>7 #include <q 3textstream.h>8 #include <q 3socket.h>9 #include <q 3valuelist.h>6 #include <qtcpserver.h> 7 #include <qtextstream.h> 8 #include <qtcpsocket.h> 9 #include <qlinkedlist.h> 10 10 #include <qobject.h> 11 11 #include <qmutex.h> 12 12 #include <qwaitcondition.h> 13 13 14 14 class MainServer; 15 15 16 class NetworkControl : public Q 3ServerSocket16 class NetworkControl : public QTcpServer 17 17 { 18 18 Q_OBJECT 19 19 public: 20 NetworkControl( int port);20 NetworkControl(); 21 21 ~NetworkControl(); 22 bool listen ( const QHostAddress & address = QHostAddress::Any, quint16 port = 0 ); 22 23 23 void newConnection(int socket);24 25 24 private slots: 25 void newConnection(); 26 26 void readClient(); 27 void discardClient();27 28 28 29 29 protected: 30 30 static void *SocketThread(void *param); … … 57 57 QMap <QString, int> keyMap; 58 58 59 59 QMutex clientLock; 60 Q 3Socket *client;61 Q 3TextStream *cs;60 QTcpSocket *client; 61 QTextStream *cs; 62 62 63 Q 3ValueList<QString> networkControlCommands;63 QLinkedList<QString> networkControlCommands; 64 64 QMutex ncLock; 65 65 QWaitCondition ncCond; 66 66 67 Q 3ValueList<QString> networkControlReplies;67 QLinkedList<QString> networkControlReplies; 68 68 QMutex nrLock; 69 69 70 70 pthread_t command_thread; -
networkcontrol.cpp
5 5 #include <QApplication> 6 6 #include <QRegExp> 7 7 #include <QStringList> 8 #include <q 3textstream.h>8 #include <qtextstream.h> 9 9 #include <QDir> 10 10 #include <QKeyEvent> 11 11 #include <QEvent> … … 45 45 return test.lower() == command.left(test.length()).lower(); 46 46 } 47 47 48 NetworkControl::NetworkControl( int port)49 : Q 3ServerSocket(port, 1),48 NetworkControl::NetworkControl() 49 : QTcpServer(), 50 50 prompt("# "), 51 51 gotAnswer(false), answer(""), 52 52 client(NULL), cs(NULL) 53 53 { 54 VERBOSE(VB_IMPORTANT, LOC +55 QString("Listening for remote connections on port %1").arg(port));56 57 54 // Eventually this map should be in the jumppoints table 58 55 jumpMap["channelpriorities"] = "Channel Recording Priorities"; 59 56 jumpMap["livetv"] = "Live TV"; … … 196 193 pthread_create(&command_thread, NULL, CommandThread, this); 197 194 198 195 gContext->addListener(this); 196 197 connect(this, SIGNAL(newConnection()), this, SLOT(newConnection())); 199 198 } 200 199 201 200 NetworkControl::~NetworkControl(void) … … 214 213 pthread_join(command_thread, NULL); 215 214 } 216 215 216 bool NetworkControl::listen(const QHostAddress & address, quint16 port) 217 { 218 if (QTcpServer::listen(address,port)) 219 { 220 VERBOSE(VB_IMPORTANT, LOC + 221 QString("Listening for remote connections on port %1").arg(port)); 222 return true; 223 } 224 return false; 225 } 226 217 227 void *NetworkControl::CommandThread(void *param) 218 228 { 219 229 NetworkControl *networkControl = (NetworkControl *)param; … … 278 288 } 279 289 } 280 290 281 void NetworkControl::newConnection( int socket)291 void NetworkControl::newConnection() 282 292 { 283 293 QString welcomeStr = ""; 284 294 bool closedOldConn = false; 285 Q 3Socket *s = new Q3Socket(this);295 QTcpSocket *s = this->nextPendingConnection(); 286 296 connect(s, SIGNAL(readyRead()), this, SLOT(readClient())); 287 connect(s, SIGNAL(delayedCloseFinished()), this, SLOT(discardClient())); 288 connect(s, SIGNAL(connectionClosed()), this, SLOT(discardClient())); 289 s->setSocket(socket); 297 connect(s, SIGNAL(disconnected()), s, SLOT(deleteLater())); 290 298 291 299 VERBOSE(VB_IMPORTANT, LOC + 292 QString("New connection established. (%1)").arg(socket));300 QString("New connection established.")); 293 301 294 Q3TextStream *os = new Q3TextStream(s);295 os->setEncoding(Q3TextStream::UnicodeUTF8);296 297 302 QMutexLocker locker(&clientLock); 303 if (cs) 304 { 305 cs->setDevice(s); 306 } 307 else 308 { 309 cs = new QTextStream(s); 310 cs->setEncoding(QTextStream::UnicodeUTF8); 311 } 312 298 313 if (client) 299 314 { 300 315 closedOldConn = true; 301 316 client->close(); 302 delete client;303 delete cs;304 317 } 305 306 318 client = s; 307 cs = os;308 319 309 320 ncLock.lock(); 310 321 networkControlCommands.clear(); … … 334 345 335 346 void NetworkControl::readClient(void) 336 347 { 337 Q 3Socket *socket = (Q3Socket *)sender();348 QTcpSocket *socket = (QTcpSocket *)sender(); 338 349 if (!socket) 339 350 return; 340 351 … … 359 370 } 360 371 } 361 372 362 void NetworkControl::discardClient(void)363 {364 Q3Socket *socket = (Q3Socket *)sender();365 if (!socket)366 return;367 368 QMutexLocker locker(&clientLock);369 if (client == socket)370 {371 delete cs;372 delete client;373 client = NULL;374 cs = NULL;375 }376 else377 delete socket;378 }379 380 373 QString NetworkControl::processJump(QStringList tokens) 381 374 { 382 375 QString result = "OK"; … … 899 892 nrLock.lock(); 900 893 replies = networkControlReplies.size(); 901 894 while (client && cs && replies > 0 && 902 client->state() == Q 3Socket::Connected)895 client->state() == QTcpSocket::Connected) 903 896 { 904 897 reply = networkControlReplies.front(); 905 898 networkControlReplies.pop_front(); 906 899 *cs << reply; 907 900 if (!reply.contains(crlfRegEx) || reply.contains(crlfcrlfRegEx)) 908 901 *cs << "\r\n" << prompt; 902 cs->flush(); 909 903 client->flush(); 910 904 911 905 replies = networkControlReplies.size(); … … 914 908 } 915 909 else if (e->type() == kNetworkControlCloseEvent) 916 910 { 917 if (client && client->state() == Q 3Socket::Connected)911 if (client && client->state() == QTcpSocket::Connected) 918 912 { 919 913 clientLock.lock(); 920 914 client->close();