-
Notifications
You must be signed in to change notification settings - Fork 792
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3812 from clemahieu/block_deserializer
Extracting nano::bootstrap::block_deserializer class
- Loading branch information
Showing
7 changed files
with
225 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include <nano/lib/blocks.hpp> | ||
#include <nano/node/bootstrap/block_deserializer.hpp> | ||
#include <nano/node/socket.hpp> | ||
#include <nano/secure/buffer.hpp> | ||
|
||
nano::bootstrap::block_deserializer::block_deserializer () : | ||
read_buffer{ std::make_shared<std::vector<uint8_t>> () } | ||
{ | ||
} | ||
|
||
void nano::bootstrap::block_deserializer::read (nano::socket & socket, callback_type const && callback) | ||
{ | ||
debug_assert (callback); | ||
read_buffer->resize (1); | ||
socket.async_read (read_buffer, 1, [this_l = shared_from_this (), &socket, callback = std::move (callback)] (boost::system::error_code const & ec, std::size_t size_a) { | ||
if (ec) | ||
{ | ||
callback (ec, nullptr); | ||
return; | ||
} | ||
if (size_a != 1) | ||
{ | ||
callback (boost::asio::error::fault, nullptr); | ||
return; | ||
} | ||
this_l->received_type (socket, std::move (callback)); | ||
}); | ||
} | ||
|
||
void nano::bootstrap::block_deserializer::received_type (nano::socket & socket, callback_type const && callback) | ||
{ | ||
nano::block_type type = static_cast<nano::block_type> (read_buffer->data ()[0]); | ||
if (type == nano::block_type::not_a_block) | ||
{ | ||
callback (boost::system::error_code{}, nullptr); | ||
return; | ||
} | ||
auto size = nano::block::size (type); | ||
if (size == 0) | ||
{ | ||
callback (boost::asio::error::fault, nullptr); | ||
return; | ||
} | ||
read_buffer->resize (size); | ||
socket.async_read (read_buffer, size, [this_l = shared_from_this (), size, type, callback = std::move (callback)] (boost::system::error_code const & ec, std::size_t size_a) { | ||
if (ec) | ||
{ | ||
callback (ec, nullptr); | ||
return; | ||
} | ||
if (size_a != size) | ||
{ | ||
callback (boost::asio::error::fault, nullptr); | ||
return; | ||
} | ||
this_l->received_block (type, std::move (callback)); | ||
}); | ||
} | ||
|
||
void nano::bootstrap::block_deserializer::received_block (nano::block_type type, callback_type const && callback) | ||
{ | ||
nano::bufferstream stream{ read_buffer->data (), read_buffer->size () }; | ||
auto block = nano::deserialize_block (stream, type); | ||
callback (boost::system::error_code{}, block); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#pragma once | ||
|
||
#include <nano/lib/blocks.hpp> | ||
|
||
#include <boost/system/error_code.hpp> | ||
|
||
#include <memory> | ||
#include <vector> | ||
|
||
namespace nano | ||
{ | ||
class block; | ||
class socket; | ||
namespace bootstrap | ||
{ | ||
/** | ||
* Class to read a block-type byte followed by a serialised block from a stream. | ||
* It is typically used to used to read a series of block-types and blocks terminated by a not-a-block type. | ||
*/ | ||
class block_deserializer : public std::enable_shared_from_this<nano::bootstrap::block_deserializer> | ||
{ | ||
public: | ||
using callback_type = std::function<void (boost::system::error_code, std::shared_ptr<nano::block>)>; | ||
|
||
block_deserializer (); | ||
/** | ||
* Read a type-prefixed block from 'socket' and pass the result, or an error, to 'callback' | ||
* A normal end to series of blocks is a marked by return no error and a nullptr for block. | ||
*/ | ||
void read (nano::socket & socket, callback_type const && callback); | ||
|
||
private: | ||
/** | ||
* Called by read method on receipt of a block type byte. | ||
* The type byte will be in the read_buffer. | ||
*/ | ||
void received_type (nano::socket & socket, callback_type const && callback); | ||
|
||
/** | ||
* Called by received_type when a block is received, it parses the block and calls the callback. | ||
*/ | ||
void received_block (nano::block_type type, callback_type const && callback); | ||
|
||
std::shared_ptr<std::vector<uint8_t>> read_buffer; | ||
}; | ||
} | ||
} |
Oops, something went wrong.