-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSocket.cpp
More file actions
246 lines (208 loc) · 9.47 KB
/
Copy pathWebSocket.cpp
File metadata and controls
246 lines (208 loc) · 9.47 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <regex>
#include <sstream>
#include <codecvt>
#include "WebSocket.h"
#if defined (_WIN32) || defined (_WIN64)
#include <Ws2tcpip.h>
#else
#include <arpa/inet.h>
#include <fcntl.h>
#include <dlfcn.h>
#define ConvertToByte(x) wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t>().to_bytes(x)
extern void OutputDebugString(const wchar_t* pOut);
// { // mkfifo /tmp/dbgout
// int fdPipe = open("/tmp/dbgout", O_WRONLY | O_NONBLOCK);
// if (fdPipe >= 0)
// {
// wstring strTmp(pOut);
// write(fdPipe, ConvertToByte(strTmp).c_str(), strTmp.size());
// close(fdPipe);
// }
// }
extern void OutputDebugStringA(const char* pOut);
#endif
using namespace std::placeholders;
#define ntohll(x) ( ( (uint64_t)(ntohl( (uint32_t)((x << 32) >> 32) )) << 32) | ntohl(((uint32_t)(x >> 32))))
#define htonll(x) ntohll(x)
std::function<size_t(void*, const uint8_t*, uint32_t)> WebSocket::WriteBackInstance;
WebSocket::WebSocket(const std::string& strPath, TcpSocket* pTcpSocket) : m_soSocketParam({0,0,0,0,0,0,0, 0, 0, 0, ""}), m_LibHandle(nullptr), pTextDataReceived(nullptr)
{
m_soSocketParam.strPath = strPath;
#if defined(_WIN32) || defined(_WIN64)
#else
m_LibHandle = dlopen("/home/c++/ModbusTCP/build/libWsLog.so", RTLD_LAZY);
if (m_LibHandle)
{
pSetWriteCallback = (void (*)(void(*callback)(void* /*pId*/, const uint8_t* /*szData*/, uint32_t /*nDataLen*/), void* pId))dlsym(m_LibHandle, "SetWriteCallback");
pTextDataReceived = (void (*)(void*, const char*, uint8_t*, uint32_t))dlsym(m_LibHandle, "TextDataReceived");
if (!pSetWriteCallback || !pTextDataReceived) {
OutputDebugStringA(std::string("Error: " + std::string(dlerror()) + "\r\n").c_str());
dlclose(m_LibHandle);
m_LibHandle = nullptr;
return;
}
WebSocket::WriteBackInstance = std::bind(&WebSocket::WriteData, this, _1, _2, _3);
pSetWriteCallback(&WebSocket::staticWriteBack, reinterpret_cast<void*>(pTcpSocket));
}
#endif
}
WebSocket::~WebSocket()
{
#if defined(_WIN32) || defined(_WIN64)
#else
if (m_LibHandle != nullptr)
dlclose(m_LibHandle);
#endif
}
void WebSocket::OnDataReceivedWebSocket(TcpSocket* pTcpSocket, uint8_t* pData, size_t nDataLen)
{
if (nDataLen > 0)
{
//OutputDebugString(wstring(L"Read:" + to_wstring(nDataLen) + L"\r\n").c_str());
uint8_t* pBuffer = pData;
uint8_t* pBufferEnd = pData + nDataLen;
do
{
size_t iOffset = 0;
if (m_soSocketParam.nLen == 0) // No Header, first call on this socket
{
m_soSocketParam.stHeader = *(reinterpret_cast<HEADER*>(pBuffer));
iOffset = 2;
m_soSocketParam.nLen = m_soSocketParam.stHeader.PLoad;
if (m_soSocketParam.nLen == 126)
m_soSocketParam.nLen = ntohs(*(reinterpret_cast<short*>(pBuffer + iOffset))), iOffset += 2;
else if (m_soSocketParam.nLen == 127)
m_soSocketParam.nLen = ntohll(*(reinterpret_cast<uint64_t*>(pBuffer + iOffset))), iOffset += 8;
if (m_soSocketParam.stHeader.Mask == 1)
m_soSocketParam.uiMask = *(reinterpret_cast<uint32_t*>(pBuffer + iOffset)), iOffset += 4;
}
uint8_t* szData = pBuffer + iOffset;
size_t nBlockSize = std::min(static_cast<size_t>(m_soSocketParam.nLen - m_soSocketParam.nReceived), nDataLen - iOffset);
#if defined(_WIN32) || defined(_WIN64)
//OutputDebugString(wstring(L"Read:" + to_wstring(nDataLen) + L", OpCode:" + to_wstring(iter->second.stHeader.OpCode) + L", FIN:" + to_wstring(iter->second.stHeader.FIN) + L", Len:" + to_wstring(iter->second.nLen) + L", BlockSize:" + to_wstring(nBlockSize) + L"\r\n").c_str());
#endif
if (m_soSocketParam.stHeader.Mask == 1)
{
for (size_t n = 0; n < nBlockSize; ++n)
{
size_t m = n % 4;
szData[n] = szData[n] ^ reinterpret_cast<char*>(&m_soSocketParam.uiMask)[m]; // original-octet-i XOR masking-key-octet-j
}
}
m_soSocketParam.nReceived += nBlockSize;
switch (m_soSocketParam.stHeader.OpCode)
{
case 0: // continue
#if defined(_WIN32) || defined(_WIN64)
//OutputDebugString(L"continue frame\r\n");
#endif
BinaryDataReceived(pTcpSocket, m_soSocketParam.strPath, szData, static_cast<uint32_t>(nBlockSize), m_soSocketParam.stHeader.FIN == 1 ? true : false);
if (m_soSocketParam.nReceived == m_soSocketParam.nLen)
{
m_soSocketParam.stHeader = { 0, 0, 0, 0, 0, 0, 0 };
m_soSocketParam.nReceived = m_soSocketParam.nLen = 0;
}
break;
case 1: // Text
{
if (m_soSocketParam.nReceived == m_soSocketParam.nLen)
{
if (m_soSocketParam.stHeader.FIN == 1)
{
int iHeaderLen = 2;
if (m_soSocketParam.nLen > 125)
iHeaderLen += 2;
if (m_soSocketParam.nLen > 65535)
iHeaderLen += 6;
OutputDebugStringA(std::string(std::string(reinterpret_cast<char*>(szData), m_soSocketParam.nLen) + "\r\n").c_str());
if (pTextDataReceived != nullptr)
pTextDataReceived(pTcpSocket, m_soSocketParam.strPath.c_str(), szData, static_cast<uint32_t>(m_soSocketParam.nLen));
else
TextDataReceived(pTcpSocket, m_soSocketParam.strPath, szData, static_cast<uint32_t>(m_soSocketParam.nLen));
}
m_soSocketParam.stHeader = { 0, 0, 0, 0, 0, 0, 0 };
m_soSocketParam.nReceived = m_soSocketParam.nLen = 0;
}
}
break;
case 2: // binary
#if defined(_WIN32) || defined(_WIN64)
//OutputDebugString(L"binary frame\r\n");
#endif
BinaryDataReceived(pTcpSocket, m_soSocketParam.strPath, szData, static_cast<uint32_t>(nBlockSize), m_soSocketParam.stHeader.FIN == 1 ? true : false);
if (m_soSocketParam.nReceived == m_soSocketParam.nLen)
{
m_soSocketParam.stHeader = { 0, 0, 0, 0, 0, 0, 0 };
m_soSocketParam.nReceived = m_soSocketParam.nLen = 0;
}
break;
case 8: // close
{
short sCode;
if (m_soSocketParam.nLen >= 2)
sCode = ntohs(*(reinterpret_cast<short*>(szData)));
szData += 2;
std::shared_ptr<uint8_t[]> spOutput(new uint8_t[m_soSocketParam.nLen + 2]);
HEADER* sHeader = reinterpret_cast<HEADER*>(spOutput.get());
*sHeader = { 0, 0, 0, 0, 0, 0, 0 };
sHeader->FIN = 1;
sHeader->OpCode = 8;
sHeader->Mask = 0;
sHeader->PLoad = m_soSocketParam.nLen;
if (m_soSocketParam.nLen >= 2)
*(reinterpret_cast<short*>(spOutput.get() + 2)) = htons(sCode);
if (m_soSocketParam.nLen > 2)
std::copy(szData, szData + m_soSocketParam.nLen - 2, spOutput.get() + 4);
pTcpSocket->Write(spOutput.get(), m_soSocketParam.nLen + 2);
}
pTcpSocket->Close();
break;
case 9: // ping
reinterpret_cast<HEADER*>(pBuffer)->OpCode = 0xA;
pTcpSocket->Write(pBuffer, nDataLen);
m_soSocketParam.nReceived = m_soSocketParam.nLen = 0;
break;
case 10:// pong
PongReceived(pTcpSocket);
m_soSocketParam.nReceived = m_soSocketParam.nLen = 0;
break;
}
pBuffer += nBlockSize + iOffset;
nDataLen -= static_cast<uint32_t>(nBlockSize + iOffset);
} while (pBuffer < pBufferEnd);
}
}
size_t WebSocket::WriteData(void* pId, const uint8_t* szData, uint32_t nDataLen)
{
TcpSocket* pTcpSocket = reinterpret_cast<TcpSocket*>(pId);
uint32_t iHeaderLen = 2;
if (nDataLen > 125)
iHeaderLen += 2;
if (nDataLen > 65535)
iHeaderLen += 6;
std::unique_ptr<uint8_t[]> spOutput(new uint8_t[nDataLen + iHeaderLen]);
HEADER* sHeader = reinterpret_cast<HEADER*>(spOutput.get());
*sHeader = { 0, 0, 0, 0, 0, 0, 0 };
sHeader->FIN = 1;
sHeader->OpCode = 1;
sHeader->Mask = 0;
sHeader->PLoad = (iHeaderLen > 2 ? (iHeaderLen == 10 ? 127 : 126) : nDataLen);
if (iHeaderLen == 10)
*(reinterpret_cast<uint64_t*>(spOutput.get() + 2)) = htonll(static_cast<uint64_t>(nDataLen));
else if (iHeaderLen > 2)
*(reinterpret_cast<short*>(spOutput.get() + 2)) = htons(static_cast<short>(nDataLen));
std::copy(szData, szData + nDataLen, &spOutput.get()[iHeaderLen]);
return pTcpSocket->Write(spOutput.get(), nDataLen + iHeaderLen);
}
size_t WebSocket::SendPing(TcpSocket* pTcpSocket)
{
uint32_t iHeaderLen = 2;
std::unique_ptr<uint8_t[]> spOutput(new uint8_t[iHeaderLen]);
HEADER* sHeader = reinterpret_cast<HEADER*>(spOutput.get());
*sHeader = { 0, 0, 0, 0, 0, 0, 0 };
sHeader->FIN = 1;
sHeader->OpCode = 9;
sHeader->Mask = 0;
sHeader->PLoad = 0;
return pTcpSocket->Write(spOutput.get(), iHeaderLen);
}