-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRemoteConsoleDlg.cpp
More file actions
361 lines (326 loc) · 9.41 KB
/
Copy pathRemoteConsoleDlg.cpp
File metadata and controls
361 lines (326 loc) · 9.41 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "stdafx.h"
#include "Axis3.h"
#include "Axis3Dlg.h"
#include "RemoteConsoleLoginDlg.h"
#include "RemoteConsoleDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
IMPLEMENT_DYNCREATE(CRemoteConsoleDlg, CDialogEx)
CRemoteConsoleDlg::CRemoteConsoleDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CRemoteConsoleDlg::IDD, pParent)
{
CWaitCursor hourglass;
m_bIsConnected = false;
m_bContinue = false;
m_bCommandPending = false;
m_iReceiveTimeout = 60000;
m_nIDTimer = 4000;
}
CRemoteConsoleDlg::~CRemoteConsoleDlg()
{
m_bContinue = false;
int i = 0;
while (m_bIsConnected && i < 100)
{
Sleep(100);
i++;
}
}
void CRemoteConsoleDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_RE_CONSOLE, m_creConsole);
DDX_Control(pDX, IDC_CONSOLE_SEND, m_ceConsoleSend);
DDX_Control(pDX, IDC_RECONNECT, m_cbReconnect);
DDX_Control(pDX, IDC_BROADCAST, m_cbBroadcast);
DDX_Check(pDX, IDC_BROADCAST, m_bBroadcast);
}
BEGIN_MESSAGE_MAP(CRemoteConsoleDlg, CDialogEx)
ON_BN_CLICKED(IDC_RECONNECT, OnReconnect)
ON_BN_CLICKED(IDC_BROADCAST, OnBroadcast)
ON_WM_TIMER()
ON_WM_SIZE()
ON_WM_SYSCOMMAND()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRemoteConsoleDlg message handlers
BOOL CRemoteConsoleDlg::OnInitDialog()
{
Axis->DBLng.BeginTransaction();
SetWindowText(CMsg(_T("Remote Console")));
GetDlgItem(IDC_RECONNECT)->SetWindowText(CMsg(_T("ReConnect")));
Axis->DBLng.CommitTransaction();
m_hIcon = AfxGetApp()->LoadIcon(IDI_COMMANDS);
SetIcon(m_hIcon, FALSE);
CDialogEx::OnInitDialog();
m_creConsole.LimitText(0x7FFFFFFF);
GetWindowRect(m_crOriginal);
m_creConsole.GetWindowRect(m_crConsole);
m_cbReconnect.GetWindowRect(m_crReConnect);
m_ceConsoleSend.GetWindowRect(m_crCommandText);
m_cbBroadcast.GetWindowRect(m_crBroadcast);
ScreenToClient(m_crOriginal);
ScreenToClient(m_crConsole);
ScreenToClient(m_crReConnect);
ScreenToClient(m_crCommandText);
ScreenToClient(m_crBroadcast);
return FALSE;
}
void CRemoteConsoleDlg::OnReconnect()
{
if (m_bIsConnected)
{
CWaitCursor hourglass;
m_bContinue = false;
m_cbReconnect.SetWindowText(CMsg(_T("ReConnect")));
}
else
{
CRemoteConsoleLoginDlg dlg;
if ( dlg.DoModal() != IDOK )
return;
m_csAddress = dlg.csAddress;
m_csPort = dlg.csPort;
if ((m_csAddress == "")||(m_csPort == ""))
{
AfxMessageBox(CMsg(_T("Couldn't determine the IP address for the remote server.")), MB_OK | MB_ICONEXCLAMATION);
return;
}
m_csAccount = dlg.csAccount;
m_csPassword = dlg.csPassword;
m_cbReconnect.SetWindowText(CMsg(_T("Disconnect")));
SetTimer(m_nIDTimer, 1000, NULL);
AfxBeginThread(&ClientThread, this);
}
}
void CRemoteConsoleDlg::OnTimer(UINT nIDEvent)
{
if (!m_bIsConnected)
{
m_cbReconnect.SetWindowText(CMsg(_T("ReConnect")));
KillTimer(nIDEvent);
}
SetWindowText(CMsg(_T("Remote Console - %1 (%2)"),true, m_csAccount, m_bIsConnected ? CMsg(_T("Connected")) : CMsg(_T("Not connected"))));
CDialogEx::OnTimer(nIDEvent);
}
void CRemoteConsoleDlg::OnConMessage(CString csText)
{
CString sToken;
int curPos = 0;
int iScrollCnt = 0;
sToken= csText.Tokenize(_T("\r\n"),curPos);
while (sToken != _T(""))
{
sToken = csText.Tokenize(_T("\r\n"), curPos);
iScrollCnt++;
}
if(m_creConsole)
{
int lastLine = m_creConsole.GetLineCount();
int lastchar = m_creConsole.LineIndex(lastLine);
m_creConsole.SetSel(lastchar, lastchar);
m_creConsole.ReplaceSel(csText);
m_creConsole.LineScroll(iScrollCnt);
}
}
void CRemoteConsoleDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if (nID == SC_CLOSE)
{
Main->m_DlgGeneral->HitConsole();
return;
}
CDialogEx::OnSysCommand(nID, lParam);
}
void CRemoteConsoleDlg::OnSize(UINT nType, int cx, int cy)
{
int dx, dy;
dx = 0; dy = 0;
CRect crNew;
GetWindowRect(crNew);
ScreenToClient(crNew);
dx = crNew.Width() - m_crOriginal.Width();
dy = crNew.Height() - m_crOriginal.Height();
m_creConsole.MoveWindow(m_crConsole.left , m_crConsole.top, m_crConsole.Width() + dx, m_crConsole.Height() + dy);
m_cbReconnect.MoveWindow(m_crReConnect.left + dx, m_crReConnect.top + dy, m_crReConnect.Width(), m_crReConnect.Height());
m_ceConsoleSend.MoveWindow(m_crCommandText.left, m_crCommandText.top + dy, m_crCommandText.Width() + dx, m_crCommandText.Height());
m_cbBroadcast.MoveWindow(m_crBroadcast.left, m_crBroadcast.top + dy, m_crBroadcast.Width(), m_crBroadcast.Height());
CDialogEx::OnSize(nType, cx, cy);
}
void CRemoteConsoleDlg::OnBroadcast()
{
UpdateData();
}
void CRemoteConsoleDlg::OnSendcommand()
{
CString csMessage = GetEditString(m_ceConsoleSend);
if ( m_bBroadcast )
m_csCommand.Format(_T("%s %s%c"), _T("B"), csMessage, 0x0d);
else
m_csCommand.Format(_T("%s%c"), csMessage, 0x0d);
if ( m_csCommand.GetLength() > 76 )
{
AfxMessageBox(CMsg(_T("Command too long.")), MB_OK);
return;
}
m_ceConsoleSend.SetWindowText(_T(""));
m_bCommandPending = true;
if (m_bIsConnected)
{
int i = 0;
while(m_bCommandPending && i < 100)
{
Sleep(100);
i++;
}
}
}
BOOL CRemoteConsoleDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN)
{
if(pMsg->wParam == VK_RETURN)
{
OnSendcommand();
return TRUE;
}
}
return CDialogEx::PreTranslateMessage(pMsg);
}
UINT ClientThread(LPVOID lpData)
{
CRemoteConsoleDlg * pConsole = (CRemoteConsoleDlg *) lpData;
BYTE szBuffer[MAX_BUFFER];
pConsole->m_bContinue = true;
CSocket sock;
sock.m_hSocket = socket(AF_INET, SOCK_STREAM, 0);
if (sock.m_hSocket == INVALID_SOCKET)
{
pConsole->OnConMessage(CMsg(_T("Error creating socket: dwErr = %1!ld!"),true, WSAGetLastError()));
closesocket(sock.m_hSocket);
sock.m_hSocket = INVALID_SOCKET;
pConsole->m_bCommandPending = false;
return 1;
}
if (!sock.Connect(pConsole->m_csAddress, _ttoi(pConsole->m_csPort)))
{
pConsole->OnConMessage(CMsg(_T("Could not connect to the remote console --\r\nLast Error was '%1'\r\nHost = % 2, Port = % 3\r\nThis is usually because the Sphere server isn't running."),true, ErrorString(GetLastError()) , pConsole->m_csAddress, pConsole->m_csPort));
closesocket(sock.m_hSocket);
sock.m_hSocket = INVALID_SOCKET;
pConsole->m_bCommandPending = false;
return 2;
}
// Log on.
Sleep( 100 );
CString cSpace((char) 0x20);
if (!sock.Send(cSpace, 1))
{
pConsole->OnConMessage(CMsg(_T("Error while sending login info to the remote console: dwErr = %1!ld!\r\nIs the Sphere server running ?"),true, GetLastError()));
closesocket(sock.m_hSocket);
sock.m_hSocket = INVALID_SOCKET;
pConsole->m_bCommandPending = false;
return 3;
}
// Receive the response
int i = 0;
while ( true )
{
memset(&szBuffer[0], 0x00, sizeof(szBuffer));
FD_SET fd;
struct timeval timeout;
FD_ZERO(&fd);
FD_SET(sock.m_hSocket, &fd);
timeout.tv_sec = 0;
timeout.tv_usec = 1000;
int rc = select(0, &fd, 0, 0, &timeout);
if ( rc < 0 )
{
char szBuffer[MAX_BUFFER];
strerror_s(szBuffer,MAX_BUFFER,WSAGetLastError());
pConsole->OnConMessage(CMsg(_T("Socket error on select(): %1!ld! - %2"),true, WSAGetLastError(), szBuffer));
closesocket(sock.m_hSocket);
sock.m_hSocket = INVALID_SOCKET;
pConsole->m_bCommandPending = false;
return 4;
}
if ( rc == 0 && i++ > pConsole->m_iReceiveTimeout )
{
pConsole->OnConMessage(CMsg(_T("Timeout exceeded while receiving data from remote server.")));
closesocket(sock.m_hSocket);
sock.m_hSocket = INVALID_SOCKET;
pConsole->m_bCommandPending = false;
return 5;
}
if ( rc > 0 )
break;
}
// Send the username?
if ( pConsole->m_csAccount != "" )
{
Sleep( 100 );
CString csAcct = pConsole->m_csAccount + (char)0x0A;
if ( !sock.Send(csAcct, csAcct.GetLength()* sizeof(TCHAR)))
{
pConsole->OnConMessage(CMsg(_T("Error while sending account to the remote console: dwErr = %1!ld!\r\nIs the Sphere server still running ?"),true, GetLastError()));
closesocket(sock.m_hSocket);
sock.m_hSocket = INVALID_SOCKET;
pConsole->m_bCommandPending = false;
return 6;
}
}
// Send the password
Sleep( 100 );
CString csPass = pConsole->m_csPassword + (char)0x0A;
if ( !sock.Send(csPass, csPass.GetLength()* sizeof(TCHAR)))
{
pConsole->OnConMessage(CMsg(_T("Error while sending password to the remote console: dwErr = %1!ld!\r\nIs the Sphere server still running ?"),true, GetLastError()));
closesocket(sock.m_hSocket);
sock.m_hSocket = INVALID_SOCKET;
pConsole->m_bCommandPending = false;
return 7;
}
pConsole->m_bIsConnected = true;
// Here's the main loop
while (pConsole->m_bContinue)
{
FD_SET fd;
struct timeval timeout;
memset(&szBuffer[0], 0x00, sizeof(szBuffer));
FD_ZERO(&fd);
FD_SET(sock.m_hSocket, &fd);
timeout.tv_sec = 0;
timeout.tv_usec = 1000;
int rc = select(0, &fd, 0, 0, &timeout);
if (rc > 0)
{
int nBytes = sock.Receive(&szBuffer[0], sizeof(szBuffer));
if (nBytes <= 0)
{
pConsole->OnConMessage(CMsg(_T("Connection lost.")));
break;
}
CString csText(szBuffer);
pConsole->OnConMessage(csText);
}
if (rc < 0)
{
pConsole->OnConMessage(CMsg(_T("Connection lost.")));
break;
}
if (pConsole->m_bCommandPending)
{
sock.Send( pConsole->m_csCommand, pConsole->m_csCommand.GetLength()* sizeof(TCHAR));
pConsole->m_bCommandPending = false;
pConsole->m_csCommand = "";
}
}
// Disconnect the socket
if ( !pConsole->m_bContinue)
pConsole->OnConMessage(CMsg(_T("Disconnecting from remote server...")));
closesocket(sock.m_hSocket);
sock.m_hSocket = INVALID_SOCKET;
pConsole->m_bCommandPending = false;
pConsole->m_bIsConnected = false;
return 0;
}