-
Notifications
You must be signed in to change notification settings - Fork 42
/
SocketHandler.cpp
119 lines (89 loc) · 2.71 KB
/
SocketHandler.cpp
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "SocketHandler.h"
#include <assert.h>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include "CallbackHandler.h"
using namespace boost::asio::ip;
SocketWrapper::~SocketWrapper() {
switch (socketType) {
case SM_SocketType_Tcp:
delete (Socket<tcp>*) socket;
break;
case SM_SocketType_Udp:
delete (Socket<udp>*) socket;
break;
#if 0
case SM_SocketType_Icmp:
delete (Socket<icmp>*) socket;
break;
#endif
}
callbackHandler.RemoveCallbacks(this);
}
template Socket<tcp>* SocketHandler::CreateSocket<tcp>(SM_SocketType);
template Socket<udp>* SocketHandler::CreateSocket<udp>(SM_SocketType);
SocketHandler::SocketHandler() : ioServiceProcessingThreadInitialized(false) {
ioService = new boost::asio::io_service();
}
SocketHandler::~SocketHandler() {
if (!socketList.empty() || ioServiceProcessingThreadInitialized) {
Shutdown();
}
#ifndef WIN32
delete ioService;
#endif
}
void SocketHandler::Shutdown() {
boost::mutex::scoped_lock l(socketListMutex);
for (std::deque<SocketWrapper*>::iterator it=socketList.begin(); it!=socketList.end(); it++) {
delete *it;
}
socketList.clear();
if (ioServiceProcessingThreadInitialized) StopProcessing();
}
template <class SocketType>
Socket<SocketType>* SocketHandler::CreateSocket(SM_SocketType st) {
boost::mutex::scoped_lock l(socketListMutex);
SocketWrapper* sp = new SocketWrapper(new Socket<SocketType>(st), st);
socketList.push_back(sp);
return (Socket<SocketType>*) sp->socket;
}
void SocketHandler::DestroySocket(SocketWrapper* sw) {
assert(sw);
{ // lock
boost::mutex::scoped_lock l(socketListMutex);
for (std::deque<SocketWrapper*>::iterator it=socketList.begin(); it!=socketList.end(); it++) {
if (*it == sw) {
socketList.erase(it);
break;
}
}
} // ~lock
delete sw;
}
void SocketHandler::StartProcessing() {
assert(!ioServiceProcessingThreadInitialized);
ioServiceProcessingThread = new boost::thread(boost::bind(&SocketHandler::RunIoService, this));
ioServiceProcessingThreadInitialized = true;
}
void SocketHandler::StopProcessing() {
assert(ioServiceProcessingThreadInitialized);
ioService->stop();
delete ioServiceWork;
ioServiceProcessingThread->join();
ioServiceProcessingThreadInitialized = false;
delete ioServiceProcessingThread;
}
void SocketHandler::RunIoService() {
//boost::asio::io_service::work work(*ioService);
ioServiceWork = new boost::asio::io_service::work(*ioService);
ioService->run();
}
SocketWrapper* SocketHandler::GetSocketWrapper(const void* socket) {
boost::mutex::scoped_lock l(socketListMutex);
for (std::deque<SocketWrapper*>::iterator it=socketList.begin(); it!=socketList.end(); it++) {
if ((*it)->socket == socket) return *it;
}
return NULL;
}
SocketHandler socketHandler;