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
|
/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
*
* This 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 software 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 software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
// -=- ConnectingDialog.cxx
#include <stdlib.h>
#include <vncviewer/ConnectingDialog.h>
#include <vncviewer/resource.h>
#include <network/TcpSocket.h>
#include <rfb/Threading.h>
#include <rfb/Hostname.h>
#include <map>
using namespace rfb;
using namespace rfb::win32;
// ConnectingDialog callback
static BOOL CALLBACK ConnectingDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) {
bool* activePtr = (bool*)GetWindowLong(hwnd, GWL_USERDATA);
switch (uMsg) {
case WM_INITDIALOG:
SetWindowLong(hwnd, GWL_USERDATA, lParam);
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDCANCEL:
if (activePtr)
*activePtr = false;
return TRUE;
}
break;
case WM_DESTROY:
if (activePtr)
*activePtr = false;
return TRUE;
}
return 0;
}
// Global map, used by ConnectingDialog::Threads to call back to their owning
// ConnectingDialogs, while coping with the fact that the owner may already have quit.
static std::map<int, ConnectingDialog*> dialogs;
static int nextDialogId = 0;
static Mutex dialogsLock;
// ConnectingDialog::Thread
// Attempts to connect to the specified host. If the connection succeeds, the
// socket is saved in the owning ConnectingDialog, if still available, and the
// event is signalled. If the connection fails, the Exception text is returned
// to the dialog. If the dialog is already gone, the Exception/socket are discarded.
// NB: This thread class cleans itself up on exit - DO NOT join()!
class ConnectingDialog::Thread : public rfb::Thread {
public:
Thread(int dialogId_, const char* hostAndPort) : dialogId(dialogId_) {
setDeleteAfterRun();
getHostAndPort(hostAndPort, &host.buf, &port);
}
virtual void run() {
try {
returnSock(new network::TcpSocket(host.buf, port));
} catch (rdr::Exception& e) {
returnException(e);
}
}
void returnSock(network::Socket* s) {
Lock l(dialogsLock);
if (dialogs.count(dialogId)) {
dialogs[dialogId]->newSocket = s;
SetEvent(dialogs[dialogId]->readyEvent);
} else {
delete s;
}
}
void returnException(const rdr::Exception& e) {
Lock l(dialogsLock);
if (dialogs.count(dialogId)) {
dialogs[dialogId]->errMsg.replaceBuf(strDup(e.str()));
SetEvent(dialogs[dialogId]->readyEvent);
}
};
CharArray host;
int port;
int dialogId;
};
ConnectingDialog::ConnectingDialog() : dialog(0), newSocket(0),
readyEvent(CreateEvent(0, TRUE, FALSE, 0)), dialogId(0) {
}
network::Socket* ConnectingDialog::connect(const char* hostAndPort) {
Thread* connectThread = 0;
bool active = true;
errMsg.replaceBuf(0);
newSocket = 0;
// Get a unique dialog identifier and create the dialog window
{
Lock l(dialogsLock);
dialogId = ++nextDialogId;
dialogs[dialogId] = this;
dialog = CreateDialogParam(GetModuleHandle(0),
MAKEINTRESOURCE(IDD_CONNECTING_DLG), 0, &ConnectingDlgProc, (long)&active);
ShowWindow(dialog, SW_SHOW);
ResetEvent(readyEvent);
}
// Create and start the connection thread
try {
connectThread = new Thread(dialogId, hostAndPort);
connectThread->start();
} catch (rdr::Exception& e) {
errMsg.replaceBuf(strDup(e.str()));
active = false;
}
// Process window messages until the connection thread signals readyEvent, or the dialog is cancelled
while (active && (MsgWaitForMultipleObjects(1, &readyEvent.h, FALSE, INFINITE, QS_ALLINPUT) == WAIT_OBJECT_0 + 1)) {
MSG msg;
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
DispatchMessage(&msg);
}
// Remove this dialog from the table
// NB: If the dialog was cancelled then the thread is still running, and will only
// discover that we're gone when it looks up our unique Id in the dialog table.
{
Lock l(dialogsLock);
dialogs.erase(dialogId);
}
// Close the dialog window
DestroyWindow(dialog); dialog=0;
// Throw the exception, if there was one
if (errMsg.buf)
throw rdr::Exception(errMsg.buf);
// Otherwise, return the socket
// NB: The socket will be null if the dialog was cancelled
return newSocket;
}
|