-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsocket_notifier.cpp
166 lines (148 loc) · 3.86 KB
/
socket_notifier.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//////////////////////////////////////////////////////////////////////////////
// socket_notifier
// * hide ipc and cross platform stuff
//
#if defined(_WIN32)
#include <io.h>
#include <tchar.h>
#include <windows.h>
#define LAST_ERROR() WSAGetLastError()
#define CLOSE_SOCKET(x) closesocket(x)
#define SHUTDOWN_BOTH 2
#else
#define SOCKET int
#define INVALID_SOCKET (-1)
#define SOCKET_ERROR (-1)
#define CLOSE_SOCKET(x) close(x)
#define LAST_ERROR() errno
#define SHUTDOWN_BOTH SHUT_RDWR
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <unistd.h>
#endif
#include <thread>
#include <mutex>
#include <string.h>
#include <vector>
#include <string>
#include "dprintf.h"
#include "socket_notifier.h"
using namespace std;
class SocketNotifierImpl
{
private:
SocketNotifier *m_who_to_notify;
SOCKET m_listen_socket;
string m_listen_address;
u_short m_listen_port;
thread m_listen_thread; // a thread to wait to know when to add the new devices;
static void listen_thread(SocketNotifierImpl *pthis);
public:
SocketNotifierImpl(SocketNotifier *who_to_notify);
~SocketNotifierImpl();
void StartListening(const char *listen_address, u_short listen_port);
void StopListening();
};
SocketNotifierImpl::SocketNotifierImpl(SocketNotifier *who_to_notify)
: m_who_to_notify(who_to_notify),
m_listen_socket(-1)
{
}
SocketNotifierImpl::~SocketNotifierImpl()
{
StopListening();
}
void SocketNotifierImpl::StartListening(const char *listen_address, u_short listen_port)
{
m_listen_address = listen_address;
m_listen_port = listen_port;
m_listen_thread = thread(listen_thread, this);
}
void SocketNotifierImpl::listen_thread(SocketNotifierImpl *pthis)
{
#ifdef _WIN32
HRESULT hr = SetThreadDescription(GetCurrentThread(), L"soft knuckles listen to activate thread");
#endif
dprintf("listen thread started\n");
pthis->m_listen_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (pthis->m_listen_socket == INVALID_SOCKET) {
dprintf("socket failed with error: %ld\n", LAST_ERROR());
return;
}
else
{
sockaddr_in service;
memset(&service, 0, sizeof(service));
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr(pthis->m_listen_address.c_str());
service.sin_port = htons(pthis->m_listen_port);
if (::bind(pthis->m_listen_socket, (sockaddr*)&service, sizeof(service)) == SOCKET_ERROR) {
dprintf("bind failed with error: %ld\n", LAST_ERROR());
CLOSE_SOCKET(pthis->m_listen_socket);
pthis->m_listen_socket = -1;
return;
}
dprintf("about to listen\n");
if (listen(pthis->m_listen_socket, 1) == SOCKET_ERROR) {
dprintf("listen failed with error: %ld\n", LAST_ERROR());
CLOSE_SOCKET(pthis->m_listen_socket);
pthis->m_listen_socket = -1;
}
else
{
SOCKET incoming = accept(pthis->m_listen_socket, nullptr, nullptr);
if (0 > incoming)
{
dprintf("accept failed with error: %ld\n", LAST_ERROR());
}
else
{
CLOSE_SOCKET(incoming);
if (CLOSE_SOCKET(pthis->m_listen_socket) < 0)
{
dprintf("close failed - must be shutting down\n");
}
else
{
dprintf("new connection from port: %d\n", pthis->m_listen_port);
if (pthis->m_who_to_notify)
{
pthis->m_who_to_notify->Notify();
}
}
}
}
}
}
void SocketNotifierImpl::StopListening()
{
// close my listener socket
if (m_listen_socket != -1)
{
//shutdown(m_listen_socket, SHUTDOWN_BOTH);
CLOSE_SOCKET(m_listen_socket);
m_listen_socket = -1;
if (m_listen_thread.joinable())
{
m_listen_thread.join();
}
}
}
SocketNotifier::SocketNotifier()
: m_impl(new SocketNotifierImpl(this))
{}
SocketNotifier::~SocketNotifier()
{
delete m_impl;
}
void SocketNotifier::StartListening(const char *listen_address, u_short listen_port)
{
m_impl->StartListening(listen_address, listen_port);
}
void SocketNotifier::StopListening()
{
m_impl->StopListening();
}