cleanup
[presencevnc] / src / vncclientthread.cpp
index 2954084..de97b5d 100644 (file)
@@ -23,7 +23,6 @@
 
 #include "vncclientthread.h"
 
-#include <QCoreApplication>
 #include <QMutexLocker>
 #include <QTimer>
 
@@ -99,7 +98,7 @@ void VncClientThread::updatefb(rfbClient* cl, int x, int y, int w, int h)
 void VncClientThread::cuttext(rfbClient* cl, const char *text, int textlen)
 {
     const QString cutText = QString::fromUtf8(text, textlen);
-    kDebug(5011) << cutText;
+    kDebug(5011) << "cuttext: " << cutText;
 
     if (!cutText.isEmpty()) {
         VncClientThread *t = (VncClientThread*)rfbClientGetClientData(cl, 0);
@@ -116,12 +115,20 @@ char *VncClientThread::passwdHandler(rfbClient *cl)
     VncClientThread *t = (VncClientThread*)rfbClientGetClientData(cl, 0);
     Q_ASSERT(t);
 
-    t->passwordRequest();
     t->m_passwordError = true;
+    t->passwordRequest();
 
     return strdup(t->password().toLocal8Bit());
 }
 
+void VncClientThread::setPassword(const QString &password)
+{
+       if(password.isNull()) //cancelled, don't retry
+               m_passwordError = false;
+
+       m_password = password;
+}
+
 void VncClientThread::outputHandler(const char *format, ...)
 {
     va_list args;
@@ -174,14 +181,11 @@ VncClientThread::~VncClientThread()
 
     const bool quitSuccess = wait(1000);
 
-    kDebug(5011) << "~VncClientThread(): Quit VNC thread success:" << quitSuccess;
+       if(!quitSuccess)
+               kDebug(5011) << "~VncClientThread(): Quit failed";
     
     delete [] frameBuffer;
     //cl is free()d when event loop exits.
-
-       kDebug(5011) << "inMessageHandler: " << inMessageHandler;
-
-       kDebug(5011) << "leaving ~VncClientThreod()";
 }
 
 void VncClientThread::checkOutputErrorMessage()
@@ -246,25 +250,29 @@ void VncClientThread::emitGotCut(const QString &text)
 
 void VncClientThread::stop()
 {
-    QMutexLocker locker(&mutex);
-//TODO: not locking the mutex leads to a crash, but at least it stops.
-//TODO: i don't see how this makes a difference
-       kDebug(5011) << "stop(): mutex locked";
-    m_stopped = true;
+       if(m_stopped)
+               return;
+
+       //also abort listening for connections, should be safe without locking
+       if(listen_port)
+               cl->listenSpecified = false;
+
+       QMutexLocker locker(&mutex);
+       m_stopped = true;
 }
 
 void VncClientThread::run()
 {
     QMutexLocker locker(&mutex);
-       bool clean = false;
 
     int passwd_failures = 0;
     while (!m_stopped) { // try to connect as long as the server allows
         m_passwordError = false;
+               outputErrorMessageString.clear(); //don't deliver error messages of old instances...
 
         rfbClientLog = outputHandler;
         rfbClientErr = outputHandler;
-        cl = rfbGetClient(8, 3, 4);
+        cl = rfbGetClient(8, 3, 4); // bitsPerSample, samplesPerPixel, bytesPerPixel
         cl->MallocFrameBuffer = newclient;
         cl->canHandleNewFBSize = true;
         cl->GetPassword = passwdHandler;
@@ -281,47 +289,37 @@ void VncClientThread::run()
             m_port += 5900;
         cl->serverPort = m_port;
 
+               cl->listenSpecified = rfbBool(listen_port > 0);
+               cl->listenPort = listen_port;
+
         kDebug(5011) << "--------------------- trying init ---------------------";
 
-        if (rfbInitClient(cl, 0, 0))
-            break;
+               if (rfbInitClient(cl, 0, 0))
+                       break;
 
+               //init failed...
         if (m_passwordError) {
                        passwd_failures++;
-                       if(passwd_failures > 2) {
-                               m_stopped = true;
-                               clean = true; //rfbInitClient cleans up after itself upon failure
-                       }
-                       continue;
+                       if(passwd_failures < 3)
+                               continue; //that's ok, try again
                }
 
-        return;
+               //stop connecting
+               m_stopped = true;
+        return; //no cleanup necessary, cl was free()d by rfbInitClient()
     }
 
     locker.unlock();
 
-       inMessageHandler = false;
-
     // Main VNC event loop
     while (!m_stopped) {
         const int i = WaitForMessage(cl, 500);
-        if (i < 0)
-            break;
-               if(m_stopped) {
-                       kDebug(5011) << "foo2";
+               if(m_stopped or i < 0)
                        break;
-               }
-        if (i) {
-                       inMessageHandler = true;
+
+        if (i)
             if (!HandleRFBServerMessage(cl))
                 break;
-                       inMessageHandler = false;
-               }
-
-               if(m_stopped) {
-                       kDebug(5011) << "foo4";
-                       break;
-               }
 
         locker.relock();
 
@@ -333,15 +331,11 @@ void VncClientThread::run()
 
         locker.unlock();
     }
-       kDebug(5011) << "exited main loop";
 
     // Cleanup allocated resources
     locker.relock();
-       kDebug(5011) << "mutex relocked";
-       if(!clean)
-               rfbClientCleanup(cl);
+       rfbClientCleanup(cl);
     m_stopped = true;
-       kDebug(5011) << "exiting run()";
 }
 
 ClientEvent::~ClientEvent()
@@ -389,5 +383,3 @@ void VncClientThread::clientCut(const QString &text)
 
     m_eventQueue.enqueue(new ClientCutEvent(text));
 }
-
-#include "moc_vncclientthread.cpp"