]> git.saurik.com Git - wxWidgets.git/blob - samples/wxsocket/client.cpp
Removed two more GUI lock-ups. This time when
[wxWidgets.git] / samples / wxsocket / client.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: client.cpp
3 // Purpose: Client for wxSocket demo
4 // Author: Guillermo Rodriguez Garcia <guille@iies.es>
5 // Modified by:
6 // Created: 1999/09/19
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Guillermo Rodriguez Garcia
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ==========================================================================
13 // declarations
14 // ==========================================================================
15
16 // --------------------------------------------------------------------------
17 // headers
18 // --------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 # pragma implementation "client.cpp"
22 # pragma interface "client.cpp"
23 #endif
24
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 # pragma hdrstop
30 #endif
31
32 // for all others, include the necessary headers
33 #ifndef WX_PRECOMP
34 # include "wx/wx.h"
35 #endif
36
37 # include "wx/socket.h"
38 # include "wx/url.h"
39 # include "wx/protocol/http.h"
40 # include "wx/progdlg.h"
41
42 // --------------------------------------------------------------------------
43 // resources
44 // --------------------------------------------------------------------------
45
46 // the application icon
47 #if defined(__WXGTK__) || defined(__WXMOTIF__)
48 # include "mondrian.xpm"
49 #endif
50
51 // --------------------------------------------------------------------------
52 // classes
53 // --------------------------------------------------------------------------
54
55 // Define a new application type
56 class MyApp : public wxApp
57 {
58 public:
59 virtual bool OnInit();
60 };
61
62 // Define a new frame type: this is going to be our main frame
63 class MyFrame : public wxFrame
64 {
65 public:
66 MyFrame();
67 ~MyFrame();
68
69 // event handlers (these functions should _not_ be virtual)
70 void OnQuit(wxCommandEvent& event);
71 void OnAbout(wxCommandEvent& event);
72 void OnOpenConnection(wxCommandEvent& event);
73 void OnTest1(wxCommandEvent& event);
74 void OnTest2(wxCommandEvent& event);
75 void OnTest3(wxCommandEvent& event);
76 void OnCloseConnection(wxCommandEvent& event);
77 void OnSocketEvent(wxSocketEvent& event);
78
79 // convenience functions
80 void UpdateStatusBar();
81
82 private:
83 wxSocketClient *m_sock;
84 wxPanel *m_panel;
85 wxTextCtrl *m_text;
86 wxMenu *m_menuFile;
87 wxMenu *m_menuSocket;
88 wxMenuBar *m_menuBar;
89 bool m_busy;
90
91 // any class wishing to process wxWindows events must use this macro
92 DECLARE_EVENT_TABLE()
93 };
94
95 // --------------------------------------------------------------------------
96 // constants
97 // --------------------------------------------------------------------------
98
99 // IDs for the controls and the menu commands
100 enum
101 {
102 // menu items
103 CLIENT_QUIT = 1000,
104 CLIENT_ABOUT,
105 CLIENT_OPEN,
106 CLIENT_TEST1,
107 CLIENT_TEST2,
108 CLIENT_TEST3,
109 CLIENT_CLOSE,
110
111 // id for socket
112 SOCKET_ID
113 };
114
115 // --------------------------------------------------------------------------
116 // event tables and other macros for wxWindows
117 // --------------------------------------------------------------------------
118
119 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
120 EVT_MENU(CLIENT_QUIT, MyFrame::OnQuit)
121 EVT_MENU(CLIENT_ABOUT, MyFrame::OnAbout)
122 EVT_MENU(CLIENT_OPEN, MyFrame::OnOpenConnection)
123 EVT_MENU(CLIENT_TEST1, MyFrame::OnTest1)
124 EVT_MENU(CLIENT_TEST2, MyFrame::OnTest2)
125 EVT_MENU(CLIENT_TEST3, MyFrame::OnTest3)
126 EVT_MENU(CLIENT_CLOSE, MyFrame::OnCloseConnection)
127 EVT_SOCKET(SOCKET_ID, MyFrame::OnSocketEvent)
128 END_EVENT_TABLE()
129
130 IMPLEMENT_APP(MyApp)
131
132 // ==========================================================================
133 // implementation
134 // ==========================================================================
135
136 // --------------------------------------------------------------------------
137 // the application class
138 // --------------------------------------------------------------------------
139
140 bool MyApp::OnInit()
141 {
142 // Create the main application window
143 MyFrame *frame = new MyFrame();
144
145 // Show it and tell the application that it's our main window
146 frame->Show(TRUE);
147 SetTopWindow(frame);
148
149 // success
150 return TRUE;
151 }
152
153 // --------------------------------------------------------------------------
154 // main frame
155 // --------------------------------------------------------------------------
156
157 // frame constructor
158 MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, -1,
159 _T("wxSocket demo: Client"),
160 wxDefaultPosition, wxSize(300, 200))
161 {
162 // Give the frame an icon
163 SetIcon(wxICON(mondrian));
164
165 // Make menus
166 m_menuFile = new wxMenu();
167 m_menuFile->Append(CLIENT_ABOUT, _T("&About...\tCtrl-A"), _T("Show about dialog"));
168 m_menuFile->AppendSeparator();
169 m_menuFile->Append(CLIENT_QUIT, _T("E&xit\tAlt-X"), _T("Quit client"));
170
171 m_menuSocket = new wxMenu();
172 m_menuSocket->Append(CLIENT_OPEN, _T("&Open session"), _T("Connect to server"));
173 m_menuSocket->AppendSeparator();
174 m_menuSocket->Append(CLIENT_TEST1, _T("Test &1"), _T("Test basic functionality"));
175 m_menuSocket->Append(CLIENT_TEST2, _T("Test &2"), _T("Test ReadMsg and WriteMsg"));
176 m_menuSocket->Append(CLIENT_TEST3, _T("Test &3"), _T("Test large data transfer"));
177 m_menuSocket->AppendSeparator();
178 m_menuSocket->Append(CLIENT_CLOSE, _T("&Close session"), _T("Close connection"));
179
180 // Append menus to the menubar
181 m_menuBar = new wxMenuBar();
182 m_menuBar->Append(m_menuFile, _T("&File"));
183 m_menuBar->Append(m_menuSocket, _T("&Socket"));
184 SetMenuBar(m_menuBar);
185
186 // Status bar
187 CreateStatusBar(2);
188
189 // Make a panel with a textctrl in it
190 m_panel = new wxPanel(this, -1, wxPoint(0, 0), GetClientSize());
191 m_text = new wxTextCtrl(m_panel, -1,
192 _T("Welcome to wxSocket demo: Client\n")
193 _T("Client ready\n\n"),
194 wxPoint(0, 0), m_panel->GetClientSize(),
195 wxTE_MULTILINE | wxTE_READONLY);
196
197 // Create the socket
198 m_sock = new wxSocketClient();
199 m_sock->SetEventHandler(*this, SOCKET_ID);
200 m_sock->SetNotify(wxSOCKET_CONNECTION_FLAG |
201 wxSOCKET_INPUT_FLAG |
202 wxSOCKET_LOST_FLAG);
203 m_sock->Notify(TRUE);
204
205 m_busy = FALSE;
206 UpdateStatusBar();
207 }
208
209 MyFrame::~MyFrame()
210 {
211 delete m_sock;
212 }
213
214 // event handlers
215
216 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
217 {
218 // TRUE is to force the frame to close
219 Close(TRUE);
220 }
221
222 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
223 {
224 wxMessageBox(_T("wxSocket demo: Client\n")
225 _T("(c) 1999 Guillermo Rodriguez Garcia\n"),
226 _T("About Client"),
227 wxOK | wxICON_INFORMATION, this);
228 }
229
230 void MyFrame::OnOpenConnection(wxCommandEvent& WXUNUSED(event))
231 {
232 wxIPV4address addr;
233
234 m_menuSocket->Enable(CLIENT_OPEN, FALSE);
235 m_menuSocket->Enable(CLIENT_CLOSE, FALSE);
236
237 // Ask server address
238 wxString hostname = wxGetTextFromUser(
239 _T("Enter the address of the wxSocket demo server:"),
240 _T("Connect ..."),
241 _T("localhost"));
242
243 addr.Hostname(hostname);
244 addr.Service(3000);
245
246 // Non-blocking connect
247 m_text->AppendText(_T("Trying to connect (timeout = 10 sec) ...\n"));
248 m_sock->Connect(addr, FALSE);
249 m_sock->WaitOnConnect(10);
250
251 if (m_sock->IsConnected())
252 m_text->AppendText(_T("Succeeded ! Connection established\n"));
253 else
254 {
255 m_sock->Close();
256 m_text->AppendText(_T("Failed ! Unable to connect\n"));
257 wxMessageBox(_T("Can't connect to the specified host"), _T("Alert !"));
258 }
259
260 UpdateStatusBar();
261 }
262
263 void MyFrame::OnTest1(wxCommandEvent& WXUNUSED(event))
264 {
265 char *buf1, *buf2;
266 char len;
267
268 // Disable socket menu entries (exception: Close Session)
269 m_busy = TRUE;
270 UpdateStatusBar();
271
272 m_text->AppendText(_T("\n=== Test 1 begins ===\n"));
273
274 // Tell the server which test we are running
275 char c = 0xBE;
276 m_sock->Write(&c, 1);
277
278 // Send some data and read it back. We know the size of the
279 // buffer, so we can specify the exact number of bytes to be
280 // sent or received and use the WAITALL flag. Also, we have
281 // disabled menu entries which could interfere with the test,
282 // so we can safely avoid the BLOCK (formerly SPEED) flag.
283 //
284 // First we send a byte with the length of the string, then
285 // we send the string itself (do NOT try to send any integral
286 // value larger than a byte "as is" acrosss the network, or
287 // you might be in trouble! Ever heard about big and little
288 // endian computers?)
289 //
290 m_sock->SetFlags(wxSOCKET_WAITALL);
291
292 buf1 = _T("Test string (less than 127 chars!)");
293 len = wxStrlen(buf1) + 1;
294 buf2 = new char[len];
295
296 m_text->AppendText(_T("Sending a test buffer to the server ..."));
297 m_sock->Write(&len, 1);
298 m_sock->Write(buf1, len);
299 m_text->AppendText(m_sock->Error() ? _T("failed !\n") : _T("done\n"));
300
301 m_text->AppendText(_T("Receiving the buffer back from server ..."));
302 m_sock->Read(buf2, len);
303 m_text->AppendText(m_sock->Error() ? _T("failed !\n") : _T("done\n"));
304
305 m_text->AppendText(_T("Comparing the two buffers ..."));
306 if (memcmp(buf1, buf2, len) != 0)
307 {
308 m_text->AppendText(_T("failed!\n"));
309 m_text->AppendText(_T("Test 1 failed !\n"));
310 }
311 else
312 {
313 m_text->AppendText(_T("done\n"));
314 m_text->AppendText(_T("Test 1 passed !\n"));
315 }
316 m_text->AppendText(_T("=== Test 1 ends ===\n"));
317
318 delete[] buf2;
319 m_busy = FALSE;
320 UpdateStatusBar();
321 }
322
323 void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event))
324 {
325 char *msg1;
326 char *msg2;
327 size_t len;
328
329 // Disable socket menu entries (exception: Close Session)
330 m_busy = TRUE;
331 UpdateStatusBar();
332
333 m_text->AppendText(_T("\n=== Test 2 begins ===\n"));
334
335 // Tell the server which test we are running
336 char c = 0xCE;
337 m_sock->Write(&c, 1);
338
339 // Here we use ReadMsg and WriteMsg to send messages with
340 // a header with size information. Also, the reception is
341 // event triggered, so we test input events as well.
342 //
343 // We need to set no flags here (ReadMsg and WriteMsg are
344 // not affected by flags)
345 //
346 m_sock->SetFlags(wxSOCKET_WAITALL);
347
348 wxString s = wxGetTextFromUser(
349 _T("Enter an arbitrary string to send to the server:"),
350 _T("Test 2 ..."),
351 _T("Yes I like wxWindows!"));
352
353 msg1 = (char *)s.c_str();
354 len = wxStrlen(msg1) + 1;
355 msg2 = (char *)malloc(len);
356
357 m_text->AppendText(_T("Sending the string with WriteMsg ..."));
358 m_sock->WriteMsg(msg1, len);
359 m_text->AppendText(m_sock->Error() ? _T("failed !\n") : _T("done\n"));
360 m_text->AppendText(_T("Waiting for an event (timeout = 2 sec)\n"));
361
362 // Wait until data available (will also return if the connection is lost)
363 m_sock->WaitForRead(2);
364
365 if (m_sock->IsData())
366 {
367 m_text->AppendText(_T("Reading the string back with ReadMsg ..."));
368 m_sock->ReadMsg(msg2, len);
369 m_text->AppendText(m_sock->Error() ? _T("failed !\n") : _T("done\n"));
370 m_text->AppendText(_T("Comparing the two buffers ..."));
371 if (memcmp(msg1, msg2, len) != 0)
372 {
373 m_text->AppendText(_T("failed!\n"));
374 m_text->AppendText(_T("Test 2 failed !\n"));
375 }
376 else
377 {
378 m_text->AppendText(_T("done\n"));
379 m_text->AppendText(_T("Test 2 passed !\n"));
380 }
381 }
382 else
383 m_text->AppendText(_T("Timeout ! Test 2 failed.\n"));
384
385 m_text->AppendText(_T("=== Test 2 ends ===\n"));
386
387 free(msg2);
388 m_busy = FALSE;
389 UpdateStatusBar();
390 }
391
392 void MyFrame::OnTest3(wxCommandEvent& WXUNUSED(event))
393 {
394 m_text->AppendText(_T("\n=== Test 3 begins ===\n"));
395 m_text->AppendText(_T("Test 3 not implemented\n"));
396 m_text->AppendText(_T("=== Test 3 ends ===\n"));
397 }
398
399 void MyFrame::OnCloseConnection(wxCommandEvent& WXUNUSED(event))
400 {
401 m_sock->Close();
402 UpdateStatusBar();
403 }
404
405 void MyFrame::OnSocketEvent(wxSocketEvent& event)
406 {
407 wxString s = _T("OnSocketEvent: ");
408
409 switch(event.SocketEvent())
410 {
411 case wxSOCKET_INPUT : s.Append(_T("wxSOCKET_INPUT\n")); break;
412 case wxSOCKET_LOST : s.Append(_T("wxSOCKET_LOST\n")); break;
413 case wxSOCKET_CONNECTION : s.Append(_T("wxSOCKET_CONNECTION\n")); break;
414 default : s.Append(_T("Unexpected event !\n")); break;
415 }
416
417 m_text->AppendText(s);
418 UpdateStatusBar();
419 }
420
421 // convenience functions
422
423 void MyFrame::UpdateStatusBar()
424 {
425 wxString s;
426
427 if (!m_sock->IsConnected())
428 {
429 s.Printf(_T("Not connected"));
430 }
431 else
432 {
433 wxIPV4address addr;
434
435 m_sock->GetPeer(addr);
436 s.Printf(_T("%s : %d"), (addr.Hostname()).c_str(), addr.Service());
437 }
438
439 SetStatusText(s, 1);
440
441 m_menuSocket->Enable(CLIENT_OPEN, !m_sock->IsConnected() && !m_busy);
442 m_menuSocket->Enable(CLIENT_TEST1, m_sock->IsConnected() && !m_busy);
443 m_menuSocket->Enable(CLIENT_TEST2, m_sock->IsConnected() && !m_busy);
444 m_menuSocket->Enable(CLIENT_TEST3, m_sock->IsConnected() && !m_busy);
445 m_menuSocket->Enable(CLIENT_CLOSE, m_sock->IsConnected());
446 }