Skip to content

Commit

Permalink
[#89] - 🎉 some first network stuff ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
aconstlink committed Apr 30, 2024
1 parent 2097999 commit c9dd12f
Show file tree
Hide file tree
Showing 19 changed files with 1,187 additions and 3 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ list( APPEND subdirs
"controls"
"msl"

"network"
"graphics"
"audio"

Expand Down
3 changes: 3 additions & 0 deletions memory/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ set( sources
"arena.hpp"

"observer.hpp"

"utility/uint8_msb_stream.hpp"
"utility/uint8_split_merge.hpp"
)

motor_vs_src_dir( sources )
Expand Down
2 changes: 1 addition & 1 deletion memory/manager/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void_ptr_t manager::alloc( size_t const sib, char_cptr_t purpose, bool_t const m
if ( _observer->on_alloc( sib, managed ) > 500000 )
{
_observer->swap_and_clear() ;
std::cout << "[MOTOR_MEMORY_OBSERVER] : swap and clear" << std::endl ;
//std::cout << "[MOTOR_MEMORY_OBSERVER] : swap and clear" << std::endl ;
}
#endif

Expand Down
103 changes: 103 additions & 0 deletions memory/utility/uint8_msb_stream.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

#pragma once

#include "../typedefs.h"
#include "uint8_split_merge.hpp"

namespace motor
{
namespace memory
{
/// streams certain numbers to an array set in the ctor.
/// @note not thread safe
class uint8_msb_stream
{
typedef uint8_msb_stream this_t ;
typedef this_t & this_ref_t ;

uint8_t * _array ;
size_t _max_len ;

size_t _cur_pos = size_t(0) ;

public:

uint8_msb_stream( uint8_t * ar, size_t max_len ) noexcept : _array(ar), _max_len(max_len) {}

public:

this_ref_t operator << ( uint8_t const n ) noexcept
{
_array[_cur_pos++] = n ;
return *this ;
}

this_ref_t operator << ( uint16_t const n ) noexcept
{
motor::memory::uint8_msb_split::of( n, &_array[_cur_pos] ) ;
_cur_pos += 2 ;
return *this ;
}

this_ref_t operator << ( uint32_t const n ) noexcept
{
motor::memory::uint8_msb_split::of( n, &_array[_cur_pos] ) ;
_cur_pos += 4 ;
return *this ;
}

this_ref_t operator << ( uint64_t const n ) noexcept
{
motor::memory::uint8_msb_split::of( n, &_array[_cur_pos] ) ;
_cur_pos += 8 ;
return *this ;
}

};

class uint8c_msb_stream
{
typedef uint8c_msb_stream this_t ;
typedef this_t & this_ref_t ;

uint8_t const * _array ;
size_t _max_len ;

size_t _cur_pos = size_t(0) ;

public:

uint8c_msb_stream( uint8_t const * ar, size_t max_len ) noexcept : _array(ar), _max_len(max_len) {}

public:

this_ref_t operator >> ( uint8_t & n ) noexcept
{
n = _array[_cur_pos++] ;
return *this ;
}

this_ref_t operator >> ( uint16_t & n ) noexcept
{
n = motor::memory::uint8_msb_merge::uint16( &_array[_cur_pos] ) ;
_cur_pos += 2 ;
return *this ;
}

this_ref_t operator >> ( uint32_t & n ) noexcept
{
n = motor::memory::uint8_msb_merge::uint32( &_array[_cur_pos] ) ;
_cur_pos += 4 ;
return *this ;
}

this_ref_t operator >> ( uint64_t & n ) noexcept
{
n = motor::memory::uint8_msb_merge::uint64( &_array[_cur_pos] ) ;
_cur_pos += 8 ;
return *this ;
}

};
}
}
91 changes: 91 additions & 0 deletions memory/utility/uint8_split_merge.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#pragma once

#include "../typedefs.h"

namespace motor
{
namespace memory
{
/// the most significant byte is written
/// first into the stream
struct uint8_msb_split
{
/// split a 16 bit number into 2 bytes
/// @todo SIMD/Vector operation
/// @precondition to must have at least 2 byte space
static void_t of( uint16_t const number, uint8_t * to ) noexcept
{
// vec2(number) >> vec2(8,0) & vec2(255)
*(to+0) = (number >> 8) & 255 ;
*(to+1) = (number >> 0) & 255 ;
}

/// split a 32 bit number into 4 bytes
/// @todo SIMD/Vector operation
/// @precondition to must have at least 4 byte space
static void_t of( uint32_t const number, uint8_t * to ) noexcept
{
// vec4(number) >> vec4(24,16,8,0) & vec4(255)
*(to+0) = (number >> 24) & 255 ;
*(to+1) = (number >> 16) & 255 ;
*(to+2) = (number >> 8) & 255 ;
*(to+3) = (number >> 0) & 255 ;
}

/// split a 64 bit number into 8 bytes
/// @todo SIMD/Vector operation
/// @precondition to must have at least 8 byte space
static void_t of( uint64_t const number, uint8_t * to ) noexcept
{
// vec4(number) >> vec4(56,48,40,32) & vec4(255)
// vec4(number) >> vec4(24,16,8,0) & vec4(255)
*(to+0) = (number >> 56) & 255 ;
*(to+1) = (number >> 48) & 255 ;
*(to+2) = (number >> 40) & 255 ;
*(to+3) = (number >> 32) & 255 ;
*(to+4) = (number >> 24) & 255 ;
*(to+5) = (number >> 16) & 255 ;
*(to+6) = (number >> 8) & 255 ;
*(to+7) = (number >> 0) & 255 ;
}
};

struct uint8_msb_merge
{
static uint16_t uint16( uint8_t const * from ) noexcept
{
uint16_t const at_1 = *(from+1) ;
uint16_t const at_0 = *(from+0) ;

return uint16_t( (at_0 << 8) | (at_1 << 0) ) ;
}

static uint32_t uint32( uint8_t const * from ) noexcept
{
uint32_t const at_3 = *(from+3) ;
uint32_t const at_2 = *(from+2) ;
uint32_t const at_1 = *(from+1) ;
uint32_t const at_0 = *(from+0) ;

return uint32_t( (at_0 << 24) | (at_1 << 16) | (at_2 << 8) | (at_3 << 0) ) ;
}

static uint64_t uint64( uint8_t const * from ) noexcept
{
uint64_t const at_7 = *(from+7) ;
uint64_t const at_6 = *(from+6) ;
uint64_t const at_5 = *(from+5) ;
uint64_t const at_4 = *(from+4) ;
uint64_t const at_3 = *(from+3) ;
uint64_t const at_2 = *(from+2) ;
uint64_t const at_1 = *(from+1) ;
uint64_t const at_0 = *(from+0) ;

return uint64_t(
(at_0 << 56) | (at_1 << 48) | (at_2 << 40) | (at_3 << 32) |
(at_4 << 24) | (at_5 << 16) | (at_6 << 8) | (at_7 << 0) ) ;
}

};
}
}
44 changes: 44 additions & 0 deletions network/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@


set( SOURCES ${SOURCES}
"api.h"
"typedefs.h"

"imodule.h"
"system.h"
"system.cpp"
"ipv4_address.hpp"

"convert.hpp"

"socket/connection_info.hpp"
"socket/tcp_socket.h"
"socket/tcp_socket.cpp"
)

motor_vs_src_dir( SOURCES )

add_library( ${cur_lib_name} ${MOTOR_LIBRARY_BUILD_TYPE} ${SOURCES} )
add_library( ${cur_alias_name} ALIAS ${cur_lib_name} )


target_link_libraries( ${cur_lib_name}
PUBLIC motor::concurrent
PUBLIC motor::log
PUBLIC motor::memory
PUBLIC motor::std
PUBLIC motor::core
)

###########################################################
# SECTION: Build Tree
###########################################################

#motor_export( ${cur_lib_name} )

###########################################################
# SECTION: Install
###########################################################

install_headers( "${sources}" "include/${PROJECT_NAME}/${cur_lib_name}" )
install_library( ${cur_lib_name} ${PROJECT_NAME}-targets )
49 changes: 49 additions & 0 deletions network/convert.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once


#include "typedefs.h"
#include <motor/memory/utility/uint8_split_merge.hpp>

namespace motor
{
namespace network
{
/// register -> network byte order(nbo)
struct to_nbo
{
static void_t of( uint16_t const number, uint8_t * to )
{
motor::memory::uint8_msb_split::of( number, to ) ;
}

static void_t of( uint32_t const number, uint8_t * to )
{
motor::memory::uint8_msb_split::of( number, to ) ;
}

static void_t of( uint64_t const number, uint8_t * to )
{
motor::memory::uint8_msb_split::of( number, to ) ;
}
} ;

/// network byte order(nbo) -> register
struct from_nbo
{
static uint16_t data_16( const uint8_t * from )
{
return motor::memory::uint8_msb_merge::uint16( from ) ;
}

static uint32_t data_32( const uint8_t * from )
{
return motor::memory::uint8_msb_merge::uint32( from ) ;
}

static uint64_t data_64( const uint8_t * from )
{
return motor::memory::uint8_msb_merge::uint64( from ) ;
}
} ;
}
}
54 changes: 54 additions & 0 deletions network/imodule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

#pragma once

#include "api.h"
#include "typedefs.h"

#include "ipv4_address.hpp"

namespace motor{ namespace network
{
class iserver_handler
{
public:

virtual motor::network::accept_result on_accept( motor::network::client_id_t const,
motor::network::ipv4::address_cref_t ) noexcept = 0 ;

virtual void_t on_close( motor::network::client_id_t const ) noexcept = 0 ;

virtual motor::network::receive_result on_receive(
motor::network::client_id_t const, byte_cptr_t, size_t const ) noexcept = 0 ;

virtual motor::network::transmit_result on_send(
motor::network::client_id_t const, byte_cptr_t & buffer, size_t & num_sib ) noexcept = 0 ;
};
motor_typedef( iserver_handler ) ;

struct create_tcp_server_info
{
motor::string_t name ;
motor::network::ipv4::binding_point_t bp ;
iserver_handler_mtr_safe_t handler ;
};
motor_typedef( create_tcp_server_info ) ;

struct create_tcp_client_info
{
motor::string_t name ;
motor::network::ipv4::binding_point_t bp ;
send_funk_t send_handler ;
recv_funk_t recv_handler ;
};
motor_typedef( create_tcp_client_info ) ;

class MOTOR_NETWORK_API imodule
{
public:

virtual socket_id_t create_tcp_client( motor::network::create_tcp_client_info_rref_t ) noexcept = 0 ;
virtual socket_id_t create_tcp_server( motor::network::create_tcp_server_info_rref_t ) noexcept = 0 ;

};
motor_typedef( imodule ) ;
} }
Loading

0 comments on commit c9dd12f

Please sign in to comment.