]>
Commit | Line | Data |
---|---|---|
f85d901f GRG |
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 | // -------------------------------------------------------------------------- | |
e2a6f233 | 19 | |
f85d901f | 20 | // For compilers that support precompilation, includes "wx/wx.h". |
f4ada568 GL |
21 | #include "wx/wxprec.h" |
22 | ||
23 | #ifdef __BORLANDC__ | |
f85d901f | 24 | # pragma hdrstop |
f4ada568 GL |
25 | #endif |
26 | ||
925e9792 | 27 | // for all others, include the necessary headers |
f4ada568 | 28 | #ifndef WX_PRECOMP |
f85d901f | 29 | # include "wx/wx.h" |
09fb22cf JS |
30 | #endif |
31 | ||
6097c3a2 GRG |
32 | #include "wx/socket.h" |
33 | #include "wx/url.h" | |
25ba2b89 | 34 | #include "wx/wfstream.h" |
e2a6f233 | 35 | |
f85d901f GRG |
36 | // -------------------------------------------------------------------------- |
37 | // resources | |
38 | // -------------------------------------------------------------------------- | |
f4ada568 | 39 | |
f85d901f | 40 | // the application icon |
618f2efa | 41 | #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) |
f85d901f | 42 | # include "mondrian.xpm" |
e2a6f233 JS |
43 | #endif |
44 | ||
f85d901f GRG |
45 | // -------------------------------------------------------------------------- |
46 | // classes | |
47 | // -------------------------------------------------------------------------- | |
f4ada568 | 48 | |
f85d901f GRG |
49 | // Define a new application type |
50 | class MyApp : public wxApp | |
51 | { | |
f4ada568 | 52 | public: |
f85d901f | 53 | virtual bool OnInit(); |
f4ada568 GL |
54 | }; |
55 | ||
f85d901f GRG |
56 | // Define a new frame type: this is going to be our main frame |
57 | class MyFrame : public wxFrame | |
f4ada568 GL |
58 | { |
59 | public: | |
f85d901f GRG |
60 | MyFrame(); |
61 | ~MyFrame(); | |
62 | ||
25ba2b89 | 63 | // event handlers for File menu |
f85d901f GRG |
64 | void OnQuit(wxCommandEvent& event); |
65 | void OnAbout(wxCommandEvent& event); | |
25ba2b89 GRG |
66 | |
67 | // event handlers for Socket menu | |
f85d901f GRG |
68 | void OnOpenConnection(wxCommandEvent& event); |
69 | void OnTest1(wxCommandEvent& event); | |
70 | void OnTest2(wxCommandEvent& event); | |
71 | void OnTest3(wxCommandEvent& event); | |
72 | void OnCloseConnection(wxCommandEvent& event); | |
25ba2b89 | 73 | |
16cafacf | 74 | #if wxUSE_URL |
25ba2b89 GRG |
75 | // event handlers for Protocols menu |
76 | void OnTestURL(wxCommandEvent& event); | |
16cafacf | 77 | #endif |
25ba2b89 | 78 | |
e51b0130 | 79 | // event handlers for DatagramSocket menu (stub) |
6097c3a2 | 80 | void OnDatagram(wxCommandEvent& event); |
f85d901f | 81 | |
25ba2b89 GRG |
82 | // socket event handler |
83 | void OnSocketEvent(wxSocketEvent& event); | |
84 | ||
f85d901f GRG |
85 | // convenience functions |
86 | void UpdateStatusBar(); | |
87 | ||
88 | private: | |
89 | wxSocketClient *m_sock; | |
f85d901f GRG |
90 | wxTextCtrl *m_text; |
91 | wxMenu *m_menuFile; | |
92 | wxMenu *m_menuSocket; | |
6097c3a2 | 93 | wxMenu *m_menuDatagramSocket; |
25ba2b89 | 94 | wxMenu *m_menuProtocols; |
f85d901f GRG |
95 | wxMenuBar *m_menuBar; |
96 | bool m_busy; | |
97 | ||
be5a51fb | 98 | // any class wishing to process wxWidgets events must use this macro |
f85d901f | 99 | DECLARE_EVENT_TABLE() |
f4ada568 GL |
100 | }; |
101 | ||
f85d901f GRG |
102 | // -------------------------------------------------------------------------- |
103 | // constants | |
104 | // -------------------------------------------------------------------------- | |
f4ada568 | 105 | |
f85d901f GRG |
106 | // IDs for the controls and the menu commands |
107 | enum | |
f4ada568 | 108 | { |
f85d901f | 109 | // menu items |
91b07357 JS |
110 | CLIENT_QUIT = wxID_EXIT, |
111 | CLIENT_ABOUT = wxID_ABOUT, | |
112 | CLIENT_OPEN = 100, | |
f85d901f GRG |
113 | CLIENT_TEST1, |
114 | CLIENT_TEST2, | |
115 | CLIENT_TEST3, | |
116 | CLIENT_CLOSE, | |
16cafacf | 117 | #if wxUSE_URL |
25ba2b89 | 118 | CLIENT_TESTURL, |
16cafacf | 119 | #endif |
6097c3a2 | 120 | CLIENT_DGRAM, |
f85d901f GRG |
121 | |
122 | // id for socket | |
123 | SOCKET_ID | |
124 | }; | |
f4ada568 | 125 | |
f85d901f | 126 | // -------------------------------------------------------------------------- |
be5a51fb | 127 | // event tables and other macros for wxWidgets |
f85d901f | 128 | // -------------------------------------------------------------------------- |
f4ada568 | 129 | |
f85d901f | 130 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
25ba2b89 GRG |
131 | EVT_MENU(CLIENT_QUIT, MyFrame::OnQuit) |
132 | EVT_MENU(CLIENT_ABOUT, MyFrame::OnAbout) | |
133 | EVT_MENU(CLIENT_OPEN, MyFrame::OnOpenConnection) | |
134 | EVT_MENU(CLIENT_TEST1, MyFrame::OnTest1) | |
135 | EVT_MENU(CLIENT_TEST2, MyFrame::OnTest2) | |
136 | EVT_MENU(CLIENT_TEST3, MyFrame::OnTest3) | |
137 | EVT_MENU(CLIENT_CLOSE, MyFrame::OnCloseConnection) | |
138 | EVT_MENU(CLIENT_DGRAM, MyFrame::OnDatagram) | |
16cafacf | 139 | #if wxUSE_URL |
25ba2b89 | 140 | EVT_MENU(CLIENT_TESTURL, MyFrame::OnTestURL) |
16cafacf | 141 | #endif |
25ba2b89 | 142 | EVT_SOCKET(SOCKET_ID, MyFrame::OnSocketEvent) |
f85d901f | 143 | END_EVENT_TABLE() |
f4ada568 | 144 | |
f85d901f | 145 | IMPLEMENT_APP(MyApp) |
f4ada568 | 146 | |
f85d901f GRG |
147 | // ========================================================================== |
148 | // implementation | |
149 | // ========================================================================== | |
f4ada568 | 150 | |
f85d901f GRG |
151 | // -------------------------------------------------------------------------- |
152 | // the application class | |
153 | // -------------------------------------------------------------------------- | |
f4ada568 | 154 | |
f85d901f GRG |
155 | bool MyApp::OnInit() |
156 | { | |
45e6e6f8 VZ |
157 | if ( !wxApp::OnInit() ) |
158 | return false; | |
159 | ||
f85d901f GRG |
160 | // Create the main application window |
161 | MyFrame *frame = new MyFrame(); | |
f4ada568 | 162 | |
f85d901f | 163 | // Show it and tell the application that it's our main window |
b62ca03d | 164 | frame->Show(true); |
f85d901f GRG |
165 | SetTopWindow(frame); |
166 | ||
167 | // success | |
b62ca03d | 168 | return true; |
f4ada568 GL |
169 | } |
170 | ||
f85d901f GRG |
171 | // -------------------------------------------------------------------------- |
172 | // main frame | |
173 | // -------------------------------------------------------------------------- | |
174 | ||
175 | // frame constructor | |
b62ca03d | 176 | MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, wxID_ANY, |
25ba2b89 | 177 | _("wxSocket demo: Client"), |
f85d901f | 178 | wxDefaultPosition, wxSize(300, 200)) |
f4ada568 | 179 | { |
f85d901f GRG |
180 | // Give the frame an icon |
181 | SetIcon(wxICON(mondrian)); | |
182 | ||
183 | // Make menus | |
184 | m_menuFile = new wxMenu(); | |
25ba2b89 | 185 | m_menuFile->Append(CLIENT_ABOUT, _("&About...\tCtrl-A"), _("Show about dialog")); |
f85d901f | 186 | m_menuFile->AppendSeparator(); |
25ba2b89 | 187 | m_menuFile->Append(CLIENT_QUIT, _("E&xit\tAlt-X"), _("Quit client")); |
f85d901f GRG |
188 | |
189 | m_menuSocket = new wxMenu(); | |
25ba2b89 | 190 | m_menuSocket->Append(CLIENT_OPEN, _("&Open session"), _("Connect to server")); |
f85d901f | 191 | m_menuSocket->AppendSeparator(); |
25ba2b89 GRG |
192 | m_menuSocket->Append(CLIENT_TEST1, _("Test &1"), _("Test basic functionality")); |
193 | m_menuSocket->Append(CLIENT_TEST2, _("Test &2"), _("Test ReadMsg and WriteMsg")); | |
194 | m_menuSocket->Append(CLIENT_TEST3, _("Test &3"), _("Test large data transfer")); | |
f85d901f | 195 | m_menuSocket->AppendSeparator(); |
25ba2b89 | 196 | m_menuSocket->Append(CLIENT_CLOSE, _("&Close session"), _("Close connection")); |
f85d901f | 197 | |
6097c3a2 | 198 | m_menuDatagramSocket = new wxMenu(); |
25ba2b89 GRG |
199 | m_menuDatagramSocket->Append(CLIENT_DGRAM, _("Send Datagram"), _("Test UDP sockets")); |
200 | ||
16cafacf | 201 | #if wxUSE_URL |
25ba2b89 GRG |
202 | m_menuProtocols = new wxMenu(); |
203 | m_menuProtocols->Append(CLIENT_TESTURL, _("Test URL"), _("Get data from the specified URL")); | |
16cafacf | 204 | #endif |
6097c3a2 | 205 | |
f85d901f GRG |
206 | // Append menus to the menubar |
207 | m_menuBar = new wxMenuBar(); | |
25ba2b89 GRG |
208 | m_menuBar->Append(m_menuFile, _("&File")); |
209 | m_menuBar->Append(m_menuSocket, _("&SocketClient")); | |
210 | m_menuBar->Append(m_menuDatagramSocket, _("&DatagramSocket")); | |
16cafacf | 211 | #if wxUSE_URL |
25ba2b89 | 212 | m_menuBar->Append(m_menuProtocols, _("&Protocols")); |
16cafacf | 213 | #endif |
f85d901f GRG |
214 | SetMenuBar(m_menuBar); |
215 | ||
8520f137 | 216 | #if wxUSE_STATUSBAR |
f85d901f | 217 | // Status bar |
f4ada568 | 218 | CreateStatusBar(2); |
8520f137 | 219 | #endif // wxUSE_STATUSBAR |
f85d901f | 220 | |
bd4d918f | 221 | // Make a textctrl for logging |
b62ca03d | 222 | m_text = new wxTextCtrl(this, wxID_ANY, |
4693b20c | 223 | _("Welcome to wxSocket demo: Client\nClient ready\n"), |
bd4d918f | 224 | wxDefaultPosition, wxDefaultSize, |
f85d901f GRG |
225 | wxTE_MULTILINE | wxTE_READONLY); |
226 | ||
227 | // Create the socket | |
228 | m_sock = new wxSocketClient(); | |
bd4d918f GRG |
229 | |
230 | // Setup the event handler and subscribe to most events | |
f85d901f GRG |
231 | m_sock->SetEventHandler(*this, SOCKET_ID); |
232 | m_sock->SetNotify(wxSOCKET_CONNECTION_FLAG | | |
233 | wxSOCKET_INPUT_FLAG | | |
234 | wxSOCKET_LOST_FLAG); | |
b62ca03d | 235 | m_sock->Notify(true); |
f85d901f | 236 | |
b62ca03d | 237 | m_busy = false; |
f85d901f | 238 | UpdateStatusBar(); |
f4ada568 GL |
239 | } |
240 | ||
241 | MyFrame::~MyFrame() | |
242 | { | |
bd4d918f | 243 | // No delayed deletion here, as the frame is dying anyway |
f85d901f | 244 | delete m_sock; |
f4ada568 GL |
245 | } |
246 | ||
f85d901f GRG |
247 | // event handlers |
248 | ||
249 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
f4ada568 | 250 | { |
b62ca03d WS |
251 | // true is to force the frame to close |
252 | Close(true); | |
f4ada568 GL |
253 | } |
254 | ||
f85d901f GRG |
255 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
256 | { | |
4693b20c | 257 | wxMessageBox(_("wxSocket demo: Client\n(c) 1999 Guillermo Rodriguez Garcia\n"), |
25ba2b89 | 258 | _("About Client"), |
f85d901f GRG |
259 | wxOK | wxICON_INFORMATION, this); |
260 | } | |
261 | ||
262 | void MyFrame::OnOpenConnection(wxCommandEvent& WXUNUSED(event)) | |
f4ada568 GL |
263 | { |
264 | wxIPV4address addr; | |
f4ada568 | 265 | |
b62ca03d WS |
266 | m_menuSocket->Enable(CLIENT_OPEN, false); |
267 | m_menuSocket->Enable(CLIENT_CLOSE, false); | |
f85d901f | 268 | |
25ba2b89 | 269 | // Ask user for server address |
f85d901f | 270 | wxString hostname = wxGetTextFromUser( |
25ba2b89 GRG |
271 | _("Enter the address of the wxSocket demo server:"), |
272 | _("Connect ..."), | |
273 | _("localhost")); | |
f85d901f GRG |
274 | |
275 | addr.Hostname(hostname); | |
f4ada568 | 276 | addr.Service(3000); |
f4ada568 | 277 | |
25ba2b89 GRG |
278 | // Mini-tutorial for Connect() :-) |
279 | // --------------------------- | |
280 | // | |
281 | // There are two ways to use Connect(): blocking and non-blocking, | |
282 | // depending on the value passed as the 'wait' (2nd) parameter. | |
283 | // | |
b62ca03d WS |
284 | // Connect(addr, true) will wait until the connection completes, |
285 | // returning true on success and false on failure. This call blocks | |
25ba2b89 GRG |
286 | // the GUI (this might be changed in future releases to honour the |
287 | // wxSOCKET_BLOCK flag). | |
288 | // | |
b62ca03d WS |
289 | // Connect(addr, false) will issue a nonblocking connection request |
290 | // and return immediately. If the return value is true, then the | |
3103e8a9 | 291 | // connection has been already successfully established. If it is |
b62ca03d | 292 | // false, you must wait for the request to complete, either with |
25ba2b89 GRG |
293 | // WaitOnConnect() or by watching wxSOCKET_CONNECTION / LOST |
294 | // events (please read the documentation). | |
295 | // | |
296 | // WaitOnConnect() itself never blocks the GUI (this might change | |
297 | // in the future to honour the wxSOCKET_BLOCK flag). This call will | |
b62ca03d | 298 | // return false on timeout, or true if the connection request |
bd4d918f GRG |
299 | // completes, which in turn might mean: |
300 | // | |
301 | // a) That the connection was successfully established | |
302 | // b) That the connection request failed (for example, because | |
303 | // it was refused by the peer. | |
304 | // | |
25ba2b89 GRG |
305 | // Use IsConnected() to distinguish between these two. |
306 | // | |
307 | // So, in a brief, you should do one of the following things: | |
308 | // | |
309 | // For blocking Connect: | |
310 | // | |
b62ca03d | 311 | // bool success = client->Connect(addr, true); |
25ba2b89 GRG |
312 | // |
313 | // For nonblocking Connect: | |
314 | // | |
b62ca03d | 315 | // client->Connect(addr, false); |
bd4d918f | 316 | // |
b62ca03d | 317 | // bool waitmore = true; |
a85139a1 | 318 | // while (! client->WaitOnConnect(seconds, millis) && waitmore ) |
bd4d918f GRG |
319 | // { |
320 | // // possibly give some feedback to the user, | |
321 | // // update waitmore if needed. | |
322 | // } | |
a85139a1 | 323 | // bool success = client->IsConnected(); |
925e9792 | 324 | // |
25ba2b89 | 325 | // And that's all :-) |
e51b0130 | 326 | |
25ba2b89 | 327 | m_text->AppendText(_("\nTrying to connect (timeout = 10 sec) ...\n")); |
b62ca03d | 328 | m_sock->Connect(addr, false); |
25ba2b89 | 329 | m_sock->WaitOnConnect(10); |
f85d901f GRG |
330 | |
331 | if (m_sock->IsConnected()) | |
25ba2b89 | 332 | m_text->AppendText(_("Succeeded ! Connection established\n")); |
f85d901f GRG |
333 | else |
334 | { | |
335 | m_sock->Close(); | |
25ba2b89 GRG |
336 | m_text->AppendText(_("Failed ! Unable to connect\n")); |
337 | wxMessageBox(_("Can't connect to the specified host"), _("Alert !")); | |
f85d901f | 338 | } |
925e9792 | 339 | |
f85d901f | 340 | UpdateStatusBar(); |
f4ada568 GL |
341 | } |
342 | ||
f85d901f | 343 | void MyFrame::OnTest1(wxCommandEvent& WXUNUSED(event)) |
f4ada568 | 344 | { |
f85d901f | 345 | // Disable socket menu entries (exception: Close Session) |
b62ca03d | 346 | m_busy = true; |
f85d901f GRG |
347 | UpdateStatusBar(); |
348 | ||
25ba2b89 | 349 | m_text->AppendText(_("\n=== Test 1 begins ===\n")); |
f85d901f GRG |
350 | |
351 | // Tell the server which test we are running | |
f4d5e009 | 352 | unsigned char c = 0xBE; |
f85d901f GRG |
353 | m_sock->Write(&c, 1); |
354 | ||
355 | // Send some data and read it back. We know the size of the | |
356 | // buffer, so we can specify the exact number of bytes to be | |
6097c3a2 GRG |
357 | // sent or received and use the wxSOCKET_WAITALL flag. Also, |
358 | // we have disabled menu entries which could interfere with | |
359 | // the test, so we can safely avoid the wxSOCKET_BLOCK flag. | |
f85d901f GRG |
360 | // |
361 | // First we send a byte with the length of the string, then | |
362 | // we send the string itself (do NOT try to send any integral | |
6097c3a2 | 363 | // value larger than a byte "as is" across the network, or |
f85d901f GRG |
364 | // you might be in trouble! Ever heard about big and little |
365 | // endian computers?) | |
e51b0130 | 366 | |
f85d901f GRG |
367 | m_sock->SetFlags(wxSOCKET_WAITALL); |
368 | ||
b6ea3123 VZ |
369 | const wxChar *buf1 = _T("Test string (less than 256 chars!)"); |
370 | unsigned char len = (unsigned char)((wxStrlen(buf1) + 1)*sizeof(wxChar)); | |
371 | wxChar *buf2 = new wxChar[wxStrlen(buf1) + 1]; | |
f85d901f | 372 | |
25ba2b89 | 373 | m_text->AppendText(_("Sending a test buffer to the server ...")); |
f187448d | 374 | m_sock->Write(&len, 1); |
f85d901f | 375 | m_sock->Write(buf1, len); |
25ba2b89 | 376 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
f85d901f | 377 | |
25ba2b89 | 378 | m_text->AppendText(_("Receiving the buffer back from server ...")); |
f85d901f | 379 | m_sock->Read(buf2, len); |
25ba2b89 | 380 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
f85d901f | 381 | |
25ba2b89 | 382 | m_text->AppendText(_("Comparing the two buffers ...")); |
f85d901f GRG |
383 | if (memcmp(buf1, buf2, len) != 0) |
384 | { | |
25ba2b89 GRG |
385 | m_text->AppendText(_("failed!\n")); |
386 | m_text->AppendText(_("Test 1 failed !\n")); | |
f85d901f GRG |
387 | } |
388 | else | |
389 | { | |
25ba2b89 GRG |
390 | m_text->AppendText(_("done\n")); |
391 | m_text->AppendText(_("Test 1 passed !\n")); | |
f85d901f | 392 | } |
25ba2b89 | 393 | m_text->AppendText(_("=== Test 1 ends ===\n")); |
f4ada568 | 394 | |
f85d901f | 395 | delete[] buf2; |
b62ca03d | 396 | m_busy = false; |
f85d901f GRG |
397 | UpdateStatusBar(); |
398 | } | |
f4ada568 | 399 | |
f85d901f GRG |
400 | void MyFrame::OnTest2(wxCommandEvent& WXUNUSED(event)) |
401 | { | |
4693b20c MB |
402 | const wxChar *msg1; |
403 | wxChar *msg2; | |
f85d901f GRG |
404 | size_t len; |
405 | ||
406 | // Disable socket menu entries (exception: Close Session) | |
b62ca03d | 407 | m_busy = true; |
f85d901f GRG |
408 | UpdateStatusBar(); |
409 | ||
25ba2b89 | 410 | m_text->AppendText(_("\n=== Test 2 begins ===\n")); |
f85d901f GRG |
411 | |
412 | // Tell the server which test we are running | |
f4d5e009 | 413 | unsigned char c = 0xCE; |
f85d901f GRG |
414 | m_sock->Write(&c, 1); |
415 | ||
416 | // Here we use ReadMsg and WriteMsg to send messages with | |
417 | // a header with size information. Also, the reception is | |
418 | // event triggered, so we test input events as well. | |
419 | // | |
420 | // We need to set no flags here (ReadMsg and WriteMsg are | |
421 | // not affected by flags) | |
e51b0130 | 422 | |
f85d901f GRG |
423 | m_sock->SetFlags(wxSOCKET_WAITALL); |
424 | ||
425 | wxString s = wxGetTextFromUser( | |
25ba2b89 GRG |
426 | _("Enter an arbitrary string to send to the server:"), |
427 | _("Test 2 ..."), | |
be5a51fb | 428 | _("Yes I like wxWidgets!")); |
f85d901f | 429 | |
4693b20c MB |
430 | msg1 = s.c_str(); |
431 | len = (wxStrlen(msg1) + 1) * sizeof(wxChar); | |
432 | msg2 = new wxChar[wxStrlen(msg1) + 1]; | |
f85d901f | 433 | |
25ba2b89 | 434 | m_text->AppendText(_("Sending the string with WriteMsg ...")); |
f85d901f | 435 | m_sock->WriteMsg(msg1, len); |
25ba2b89 GRG |
436 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
437 | m_text->AppendText(_("Waiting for an event (timeout = 2 sec)\n")); | |
f85d901f GRG |
438 | |
439 | // Wait until data available (will also return if the connection is lost) | |
440 | m_sock->WaitForRead(2); | |
441 | ||
442 | if (m_sock->IsData()) | |
443 | { | |
25ba2b89 | 444 | m_text->AppendText(_("Reading the string back with ReadMsg ...")); |
f85d901f | 445 | m_sock->ReadMsg(msg2, len); |
25ba2b89 GRG |
446 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
447 | m_text->AppendText(_("Comparing the two buffers ...")); | |
f85d901f GRG |
448 | if (memcmp(msg1, msg2, len) != 0) |
449 | { | |
925e9792 | 450 | m_text->AppendText(_("failed!\n")); |
25ba2b89 | 451 | m_text->AppendText(_("Test 2 failed !\n")); |
f85d901f GRG |
452 | } |
453 | else | |
454 | { | |
25ba2b89 GRG |
455 | m_text->AppendText(_("done\n")); |
456 | m_text->AppendText(_("Test 2 passed !\n")); | |
f85d901f | 457 | } |
765e386b | 458 | } |
f85d901f | 459 | else |
25ba2b89 | 460 | m_text->AppendText(_("Timeout ! Test 2 failed.\n")); |
765e386b | 461 | |
25ba2b89 | 462 | m_text->AppendText(_("=== Test 2 ends ===\n")); |
765e386b | 463 | |
6097c3a2 | 464 | delete[] msg2; |
b62ca03d | 465 | m_busy = false; |
f85d901f | 466 | UpdateStatusBar(); |
765e386b GL |
467 | } |
468 | ||
f85d901f | 469 | void MyFrame::OnTest3(wxCommandEvent& WXUNUSED(event)) |
f4ada568 | 470 | { |
6097c3a2 GRG |
471 | char *buf1; |
472 | char *buf2; | |
473 | unsigned char len; | |
474 | ||
475 | // Disable socket menu entries (exception: Close Session) | |
b62ca03d | 476 | m_busy = true; |
6097c3a2 GRG |
477 | UpdateStatusBar(); |
478 | ||
25ba2b89 | 479 | m_text->AppendText(_("\n=== Test 3 begins ===\n")); |
6097c3a2 GRG |
480 | |
481 | // Tell the server which test we are running | |
f4d5e009 | 482 | unsigned char c = 0xDE; |
6097c3a2 GRG |
483 | m_sock->Write(&c, 1); |
484 | ||
485 | // This test also is similar to the first one but it sends a | |
486 | // large buffer so that wxSocket is actually forced to split | |
487 | // it into pieces and take care of sending everything before | |
488 | // returning. | |
e51b0130 | 489 | |
6097c3a2 GRG |
490 | m_sock->SetFlags(wxSOCKET_WAITALL); |
491 | ||
492 | // Note that len is in kbytes here! | |
f4d5e009 | 493 | len = 32; |
6097c3a2 GRG |
494 | buf1 = new char[len * 1024]; |
495 | buf2 = new char[len * 1024]; | |
496 | ||
497 | for (int i = 0; i < len * 1024; i ++) | |
498 | buf1[i] = (char)(i % 256); | |
499 | ||
25ba2b89 | 500 | m_text->AppendText(_("Sending a large buffer (32K) to the server ...")); |
f187448d | 501 | m_sock->Write(&len, 1); |
6097c3a2 | 502 | m_sock->Write(buf1, len * 1024); |
25ba2b89 | 503 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
6097c3a2 | 504 | |
25ba2b89 | 505 | m_text->AppendText(_("Receiving the buffer back from server ...")); |
6097c3a2 | 506 | m_sock->Read(buf2, len * 1024); |
25ba2b89 | 507 | m_text->AppendText(m_sock->Error() ? _("failed !\n") : _("done\n")); |
6097c3a2 | 508 | |
25ba2b89 | 509 | m_text->AppendText(_("Comparing the two buffers ...")); |
6097c3a2 GRG |
510 | if (memcmp(buf1, buf2, len) != 0) |
511 | { | |
25ba2b89 GRG |
512 | m_text->AppendText(_("failed!\n")); |
513 | m_text->AppendText(_("Test 3 failed !\n")); | |
6097c3a2 GRG |
514 | } |
515 | else | |
516 | { | |
25ba2b89 GRG |
517 | m_text->AppendText(_("done\n")); |
518 | m_text->AppendText(_("Test 3 passed !\n")); | |
6097c3a2 | 519 | } |
25ba2b89 | 520 | m_text->AppendText(_("=== Test 3 ends ===\n")); |
6097c3a2 GRG |
521 | |
522 | delete[] buf2; | |
b62ca03d | 523 | m_busy = false; |
6097c3a2 | 524 | UpdateStatusBar(); |
f4ada568 GL |
525 | } |
526 | ||
f85d901f | 527 | void MyFrame::OnCloseConnection(wxCommandEvent& WXUNUSED(event)) |
f4ada568 | 528 | { |
f85d901f GRG |
529 | m_sock->Close(); |
530 | UpdateStatusBar(); | |
f4ada568 GL |
531 | } |
532 | ||
6097c3a2 GRG |
533 | void MyFrame::OnDatagram(wxCommandEvent& WXUNUSED(event)) |
534 | { | |
f187448d | 535 | m_text->AppendText(_("\n=== Datagram test begins ===\n")); |
36bec0ac GRG |
536 | m_text->AppendText(_("Sorry, not implemented\n")); |
537 | m_text->AppendText(_("=== Datagram test ends ===\n")); | |
6097c3a2 GRG |
538 | } |
539 | ||
16cafacf RN |
540 | #if wxUSE_URL |
541 | ||
25ba2b89 GRG |
542 | void MyFrame::OnTestURL(wxCommandEvent& WXUNUSED(event)) |
543 | { | |
544 | // Note that we are creating a new socket here, so this | |
545 | // won't mess with the client/server demo. | |
546 | ||
547 | // Ask for the URL | |
548 | m_text->AppendText(_("\n=== URL test begins ===\n")); | |
549 | wxString urlname = wxGetTextFromUser(_("Enter an URL to get"), | |
550 | _("URL:"), | |
8dfea369 | 551 | _T("http://localhost")); |
25ba2b89 GRG |
552 | |
553 | // Parse the URL | |
554 | wxURL url(urlname); | |
6f505cae GRG |
555 | if (url.GetError() != wxURL_NOERR) |
556 | { | |
557 | m_text->AppendText(_("Error: couldn't parse URL\n")); | |
558 | m_text->AppendText(_("=== URL test ends ===\n")); | |
559 | return; | |
560 | } | |
25ba2b89 GRG |
561 | |
562 | // Try to get the input stream (connects to the given URL) | |
563 | m_text->AppendText(_("Trying to establish connection...\n")); | |
564 | wxYield(); | |
565 | wxInputStream *data = url.GetInputStream(); | |
566 | if (!data) | |
567 | { | |
568 | m_text->AppendText(_("Error: couldn't read from URL\n")); | |
569 | m_text->AppendText(_("=== URL test ends ===\n")); | |
570 | return; | |
571 | } | |
572 | ||
573 | // Print the contents type and file size | |
574 | wxString s; | |
4693b20c | 575 | s.Printf(_("Contents type: %s\nFile size: %i\nStarting to download...\n"), |
25ba2b89 GRG |
576 | url.GetProtocol().GetContentType().c_str(), |
577 | data->GetSize()); | |
578 | m_text->AppendText(s); | |
579 | wxYield(); | |
580 | ||
581 | // Get the data | |
061dccd2 | 582 | wxFile fileTest(wxT("test.url"), wxFile::write); |
0a3d26b8 | 583 | wxFileOutputStream sout(fileTest); |
6f505cae GRG |
584 | if (!sout.Ok()) |
585 | { | |
586 | m_text->AppendText(_("Error: couldn't open file for output\n")); | |
587 | m_text->AppendText(_("=== URL test ends ===\n")); | |
588 | return; | |
589 | } | |
590 | ||
25ba2b89 GRG |
591 | data->Read(sout); |
592 | m_text->AppendText(_("Results written to file: test.url\n")); | |
593 | m_text->AppendText(_("Done.\n")); | |
594 | m_text->AppendText(_("=== URL test ends ===\n")); | |
595 | ||
596 | delete data; | |
597 | } | |
598 | ||
16cafacf RN |
599 | #endif |
600 | ||
f85d901f | 601 | void MyFrame::OnSocketEvent(wxSocketEvent& event) |
f4ada568 | 602 | { |
25ba2b89 | 603 | wxString s = _("OnSocketEvent: "); |
f85d901f | 604 | |
bc7e88ae | 605 | switch(event.GetSocketEvent()) |
f85d901f | 606 | { |
25ba2b89 GRG |
607 | case wxSOCKET_INPUT : s.Append(_("wxSOCKET_INPUT\n")); break; |
608 | case wxSOCKET_LOST : s.Append(_("wxSOCKET_LOST\n")); break; | |
609 | case wxSOCKET_CONNECTION : s.Append(_("wxSOCKET_CONNECTION\n")); break; | |
610 | default : s.Append(_("Unexpected event !\n")); break; | |
f85d901f | 611 | } |
f4ada568 | 612 | |
f85d901f GRG |
613 | m_text->AppendText(s); |
614 | UpdateStatusBar(); | |
f4ada568 GL |
615 | } |
616 | ||
f85d901f | 617 | // convenience functions |
a324a7bc | 618 | |
f85d901f | 619 | void MyFrame::UpdateStatusBar() |
a324a7bc | 620 | { |
f85d901f | 621 | wxString s; |
a324a7bc | 622 | |
f85d901f GRG |
623 | if (!m_sock->IsConnected()) |
624 | { | |
25ba2b89 | 625 | s.Printf(_("Not connected")); |
a324a7bc | 626 | } |
f85d901f GRG |
627 | else |
628 | { | |
629 | wxIPV4address addr; | |
a324a7bc | 630 | |
f85d901f | 631 | m_sock->GetPeer(addr); |
25ba2b89 | 632 | s.Printf(_("%s : %d"), (addr.Hostname()).c_str(), addr.Service()); |
f85d901f | 633 | } |
f4ada568 | 634 | |
8520f137 | 635 | #if wxUSE_STATUSBAR |
f85d901f | 636 | SetStatusText(s, 1); |
8520f137 | 637 | #endif // wxUSE_STATUSBAR |
a737331d | 638 | |
f85d901f GRG |
639 | m_menuSocket->Enable(CLIENT_OPEN, !m_sock->IsConnected() && !m_busy); |
640 | m_menuSocket->Enable(CLIENT_TEST1, m_sock->IsConnected() && !m_busy); | |
641 | m_menuSocket->Enable(CLIENT_TEST2, m_sock->IsConnected() && !m_busy); | |
642 | m_menuSocket->Enable(CLIENT_TEST3, m_sock->IsConnected() && !m_busy); | |
643 | m_menuSocket->Enable(CLIENT_CLOSE, m_sock->IsConnected()); | |
f4ada568 | 644 | } |