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
#include "tcp_session_tpl.h"
#include <fstream>
#include <boost/archive/xml_iarchive.hpp>
#include "OPCodesTpl.h"

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

namespace Divide {

tcp_session_tpl::tcp_session_tpl(boost::asio::io_service& io_service, channel& ch) :  start_time(time(nullptr)),
                                         channel_(ch),
                                         socket_(io_service),
                                         input_deadline_(io_service),
                                         non_empty_output_queue_(io_service),
                                         output_deadline_(io_service),
                                        _strand(io_service)
{
    input_deadline_.expires_at(boost::posix_time::pos_infin);
    output_deadline_.expires_at(boost::posix_time::pos_infin);
    non_empty_output_queue_.expires_at(boost::posix_time::pos_infin);
}

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

    start_read();

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

    await_output();

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

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

    socket_.close();
    input_deadline_.cancel();
    non_empty_output_queue_.cancel();
    output_deadline_.cancel();
}

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

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

void tcp_session_tpl::sendFile(const std::string& name)
{
    output_file_queue_.push_back(name);
}

void tcp_session_tpl::start_read()
{
    header = 0;
    input_buffer_.consume(input_buffer_.size());
    input_deadline_.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)
{
    if (stopped())
      return;

    if (!ec)
    {
        input_deadline_.expires_from_now(boost::posix_time::seconds(30));
        boost::asio::async_read(socket_, input_buffer_.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)
{
    if (stopped())
      return;

    if (!ec)
    {
        input_buffer_.commit(header);
        std::cout << "Buffer size: " << header << std::endl;
        std::istream is(&input_buffer_);
        WorldPacket packet;
        try
        {
            boost::archive::text_iarchive ar(is);
            ar & packet;
        }
        catch(std::exception& e)
        {
            std::cout << e.what() << std::endl;
        }

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

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

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

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

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

    size_t header = buf.size();
    std::vector<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.getOpcode() == 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)
{
    boost::asio::streambuf request_;
    std::string filePath = output_file_queue_.front();
    std::ifstream source_file;
    source_file.open(filePath.c_str(), std::ios_base::binary | std::ios_base::ate);
    if (!source_file)
    {
       std::cout << "failed to open " << filePath << std::endl;
       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";
    std::cout << "request size:"<<request_.size()<<std::endl;

    // Start an asynchronous resolve to translate the server and service names
    // into a list of endpoints.
    output_file_queue_.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)
    {
      output_queue_.pop_front();
      await_output();
    }
    else
    {
      stop();
    }
}

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

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

void tcp_session_tpl::check_deadline(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() <= 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 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();
    std::vector<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);
}

}; //namespace Divide

Commits for Divide-Framework/trunk/Source Code/Libs/src/DivideNetworking/tcp_session_tpl.cpp

Diff revisions: vs.
Revision Author Commited Message
318 Diff Diff IonutCava picture IonutCava Sat 30 Aug, 2014 17:35:53 +0000

[Ionut]
- Wrapped the entire code in a “Divide” namespace
- VertexBuffers now call “shrink_to_fit” on all internal data storage
- Improved some vector performance by preferring “emplace_back” instead of “push_back” + proepr usage of reserve / resize
- Wrapped OIS specific types and classes in Divide::Input namespace
- Added the messageBox.layout file (forgot it in the previous few commits)

249 Diff Diff k1ngp1n picture k1ngp1n Mon 31 Mar, 2014 20:42:31 +0000

[Ionut] [[BR]]
- Migrated to platform toolset v100 [[BR]]
- Updated boost to 1.55 [[BR]]
- Moved from “NULL” to “nullptr” [[BR]]
- DevIL is now linked dynamically [[BR]]

171 Diff Diff k1ngp1n picture k1ngp1n Sun 03 Nov, 2013 18:15:06 +0000

-Replaced all tabs with spaces [[BR]]

152 k1ngp1n picture k1ngp1n Thu 20 Jun, 2013 14:23:31 +0000

[Ionut] [[BR]]
- New rendering pipeline (experimental and unstable): [[BR]]
— OpenGL 3.x clean context (glLight commands are still used for now, but UBO support is there, but not used yet) [[BR]]
— Custom matrix stacks and UBO based shader data (UBOs cause some link errors on some nVidia cards)[[BR]]
— Multi-threaded texture loading and shader compilation (mt shader compilation disabled for now. needs more tests) [[BR]]
— VAO rendering is self contained (no more enable/disable calls before a draw request) [[BR]]
— Render instance class used for rendering information [[BR]]
[[BR]]
- New unique ID generation base class: GUIDWrapper. Each class that derives from this, gets an application wide unique id on construction [[BR]]
- Added base code for an in-game editor using CEGUI [[BR]]
- Replaced some mutex locks with lockless data structures or atomic data [[BR]]
- CEGUI 0.8.2, FTGL-GL3 and GLIM 0.4 modified to support GLEW_MX if defined [[BR]]
- Dropped VS2008 support [[BR]]
- Networking library, server framework and ReCast files are now projects inside the Divide Framework solution [[BR]]
- New console command “setFov” that modifies the horizontal FoV value in realtime [[BR]]
- New console command “recompileShader” that reparses shader files, recompiles shaders and relinks shader programs who’s names match or contain the argument string [[BR]]
- New debug utility: axis angle drawn in corner of the screen if needed [[BR]]
- More ‘const’-s pasted around the code, more loop performance tweaks (allocations before a loop, tighter loops, etc), more utility member functions added to various classes [[BR]]