]> git.saurik.com Git - wxWidgets.git/blame - samples/wxsocket/server.cpp
1. wxNotifyEvent documented
[wxWidgets.git] / samples / wxsocket / server.cpp
CommitLineData
f4ada568
GL
1/*
2 * File: server.cpp
3 * Purpose: wxSocket: server demo
e2a6f233 4 * Author: LAVAUX Guilhem
f4ada568
GL
5 * Created: June 1997
6 * Updated:
e2a6f233 7 * Copyright: (C) 1997, LAVAUX Guilhem
f4ada568 8 */
e2a6f233 9
f4ada568
GL
10#ifdef __GNUG__
11#pragma implementation
12#pragma interface
13#endif
14
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19#pragma hdrstop
20#endif
21
22#ifndef WX_PRECOMP
23#include "wx/wx.h"
24#endif
e2a6f233 25
f4ada568 26#include "wx/socket.h"
6b3eb77a 27#include "wx/thread.h"
f4ada568 28
e2a6f233
JS
29#if defined(__WXMOTIF__) || defined(__WXGTK__)
30#include "mondrian.xpm"
31#endif
32
f4ada568
GL
33// Define a new application type
34class MyApp: public wxApp
35{ public:
36 bool OnInit(void);
37};
38
39class MyServer;
40
41// Define a new frame type
42class MyFrame: public wxFrame
43{
44 DECLARE_EVENT_TABLE()
45public:
a737331d 46 wxSocketServer *sock;
f4ada568
GL
47 int nb_clients;
48
49 MyFrame(wxFrame *frame);
50 virtual ~MyFrame();
51 void Menu_Exit(wxCommandEvent& evt);
a737331d
GL
52 void OnSockRequest(wxSocketEvent& evt);
53 void OnSockRequestServer(wxSocketEvent& evt);
f4ada568
GL
54 void ExecTest1(wxSocketBase *sock_o);
55 void UpdateStatus(int incr);
56};
57
58#define SKDEMO_QUIT 101
a737331d
GL
59#define SKDEMO_SOCKET_SERV 102
60#define SKDEMO_SOCKET 103
f4ada568
GL
61
62BEGIN_EVENT_TABLE(MyFrame, wxFrame)
63 EVT_MENU(SKDEMO_QUIT, MyFrame::Menu_Exit)
a737331d
GL
64 EVT_SOCKET(SKDEMO_SOCKET_SERV, MyFrame::OnSockRequestServer)
65 EVT_SOCKET(SKDEMO_SOCKET, MyFrame::OnSockRequest)
f4ada568
GL
66END_EVENT_TABLE()
67
f4ada568
GL
68IMPLEMENT_APP(MyApp)
69
70// `Main program' equivalent, creating windows and returning main app frame
71bool MyApp::OnInit(void)
72{
73 // Create the main frame window
74 MyFrame *frame = new MyFrame(NULL);
75
76 // Give it an icon
e2a6f233 77 frame->SetIcon(wxICON(mondrian));
f4ada568
GL
78
79 // Make a menubar
80 wxMenu *file_menu = new wxMenu;
81
82 file_menu->Append(SKDEMO_QUIT, "E&xit");
83 wxMenuBar *menu_bar = new wxMenuBar;
84 menu_bar->Append(file_menu, "File");
85 frame->SetMenuBar(menu_bar);
86
87 // Make a panel with a message
88 (void)new wxPanel(frame, 0, 0, 300, 100);
89
90 // Show the frame
91 frame->Show(TRUE);
92
93 // Return the main frame window
94 return TRUE;
95}
96
a737331d
GL
97extern wxList wxPendingDelete;
98
99void MyFrame::OnSockRequest(wxSocketEvent& evt)
f4ada568 100{
6b3eb77a
RR
101 /* this routine gets called from within the
102 waiting socket thread, i.e. here we are
103 not in the main GUI thread and thus we
104 must not call any GUI function here. */
105
a737331d 106 wxSocketBase *sock = evt.Socket();
f4ada568 107
a737331d
GL
108 printf("OnSockRequest OK\n");
109 printf("OnSockRequest (event = %d)\n",evt.SocketEvent());
110 switch (evt.SocketEvent()) {
111 case wxSocketBase::EVT_READ:
f4ada568
GL
112 unsigned char c;
113
a737331d 114 sock->Read((char *)&c, 1);
f4ada568 115 if (c == 0xbe)
a737331d 116 ExecTest1(sock);
f4ada568
GL
117
118 break;
a737331d 119 case wxSocketBase::EVT_LOST:
a737331d
GL
120 printf("Destroying socket\n");
121 wxPendingDelete.Append(sock);
122 return;
f4ada568
GL
123 break;
124 }
a737331d
GL
125 printf("OnSockRequest Exiting\n");
126 sock->SetNotify(wxSocketBase::REQ_READ | wxSocketBase::REQ_LOST);
f4ada568
GL
127}
128
a737331d 129void MyFrame::OnSockRequestServer(wxSocketEvent& evt)
f4ada568 130{
6b3eb77a
RR
131 /* this routine gets called from within the
132 waiting socket thread, i.e. here we are
133 not in the main GUI thread and thus we
134 must not call any GUI function here. */
135
a737331d
GL
136 wxSocketBase *sock2;
137 wxSocketServer *server = (wxSocketServer *) evt.Socket();
f4ada568 138
a737331d 139 printf("OnSockRequestServer OK\n");
6b3eb77a 140 printf("OnSockRequest (event = %d)\n",evt.SocketEvent());
f4ada568 141
a737331d
GL
142 sock2 = server->Accept();
143 if (sock2 == NULL)
144 return;
f4ada568 145
9111db68 146 sock2->SetFlags(wxSocketBase::SPEED);
f4ada568 147 sock2->Notify(TRUE);
a737331d
GL
148 sock2->SetEventHandler(*this, SKDEMO_SOCKET);
149 server->SetNotify(wxSocketBase::REQ_ACCEPT);
f4ada568
GL
150}
151
152// My frame Constructor
153MyFrame::MyFrame(wxFrame *frame):
154 wxFrame(frame, -1, "wxSocket sample (server)", wxDefaultPosition,
155 wxSize(300, 200))
156{
157 wxIPV4address addr;
158 addr.Service(3000);
159
160 // Init all
161 wxSocketHandler::Master();
162
a737331d 163 sock = new wxSocketServer(addr);
f4ada568 164 wxSocketHandler::Master().Register(sock);
f4ada568 165 sock->SetNotify(wxSocketBase::REQ_ACCEPT);
a737331d 166 sock->SetEventHandler(*this, SKDEMO_SOCKET_SERV);
9111db68 167 sock->SetFlags(wxSocketBase::SPEED);
f4ada568
GL
168 sock->Notify(TRUE);
169 nb_clients = 0;
170
171 CreateStatusBar(1);
172 UpdateStatus(0);
173}
174
175MyFrame::~MyFrame()
176{
177 delete sock;
178}
179
180// Intercept menu commands
d18ed59a 181void MyFrame::Menu_Exit(wxCommandEvent& WXUNUSED(event))
f4ada568
GL
182{
183 Close(TRUE);
184}
185
186void MyFrame::ExecTest1(wxSocketBase *sock_o)
187{
188 char *buf = new char[50];
189 size_t l;
190
191 l = sock_o->Read(buf, 50).LastCount();
192 sock_o->Write(buf, l);
193}
194
195void MyFrame::UpdateStatus(int incr)
196{
197 char s[30];
198 nb_clients += incr;
199 sprintf(s, "%d clients connected", nb_clients);
200 SetStatusText(s);
201}