Last active
November 11, 2018 13:59
-
-
Save pperehozhih/fe71912c01c47ea0525879a548b7938e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Connection { | |
public: | |
virtual void OnData(uint8_t* data, size_t size) = 0; | |
void Write(uint8_t* data, size_t size); | |
}; |
This file contains hidden or 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
class ConnectionImpl : public Connection { | |
struct StreamWrap { | |
using executor_type = StreamWrap; | |
template<typename MutableBufferSequence> | |
std::size_t read_some(const MutableBufferSequence & buffers) { | |
boost::system::error_code ec; | |
read_some(buffers, ec); | |
} | |
template<typename MutableBufferSequence> | |
std::size_t read_some(const MutableBufferSequence& buffer, boost::system::error_code& ec) { | |
if (boost::asio::buffer_size(buffers) == 0 || _len = 0) | |
return 0; | |
boost::asio::mutable_buffer data = boost::asio::detail::buffer_sequence_adapter<boost::asio::mutable_buffer, MutableBufferSequence>::first(buffers); | |
const size_t len = std::min(boost::asio::buffer_size(data), _len); | |
memcpy(boost::asio::buffer_cast<void*>(data), | |
_data, | |
len); | |
_len -= len; | |
if (_len == 0) { | |
_data = nullptr; | |
} else { | |
_data = &_data[len]; | |
} | |
return len; | |
} | |
template<typename ConstBufferSequence> | |
std::size_t write_some(const ConstBufferSequence & buffers) { | |
boost::system::error_code ec; | |
write_some(buffers, ec); | |
} | |
template<typename ConstBuffer> | |
std::size_t write_some(ConstBufferr& buffer, boost::system::error_code& ec) { | |
} | |
void SetData(uint8_t* data, size_t len) { | |
_data = data; | |
_len = len; | |
} | |
private: | |
uint8_t* _data = nullptr; | |
size_t _len = 0; | |
}; | |
public: | |
ConnectionImpl():_ws(StreamWrap()){ | |
} | |
virtual void OnData(uint8_t* data, size_t size) override { | |
_data = data; | |
_len = size; | |
next_layer().SetData(data, size); | |
std::size_t read_size = _len; | |
while(_len > 0) { | |
boost::asio::streambuf buffer; | |
read_size = _ws->read(buffer, ec); | |
if (read_size > 0) | |
DoSomething(buffer); | |
} | |
} | |
private: | |
boost::beast::websocket::stream<StreamWrap> _ws; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment