-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketContext.cpp
More file actions
204 lines (159 loc) · 4.21 KB
/
SocketContext.cpp
File metadata and controls
204 lines (159 loc) · 4.21 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
#include <winsock2.h>
#include <unordered_map>
//#include <vector>
#include <memory>
#include "Logger.h"
#include "SocketContext.h"
using namespace std;
using namespace CPlusPlusLogging;
SocketContext::SocketContext()
{
m_Socket = SOCKET_ERROR;
ZeroMemory(&m_IoRecv, sizeof(IO_OPERATION_DATA));
m_IoRecv.dataBuf.buf = (char*)&m_IoRecv.buffer;
m_IoRecv.dataBuf.len = MAX_BUFFER_LEN;
m_IoRecv.IoType = OP_READ;
ZeroMemory(&m_IoSend, sizeof(IO_OPERATION_DATA));
m_IoSend.dataBuf.buf = (char*)&m_IoSend.buffer;
m_IoSend.dataBuf.len = MAX_BUFFER_LEN;
m_IoSend.IoType = OP_WRITE;
ZeroMemory(m_SockType, sizeof(m_SockType));
m_ProxySocket = FALSE;
m_pBuddy.reset();
}
BOOL SocketContext::Recv()
{
BOOL status = TRUE;
DWORD dwFlags = 0;
int nBytesRecv = 0;
ZeroMemory(&m_IoRecv, sizeof(IO_OPERATION_DATA));
m_IoRecv.IoType = OP_READ;
m_IoRecv.dataBuf.buf = (char*)&m_IoRecv.buffer;
m_IoRecv.dataBuf.len = MAX_BUFFER_LEN;
dwFlags = 0;
nBytesRecv = WSARecv(m_Socket, &m_IoRecv.dataBuf, 1, NULL, &dwFlags, &m_IoRecv.overlapped, NULL);
if ((SOCKET_ERROR == nBytesRecv) && (WSA_IO_PENDING != WSAGetLastError()))
{
status = FALSE;
}
return status;
}
BOOL SocketContext::Send(char* szBuffer, UINT16 length)
{
LOG_DEBUG("%s %ld: Sending %ld bytes.", m_SockType, m_Id, length);
BOOL status = TRUE;
int nBytesSent = 0;
DWORD dwFlags = 0;
ZeroMemory(&m_IoSend, sizeof(IO_OPERATION_DATA));
m_IoSend.IoType = OP_WRITE;
strcpy(m_IoSend.buffer, szBuffer);
m_IoSend.len = length;
m_IoSend.dataBuf.buf = (char*)&m_IoSend.buffer;
m_IoSend.dataBuf.len = m_IoSend.len;
nBytesSent = WSASend(m_Socket, &m_IoSend.dataBuf, 1, NULL, dwFlags, &m_IoSend.overlapped, NULL);
if ((SOCKET_ERROR == nBytesSent) && (WSA_IO_PENDING != WSAGetLastError()))
{
status = FALSE;
}
return status;
}
/*
Forward data from recv buffer of client socket to
send buffer of fwd socket and send it to destination from
fwd socket.
*/
BOOL SocketContext::Forward(UINT16 length)
{
LOG_DEBUG("%s %ld: Forwarding %ld bytes to socket id %d.",
m_SockType, m_Id, length, m_pBuddy.lock()->GetId());
BOOL status = TRUE;
using wt = std::weak_ptr<SocketContext>;
if (!m_pBuddy.owner_before(wt{})
&& !wt{}.owner_before(m_pBuddy) || m_pBuddy.expired())
{
status = FALSE;
goto Exit;
}
status = m_pBuddy.lock()->Send(m_IoRecv.buffer, length);
Exit:
return status;
}
void SocketContext::SetSockType(const char* s)
{
strcpy(m_SockType, s);
}
void SocketContext::GetSockType(char* s)
{
strcpy(s, m_SockType);
}
void SocketContext::SocketAndIOCleanup()
{
LOG_DEBUG("%s %ld: Closing socket and cancelling all I/O",
m_SockType, m_Id);
//Cancel all Recv IO operations
CancelIoEx((HANDLE)m_Socket, &m_IoRecv.overlapped);
SleepEx(0, TRUE); // the completion will be called here
//Cancel all Send IO operation
CancelIoEx((HANDLE)m_Socket, &m_IoSend.overlapped);
SleepEx(0, TRUE); // the completion will be called here
closesocket(m_Socket);
}
SocketContext::~SocketContext()
{
LOG_DEBUG("%s %ld: SocketContext::~SocketContext(): "
"Cleaning up socket context.", m_SockType, m_Id);
SocketAndIOCleanup();
}
std::shared_ptr<SocketContext> SocketContext::GetBuddySocketContext() const
{
//return m_pBuddy.Get();
return m_pBuddy.lock();
}
void SocketContext::SetBuddySocketContext(const std::shared_ptr<SocketContext>& sc)
{
m_pBuddy = sc;
}
/*
SocketContext::BuddySocketContextPtr::BuddySocketContextPtr()
{
_strong = nullptr;
_weak.reset();
}
SocketContext::BuddySocketContextPtr::BuddySocketContextPtr(const std::shared_ptr<SocketContext> sc)
{
_strong = nullptr;
_weak.reset();
Set(sc);
}
SocketContext::BuddySocketContextPtr& SocketContext::BuddySocketContextPtr::operator=(const std::shared_ptr<SocketContext> sc) {
return Set(sc);
}
std::shared_ptr<SocketContext> SocketContext::BuddySocketContextPtr::Get() const
{
if (nullptr != _strong)
{
return _strong;
}
else
{
return _weak.lock();
}
}
SocketContext::BuddySocketContextPtr& SocketContext::BuddySocketContextPtr::Set(std::shared_ptr<SocketContext> sc)
{
if (nullptr == sc->GetBuddySocketContext())
{
_strong = sc;
_weak.reset();
}
else
{
_strong = nullptr;
_weak = sc;
}
return *this;
}*/
char* SocketContext::GetSocketIpAddress()
{
return inet_ntoa(m_SockAddress.sin_addr);
}