Skip to content

Instantly share code, notes, and snippets.

@Jackarain
Jackarain / ssl_stream.hpp
Created November 25, 2023 08:06
基于 c++ 20 的 boost asio ssl_stream 实现
//
// ssl_stream.hpp
// ~~~~~~~~~~~~~~
//
// Copyright (c) 2023 Jack (jack dot wgm at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
@Jackarain
Jackarain / print_data_len.cpp
Created November 22, 2023 15:36
print_data_len
void print_data_len(const std::vector<uint8_t>& data, uint16_t target_len = 0x7FFF)
{
if (data.size() < 4) {
std::cerr << "数据长度小于4,无法转换为 uint16_t。" << std::endl;
return;
}
std::cout << "print len: " << data.size() << "\n";
uint16_t p1 = 0;
@Jackarain
Jackarain / logging.hpp
Last active August 16, 2022 14:47
一个现代c++实现的日志库
//
// Copyright (C) 2019 Jack.
//
// Author: jack
// Email: jack.wgm at gmail dot com
//
#pragma once
#include <version>
#include <codecvt>
//
// yield_cancellation_slot_bind.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2019 Jack (jack dot wgm at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
@Jackarain
Jackarain / async_connect.hpp
Last active September 3, 2022 13:06
Happy Eyeballs support
//
// async_connect.hpp
// ~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2019 Jack (jack dot wgm at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
@Jackarain
Jackarain / yield_cancellation_slot_bind.hpp
Created March 7, 2022 04:22
用于 asio 的 basic_yield_context 绑定 cancellation 信号
//
// yield_cancellation_slot_bind.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2019 Jack (jack dot wgm at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
@Jackarain
Jackarain / command key map
Last active January 11, 2022 20:12
Command autohotkey 键映射
#c::
Send,^{c}
Return
#v::
Send,^{v}
Return
#s::
Send,^{s}
@Jackarain
Jackarain / multipart.hpp
Last active September 13, 2021 06:25
This is a parser for multipart written in C++
//
// Copyright (C) 2021 Jack.
//
// Author: jack
// Email: jack.wgm at gmail dot com
//
#pragma once
#include <boost/exception/all.hpp>
@Jackarain
Jackarain / asio.md
Last active March 16, 2019 18:09
asio中associated_allocator和associated_executor的精巧设计

在asio中,设计了associated_allocator和associated_executor用于用户管理handler统一分配内存和统一调用,通过adl实现hook结构。

如用户提交一个异步操作,此时用户的handler需要保存在asio的内部op队列中,待操作完成,asio将从op队列中取出该op(不一定要直接取,像iocp中,可以绑定在OVERLAPPED结构中,事件完成,直接可以OVERLAPPED中拿到),然后通过op中的handler回调用户,通知异步操作完成。

其中,asio使用了associated_allocator和associated_executor这2个设计,在asio内部存取用户handler到op队列的时候用到,以实现用户定义的allocator和executor的调用。

下面是关于iocp(默认proactor模型更易于分析)中async_receive异步处理的分析(async_send类似),非关键部分代码省略,只例出与主题相关的代码。

// 文件 boost/boost/asio/detail/win_iocp_operation.hpp
@Jackarain
Jackarain / anycall.cpp
Created March 12, 2019 06:09
anycall.cpp
template <typename Handler, typename Request, typename Reply>
class type_erasure_handler
{
public:
type_erasure_handler(Handler&& handler)
: handler_(std::forward<Handler>(handler))
{}
~type_erasure_handler()
{}