
C++
WanJunCoder
make the world a better place!
展开
-
TCP客户端非阻塞connect,EPOLL异步响应
废话不多说,直接上代码下面展示一些 内联代码片。#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h>#include <iostream&g原创 2020-10-18 23:50:39 · 1709 阅读 · 1 评论 -
PcapPlusPlus TCP数据结构
直接上代码 /** * @struct tcphdr * Represents an TCP protocol header */#pragma pack(push,1) struct tcphdr { /** Source TCP port */ uint16_t portSrc; /** Destination TCP port */ uint16_t por...原创 2019-12-12 14:58:01 · 862 阅读 · 0 评论 -
C语言使用sem_t同步线程的启动
场景描述:在程序开始阶段需要开启多个线程进行后续的处理,需要同步开启线程(即thread1启动完成后再启动thread2)sem_t pthread_sem; // 信号量,同步线程的启动顺序void ProcessQuery(){ cout<<"QueryHandle, Thread "<<iCoreID<<" Create Successful...原创 2019-12-03 22:10:35 · 1170 阅读 · 0 评论 -
google benchmark安装
sudo apt install cmakegit clone https://github.com/google/benchmark.gitgit clone https://github.com/google/googletest.git benchmark/googletestmkdir build && cd buildcmake -DCMAKE_BUILD_TY...原创 2019-05-08 12:04:23 · 1597 阅读 · 0 评论 -
muduo net模块 channel解析
源码// 绑定在 loop 上一个 fd 的管道,针对事件使用回调函数// Copyright 2010, Shuo Chen. All rights reserved.// http://code.google.com/p/muduo///// Use of this source code is governed by a BSD-style license// that c...原创 2019-04-23 12:57:21 · 262 阅读 · 0 评论 -
muduo LogStream解析
数字转字符串template<typename T>size_t convert(char buf[], T value){ T i = value; char* p = buf;// 201 分别取数字 1 0 2 do { int lsd = static_cast<int>(i % 10); i /= 10; *p++...原创 2019-04-02 23:42:15 · 325 阅读 · 0 评论 -
FIONREAD 判断 socket有多少数据可读
Ioctl(sockfd, FIONREAD, &npend); /* check FIONREAD support */检查 sockfd 表示的文件描述符中有多少数据可以读取原创 2019-04-12 00:25:24 · 1545 阅读 · 0 评论 -
clang Thread Safety Analysis安全线程分析
IntroductionClang Thread Safety Analysis is a C++ language extension which warns about potential race conditions in code. The analysis is completely static (i.e. compile-time); there is no run-time o...翻译 2019-03-27 11:55:23 · 264 阅读 · 0 评论 -
C++ 原子操作
n++类type __sync_fetch_and_add(type *ptr, type value, …); // m+ntype __sync_fetch_and_sub(type *ptr, type value, …); // m-ntype __sync_fetch_and_or(type *ptr, type value, …); // m|ntype __sync_fet...原创 2019-03-26 23:53:03 · 513 阅读 · 0 评论 -
muduo AsyncLogging 解析(异步日志)
异步日志原理需要了解其中使用的几个类使用方法muduo::detail::FixedBuffer<muduo::detail::kLargeBuffer> Buffer;std::vector<std::unique_ptr<Buffer>> BufferVector; BufferVector::value_type BufferPtr;其中 Bu...原创 2019-04-03 21:39:22 · 965 阅读 · 0 评论 -
epoll server实例
ServerEpollepoll [enhancement poll]this is a async server base on epoll at Linux这是一个在Linux系统上基于epoll机制的异步服务器class EpollService 主要功能就是,创建一个后台服务器监听指定端口号,并且接受来自客户端的连接请求。EpollService 使用两个 epoll 机制来完成...原创 2019-03-22 22:12:59 · 1267 阅读 · 0 评论 -
muduo Mutex.h Condition.h 解析
mutex 配合 线程idclass CAPABILITY("mutex") MutexLock : noncopyable{ public: MutexLock() : holder_(0) { MCHECK(pthread_mutex_init(&mutex_, NULL)); } // 只有未上锁的状态可以析构 ~MutexLock() ...原创 2019-03-26 13:09:09 · 350 阅读 · 0 评论 -
C++ 大小端判断 endian
直接上代码typedef union uEndianTest { struct { bool flittle_endian; bool fill[3]; }; long value;} EndianTest;static const EndianTest __Endian_Test__ = { (long)1 };const bool...原创 2019-05-18 23:37:28 · 1081 阅读 · 0 评论 -
eventfd、timerfd_create使用
使用场景在 pipe 仅用于发出事件信号的所有情况下,都可以使用 eventfd 取而代之。exampleint createEventfd(){ int evtfd = ::eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); if (evtfd < 0) { LOG_SYSERR << "Failed in eventfd...原创 2019-05-30 20:05:26 · 5390 阅读 · 0 评论 -
pcap入门教程
第一件事就是需要了解 pcap 应用的格式1.发现哪个网卡需要我们去sniff 嗅探linux下也许是eth0BSD下可能是 xl1我们可以将设备定义为string然后打开该设备,或者我们可以让pcap自己查找然后提供我们网卡的名称2.初始化pcap ,实际上就是我们告诉pcap 去嗅探哪个网卡设备。如果我们想的话,可以同时嗅探多个网卡设备。我们使用文件描述符去区分这些网卡,就像我们打...翻译 2019-08-14 14:46:26 · 3906 阅读 · 0 评论 -
TCP协议笔记
数据结构typedef struct tcp_hdr{ u_short sport:16; // 源端口号 u_short dport:16; // 目标端口号 u_int seq:32; // 序列值 u_int ack:32; // ...原创 2019-07-23 10:49:12 · 144 阅读 · 0 评论 -
协议分析-libpcap的安装与使用
下载源代码http://www.tcpdump.org/release/通过 tar zxvf libpcap-1.0.0.tar.gz 解压文件。进入文件目录后依次执行./configuremake && make install配置过程中缺少什么就使用 sudo apt install xx 安装实例代码代码的作用是读取一个 .pcap 文件,并解析其中的T...原创 2019-07-24 09:21:18 · 396 阅读 · 0 评论 -
gperftools安装和使用
gperftools 下载和安装github 上找到源码,下载并解压64位机器需要先安装 apt-get install libunwind8-dev./autogen.sh./configuremake -j8# 安装到系统文件夹sudo make installldconfig 刷新动态库文件CPU使用方法一gcc [...] -o myprogram -lprofil...原创 2019-07-11 09:55:05 · 5092 阅读 · 0 评论 -
获得真正的线程id
使用目的在linux后台服务器系统中,首先需要实现的就是日志库。在使用日志库的过程中会发现,当引入多线程后,为了更好的排查错误和整理业务逻辑需要对多线程的日志进行区分。一个很好的方法就是在每条日志中打印相对应的线程id简单实现#include <unistd.h>long int id = sys_call(SYS_gettid);...原创 2019-07-02 17:49:00 · 358 阅读 · 0 评论 -
Linux大小端转换实现
实现#include <byteswap.h>#include <stdint.h>/** * @brief 8字节类型的字节序转化 */template<class T>typename std::enable_if<sizeof(T) == sizeof(uint64_t), T>::typebyteswap(T value)...原创 2019-07-02 14:39:01 · 3716 阅读 · 0 评论 -
linux套接字地址结构
IPv4#include <netinet/in.h>struct in_addr{ in_addr s_addr; // 32bit};struct sockaddr_in{ uint8_t sin_len; sa_family_t sin_family; in_port_t sin_port; // 16bit struct in_addr ...原创 2019-06-14 15:30:06 · 246 阅读 · 0 评论 -
获取文件信息
struct statfd_(::open(filename.c_str(), O_RDONLY | O_CLOEXEC))fd_ 用于打开一个文件,作为 file descriptor struct stat statbuf; // 获取 文件的信息 存放在 statbuf 中 if (::fstat(fd_, &statbuf) == 0) ...原创 2019-06-14 12:34:36 · 571 阅读 · 0 评论 -
muduo中eventloop、channel、fd解析
最近在研究muduo源代码,因为之前的项目内部的逻辑与muduo类似,不过使用的是libevent完成内核的构造,muduo使用了自己写的poll和epoll方式完成内部的io复用。之前的项目与muduo使用的策略一致,使用了主线程专门用于接收tcp连接,连接后的connection发送到多个子线程中来支持高并发。eventloop.h这个类是muduo的核心,头文件如下(已做注释):///...原创 2019-05-30 20:41:13 · 533 阅读 · 0 评论 -
ubuntu clang llvm使用
安装 clang、llvmsudo apt-get install llvmsudo apt-get install clang配置默认的C++编译sudo update-alternatives --config c++There are 2 choices for the alternative c++ (providing /usr/bin/c++).Selection ...原创 2019-03-26 10:11:54 · 1853 阅读 · 0 评论 -
epoll_create和epoll_create1踩坑,导致cpu100%
epoll_create 和 epoll_create1的区别不知道但是在使用的过程中发现:运行后使用htop没有什么异常;但是使用epoll_create1后CPU的使用率直接标到了99%,严重的影响性能。...原创 2019-03-19 10:55:06 · 6876 阅读 · 4 评论 -
grpc c++ async server 源码解读
#include &lt;memory&gt;#include &lt;iostream&gt;#include &lt;string&gt;#include &lt;thread&gt;#include &lt;grpcpp/grpcpp.h&gt;#include &lt;grpc/support/log.h&原创 2019-02-03 18:31:09 · 2567 阅读 · 0 评论 -
C++ grpc简单实例解析,源码分析
proto 文件syntax = &amp;amp;amp;quot;proto3&amp;amp;amp;quot;;option java_multiple_files = true;option java_package = &amp;amp;amp;quot;io.grpc.examples.helloworld&amp;amp;amp;quot;;option java_outer_classname = &原创 2019-02-02 15:07:27 · 5325 阅读 · 1 评论 -
protobuf 中 message默认的静态实例
最近在研究protobuf ,运行官网给的实例的时候发现了一个有趣的事情。以下是代码// wanjun.person.protosyntax="proto3";package wanjun;// proto3 移除了 requiredmessage Person{ string name = 1; int32 age = 2;};// dont forget t...原创 2019-01-27 15:02:55 · 1641 阅读 · 0 评论 -
STL std::vector::insert 详解
详细手册地址iterator insert( iterator pos, const T& value );iterator insert( const_iterator pos, const T& value );// 最普遍的使用方式,pos 找到要插入的位置,第二个参数 value 表示要插入的数据,// 自从C++11开始后使用 const_iterator , ...原创 2019-01-21 23:55:21 · 13569 阅读 · 0 评论 -
string.c_str() 和 string.data() 的区别
运行后的结果如图由此可见 string.data() 和 string.c_str() 没有区别原创 2019-01-21 21:44:29 · 12422 阅读 · 10 评论 -
protobuf 中 repeated fields 细节
proto2 or proto3proto2 中还有 repeated 可选,在proto3 中则已经被摒弃并且默认格式为pack。实现细节 报文格式想想一下你有以下的 message 格式message Test4 { repeated int32 d = 4 [packed=true];}Now let’s say you construct a Test4, providi...原创 2019-01-25 23:38:47 · 4444 阅读 · 0 评论 -
protobuf 中使用的Varint技术
Varint 是一种紧凑的表示数字的方法。它用一个或多个字节来表示一个数字,值越小的数字使用越少的字节数。这能减少用来表示数字的字节数。比如对于 int32 类型的数字,一般需要 4 个 byte 来表示。但是采用 Varint,对于很小的 int32 类型的数字,则可以用 1 个 byte 来表示。当然凡事都有好的也有不好的一面,采用 Varint 表示法,大的数字则需要 5 个 byte 来...原创 2019-01-25 10:42:34 · 2187 阅读 · 0 评论 -
protobuf 安装及使用
首先下载 githhub 上的压缩文件链接: https://pan.baidu.com/s/15_E9V75LitClIHbAvkcIsw 提取码: h27s 复制这段内容后打开百度网盘手机App,操作更方便哦安装tar -xzf protobuf-2.1.0.tar.gz cd protobuf-2.1.0 ./configure --prefix=$INSTALL_DIR mak...原创 2019-01-24 22:50:42 · 323 阅读 · 0 评论 -
std::future和std::shared_future区别
std::future使用可以处理所有在线程间数据转移的必要同步,但是std::future模型独享同步结果的所有权。并且通过 get() 函数,一次性的获取数据,让并发访问变的毫无意义。你的并发代码没有办法让多个线程等待同一个事件。std::shared_futurestd::shared_future 可以完成让多个线程的等待std::promise<std::string>...原创 2019-01-19 23:12:32 · 2380 阅读 · 0 评论 -
C++ lock_guard 和 unique_lock区别
lock_guard更加的高效,是一种RAII的体现。只能在析构函数中解锁。unique_lock更加的灵活,但是比 lock_guard 更消耗资源,可以自己随时 unlock 解锁原创 2019-01-13 00:16:35 · 1594 阅读 · 0 评论 -
pthread.h 线程部分 源码解读
int pthread_create (pthread_t *__restrict __newthread, const pthread_attr_t *__restrict __attr, void *(*__start_routine) (void *)创建线程void pthread_exit (void *__retval)退出线程int pthread...原创 2019-02-18 15:40:11 · 1140 阅读 · 0 评论 -
volatil 注意点
C/C++ Volatile关键词的第三个特性:”顺序性”,能够保证Volatile变量间的顺序性,编译器不会进行乱序优化。Volatile变量与非Volatile变量的顺序,编译器不保证顺序,可能会进行乱序优化。同时,C/C++ Volatile关键词,并不能用于构建happens-before语义,因此在进行多线程程序设计时,要小心使用volatile,不要掉入volatile变量的使用陷阱之...原创 2019-02-18 16:09:55 · 220 阅读 · 0 评论 -
muduo CurrentThread 解析
丧心病狂的C++优化 inline int tid() { if (__builtin_expect(t_cachedTid == 0, 0)) { cacheTid(); } return t_cachedTid; }__builtin_expect 宏 的作用:其实__builtin_expect这个宏定义变量本身的值是没有产生影响...原创 2019-03-25 21:46:23 · 997 阅读 · 0 评论 -
muduo type.h 解析 C++类型转换技巧
初始化// 简化了 memset 的使用inline void memZero(void* p, size_t n){ memset(p, 0, n);}隐式转化// 用于在继承关系中, 子类指针转化为父类指针;隐式转换// inferred 推断,因为 from type 可以被推断出来,所以使用方法和 static_cast 相同template<typename ...原创 2019-03-25 18:45:11 · 250 阅读 · 0 评论 -
linux下pthread_cleanup_push和pthread_cleanup_pop
pthread_cleanup_push的使用该函数为当前的线程注册一个清理回调函数,清理函数在使用push的时候压入线程的一个栈中。那么该清理函数何时会被调用呢?记住,pthread_cleanup_push必须和pthread_cleanup_pop同时使用。当push后,在线程退出前使用pop,便会调用清理函数。pthread_cleanup_pop有一个参数,如果在最后调用pop...原创 2019-03-16 22:00:44 · 1314 阅读 · 0 评论