Skip to content

Commit

Permalink
poc of exchange for fork process
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex0vSky committed Jun 1, 2024
1 parent c967ec3 commit 3af688f
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 17 deletions.
6 changes: 4 additions & 2 deletions _msvcStuff/BattleCityEos.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ SDL2_image.lib
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PreprocessorDefinitions>A0S_SCHEMA_CISTA;SDL_MAIN_HANDLED;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\_msvcStuff;..\src;..\src\app_state;..\ThirdParty;..\src\schema\cista;C:\Prj\_L\SDL\SDL2\include;C:\Prj\_L\SDL\SDL2\include\SDL2;C:\Prj\_L\SDL\SDL2_mixer\include;C:\Prj\_L\SDL\SDL2_ttf\include;C:\Prj\_L\SDL\SDL2_image\include;</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..\_msvcStuff;..\src;..\src\app_state;..\ThirdParty;..\src\schema\cista;C:\Prj\_L\SDL\SDL2\include;C:\Prj\_L\SDL\SDL2\include\SDL2;C:\Prj\_L\SDL\SDL2_mixer\include;C:\Prj\_L\SDL\SDL2_ttf\include;C:\Prj\_L\SDL\SDL2_image\include;C:\Prj\_L\boost_1_77;</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<Optimization>Disabled</Optimization>
<ForcedIncludeFiles>stdafx.h</ForcedIncludeFiles>
Expand All @@ -279,7 +279,8 @@ SDL2_image.lib
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<SupportJustMyCode>false</SupportJustMyCode>
<AdditionalOptions>/Zc:sizedDealloc- %(AdditionalOptions)</AdditionalOptions>
<AdditionalOptions>/await %(AdditionalOptions)</AdditionalOptions>
<ConformanceMode>false</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down Expand Up @@ -703,6 +704,7 @@ C:\Prj\_L\Qt\lib\Qt5QuickWidgets.lib %(AdditionalOptions)
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\_L\boost_1_77\boost\config\user.hpp" />
<ClInclude Include="..\resource\level\level.h" />
<ClInclude Include="..\resource\resource.h" />
<ClInclude Include="..\src\app.h" />
Expand Down
6 changes: 6 additions & 0 deletions _msvcStuff/BattleCityEos.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
<Filter Include="Source Files\net">
<UniqueIdentifier>{76ab27c3-b468-44cf-ae2a-ad42bc7f0ad8}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\objects\cista\boost">
<UniqueIdentifier>{993a376d-54ff-4034-bfec-b9b666ae34bf}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
Expand Down Expand Up @@ -332,6 +335,9 @@
<ClInclude Include="..\resource\resource.h">
<Filter>Resource Files</Filter>
</ClInclude>
<ClInclude Include="..\..\..\_L\boost_1_77\boost\config\user.hpp">
<Filter>Header Files\objects\cista\boost</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="cpp.hint" />
Expand Down
12 changes: 12 additions & 0 deletions _msvcStuff/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@
#pragma warning( disable: 4308 )
#pragma warning( disable: 4018 )
# include "cista.h"
// tmp BOOST_LIB_VERSION: 1_77
//# define BOOST_ASIO_HAS_CO_AWAIT
//# define BOOST_ASIO_HAS_STD_COROUTINE
//# define BOOST_ASIO_HAS_MOVE
# define BOOST_DATE_TIME_NO_LIB
# define BOOST_REGEX_NO_LIB
# include <boost/asio.hpp>
# include <boost/asio/experimental/as_single.hpp>
# include <boost/asio/experimental/as_tuple.hpp>
# if BOOST_VERSION != 107700
# error "my msvc support only boost BOOST_LIB_VERSION 1_77"
# endif //BOOST_VERSION != 107700
#endif // A0S_SCHEMA_CISTA

// resources binToHex
Expand Down
90 changes: 79 additions & 11 deletions src/net/net.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright 2024 Alex0vSky (https://github.com/Alex0vSky), Copyright 2015-2021 (https://github.com/KrystianKaluzny/Tanks)
#include "net.h"

#include <boost/config/user.hpp>
#include <boost/asio.hpp>

namespace net {

void NetPlayer::update(Uint32 dt) {
Expand Down Expand Up @@ -49,19 +52,84 @@ void NetPlayer::update(Uint32 dt) {
stop = false;
}

NetGame::NetGame(int players_count) :
Game( 1 )
, m_ioContext( std::make_unique< boost::asio::io_context >( 1 ) )
{}

void NetGame::update(Uint32 dt) {
// TODO(alex): get from network
constexpr auto MODE = cista::mode::NONE
| cista::mode::WITH_VERSION
| cista::mode::WITH_INTEGRITY
| cista::mode::DEEP_CHECK
;
if ( m_player ) {
std::vector< unsigned char > buffer;
NetPlayer *player;
buffer = cista::serialize< MODE >( *m_player );
player = cista::deserialize< NetPlayer, MODE >( buffer );
*m_player = *player;
//NetPlayer *player;
//cista::byte_buf buffer = cista::serialize< c_MODE >( *m_player );
//player = cista::deserialize< NetPlayer, c_MODE >( buffer );
//*m_player = *player;

// No signals and calling shortcut not working in my msvc: as_tuple_t< use_awaitable_t< > >;
co_spawn_( [this]() mutable ->awaitable {
printf( "[serv] acceptor...\n" );
tcp::acceptor acceptor( *m_ioContext, { tcp::v4( ), c_port } );
while ( !m_ioContext ->stopped( ) ) {
auto [error1, socket] = co_await acceptor.async_accept( c_tuple );
if ( !socket.is_open( ) || error1 )
continue;
command_t commandRaw; const size_t commandSizeof = sizeof( commandRaw );
auto [error3, n] = co_await socket.async_read_some( boost::asio::buffer( &commandRaw, commandSizeof ), c_tuple );
if ( n != commandSizeof || error3 )
continue;
const auto command = static_cast< Command >( commandRaw );
if ( Command::Something == command ) {
cista::byte_buf buffer = cista::serialize< c_MODE >( *m_player );
auto [error2, nwritten] = co_await boost::asio::async_write( socket, boost::asio::buffer( buffer ), c_tuple );
if ( buffer.size( ) != nwritten ) {
printf( "[serv] write failed: '%s'\n", error2.message( ).c_str( ) );
continue;
}
}
// TODO(alex): makeme Command::GetFullMap
//for ( auto row : m_level )
// for ( auto item : row )
// if ( item != nullptr ) item ...;
socket.shutdown( boost::asio::socket_base::shutdown_send );
}
} );

co_spawn_( [this]() mutable ->awaitable {
while ( !m_ioContext ->stopped( ) ) {
tcp::socket socket( co_await boost::asio::this_coro::executor );
auto [error1] = co_await socket.async_connect( { address_v4( c_host ), c_port }, c_tuple );
if ( error1 ) {
printf( "[reader] connect failed: '%s'\n", error1.message( ).c_str( ) );
continue;
}

const auto command = static_cast< command_t >( Command::Something );
const size_t commandSizeof = sizeof( command );
auto [error3, nwritten] = co_await boost::asio::async_write( socket, boost::asio::buffer( &command, commandSizeof ), c_tuple );
if ( nwritten != commandSizeof || error3 )
continue;

cista::byte_buf buffer;
while ( true ) {
std::uint8_t data[ 1024 ];
auto [error2, n] = co_await socket.async_read_some( boost::asio::buffer( data ), c_tuple );
if ( n )
std::copy( data, data + n, std::back_inserter( buffer ) );
if ( boost::asio::error::eof == error2 ) {
socket.shutdown( boost::asio::socket_base::shutdown_receive );
*m_player = *cista::deserialize< NetPlayer, c_MODE >( buffer );
m_ioContext ->stop( );
break;
}
if ( error2 ) {
printf( "[reader] read failed: '%s'\n", error2.message( ).c_str( ) );
break;
}
}
}
} );
if ( m_ioContext ->stopped( ) )
m_ioContext ->restart( );
m_ioContext ->run( );
__nop( );
}
// Initial rewrite
Expand Down
45 changes: 41 additions & 4 deletions src/net/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,56 @@ class NetPlayer : public Player {
};

class NetGame : public ::Game {
using awaitable = boost::asio::awaitable<void>;
using tcp = boost::asio::ip::tcp;
using address_v4 = boost::asio::ip::address_v4;
// TODO(alex): get from network
static constexpr auto c_MODE = cista::mode::NONE
| cista::mode::WITH_VERSION
| cista::mode::WITH_INTEGRITY
| cista::mode::DEEP_CHECK
;
static constexpr u_short c_port = 55555;
static constexpr address_v4::bytes_type c_host{ 127, 0, 0, 1 };

// aliases
template <typename... Args>
static constexpr auto as_tuple(Args&&... args) {
return boost::asio::experimental::as_tuple( std::forward< Args >( args )... );
}
template <typename... Args>
static constexpr auto as_single(Args&&... args) {
return boost::asio::experimental::as_single( std::forward< Args >( args )... );
}
static constexpr auto c_tuple = as_tuple( boost::asio::use_awaitable_t{ } );
static constexpr auto c_single = as_single( boost::asio::use_awaitable_t{ } );
static constexpr auto c_detached = boost::asio::detached;

template <typename F>
inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE( decltype( c_detached ),
typename detail::awaitable_signature<typename result_of<F()>::type>::type)
co_spawn_(F&& f) {
return co_spawn( m_ioContext ->get_executor( ), std::forward<F>(f), c_detached );
}

void update(Uint32 dt) override;

// TODO(alex): uglyAndFast, omitt `static`, delete in App::run
inline static std::shared_ptr< NetPlayer > m_player;

using command_t = uint32_t;
enum class Command : command_t { undefined
, GetFullMap
, ChangeMapElement
, Something
};
std::unique_ptr< boost::asio::io_context > m_ioContext;

public:
/**
* Allows multi-player
*/
NetGame(int players_count) :
// tmp
Game( 1 )
{}
NetGame(int players_count);

void draw() override;
};
Expand Down

0 comments on commit 3af688f

Please sign in to comment.