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