Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
Client.h
Go to the documentation of this file.
1/*
2Copyright (c) 2018 DIVIDE-Studio
3Copyright (c) 2009 Ionut Cava
4
5This file is part of DIVIDE Framework.
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software
9and associated documentation files (the "Software"), to deal in the Software
10without restriction,
11including without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense,
13and/or sell copies of the Software, and to permit persons to whom the
14Software is furnished to do so,
15subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in
18all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED,
22INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23PARTICULAR PURPOSE AND NONINFRINGEMENT.
24IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25DAMAGES OR OTHER LIABILITY,
26WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27IN CONNECTION WITH THE SOFTWARE
28OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30*/
31
32#pragma once
33#ifndef DVD_CLIENT_H_
34#define DVD_CLIENT_H_
35
36#include "WorldPacket.h"
37
38namespace Divide
39{
40
41 class OPCodes;
42 class ASIO;
43
44 class Client
45 {
46 public:
47 Client( ASIO* asioPointer, boost::asio::io_context& service, bool debugOutput );
48
49 // Start:: Called by the user of the client class to initiate the connection
50 // process.
51 // The endpoint iterator will have been obtained using a tcp::resolver.
52 // Stop:: This function terminates all the actors to shut down the
53 // connection. It
54 // may be called by the user of the client class, or by the class itself in
55 // response to graceful termination or an unrecoverable error.
56
57 void start( boost::asio::ip::tcp::resolver::iterator endpoint_iter );
58 void stop();
59
60 [[nodiscard]] inline tcp_socket& getSocket() noexcept
61 {
62 return _socket;
63 }
64
65 // Packet I/O
66 bool sendPacket( const WorldPacket& p );
67 void receivePacket( WorldPacket& p ) const;
68
69 void toggleDebugOutput( const bool debugOutput ) noexcept
70 {
71 _debugOutput = debugOutput;
72 }
73
74 private:
75 // Connection
76 void start_connect( boost::asio::ip::tcp::resolver::iterator endpoint_iter );
77 void handle_connect( const boost::system::error_code& ec, boost::asio::ip::tcp::resolver::iterator endpoint_iter );
78
79 // Read
80 void start_read();
81 void handle_read_body( const boost::system::error_code& ec,
82 size_t bytes_transferred );
83 void handle_read_packet( const boost::system::error_code& ec,
84 size_t bytes_transferred );
85 // File Input
87 void handle_read_file( const boost::system::error_code& ec,
88 size_t bytes_transferred );
89
90 // Write
91 void start_write();
92 void handle_write( const boost::system::error_code& ec );
93 void handle_read_file_content( const boost::system::error_code& err, std::size_t bytes_transferred );
94
95 // Timers
96 void check_deadline();
97
98 private:
99
100 bool _stopped = false, _debugOutput;
102 size_t _header = 0;
103 boost::asio::streambuf _inputBuffer;
106 eastl::deque<WorldPacket> _packetQueue;
107
108 // File Data
109 std::ofstream _outputFile;
110 boost::asio::streambuf _requestBuf;
111 size_t _fileSize = 0;
112 std::array<char, 1024> _buf{};
114 };
115
116}; // namespace Divide
117
118#endif //DVD_CLIENT_H_
void receivePacket(WorldPacket &p) const
Definition: Client.cpp:35
size_t _header
Definition: Client.h:102
void handle_read_packet(const boost::system::error_code &ec, size_t bytes_transferred)
Definition: Client.cpp:96
ASIO * _asioPointer
Definition: Client.h:113
void handle_read_file_content(const boost::system::error_code &err, std::size_t bytes_transferred)
Definition: Client.cpp:177
eastl::deque< WorldPacket > _packetQueue
Definition: Client.h:106
void start_read()
Definition: Client.cpp:57
std::ofstream _outputFile
Definition: Client.h:109
deadline_timer _heartbeatTimer
Definition: Client.h:105
boost::asio::streambuf _inputBuffer
Definition: Client.h:103
bool _stopped
Definition: Client.h:100
void start_write()
Definition: Client.cpp:194
void handle_read_file(const boost::system::error_code &ec, size_t bytes_transferred)
Definition: Client.cpp:135
tcp_socket _socket
Definition: Client.h:101
void start(boost::asio::ip::tcp::resolver::iterator endpoint_iter)
Definition: Client.cpp:40
void toggleDebugOutput(const bool debugOutput) noexcept
Definition: Client.h:69
void check_deadline()
Definition: Client.cpp:250
bool _debugOutput
Definition: Client.h:100
bool sendPacket(const WorldPacket &p)
Definition: Client.cpp:27
void stop()
Definition: Client.cpp:49
deadline_timer _deadline
Definition: Client.h:104
std::array< char, 1024 > _buf
Definition: Client.h:112
void receiveFile()
void handle_read_body(const boost::system::error_code &ec, size_t bytes_transferred)
Definition: Client.cpp:72
void handle_connect(const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator endpoint_iter)
Definition: Client.cpp:306
boost::asio::streambuf _requestBuf
Definition: Client.h:110
void start_connect(boost::asio::ip::tcp::resolver::iterator endpoint_iter)
Definition: Client.cpp:278
size_t _fileSize
Definition: Client.h:111
void handle_write(const boost::system::error_code &ec)
Definition: Client.cpp:224
tcp_socket & getSocket() noexcept
Definition: Client.h:60
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
boost::asio::basic_stream_socket< boost::asio::ip::tcp, boost::asio::io_context::executor_type > tcp_socket
Definition: Utils.h:45
boost::asio::basic_deadline_timer< boost::posix_time::ptime, boost::asio::time_traits< boost::posix_time::ptime >, boost::asio::io_context::executor_type > deadline_timer
Definition: Utils.h:43