]> git.saurik.com Git - wxWidgets.git/blob - samples/wxsocket/server.cpp
1. wxNotifyEvent documented
[wxWidgets.git] / samples / wxsocket / server.cpp
1 /*
2 * File: server.cpp
3 * Purpose: wxSocket: server demo
4 * Author: LAVAUX Guilhem
5 * Created: June 1997
6 * Updated:
7 * Copyright: (C) 1997, LAVAUX Guilhem
8 */
9
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
25
26 #include "wx/socket.h"
27 #include "wx/thread.h"
28
29 #if defined(__WXMOTIF__) || defined(__WXGTK__)
30 #include "mondrian.xpm"
31 #endif
32
33 // Define a new application type
34 class MyApp: public wxApp
35 { public:
36 bool OnInit(void);
37 };
38
39 class MyServer;
40
41 // Define a new frame type
42 class MyFrame: public wxFrame
43 {
44 DECLARE_EVENT_TABLE()
45 public:
46 wxSocketServer *sock;
47 int nb_clients;
48
49 MyFrame(wxFrame *frame);
50 virtual ~MyFrame();
51 void Menu_Exit(wxCommandEvent& evt);
52 void OnSockRequest(wxSocketEvent& evt);
53 void OnSockRequestServer(wxSocketEvent& evt);
54 void ExecTest1(wxSocketBase *sock_o);
55 void UpdateStatus(int incr);
56 };
57
58 #define SKDEMO_QUIT 101
59 #define SKDEMO_SOCKET_SERV 102
60 #define SKDEMO_SOCKET 103
61
62 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
63 EVT_MENU(SKDEMO_QUIT, MyFrame::Menu_Exit)
64 EVT_SOCKET(SKDEMO_SOCKET_SERV, MyFrame::OnSockRequestServer)
65 EVT_SOCKET(SKDEMO_SOCKET, MyFrame::OnSockRequest)
66 END_EVENT_TABLE()
67
68 IMPLEMENT_APP(MyApp)
69
70 // `Main program' equivalent, creating windows and returning main app frame
71 bool MyApp::OnInit(void)
72 {
73 // Create the main frame window
74 MyFrame *frame = new MyFrame(NULL);
75
76 // Give it an icon
77 frame->SetIcon(wxICON(mondrian));
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
97 extern wxList wxPendingDelete;
98
99 void MyFrame::OnSockRequest(wxSocketEvent& evt)
100 {
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
106 wxSocketBase *sock = evt.Socket();
107
108 printf("OnSockRequest OK\n");
109 printf("OnSockRequest (event = %d)\n",evt.SocketEvent());
110 switch (evt.SocketEvent()) {
111 case wxSocketBase::EVT_READ:
112 unsigned char c;
113
114 sock->Read((char *)&c, 1);
115 if (c == 0xbe)
116 ExecTest1(sock);
117
118 break;
119 case wxSocketBase::EVT_LOST:
120 printf("Destroying socket\n");
121 wxPendingDelete.Append(sock);
122 return;
123 break;
124 }
125 printf("OnSockRequest Exiting\n");
126 sock->SetNotify(wxSocketBase::REQ_READ | wxSocketBase::REQ_LOST);
127 }
128
129 void MyFrame::OnSockRequestServer(wxSocketEvent& evt)
130 {
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
136 wxSocketBase *sock2;
137 wxSocketServer *server = (wxSocketServer *) evt.Socket();
138
139 printf("OnSockRequestServer OK\n");
140 printf("OnSockRequest (event = %d)\n",evt.SocketEvent());
141
142 sock2 = server->Accept();
143 if (sock2 == NULL)
144 return;
145
146 sock2->SetFlags(wxSocketBase::SPEED);
147 sock2->Notify(TRUE);
148 sock2->SetEventHandler(*this, SKDEMO_SOCKET);
149 server->SetNotify(wxSocketBase::REQ_ACCEPT);
150 }
151
152 // My frame Constructor
153 MyFrame::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
163 sock = new wxSocketServer(addr);
164 wxSocketHandler::Master().Register(sock);
165 sock->SetNotify(wxSocketBase::REQ_ACCEPT);
166 sock->SetEventHandler(*this, SKDEMO_SOCKET_SERV);
167 sock->SetFlags(wxSocketBase::SPEED);
168 sock->Notify(TRUE);
169 nb_clients = 0;
170
171 CreateStatusBar(1);
172 UpdateStatus(0);
173 }
174
175 MyFrame::~MyFrame()
176 {
177 delete sock;
178 }
179
180 // Intercept menu commands
181 void MyFrame::Menu_Exit(wxCommandEvent& WXUNUSED(event))
182 {
183 Close(TRUE);
184 }
185
186 void 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
195 void 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 }