Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex0vSky committed Jun 9, 2024
1 parent b816379 commit f03f3af
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
13 changes: 4 additions & 9 deletions src/net/tx/commander.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
// Copyright 2024 Alex0vSky (https://github.com/Alex0vSky), Copyright 2015-2021 (https://github.com/KrystianKaluzny/Tanks)
#include "net/tx/commander.h"

namespace net::tx {
boost::asio::awaitable<Commander::Command> Commander::readCommand_(Base::tcp::socket &socket, commandsBuffer_t const& commandsBuffer) {
command_t commandRaw;
auto [error1, n] = co_await socket.async_read_some( boost::asio::buffer( &commandRaw, c_sizeofCommand ), Base::c_tuple );
if ( n != c_sizeofCommand || error1 )
if ( !co_await async_read_( socket, &commandRaw ) )
co_return Command::undefined;
auto command = static_cast< Command >( commandRaw );
answerSize_t answerSize = 0;
if ( auto it = commandsBuffer.find( command ); it != commandsBuffer.end( ) )
answerSize = it ->second.size( );
auto [error2, nwritten] = co_await boost::asio::async_write( socket, boost::asio::buffer( &answerSize, c_sizeofAnswer ), Base::c_tuple );
if ( nwritten != c_sizeofAnswer || error2 )
if ( !co_await async_write_( socket, &answerSize ) )
co_return Command::undefined;
co_return command;
}
boost::asio::awaitable<bool> Commander::writeCommand_(Base::tcp::socket &socket, Command command, answerSize_t *answerSize) {
const auto commandRaw = static_cast< command_t >( command );
auto [error1, nwritten] = co_await boost::asio::async_write( socket, boost::asio::buffer( &commandRaw, c_sizeofCommand ), Base::c_tuple );
if ( nwritten != c_sizeofCommand || error1 )
if ( !co_await async_write_( socket, &commandRaw ) )
co_return false;
auto [error2, n] = co_await socket.async_read_some( boost::asio::buffer( answerSize, c_sizeofAnswer ), Base::c_tuple );
if ( n != c_sizeofAnswer || error2 )
if ( !co_await async_read_( socket, answerSize ) )
co_return false;
co_return true;
}
Expand Down
15 changes: 11 additions & 4 deletions src/net/tx/commander.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@ class Commander : public Base {
};
using answerSize_t = uint32_t;

template <typename T>
static boost::asio::awaitable< bool > async_read_(Base::tcp::socket &socket, T *t) {
auto [error, n] = co_await boost::asio::async_read( socket, boost::asio::buffer( t, sizeof( *t ) ), Base::c_tuple );
co_return ( sizeof( *t ) == n && !error );
}
template <typename T>
static boost::asio::awaitable< bool > async_write_(Base::tcp::socket &socket, T *t) {
auto [error, n] = co_await boost::asio::async_write( socket, boost::asio::buffer( t, sizeof( *t ) ), Base::c_tuple );
co_return ( sizeof( *t ) == n && !error );
}

protected:
using unit_t = cista::byte_buf;
using commandsBuffer_t = std::unordered_map< Commander::Command, unit_t >;
static boost::asio::awaitable<Command> readCommand_(Base::tcp::socket &socket, commandsBuffer_t const&);
static boost::asio::awaitable<bool> writeCommand_(Base::tcp::socket &socket, Command command, answerSize_t *answerSize);

private:
static constexpr size_t c_sizeofCommand = sizeof( command_t );
static constexpr size_t c_sizeofAnswer = sizeof( answerSize_t );
};
} // namespace net::tx
1 change: 1 addition & 0 deletions src/net/tx/exchanger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ boost::asio::awaitable<bool> Exchanger::clientSide(Commander::Command command, u
}
//printf( "[Exchanger::clientSide] read: %d\n", buffer ->size( ) ); //
co_return !buffer ->empty( );
//co_return true;
}

// TODO(alex): process not more then XXX time
Expand Down

0 comments on commit f03f3af

Please sign in to comment.