forked from rileytestut/AltServer-Windows
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathWirelessConnection.cpp
More file actions
executable file
·148 lines (116 loc) · 3.29 KB
/
Copy pathWirelessConnection.cpp
File metadata and controls
executable file
·148 lines (116 loc) · 3.29 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
#include "WirelessConnection.h"
#include <WinSock2.h>
#include <cpprest/json.h>
#define odslog(msg) { std::stringstream ss; ss << msg << std::endl; OutputDebugStringA(ss.str().c_str()); }
#include "ServerError.hpp"
#if SIZE_MAX == UINT_MAX
typedef int ssize_t; /* common 32 bit case */
#elif SIZE_MAX == ULONG_MAX
typedef long ssize_t; /* linux 64 bits */
#elif SIZE_MAX == ULLONG_MAX
typedef long long ssize_t; /* windows 64 bits */
#elif SIZE_MAX == USHRT_MAX
typedef short ssize_t; /* is this even possible? */
#else
#error platform has exotic SIZE_MAX
#endif
WirelessConnection::WirelessConnection(int socket) : _socket(socket)
{
}
WirelessConnection::~WirelessConnection()
{
}
void WirelessConnection::Disconnect()
{
if (this->socket() == 0)
{
return;
}
closesocket(this->socket());
_socket = 0;
}
pplx::task<void> WirelessConnection::SendData(std::vector<unsigned char>& data)
{
return pplx::create_task([this, data]() {
fd_set input_set;
fd_set copy_set;
int64_t totalSentBytes = 0;
while (true)
{
struct timeval tv;
tv.tv_sec = 1; /* 1 second timeout */
tv.tv_usec = 0; /* no microseconds. */
/* Selection */
FD_ZERO(&input_set); /* Empty the FD Set */
FD_SET(this->socket(), &input_set); /* Listen to the input descriptor */
FD_ZERO(©_set); /* Empty the FD Set */
FD_SET(this->socket(), ©_set); /* Listen to the input descriptor */
ssize_t sentBytes = send(this->socket(), (const char*)data.data(), (size_t)(data.size() - totalSentBytes), 0);
totalSentBytes += sentBytes;
if (sentBytes == 0)
{
throw ServerError(ServerErrorCode::ConnectionFailed);
}
std::cout << "Sent Bytes Count: " << sentBytes << " (" << totalSentBytes << ")" << std::endl;
if (totalSentBytes >= sentBytes)
{
break;
}
}
std::cout << "Sent Data: " << totalSentBytes << " Bytes" << std::endl;
});
}
pplx::task<std::vector<unsigned char>> WirelessConnection::ReceiveData(int size)
{
return pplx::create_task([this, size]() {
std::vector<unsigned char> data;
data.reserve(size);
char buffer[4096];
fd_set input_set;
fd_set copy_set;
while (true)
{
struct timeval tv;
tv.tv_sec = 1; /* 1 second timeout */
tv.tv_usec = 0; /* no microseconds. */
int socket = this->socket();
std::cout << "Checking socket: " << socket << std::endl;
/* Selection */
FD_ZERO(&input_set); /* Empty the FD Set */
FD_SET(socket, &input_set); /* Listen to the input descriptor */
FD_ZERO(©_set); /* Empty the FD Set */
FD_SET(socket, ©_set); /* Listen to the input descriptor */
int result = select(this->socket() + 1, &input_set, ©_set, NULL, &tv);
if (result == 0)
{
continue;
}
else if (result == -1)
{
std::cout << "Error!" << std::endl;
}
else
{
ssize_t readBytes = recv(this->socket(), buffer, min((ssize_t)4096, (ssize_t)(size - data.size())), 0);
if (readBytes == 0)
{
throw ServerError(ServerErrorCode::ConnectionFailed);
}
for (int i = 0; i < readBytes; i++)
{
data.push_back(buffer[i]);
}
odslog("Received bytes: " << data.size() << "(of " << size << ")");
if (data.size() >= size)
{
break;
}
}
}
return data;
});
}
int WirelessConnection::socket() const
{
return _socket;
}