-
Notifications
You must be signed in to change notification settings - Fork 3
/
peer.cpp
245 lines (166 loc) · 5.34 KB
/
peer.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
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
#include "peer.h"
namespace p2psp
{
peer::peer(QWidget *parent) : QWidget(parent), io_service_(),
acceptor_(io_service_),
source_socket_(io_service_),
player_socket_(io_service_)
{
//TRACE("Console initialized");
}
void peer::ReceiveSourceEndpoint()
{
boost::array<char, 6> buffer;
read(splitter_socket_, ::buffer(buffer));
char *raw_data = buffer.data();
in_addr ip_raw = *(in_addr *)(raw_data);
source.addr = ip::address::from_string(inet_ntoa(ip_raw));
source.port = ntohs(*(short *)(raw_data + 4));
TRACE("source_endpoint = ("
<< source.addr.to_string()
<< ","
<< std::to_string(source.port)
<< ")");
}
ip::address peer::GetSourceAddr()
{
return source.addr;
}
PORT_TYPE peer::GetSourcePort()
{
return source.port;
}
void peer::SetGETMessage()
{
std::stringstream ss;
ss << "GET /" << channel_ << " HTTP/1.1\r\n"
<< "\r\n";
GET_message_ = ss.str();
TRACE("GET_message = "
<< GET_message_);
ss.str("");
}
void peer::ReceiveChannel()
{
unsigned short channel_size;
{
std::vector<char> message(2);
read(splitter_socket_, boost::asio::buffer(message/*,2*/));
channel_size = ntohs(*(short *)(message.data()));
}
TRACE("channel_size = "
<< channel_size);
{
std::vector<char> messagex(channel_size);
boost::asio::read(splitter_socket_, boost::asio::buffer(messagex/*, channel_size*/));
channel_ = std::string(messagex.data(), channel_size);
}
TRACE("channel = "
<< channel_);
SetGETMessage();
}
std::string peer::GetChannel()
{
return channel_;
}
void peer::ReceiveHeaderSize()
{
boost::array<char, 2> buffer;
read(splitter_socket_, ::buffer(buffer));
header_size_ = ntohs(*(short *)(buffer.c_array()));
TRACE("header_size (in bytes) = "
<< std::to_string(header_size_));
}
HEADER_SIZE_TYPE peer::GetHeaderSize()
{
return header_size_;
}
void peer::ConnectToTheSource() /*throw(boost::system::system_error) */
{
ip::tcp::endpoint source_ep(source.addr, source.port);
boost::system::error_code ec;
source_socket_.connect(source_ep, ec);
if (ec) {
ERROR(ec.message());
ERROR(source_socket_.local_endpoint().address().to_string()
<< "\b: unable to connect to the source ("
<< source.addr
<< ", "
<< to_string(source.port)
<< ")");
source_socket_.close();
exit(-1);
}
TRACE("Connected to the source at ("
<< source.addr.to_string()
<< ","
<< std::to_string(source.port)
<< ") from "
<< source_socket_.local_endpoint().address().to_string());
}
void peer::RequestHeader()
{
source_socket_.send(boost::asio::buffer(GET_message_));
}
void peer::RelayHeader()
{
boost::array<char, 128> buf;
//boost::system::error_code error;
for (int header_load_counter_ = 0; header_load_counter_ < GetHeaderSize();) {
//size_t len = socket.read_some(boost::asio::buffer(buf), error);
size_t len = source_socket_.read_some(boost::asio::buffer(buf));
header_load_counter_ += len;
if (len <= 0) break;
player_socket_.send(boost::asio::buffer(buf, len));
//boost::asio::write(socket, boost::asio::buffer(message), ignored_error);
}
}
void peer::WaitForThePlayer()
{
ip::tcp::endpoint endpoint(ip::tcp::v4(), player_port_);
acceptor_.open(endpoint.protocol());
acceptor_.set_option(ip::tcp::acceptor::reuse_address(true));
acceptor_.bind(endpoint);
acceptor_.listen();
std::cout
<< "Waiting for the player at ("
<< endpoint.address().to_string()
<< ","
<< std::to_string(endpoint.port())
<< ")"
<< std::endl;
acceptor_.accept(player_socket_);
TRACE("Player connected. Player is ("
<< player_socket_.remote_endpoint().address().to_string()
<< ","
<< std::to_string(player_socket_.remote_endpoint().port())
<< ")");
}
bool peer::PlayChunk(int chunk)
{
try {
write(player_socket_, buffer(chunk_ptr[chunk % buffer_size_].data));
return true;
} catch (std::exception e) {
std::cout
<< "Player disconnected"
<< std::endl;
//std::cout << e.what() << std::endl;
//player_alive_ = false;
return false;
//return true;
}
}
void peer::SetPlayerPort(uint16_t player_port)
{
player_port_ = player_port;
}
uint16_t peer::GetPlayerPort()
{
return player_port_;
}
uint16_t peer::GetDefaultPlayerPort()
{
return 9999;
}
}