Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "stdafx.h"

#include "Headers/tcp_session_tpl.h"
#include "Headers/OPCodesTpl.h"

#include "Networking/Headers/ASIO.h"

///////////////////////////////////////////////////////////////////////////////////////
//                                     TCP                                           //
///////////////////////////////////////////////////////////////////////////////////////

namespace Divide {

tcp_session_tpl::tcp_session_tpl(boost::asio::io_service& io_service, channel& ch)
    : _startTime(time(nullptr)),
      _channel(ch),
      _socket(io_service),
      _inputDeadline(io_service),
      _nonEmptyOutputQueue(io_service),
      _outputDeadline(io_service),
      _strand(io_service)
{
    _inputDeadline.expires_at(boost::posix_time::pos_infin);
    _outputDeadline.expires_at(boost::posix_time::pos_infin);
    _nonEmptyOutputQueue.expires_at(boost::posix_time::pos_infin);
}

void tcp_session_tpl::start() {
    _channel.join(shared_from_this());

    start_read();

    _inputDeadline.async_wait(_strand.wrap(boost::bind(&tcp_session_tpl::check_deadline, shared_from_this(), &_inputDeadline)));

    await_output();

    _outputDeadline.async_wait(_strand.wrap(boost::bind(&tcp_session_tpl::check_deadline, shared_from_this(), &_outputDeadline)));
}

void tcp_session_tpl::stop() {
    _channel.leave(shared_from_this());

    _socket.close();
    _inputDeadline.cancel();
    _nonEmptyOutputQueue.cancel();
    _outputDeadline.cancel();
}

bool tcp_session_tpl::stopped() const {
    return !_socket.is_open();
}

void tcp_session_tpl::sendPacket(const WorldPacket& p) {
    _outputQueue.push_back(p);
    _nonEmptyOutputQueue.expires_at(boost::posix_time::neg_infin);
}

void tcp_session_tpl::sendFile(const stringImpl& name) {
    _outputFileQueue.push_back(name);
}

void tcp_session_tpl::start_read() {
    _header = 0;
    _inputBuffer.consume(_inputBuffer.size());
    _inputDeadline.expires_from_now(boost::posix_time::seconds(30));
    boost::asio::async_read(
        _socket, 
        boost::asio::buffer(&_header, sizeof(_header)),
        _strand.wrap(boost::bind(&tcp_session_tpl::handle_read_body, shared_from_this(), _1, boost::asio::placeholders::bytes_transferred)));
}

void tcp_session_tpl::handle_read_body(const boost::system::error_code& ec, size_t bytes_transfered) {
    ACKNOWLEDGE_UNUSED(bytes_transfered);

    if (stopped()) {
        return;
    }

    if (!ec) {
        _inputDeadline.expires_from_now(boost::posix_time::seconds(30));
        boost::asio::async_read(
            _socket, _inputBuffer.prepare(_header),
            _strand.wrap(boost::bind(
                &tcp_session_tpl::handle_read_packet, shared_from_this(), _1,
                boost::asio::placeholders::bytes_transferred)));
    } else {
        stop();
    }
}

void tcp_session_tpl::handle_read_packet(const boost::system::error_code& ec,
                                         size_t bytes_transfered) {
    ACKNOWLEDGE_UNUSED(bytes_transfered);

    if (stopped()) {
        return;
    }

    if (!ec) {
        _inputBuffer.commit(_header);
        ASIO::LOG_PRINT(("Buffer size: " + to_stringImpl(_header)).c_str());
        std::istream is(&_inputBuffer);
        WorldPacket packet;
        try {
            boost::archive::text_iarchive ar(is);
            ar& packet;
        } catch (std::exception& e) {
            ASIO::LOG_PRINT(e.what(), true);
        }

        handlePacket(packet);
        start_read();
    } else {
        stop();
    }
}

void tcp_session_tpl::start_write() {
    if (_outputQueue.empty()) await_output();

    boost::asio::streambuf buf;
    std::ostream os(&buf);

    // Set a deadline for the write operation.
    _outputDeadline.expires_from_now(boost::posix_time::seconds(30));

    WorldPacket& p = _outputQueue.front();
    boost::archive::text_oarchive ar(os);
    ar& p;  // Archive the packet

    size_t header = buf.size();
    vectorImpl<boost::asio::const_buffer> buffers;
    buffers.push_back(boost::asio::buffer(&header, sizeof(header)));
    buffers.push_back(buf.data());
    // Start an asynchronous operation to send a message.
    if (p.opcode() == OPCodes::SMSG_SEND_FILE) {
        boost::asio::async_write(
            _socket, buffers,
            _strand.wrap(boost::bind(&tcp_session_tpl::handle_write_file,
                                     shared_from_this(), _1)));
    } else {
        boost::asio::async_write(
            _socket, buffers,
            _strand.wrap(boost::bind(&tcp_session_tpl::handle_write,
                                     shared_from_this(), _1)));
    }
}
void tcp_session_tpl::handle_write_file(const boost::system::error_code& ec) {
    ACKNOWLEDGE_UNUSED(ec);

    boost::asio::streambuf request_;
    stringImpl filePath = _outputFileQueue.front();
    std::ifstream source_file;
    source_file.open(filePath.c_str(),
                     std::ios_base::binary | std::ios_base::ate);
    if (!source_file) {
        ASIO::LOG_PRINT(("failed to open " + filePath).c_str());
        return;
    }
    size_t file_size = sizeof(source_file);  //.tellg();
    source_file.seekg(0);
    // first send file name and file size to server
    std::ostream request_stream(&request_);
    request_stream << filePath << "\n" << file_size << "\n\n";
    ASIO::LOG_PRINT(("request size:" + to_stringImpl(request_.size())).c_str());

    // Start an asynchronous resolve to translate the server and service names
    // into a list of endpoints.
    _outputFileQueue.pop_front();
    boost::asio::async_write(
        _socket, request_,
        _strand.wrap(boost::bind(&tcp_session_tpl::handle_write,
                                 shared_from_this(), _1)));
}

void tcp_session_tpl::handle_write(const boost::system::error_code& ec) {
    if (stopped()) return;

    if (!ec) {
        _outputQueue.pop_front();
        await_output();
    } else {
        stop();
    }
}

void tcp_session_tpl::await_output() {
    if (stopped()) return;

    if (_outputQueue.empty()) {
        if (_outputQueue.empty()) {
            _nonEmptyOutputQueue.expires_at(boost::posix_time::pos_infin);
            _nonEmptyOutputQueue.async_wait(boost::bind(
                &tcp_session_tpl::await_output, shared_from_this()));
        }
    } else {
        start_write();
    }
}

void tcp_session_tpl::check_deadline(boost::asio::deadline_timer* deadline) {
    if (stopped()) return;

    // Check whether the deadline has passed. We compare the deadline against
    // the current time since a new asynchronous operation may have moved the
    // deadline before this actor had a chance to run.
    if (deadline->expires_at() <= 
        boost::asio::deadline_timer::traits_type::now()) {
        // The deadline has passed. Stop the session. The other actors will
        // terminate as soon as possible.
        stop();
    } else {
        // Put the actor back to sleep.
        deadline->async_wait(boost::bind(&tcp_session_tpl::check_deadline,
                                         shared_from_this(), deadline));
    }
}

///////////////////////////////////////////////////////////////////////////////////////
//                                     UDP                                           //
///////////////////////////////////////////////////////////////////////////////////////

udp_broadcaster::udp_broadcaster(boost::asio::io_service& io_service,
                      const boost::asio::ip::udp::endpoint& broadcast_endpoint)
    : socket_(io_service) {
    socket_.connect(broadcast_endpoint);
}

void udp_broadcaster::sendPacket(const WorldPacket& p) {
    boost::asio::streambuf buf;
    std::ostream os(&buf);
    boost::archive::text_oarchive ar(os);
    ar& p;  // Archive the packet

    size_t header = buf.size();
    vectorImpl<boost::asio::const_buffer> buffers;
    buffers.push_back(boost::asio::buffer(&header, sizeof(header)));
    buffers.push_back(buf.data());
    boost::system::error_code ignored_ec;
    socket_.send(buffers, 0, ignored_ec);
}

void tcp_session_tpl::handlePacket(WorldPacket& p) {
    switch (p.opcode()) {
        case OPCodes::MSG_HEARTBEAT:
            ASIO::LOG_PRINT("Received [ MSG_HEARTBEAT ]");
            HandleHeartBeatOpCode(p);
            break;
        case OPCodes::CMSG_PING:
            ASIO::LOG_PRINT("Received [ CMSG_PING ]");
            HandlePingOpCode(p);
            break;
        case OPCodes::CMSG_REQUEST_DISCONNECT:
            HandleDisconnectOpCode(p);
            break;
        case OPCodes::CMSG_ENTITY_UPDATE:
            HandleEntityUpdateOpCode(p);
            break;
        default:
            DIVIDE_UNEXPECTED_CALL("Unknown network message!");
            break;
    }
}

void tcp_session_tpl::HandleHeartBeatOpCode(WorldPacket& p) {
    ACKNOWLEDGE_UNUSED(p);

    WorldPacket r(OPCodes::MSG_HEARTBEAT);
    ASIO::LOG_PRINT("Sending  [ MSG_HEARTBEAT]");
    r << (I8)0;
    sendPacket(r);
}

void tcp_session_tpl::HandlePingOpCode(WorldPacket& p) {
    F32 time = 0;
    p >> time;
    ASIO::LOG_PRINT(("Sending  [ SMSG_PONG ] with data: " + to_stringImpl(time)).c_str());
    WorldPacket r(OPCodes::SMSG_PONG);
    r << time;
    sendPacket(r);
}

void tcp_session_tpl::HandleDisconnectOpCode(WorldPacket& p) {
    stringImpl client;
    p >> client;
    ASIO::LOG_PRINT(("Received [ CMSG_REQUEST_DISCONNECT ] from: [ " + client + " ]").c_str());
    WorldPacket r(OPCodes::SMSG_DISCONNECT);
    r << (U8)0;  // this will be the error code returned after safely saving
                 // client
    sendPacket(r);
}

void tcp_session_tpl::HandleEntityUpdateOpCode(WorldPacket& p) {
    ASIO::LOG_PRINT("Received [ CMSG_ENTITY_UPDATE ] !");
    UpdateEntities(p);
}

};  // namespace Divide

Commits for Divide-Framework/trunk/Source Code/Networking/tcp_session_tpl.cpp

Diff revisions: vs.
Revision Author Commited Message
919 Diff Diff IonutCava picture IonutCava Wed 26 Jul, 2017 22:37:18 +0000

[Ionut]
- Add a configurable log printing system to the networking library.
- Remove the need to specify swap buffer necessity with endFrame calls. Swap buffer requirement is now a per-window property.
- Do not load an image file multiple times for the same texture. cache data for each array layer to speed up duplicates. ToDo: Cache data globally?
- Other small bugfixes

898 Diff Diff IonutCava picture IonutCava Thu 22 Jun, 2017 22:05:34 +0000

[IonutCava]
- Use vectorImpl instead of vector in the few remaining places it wasn’t used
- Force GLIM to use the submitRenderCommand / GenericDrawCommand system
- Add basic framework for a “pushConstants” type buffer that will replace Uniform() calls
- Add some code to allow hardware query rewrite in the future

890 Diff Diff IonutCava picture IonutCava Sun 14 May, 2017 20:54:59 +0000

[Ionut]
- Add pre-compiled header support
- Initial code for per-stage tessellation computation

844 Diff Diff IonutCava picture IonutCava Thu 02 Feb, 2017 21:49:28 +0000

[IonutCava]
- More scripting prep work and experiments

838 Diff Diff IonutCava picture IonutCava Mon 30 Jan, 2017 17:28:35 +0000

[IonutCava]
- Added a NetworkingComponent to SGNs
- Started cleaning up Client / Server architecture

837 Diff Diff IonutCava picture IonutCava Sun 29 Jan, 2017 15:45:58 +0000

[IonutCava]
- File system updates

827 Diff Diff IonutCava picture IonutCava Sun 22 Jan, 2017 21:59:36 +0000

[IonutCava]
- ShaderBuffers now decide heuristically if they get persistently mapped or not (depends on size. Current limit is 512Kb for subData calls.
- Separate visible node command generation into 2 steps: onRender and getDrawPackage. onRender allows the creation of parallel tasks that should finish by the time the second call, getDrawPackage reaches the same node
- Update ParticleEmitter to take into account multiple players and increase parallelism at the cost of memory consumption and complexity.
- Add a default bounding box for Players (add extents to SceneTransform nodes)
- All buffers (ShaderBuffer, d3dConstantBuffer, glUniformBuffer, glBufferImpl, glGenericBuffer, etc) use BufferParams for creation.
— Constructor and create calls have been merged together

671 Diff Diff IonutCava picture IonutCava Fri 08 Apr, 2016 16:26:49 +0000

[Ionut]
- Custom memory allocation system part 1:
— Add http://www.codeproject.com/Articles/1089905/A-Custom-STL-std-allocator-Replacement-Improves-Pe
— Add custom allocators to strings and vectors
— Add custom allocators to a few test classes
— No support for aligned memory yet, so vec4<F32> and mat4<F32> vectors use the old allocation system
- Replace more std::async call with our custom task system

466 Diff Diff IonutCava picture IonutCava Fri 05 Jun, 2015 15:07:17 +0000

[Ionut]
- Moved EASTL, SimpleINI and Threadpool libraries from Dependencies repo to engine repo
- Disabled EASTL string and vector support as they are really out of date and missing required features.
— EASTL hash map is still usable

457 IonutCava picture IonutCava Mon 01 Jun, 2015 21:45:52 +0000

[Ionut]
- Windows build fix for previous commit