]> git.saurik.com Git - wxWidgets.git/blame - samples/wxsocket/server.cpp
Distrib changes.
[wxWidgets.git] / samples / wxsocket / 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
f4ada568 20#ifdef __GNUG__
f85d901f
GRG
21# pragma implementation "server.cpp"
22# pragma interface "server.cpp"
f4ada568
GL
23#endif
24
f85d901f 25// For compilers that support precompilation, includes "wx/wx.h".
f4ada568
GL
26#include "wx/wxprec.h"
27
28#ifdef __BORLANDC__
f85d901f 29# pragma hdrstop
f4ada568
GL
30#endif
31
f85d901f 32// for all others, include the necessary headers
f4ada568 33#ifndef WX_PRECOMP
f85d901f
GRG
34# include "wx/wx.h"
35# include "wx/socket.h"
f4ada568 36#endif
e2a6f233 37
f85d901f
GRG
38// --------------------------------------------------------------------------
39// resources
40// --------------------------------------------------------------------------
f4ada568 41
f85d901f
GRG
42// the application icon
43#if defined(__WXGTK__) || defined(__WXMOTIF__)
44# include "mondrian.xpm"
e2a6f233
JS
45#endif
46
f85d901f
GRG
47// --------------------------------------------------------------------------
48// classes
49// --------------------------------------------------------------------------
50
f4ada568 51// Define a new application type
f85d901f
GRG
52class MyApp : public wxApp
53{
54public:
55 virtual bool OnInit();
f4ada568
GL
56};
57
f85d901f
GRG
58// Define a new frame type: this is going to be our main frame
59class MyFrame : public wxFrame
60{
f4ada568 61public:
f85d901f
GRG
62 MyFrame();
63 ~MyFrame();
64
65 // event handlers (these functions should _not_ be virtual)
66 void OnQuit(wxCommandEvent& event);
67 void OnAbout(wxCommandEvent& event);
68 void OnServerEvent(wxSocketEvent& event);
69 void OnSocketEvent(wxSocketEvent& event);
70
71 void Test1(wxSocketBase *sock);
72 void Test2(wxSocketBase *sock);
73 void Test3(wxSocketBase *sock);
74
75 // convenience functions
76 void UpdateStatusBar();
77
78private:
79 wxSocketServer *m_server;
80 wxPanel *m_panel;
81 wxTextCtrl *m_text;
82 wxMenu *m_menuFile;
83 wxMenuBar *m_menuBar;
84 bool m_busy;
85 int m_numClients;
86
87 // any class wishing to process wxWindows events must use this macro
88 DECLARE_EVENT_TABLE()
89};
90
91// --------------------------------------------------------------------------
92// constants
93// --------------------------------------------------------------------------
94
95// IDs for the controls and the menu commands
96enum
97{
98 // menu items
99 SERVER_QUIT = 1000,
100 SERVER_ABOUT,
101
102 // id for sockets
103 SERVER_ID,
104 SOCKET_ID
f4ada568
GL
105};
106
f85d901f
GRG
107// --------------------------------------------------------------------------
108// event tables and other macros for wxWindows
109// --------------------------------------------------------------------------
f4ada568
GL
110
111BEGIN_EVENT_TABLE(MyFrame, wxFrame)
f85d901f
GRG
112 EVT_MENU(SERVER_QUIT, MyFrame::OnQuit)
113 EVT_MENU(SERVER_ABOUT, MyFrame::OnAbout)
114 EVT_SOCKET(SERVER_ID, MyFrame::OnServerEvent)
115 EVT_SOCKET(SOCKET_ID, MyFrame::OnSocketEvent)
f4ada568
GL
116END_EVENT_TABLE()
117
f4ada568
GL
118IMPLEMENT_APP(MyApp)
119
f4ada568 120
f85d901f
GRG
121// To append sockets for delayed deletion
122extern wxList wxPendingDelete;
123
f4ada568 124
f85d901f
GRG
125// ==========================================================================
126// implementation
127// ==========================================================================
f4ada568 128
f85d901f
GRG
129// --------------------------------------------------------------------------
130// the application class
131// --------------------------------------------------------------------------
f4ada568 132
f85d901f
GRG
133bool MyApp::OnInit()
134{
135 // Create the main application window
136 MyFrame *frame = new MyFrame();
f4ada568 137
f85d901f 138 // Show it and tell the application that it's our main window
f4ada568 139 frame->Show(TRUE);
f85d901f 140 SetTopWindow(frame);
2b98cb1f 141
f85d901f 142 // success
f4ada568
GL
143 return TRUE;
144}
145
f85d901f
GRG
146// --------------------------------------------------------------------------
147// main frame
148// --------------------------------------------------------------------------
a737331d 149
f85d901f
GRG
150// frame constructor
151MyFrame::MyFrame() : wxFrame((wxFrame *)NULL, -1,
152 _T("wxSocket demo: Server"),
153 wxDefaultPosition, wxSize(300, 200))
f4ada568 154{
f85d901f
GRG
155 // Give the frame an icon
156 SetIcon(wxICON(mondrian));
157
158 // Make menus
159 m_menuFile = new wxMenu();
160 m_menuFile->Append(SERVER_ABOUT, _T("&About...\tCtrl-A"), _T("Show about dialog"));
161 m_menuFile->AppendSeparator();
162 m_menuFile->Append(SERVER_QUIT, _T("E&xit\tAlt-X"), _T("Quit server"));
163
164 // Append menus to the menubar
165 m_menuBar = new wxMenuBar();
166 m_menuBar->Append(m_menuFile, _T("&File"));
167 SetMenuBar(m_menuBar);
168
169 // Status bar
170 CreateStatusBar(2);
171
172 // Make a panel with a textctrl in it
173 m_panel = new wxPanel(this, -1, wxPoint(0, 0), GetClientSize());
174 m_text = new wxTextCtrl(m_panel, -1,
175 _T("Welcome to wxSocket demo: Server\n"),
176 wxPoint(0, 0), m_panel->GetClientSize(),
177 wxTE_MULTILINE | wxTE_READONLY);
178
179 // Create the socket
180 wxIPV4address addr;
181 addr.Service(3000);
182
183 m_server = new wxSocketServer(addr);
184 m_server->SetEventHandler(*this, SERVER_ID);
185 m_server->SetNotify(wxSOCKET_CONNECTION_FLAG);
186 m_server->Notify(TRUE);
187
188 // We use Ok() here to see if the server is really listening
189 if (m_server->Ok())
190 m_text->AppendText(_T("Server listening.\n\n"));
191 else
192 m_text->AppendText(_T("Could not listen at the specified port !\n\n"));
193
194 m_busy = FALSE;
195 m_numClients = 0;
196 UpdateStatusBar();
f4ada568
GL
197}
198
f85d901f 199MyFrame::~MyFrame()
f4ada568 200{
f85d901f
GRG
201 delete m_server;
202}
f4ada568 203
f85d901f 204// event handlers
f4ada568 205
f85d901f
GRG
206void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
207{
208 // TRUE is to force the frame to close
209 Close(TRUE);
210}
f4ada568 211
f85d901f
GRG
212void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
213{
214 wxMessageBox(_T("wxSocket demo: Server\n")
215 _T("(c) 1999 Guillermo Rodriguez Garcia\n"),
216 _T("About Server"),
217 wxOK | wxICON_INFORMATION, this);
f4ada568
GL
218}
219
f85d901f 220void MyFrame::Test1(wxSocketBase *sock)
f4ada568 221{
f85d901f
GRG
222 unsigned char len;
223 char *buf;
f4ada568 224
f85d901f 225 m_text->AppendText(_T("Test 1 begins\n"));
f4ada568 226
f85d901f
GRG
227 // Receive data from socket and send it back. We will first
228 // get a byte with the buffer size, so we can specify the
229 // exact size and use the WAITALL flag. Also, we disabled
230 // input events so we won't have unwanted reentrance. This
231 // way we can avoid the infamous BLOCK (formerly SPEED) flag.
232 //
233 sock->SetFlags(wxSOCKET_WAITALL);
234
235 sock->Read((char *)&len, 1);
236
237 buf = (char *)malloc(len);
238 sock->Read(buf, len);
239 sock->Write(buf, len);
240 free(buf);
241
242 m_text->AppendText(_T("Test 1 ends\n"));
f4ada568
GL
243}
244
f85d901f 245void MyFrame::Test2(wxSocketBase *sock)
f4ada568 246{
f85d901f
GRG
247#define MAX_MSG_SIZE 10000
248
249 wxString s;
250 char *buf = (char *)malloc(MAX_MSG_SIZE);
251 wxUint32 len;
252
253 m_text->AppendText(_T("Test 2 begins\n"));
254
255 // We don't need to set flags because ReadMsg and WriteMsg
256 // are not affected by them anyway.
257 //
258 len = sock->ReadMsg(buf, MAX_MSG_SIZE).LastCount();
259
260 s.Printf(_T("Client says: %s\n"), buf);
261 m_text->AppendText(s);
262
263 sock->WriteMsg(buf, len);
264 free(buf);
265
266 m_text->AppendText(_T("Test 2 ends\n"));
267
268#undef MAX_MSG_SIZE
f4ada568
GL
269}
270
f85d901f 271void MyFrame::Test3(wxSocketBase *sock)
f4ada568 272{
f85d901f
GRG
273 m_text->AppendText(_T("Test 3 begins\n"));
274 m_text->AppendText(_T("(not implemented)\n"));
275 m_text->AppendText(_T("Test 3 ends\n"));
f4ada568
GL
276}
277
f85d901f 278void MyFrame::OnServerEvent(wxSocketEvent& event)
f4ada568 279{
f85d901f
GRG
280 wxString s = _T("OnServerEvent: ");
281 wxSocketBase *sock;
282
283 switch(event.SocketEvent())
284 {
285 case wxSOCKET_CONNECTION : s.Append(_T("wxSOCKET_CONNECTION\n")); break;
286 default : s.Append(_T("Unexpected event !\n")); break;
287 }
f4ada568 288
f85d901f 289 m_text->AppendText(s);
765e386b 290
f85d901f
GRG
291 // Accept new connection if there is one in the pending
292 // connections queue, else exit. We use Accept(FALSE) for
293 // non-blocking accept (although if we got here, there
294 // should ALWAYS be a pending connection).
295 //
296 sock = m_server->Accept(FALSE);
297
298 if (sock)
299 {
300 m_text->AppendText(_T("New client connection accepted\n"));
301 }
302 else
303 {
304 m_text->AppendText(_T("Error: couldn't accept a new connection"));
305 return;
306 }
307
308 sock->SetEventHandler(*this, SOCKET_ID);
309 sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
310 sock->Notify(TRUE);
311
312 m_numClients++;
313 UpdateStatusBar();
f4ada568
GL
314}
315
f85d901f
GRG
316void MyFrame::OnSocketEvent(wxSocketEvent& event)
317{
318 wxSocketBase *sock = event.Socket();
319 wxString s = _T("OnSocketEvent: ");
320
321 // We first print a msg
322 switch(event.SocketEvent())
323 {
324 case wxSOCKET_INPUT: s.Append(_T("wxSOCKET_INPUT\n")); break;
325 case wxSOCKET_LOST: s.Append(_T("wxSOCKET_LOST\n")); break;
326 default: s.Append(_T("unexpected event !\n"));
327 }
328
329 m_text->AppendText(s);
330
331 // Now we process the event
332 switch(event.SocketEvent())
333 {
334 case wxSOCKET_INPUT:
335 {
336 // We disable input events, so that the test doesn't trigger
337 // wxSocketEvent again.
338 sock->SetNotify(wxSOCKET_LOST_FLAG);
339
340 // Which test are we going to run?
341 unsigned char c;
342 sock->Read((char *)&c ,1);
343
344 switch (c)
345 {
346 case 0xBE: Test1(sock); break;
347 case 0xCE: Test2(sock); break;
348 case 0xDE: Test3(sock); break;
349 default: s.Append(_T("Unknown test id received from client\n"));
350 }
351
352 // Enable input events again.
353 sock->SetNotify(wxSOCKET_LOST_FLAG | wxSOCKET_INPUT_FLAG);
354 break;
355 }
356 case wxSOCKET_LOST:
357 {
358 m_numClients--;
359
360 // We cannot delete the socket right now because we can
361 // be in the middle of a test or something. So we append
362 // it to the list of objects to be deleted.
363 m_text->AppendText(_T("Deleting socket.\n"));
364 wxPendingDelete.Append(sock);
365 break;
366 }
367 default: ;
368 }
369
370 UpdateStatusBar();
371}
372
373// convenience functions
374
375void MyFrame::UpdateStatusBar()
f4ada568 376{
f85d901f
GRG
377 wxString s;
378 s.Printf(_T("%d clients connected"), m_numClients);
379 SetStatusText(s, 1);
f4ada568 380}