]> git.saurik.com Git - wxWidgets.git/blame - samples/sockets/server.cpp
Corrected wxRTTI for wxNotebook so dynamic casting to wxBookCtrlBase works
[wxWidgets.git] / samples / sockets / server.cpp
CommitLineData
f85d901f
GRG
1/////////////////////////////////////////////////////////////////////////////
2// Name: server.cpp
3// Purpose: Server 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
677a9f0c 27// for all others, include the necessary headers
f4ada568 28#ifndef WX_PRECOMP
f85d901f 29# include "wx/wx.h"
f4ada568 30#endif
e2a6f233 31
09fb22cf
JS
32#include "wx/socket.h"
33
f85d901f
GRG
34// --------------------------------------------------------------------------
35// resources
36// --------------------------------------------------------------------------
f4ada568 37
f85d901f 38// the application icon
2804f77d 39#include "mondrian.xpm"
e2a6f233 40
f85d901f
GRG
41// --------------------------------------------------------------------------
42// classes
43// --------------------------------------------------------------------------
44
f4ada568 45// Define a new application type
f85d901f
GRG
46class MyApp : public wxApp
47{
48public:
49 virtual bool OnInit();
f4ada568
GL
50};
51
f85d901f
GRG
52// Define a new frame type: this is going to be our main frame
53class MyFrame : public wxFrame
54{
f4ada568 55public:
f85d901f
GRG
56 MyFrame();
57 ~MyFrame();
58
59 // event handlers (these functions should _not_ be virtual)
60 void OnQuit(wxCommandEvent& event);
61 void OnAbout(wxCommandEvent& event);
62 void OnServerEvent(wxSocketEvent& event);
63 void OnSocketEvent(wxSocketEvent& event);
64
65 void Test1(wxSocketBase *sock);
66 void Test2(wxSocketBase *sock);
67 void Test3(wxSocketBase *sock);
68
69 // convenience functions
70 void UpdateStatusBar();
71
72private:
73 wxSocketServer *m_server;
f85d901f
GRG
74 wxTextCtrl *m_text;
75 wxMenu *m_menuFile;
76 wxMenuBar *m_menuBar;
77 bool m_busy;
78 int m_numClients;
79
be5a51fb 80 // any class wishing to process wxWidgets events must use this macro
f85d901f
GRG
81 DECLARE_EVENT_TABLE()
82};
83
84// --------------------------------------------------------------------------
85// constants
86// --------------------------------------------------------------------------
87
88// IDs for the controls and the menu commands
89enum
90{
91 // menu items
91b07357
JS
92 SERVER_QUIT = wxID_EXIT,
93 SERVER_ABOUT = wxID_ABOUT,
f85d901f
GRG
94
95 // id for sockets
91b07357 96 SERVER_ID = 100,
f85d901f 97 SOCKET_ID
f4ada568
GL
98};
99
f85d901f 100// --------------------------------------------------------------------------
be5a51fb 101// event tables and other macros for wxWidgets
f85d901f 102// --------------------------------------------------------------------------
f4ada568
GL
103
104BEGIN_EVENT_TABLE(MyFrame, wxFrame)
f85d901f
GRG
105 EVT_MENU(SERVER_QUIT, MyFrame::OnQuit)
106 EVT_MENU(SERVER_ABOUT, MyFrame::OnAbout)
107 EVT_SOCKET(SERVER_ID, MyFrame::OnServerEvent)
108 EVT_SOCKET(SOCKET_ID, MyFrame::OnSocketEvent)
f4ada568
GL
109END_EVENT_TABLE()
110
f4ada568
GL
111IMPLEMENT_APP(MyApp)
112
f4ada568 113
f85d901f
GRG
114// ==========================================================================
115// implementation
116// ==========================================================================
f4ada568 117
f85d901f
GRG
118// --------------------------------------------------------------------------
119// the application class
120// --------------------------------------------------------------------------
f4ada568 121
f85d901f
GRG
122bool MyApp::OnInit()
123{
45e6e6f8
VZ
124 if ( !wxApp::OnInit() )
125 return false;
126
f85d901f
GRG
127 // Create the main application window
128 MyFrame *frame = new MyFrame();
f4ada568 129
f85d901f 130 // Show it and tell the application that it's our main window
b62ca03d 131 frame->Show(true);
f85d901f 132 SetTopWindow(frame);
2b98cb1f 133
bd4d918f 134 // Success
b62ca03d 135 return true;
f4ada568
GL
136}
137
f85d901f
GRG
138// --------------------------------------------------------------------------
139// main frame
140// --------------------------------------------------------------------------
a737331d 141
f85d901f 142// frame constructor
bd4d918f 143
b62ca03d 144MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, wxID_ANY,
bd4d918f 145 _("wxSocket demo: Server"),
f85d901f 146 wxDefaultPosition, wxSize(300, 200))
f4ada568 147{
f85d901f
GRG
148 // Give the frame an icon
149 SetIcon(wxICON(mondrian));
150
151 // Make menus
152 m_menuFile = new wxMenu();
bd4d918f 153 m_menuFile->Append(SERVER_ABOUT, _("&About...\tCtrl-A"), _("Show about dialog"));
f85d901f 154 m_menuFile->AppendSeparator();
bd4d918f 155 m_menuFile->Append(SERVER_QUIT, _("E&xit\tAlt-X"), _("Quit server"));
f85d901f
GRG
156
157 // Append menus to the menubar
158 m_menuBar = new wxMenuBar();
bd4d918f 159 m_menuBar->Append(m_menuFile, _("&File"));
f85d901f
GRG
160 SetMenuBar(m_menuBar);
161
8520f137 162#if wxUSE_STATUSBAR
f85d901f
GRG
163 // Status bar
164 CreateStatusBar(2);
8520f137 165#endif // wxUSE_STATUSBAR
f85d901f 166
bd4d918f 167 // Make a textctrl for logging
b62ca03d 168 m_text = new wxTextCtrl(this, wxID_ANY,
bd4d918f
GRG
169 _("Welcome to wxSocket demo: Server\n"),
170 wxDefaultPosition, wxDefaultSize,
f85d901f
GRG
171 wxTE_MULTILINE | wxTE_READONLY);
172
bd4d918f 173 // Create the address - defaults to localhost:0 initially
8575ff50
VZ
174#if wxUSE_IPV6
175 wxIPV6address addr;
176#else
f85d901f 177 wxIPV4address addr;
8575ff50 178#endif
f85d901f
GRG
179 addr.Service(3000);
180
bd4d918f 181 // Create the socket
f85d901f 182 m_server = new wxSocketServer(addr);
f85d901f
GRG
183
184 // We use Ok() here to see if the server is really listening
bd4d918f
GRG
185 if (! m_server->Ok())
186 {
187 m_text->AppendText(_("Could not listen at the specified port !\n\n"));
188 return;
189 }
f85d901f 190 else
bd4d918f
GRG
191 {
192 m_text->AppendText(_("Server listening.\n\n"));
193 }
194
195 // Setup the event handler and subscribe to connection events
196 m_server->SetEventHandler(*this, SERVER_ID);
197 m_server->SetNotify(wxSOCKET_CONNECTION_FLAG);
b62ca03d 198 m_server->Notify(true);
f85d901f 199
b62ca03d 200 m_busy = false;
f85d901f
GRG
201 m_numClients = 0;
202 UpdateStatusBar();
f4ada568
GL
203}
204
f85d901f 205MyFrame::~MyFrame()
f4ada568 206{
bd4d918f 207 // No delayed deletion here, as the frame is dying anyway
f85d901f
GRG
208 delete m_server;
209}
f4ada568 210
f85d901f 211// event handlers
f4ada568 212
f85d901f
GRG
213void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
214{
b62ca03d
WS
215 // true is to force the frame to close
216 Close(true);
f85d901f 217}
f4ada568 218
f85d901f
GRG
219void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
220{
4693b20c 221 wxMessageBox(_("wxSocket demo: Server\n(c) 1999 Guillermo Rodriguez Garcia\n"),
bd4d918f 222 _("About Server"),
f85d901f 223 wxOK | wxICON_INFORMATION, this);
f4ada568
GL
224}
225
f85d901f 226void MyFrame::Test1(wxSocketBase *sock)
f4ada568 227{
f85d901f
GRG
228 unsigned char len;
229 char *buf;
f4ada568 230
bd4d918f 231 m_text->AppendText(_("Test 1 begins\n"));
f4ada568 232
f85d901f
GRG
233 // Receive data from socket and send it back. We will first
234 // get a byte with the buffer size, so we can specify the
6097c3a2
GRG
235 // exact size and use the wxSOCKET_WAITALL flag. Also, we
236 // disabled input events so we won't have unwanted reentrance.
237 // This way we can avoid the infamous wxSOCKET_BLOCK flag.
e51b0130 238
f85d901f
GRG
239 sock->SetFlags(wxSOCKET_WAITALL);
240
bc7e88ae 241 // Read the size
f187448d 242 sock->Read(&len, 1);
6097c3a2 243 buf = new char[len];
bc7e88ae
GRG
244
245 // Read the data
f85d901f 246 sock->Read(buf, len);
bc7e88ae
GRG
247 m_text->AppendText(_("Got the data, sending it back\n"));
248
249 // Write it back
f85d901f 250 sock->Write(buf, len);
6097c3a2 251 delete[] buf;
f85d901f 252
bc7e88ae 253 m_text->AppendText(_("Test 1 ends\n\n"));
f4ada568
GL
254}
255
f85d901f 256void MyFrame::Test2(wxSocketBase *sock)
f4ada568 257{
f85d901f
GRG
258#define MAX_MSG_SIZE 10000
259
260 wxString s;
4693b20c 261 wxChar *buf = new wxChar[MAX_MSG_SIZE];
f85d901f
GRG
262 wxUint32 len;
263
bd4d918f 264 m_text->AppendText(_("Test 2 begins\n"));
f85d901f
GRG
265
266 // We don't need to set flags because ReadMsg and WriteMsg
267 // are not affected by them anyway.
e51b0130 268
bc7e88ae 269 // Read the message
4693b20c 270 len = sock->ReadMsg(buf, MAX_MSG_SIZE * sizeof(wxChar)).LastCount();
bd4d918f 271 s.Printf(_("Client says: %s\n"), buf);
f85d901f 272 m_text->AppendText(s);
36bec0ac 273 m_text->AppendText(_("Sending the data back\n"));
f85d901f 274
bc7e88ae 275 // Write it back
f85d901f 276 sock->WriteMsg(buf, len);
6097c3a2 277 delete[] buf;
f85d901f 278
bc7e88ae 279 m_text->AppendText(_("Test 2 ends\n\n"));
f85d901f
GRG
280
281#undef MAX_MSG_SIZE
f4ada568
GL
282}
283
f85d901f 284void MyFrame::Test3(wxSocketBase *sock)
f4ada568 285{
6097c3a2
GRG
286 unsigned char len;
287 char *buf;
288
bd4d918f 289 m_text->AppendText(_("Test 3 begins\n"));
6097c3a2 290
677a9f0c 291 // This test is similar to the first one, but the len is
6097c3a2 292 // expressed in kbytes - this tests large data transfers.
e51b0130 293
6097c3a2
GRG
294 sock->SetFlags(wxSOCKET_WAITALL);
295
bc7e88ae 296 // Read the size
f187448d 297 sock->Read(&len, 1);
6097c3a2 298 buf = new char[len * 1024];
bc7e88ae
GRG
299
300 // Read the data
6097c3a2 301 sock->Read(buf, len * 1024);
bc7e88ae
GRG
302 m_text->AppendText(_("Got the data, sending it back\n"));
303
304 // Write it back
6097c3a2
GRG
305 sock->Write(buf, len * 1024);
306 delete[] buf;
307
bc7e88ae 308 m_text->AppendText(_("Test 3 ends\n\n"));
f4ada568
GL
309}
310
f85d901f 311void MyFrame::OnServerEvent(wxSocketEvent& event)
f4ada568 312{
bd4d918f 313 wxString s = _("OnServerEvent: ");
f85d901f
GRG
314 wxSocketBase *sock;
315
bc7e88ae 316 switch(event.GetSocketEvent())
f85d901f 317 {
bd4d918f
GRG
318 case wxSOCKET_CONNECTION : s.Append(_("wxSOCKET_CONNECTION\n")); break;
319 default : s.Append(_("Unexpected event !\n")); break;
f85d901f 320 }
f4ada568 321
f85d901f 322 m_text->AppendText(s);
765e386b 323
f85d901f 324 // Accept new connection if there is one in the pending
b62ca03d 325 // connections queue, else exit. We use Accept(false) for
f85d901f
GRG
326 // non-blocking accept (although if we got here, there
327 // should ALWAYS be a pending connection).
e51b0130 328
b62ca03d 329 sock = m_server->Accept(false);
f85d901f
GRG
330
331 if (sock)
332 {
bc7e88ae 333 m_text->AppendText(_("New client connection accepted\n\n"));
f85d901f
GRG
334 }
335 else
336 {
bc7e88ae 337 m_text->AppendText(_("Error: couldn't accept a new connection\n\n"));
f85d901f
GRG
338 return;
339 }
340
341 sock->SetEventHandler(*this, SOCKET_ID);
342 sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
b62ca03d 343 sock->Notify(true);
f85d901f
GRG
344
345 m_numClients++;
346 UpdateStatusBar();
f4ada568
GL
347}
348
f85d901f
GRG
349void MyFrame::OnSocketEvent(wxSocketEvent& event)
350{
bd4d918f 351 wxString s = _("OnSocketEvent: ");
f4d5e009 352 wxSocketBase *sock = event.GetSocket();
f85d901f 353
f4d5e009 354 // First, print a message
bc7e88ae 355 switch(event.GetSocketEvent())
f85d901f 356 {
f4d5e009
GRG
357 case wxSOCKET_INPUT : s.Append(_("wxSOCKET_INPUT\n")); break;
358 case wxSOCKET_LOST : s.Append(_("wxSOCKET_LOST\n")); break;
359 default : s.Append(_("Unexpected event !\n")); break;
f85d901f
GRG
360 }
361
362 m_text->AppendText(s);
363
364 // Now we process the event
bc7e88ae 365 switch(event.GetSocketEvent())
f85d901f
GRG
366 {
367 case wxSOCKET_INPUT:
368 {
369 // We disable input events, so that the test doesn't trigger
370 // wxSocketEvent again.
371 sock->SetNotify(wxSOCKET_LOST_FLAG);
372
373 // Which test are we going to run?
374 unsigned char c;
f4d5e009 375 sock->Read(&c, 1);
f85d901f
GRG
376
377 switch (c)
378 {
379 case 0xBE: Test1(sock); break;
380 case 0xCE: Test2(sock); break;
381 case 0xDE: Test3(sock); break;
f4d5e009
GRG
382 default:
383 m_text->AppendText(_("Unknown test id received from client\n\n"));
f85d901f
GRG
384 }
385
386 // Enable input events again.
387 sock->SetNotify(wxSOCKET_LOST_FLAG | wxSOCKET_INPUT_FLAG);
388 break;
389 }
390 case wxSOCKET_LOST:
391 {
392 m_numClients--;
393
bd4d918f
GRG
394 // Destroy() should be used instead of delete wherever possible,
395 // due to the fact that wxSocket uses 'delayed events' (see the
396 // documentation for wxPostEvent) and we don't want an event to
397 // arrive to the event handler (the frame, here) after the socket
398 // has been deleted. Also, we might be doing some other thing with
399 // the socket at the same time; for example, we might be in the
400 // middle of a test or something. Destroy() takes care of all
bc7e88ae 401 // this for us.
bd4d918f 402
bc7e88ae 403 m_text->AppendText(_("Deleting socket.\n\n"));
bd4d918f 404 sock->Destroy();
f85d901f
GRG
405 break;
406 }
407 default: ;
408 }
409
410 UpdateStatusBar();
411}
412
413// convenience functions
414
415void MyFrame::UpdateStatusBar()
f4ada568 416{
8520f137 417#if wxUSE_STATUSBAR
f85d901f 418 wxString s;
bd4d918f 419 s.Printf(_("%d clients connected"), m_numClients);
f85d901f 420 SetStatusText(s, 1);
8520f137 421#endif // wxUSE_STATUSBAR
f4ada568 422}