]> git.saurik.com Git - wxWidgets.git/blame - samples/wxsocket/server.cpp
Small distrib updates,
[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 5 * Created: June 1997
062c4861 6 * CVS Id: $Id$
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. */
062c4861
GL
105 /* Wrong ! This routine is called by the main GUI thread
106 because the main GUI thread received a signal from the other
107 thread using wxEvent::ProcessThreadEvent */
6b3eb77a 108
a737331d 109 wxSocketBase *sock = evt.Socket();
f4ada568 110
a737331d
GL
111 printf("OnSockRequest OK\n");
112 printf("OnSockRequest (event = %d)\n",evt.SocketEvent());
113 switch (evt.SocketEvent()) {
a324a7bc 114 case GSOCK_INPUT:
f4ada568
GL
115 unsigned char c;
116
a737331d 117 sock->Read((char *)&c, 1);
f4ada568 118 if (c == 0xbe)
a737331d 119 ExecTest1(sock);
f4ada568
GL
120
121 break;
a324a7bc 122 case GSOCK_LOST:
a737331d
GL
123 printf("Destroying socket\n");
124 wxPendingDelete.Append(sock);
062c4861 125 UpdateStatus(-1);
a737331d 126 return;
f4ada568
GL
127 break;
128 }
a737331d 129 printf("OnSockRequest Exiting\n");
f4ada568
GL
130}
131
a737331d 132void MyFrame::OnSockRequestServer(wxSocketEvent& evt)
f4ada568 133{
6b3eb77a
RR
134 /* this routine gets called from within the
135 waiting socket thread, i.e. here we are
136 not in the main GUI thread and thus we
137 must not call any GUI function here. */
062c4861
GL
138 /* Wrong ! This routine is called by the main GUI thread
139 because the main GUI thread received a signal from the other
140 thread using wxEvent::ProcessThreadEvent */
6b3eb77a 141
a737331d
GL
142 wxSocketBase *sock2;
143 wxSocketServer *server = (wxSocketServer *) evt.Socket();
f4ada568 144
a737331d 145 printf("OnSockRequestServer OK\n");
062c4861 146 printf("OnSockRequest (Main = %d) (event = %d)\n",wxThread::IsMain(), evt.SocketEvent());
f4ada568 147
a737331d
GL
148 sock2 = server->Accept();
149 if (sock2 == NULL)
150 return;
f4ada568 151
062c4861 152 UpdateStatus(1);
a324a7bc 153 sock2->SetFlags(wxSocketBase::NONE);
f4ada568 154 sock2->Notify(TRUE);
a737331d 155 sock2->SetEventHandler(*this, SKDEMO_SOCKET);
a324a7bc 156 sock2->SetNotify(GSOCK_INPUT_FLAG | GSOCK_LOST_FLAG);
f4ada568
GL
157}
158
159// My frame Constructor
160MyFrame::MyFrame(wxFrame *frame):
161 wxFrame(frame, -1, "wxSocket sample (server)", wxDefaultPosition,
162 wxSize(300, 200))
163{
164 wxIPV4address addr;
165 addr.Service(3000);
166
167 // Init all
f4ada568 168
a737331d 169 sock = new wxSocketServer(addr);
a324a7bc 170 sock->SetNotify(GSOCK_CONNECTION_FLAG);
a737331d 171 sock->SetEventHandler(*this, SKDEMO_SOCKET_SERV);
9111db68 172 sock->SetFlags(wxSocketBase::SPEED);
f4ada568
GL
173 sock->Notify(TRUE);
174 nb_clients = 0;
175
176 CreateStatusBar(1);
177 UpdateStatus(0);
178}
179
180MyFrame::~MyFrame()
181{
182 delete sock;
183}
184
185// Intercept menu commands
d18ed59a 186void MyFrame::Menu_Exit(wxCommandEvent& WXUNUSED(event))
f4ada568
GL
187{
188 Close(TRUE);
189}
190
191void MyFrame::ExecTest1(wxSocketBase *sock_o)
192{
193 char *buf = new char[50];
194 size_t l;
195
196 l = sock_o->Read(buf, 50).LastCount();
197 sock_o->Write(buf, l);
198}
199
200void MyFrame::UpdateStatus(int incr)
201{
202 char s[30];
203 nb_clients += incr;
204 sprintf(s, "%d clients connected", nb_clients);
205 SetStatusText(s);
206}