Skip to content

Instantly share code, notes, and snippets.

@Jackarain
Jackarain / TcpConnection.ts
Last active June 28, 2025 07:48
TcpConnection.ts 将 tcp 连接转换成能 await 读写的类,这在一些异步交互协议的读取中十分有效,从而避免 on('data') 的使用
import * as net from 'net';
// TCP 连接类,仅处理发送和接收
export class TcpConnection {
// Socket 由外部传入
private socket: net.Socket;
// 存储接收到的数据
private buffer: Buffer;
// 读取数据限制, 0 表示无限制
private readLimit: number;
@Jackarain
Jackarain / jsonrpc.hpp
Last active June 18, 2025 04:45
Json rpc 实现
//
// jsonrpc.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 / index.ts
Last active June 3, 2025 14:30
btc/eth/tron 地址转换
/*
* 需要安装以下依赖:
* yarn add ethers bitcoinjs-lib ecpair tiny-secp256k1 bs58
* yarn add @types/bitcoinjs-lib @types/ecpair @types/tiny-secp256k1 @types/bs58
* yarn add ts-node typescript
*/
import { ethers } from 'ethers';
import { ECPairFactory } from 'ecpair';
import bitcoin from 'bitcoinjs-lib';
@Jackarain
Jackarain / stackoverflow.cpp
Last active December 17, 2024 15:56
async_simple 的爆栈问题测试代码
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "async_simple/coro/Lazy.h"
#include "async_simple/coro/SyncAwait.h"
// asio_coro_util.hpp 在 demo_example 下, 这里仅仅为了调用 AsioCallbackAwaiter 这个简单的 Awaiter 而已.
#include "asio_coro_util.hpp"
@Jackarain
Jackarain / awaitable.hpp
Last active September 29, 2024 13:52
c++ 20 协程简易实现
#pragma once
#include <coroutine>
#include <functional>
#include <type_traits>
#if defined(DEBUG) || defined(_DEBUG)
#include <unordered_set>
std::unordered_set<void*> global_crors;
#endif
@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)
//