-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServer.cpp
More file actions
85 lines (62 loc) · 1.36 KB
/
Server.cpp
File metadata and controls
85 lines (62 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* The MIT License - see LICENSE file for details
*/
#include <Poco/Net/ServerSocket.h>
#include <Poco/Net/StreamSocket.h>
#include <Poco/Net/SocketStream.h>
#include <Poco/Net/SocketAddress.h>
#include "Server.h"
#include <boost/foreach.hpp>
Server::Server(unsigned int port, MainFrame *mainFrame) :
port(port), mainFrame(mainFrame), thread(NULL)
{
}
Server::~Server()
{
if (thread != NULL)
{
stop();
}
}
void Server::start()
{
assert (thread == NULL);
thread = new Poco::Thread();
thread->start(*this);
}
void Server::stop()
{
assert (thread != NULL);
running = false;
thread->join();
delete thread;
thread = NULL;
}
void Server::run()
{
Poco::Net::ServerSocket socket(port);
running = true;
while (running)
{
if (socket.poll(Poco::Timespan(1, 0),
Poco::Net::ServerSocket::SELECT_READ))
{
Poco::Net::StreamSocket ss = socket.acceptConnection();
DebugThread *runnable = new DebugThread(ss);
ClientThread *c = new ClientThread(new Poco::Thread(), runnable);
clients.push_back(c);
wxCommandEvent event(wxEVT_MY_EVENT_ADD_DEBUG_THREAD);
event.SetClientData((void *) runnable);
wxPostEvent(mainFrame, event);
c->thread->start(*(c->runnable));
}
}
BOOST_FOREACH(ClientThread *c, clients)
{
c->runnable->stop();
c->thread->join();
delete c->runnable;
delete c->thread;
delete c;
}
}