]> git.saurik.com Git - wxWidgets.git/blame - samples/wxsocket/server.cpp
1. Harm's patch for AppendText()
[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
GL
26#include "wx/socket.h"
27
e2a6f233
JS
28#if defined(__WXMOTIF__) || defined(__WXGTK__)
29#include "mondrian.xpm"
30#endif
31
f4ada568
GL
32// Define a new application type
33class MyApp: public wxApp
34{ public:
35 bool OnInit(void);
36};
37
38class MyServer;
39
40// Define a new frame type
41class MyFrame: public wxFrame
42{
43 DECLARE_EVENT_TABLE()
44public:
45 MyServer *sock;
46 int nb_clients;
47
48 MyFrame(wxFrame *frame);
49 virtual ~MyFrame();
50 void Menu_Exit(wxCommandEvent& evt);
51 void ExecTest1(wxSocketBase *sock_o);
52 void UpdateStatus(int incr);
53};
54
55#define SKDEMO_QUIT 101
56
57BEGIN_EVENT_TABLE(MyFrame, wxFrame)
58 EVT_MENU(SKDEMO_QUIT, MyFrame::Menu_Exit)
59END_EVENT_TABLE()
60
61class MySock: public wxSocketBase {
62public:
63 MyFrame *frame;
64
65 void OldOnNotify(wxRequestEvent flags);
66};
67
68class MyServer: public wxSocketServer {
69public:
70 MyFrame *frame;
71
72 MyServer(wxSockAddress& addr) : wxSocketServer(addr) { }
73 void OldOnNotify(wxRequestEvent flags);
74};
75
76IMPLEMENT_APP(MyApp)
77
78// `Main program' equivalent, creating windows and returning main app frame
79bool MyApp::OnInit(void)
80{
81 // Create the main frame window
82 MyFrame *frame = new MyFrame(NULL);
83
84 // Give it an icon
e2a6f233 85 frame->SetIcon(wxICON(mondrian));
f4ada568
GL
86
87 // Make a menubar
88 wxMenu *file_menu = new wxMenu;
89
90 file_menu->Append(SKDEMO_QUIT, "E&xit");
91 wxMenuBar *menu_bar = new wxMenuBar;
92 menu_bar->Append(file_menu, "File");
93 frame->SetMenuBar(menu_bar);
94
95 // Make a panel with a message
96 (void)new wxPanel(frame, 0, 0, 300, 100);
97
98 // Show the frame
99 frame->Show(TRUE);
100
101 // Return the main frame window
102 return TRUE;
103}
104
105void MySock::OldOnNotify(wxRequestEvent flags)
106{
3f2c845e 107 extern wxList WXDLLEXPORT wxPendingDelete;
f4ada568
GL
108
109 switch (flags) {
110 case EVT_READ:
111 unsigned char c;
112
113 ReadMsg((char *)&c, 1);
114 if (c == 0xbe)
115 frame->ExecTest1(this);
116
117 break;
118 case EVT_LOST:
119 frame->UpdateStatus(-1);
120 wxPendingDelete.Append(this);
121 break;
122 }
123}
124
125void MyServer::OldOnNotify(wxRequestEvent WXUNUSED(flags))
126{
127 MySock *sock2 = new MySock();
128
129 if (!AcceptWith(*sock2))
130 return;
131
132 m_handler->Register(sock2);
133
134 sock2->SetFlags(NONE);
135 sock2->frame = frame;
136 sock2->SetNotify(REQ_READ | REQ_LOST);
137 sock2->Notify(TRUE);
138 frame->UpdateStatus(1);
139}
140
141// My frame Constructor
142MyFrame::MyFrame(wxFrame *frame):
143 wxFrame(frame, -1, "wxSocket sample (server)", wxDefaultPosition,
144 wxSize(300, 200))
145{
146 wxIPV4address addr;
147 addr.Service(3000);
148
149 // Init all
150 wxSocketHandler::Master();
151
152 sock = new MyServer(addr);
153 wxSocketHandler::Master().Register(sock);
154 sock->frame = this;
155 sock->SetNotify(wxSocketBase::REQ_ACCEPT);
156 sock->Notify(TRUE);
157 nb_clients = 0;
158
159 CreateStatusBar(1);
160 UpdateStatus(0);
161}
162
163MyFrame::~MyFrame()
164{
165 delete sock;
166}
167
168// Intercept menu commands
d18ed59a 169void MyFrame::Menu_Exit(wxCommandEvent& WXUNUSED(event))
f4ada568
GL
170{
171 Close(TRUE);
172}
173
174void MyFrame::ExecTest1(wxSocketBase *sock_o)
175{
176 char *buf = new char[50];
177 size_t l;
178
179 l = sock_o->Read(buf, 50).LastCount();
180 sock_o->Write(buf, l);
181}
182
183void MyFrame::UpdateStatus(int incr)
184{
185 char s[30];
186 nb_clients += incr;
187 sprintf(s, "%d clients connected", nb_clients);
188 SetStatusText(s);
189}