Skip to content

Commit

Permalink
patched
Browse files Browse the repository at this point in the history
  • Loading branch information
MyLibh committed Nov 3, 2018
1 parent cdab4bd commit 088ea01
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Client Console/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Client::~Client() noexcept = default;
void Client::init(std::string_view ip, std::string_view port)
{
m_tcp_client = std::make_unique<TCPClient>(m_io, boost::asio::ip::tcp::resolver(m_io).resolve(ip, port));
///m_udp_client = std::make_unique<UDPClient>(m_io, boost::asio::ip::udp::resolver(m_io).resolve(ip, port));
m_udp_client = std::make_unique<UDPClient>(m_io, boost::asio::ip::udp::resolver(m_io).resolve(ip, port));
m_thread = std::thread{ [this]() { m_io.run(); } };
}

Expand Down
15 changes: 15 additions & 0 deletions src/Network/Protocols/CommandMessageProtocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class CommandMessageProtocol
public:
inline constexpr CommandMessageProtocol() noexcept;


[[nodiscard]] inline auto get_data() noexcept { return (gsl::span{ m_data }); }
[[nodiscard]] inline const auto get_data() const noexcept { return (gsl::span{ m_data }); }
[[nodiscard]] inline auto get_body() noexcept { return (gsl::span{ m_data }.subspan<HEADER_LENGTH>()); }
Expand All @@ -51,9 +52,23 @@ class CommandMessageProtocol

using CMPROTO = CommandMessageProtocol;

/*!
* \brief Constructs message "SUCCESS"
*
* \return Message
*
* \exception ???
*/
[[nodiscard]]
std::unique_ptr<CMPROTO> make_success_msg();

/*!
* \brief Constructs message "FAILURE"
*
* \return Message
*
* \exception ???
*/
[[nodiscard]]
std::unique_ptr<CMPROTO> make_failure_msg();

Expand Down
4 changes: 0 additions & 4 deletions src/Network/TCP/TCPConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@ TCPConnection::~TCPConnection() noexcept = default;
/*************************************************************************************************************************************************************************************************************/
void TCPConnection::write(std::unique_ptr<CMPROTO> &&msg)
{
std::cout << '1' << '(' << msg->get_data().data() << ')' << std::endl;

boost::asio::post(m_io,
[this, msg{ std::move(msg) }]() mutable
{
std::cout << '2' << '(' << msg->get_data().data() << ')' << std::endl;
const bool write_in_progress{ !m_write_msgs.empty() };
m_write_msgs.push_back(std::move(msg));
std::cout << '3' << '(' << m_write_msgs.front()->get_data().data() << ')' << std::endl;
if (!write_in_progress)
write();
});
Expand Down
9 changes: 5 additions & 4 deletions src/Network/UDP/UDPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@
#include "..\Protocols\ImageMessageProtocol.hpp"
#include "..\..\Service\Debugger.hpp"

/*************************************************************************************************************************************************************************************************************/
UDPClient::UDPClient(boost::asio::io_context &io_context, const boost::asio::ip::udp::resolver::results_type &endpoint) :
m_io { io_context },
m_endpoint{ *endpoint }
{ }


void UDPClient::send(const std::shared_ptr<CMPROTO> &msg)
/*************************************************************************************************************************************************************************************************************/
void UDPClient::start(std::unique_ptr<CMPROTO> &&msg)
{
std::thread t(
[this, &msg]()
[this, msg{ std::move(msg) }]()
{
std::unique_ptr<UDPParticipiant> client{ std::make_unique<UDPParticipiant>(m_io, m_endpoint) };
client->send(msg);
//client->send(std::move(msg));

std::shared_ptr<IMPROTO> imsg{ std::make_shared<IMPROTO>() };
while (true)
Expand Down
3 changes: 1 addition & 2 deletions src/Network/UDP/UDPClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ class UDPClient : private boost::noncopyable
{
public:
UDPClient(boost::asio::io_context &io_context, const boost::asio::ip::udp::resolver::results_type &endpoint);
~UDPClient() noexcept = default;

void send(const std::shared_ptr<CMPROTO> &msg);
void start(std::unique_ptr<CMPROTO> &&msg);

private:
boost::asio::io_context &m_io;
Expand Down
12 changes: 4 additions & 8 deletions src/Network/UDP/UDPParticipiant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,14 @@ UDPParticipiant::UDPParticipiant(boost::asio::io_context &io_context, const boos
// m_socket.set_option(boost::asio::socket_base::broadcast(true));
}

#pragma warning(suppress : 26415 26418)
// warning C26415: Smart pointer parameter 'msg' is used only to access contained pointer. Use T* or T& instead (r.30).
// warning C26418: Shared pointer parameter 'msg' is not copied or moved. Use T* or T& instead (r.36).
void UDPParticipiant::send(const std::shared_ptr<CMPROTO> &msg)
/*************************************************************************************************************************************************************************************************************/
void UDPParticipiant::send(std::unique_ptr<CMPROTO> &&msg)
{
while(m_socket.send_to(boost::asio::buffer(msg->get_data().data(), msg->get_length()), m_endpoint) != msg->get_length());
}

#pragma warning(suppress : 26415 26418)
// warning C26415: Smart pointer parameter 'msg' is used only to access contained pointer. Use T* or T& instead (r.30).
// warning C26418: Shared pointer parameter 'msg' is not copied or moved. Use T* or T& instead (r.36).
void UDPParticipiant::recv(const std::shared_ptr<CMPROTO> &msg)
/*************************************************************************************************************************************************************************************************************/
void UDPParticipiant::recv(std::unique_ptr<CMPROTO> &msg)
{
while(m_socket.receive_from(boost::asio::buffer(msg->get_data().data(), msg->get_length()), m_endpoint) != msg->get_length());

Expand Down
4 changes: 2 additions & 2 deletions src/Network/UDP/UDPParticipiant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class UDPParticipiant : private boost::noncopyable

UDPParticipiant(boost::asio::io_context &io_context, const boost::asio::ip::udp::endpoint &endpoint);

void send(const std::shared_ptr<CMPROTO> &msg);
void recv(const std::shared_ptr<CMPROTO> &msg);
void send(std::unique_ptr<CMPROTO> &&msg);
void recv(std::unique_ptr<CMPROTO> &msg);

void send(const std::shared_ptr<IMPROTO> &msg);
void recv(const std::shared_ptr<IMPROTO> &msg);
Expand Down
2 changes: 1 addition & 1 deletion src/Service/Log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* \param[in] filename Formated string for the name of log file
*
* \exception
* \exception ???
*/
void InitLog(const std::string_view filename);

Expand Down

0 comments on commit 088ea01

Please sign in to comment.