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