asio

通俗地解释一下RPC框架

南笙酒味 提交于 2021-02-20 08:26:04
什么是 RPC ? RPC (Remote Procedure Call)即远程过程调用,是分布式系统常见的一种通信方法,已经有 40 多年历史。当两个物理分离的子系统需要建立逻辑上的关联时,RPC 是牵线搭桥的常见技术手段之一。除 RPC 之外,常见的多系统数据交互方案还有分布式消息队列、HTTP 请求调用、数据库和分布式缓存等。 其中 RPC 和 HTTP 调用是没有经过中间件的,它们是端到端系统的直接数据交互。HTTP 调用其实也可以看成是一种特殊的 RPC,只不过传统意义上的 RPC 是指长连接数据交互,而 HTTP 一般是指即用即走的短链接。 RPC 在我们熟知的各种中间件中都有它的身影。Nginx/Redis/MySQL/Dubbo/Hadoop/Spark/Tensorflow 等重量级开源产品都是在 RPC 技术的基础上构建出来的,我们这里说的 RPC 指的是广义的 RPC,也就是分布式系统的通信技术。RPC 在技术中的地位好比我们身边的空气,它无处不在,但是又有很多人根本不知道它的存在。 本地过程调用 RPC就是要像调用本地的函数一样去调远程函数。在研究RPC前,我们先看看本地调用是怎么调的。假设我们要调用函数Multiply来计算lvalue * rvalue的结果: 1 int Multiply(int l, int r) { 2 int y = l * r;

boost::asio::ip::tcp实现网络通信的小例子

ⅰ亾dé卋堺 提交于 2021-02-18 02:15:14
同步方式: Boost.Asio是一个跨平台的网络及底层IO的C++编程库,它使用现代C++手法实现了统一的异步调用模型。 头文件 #include <boost/asio.hpp> 名空间 using namespace boost::asio; ASIO库能够使用TCP、UDP、ICMP、串口来发送/接收数据,下面先介绍TCP协议的读写操作 对于读写方式,ASIO支持同步和异步两种方式,首先登场的是同步方式,下面请同步方式自我介绍一下: 大家好!我是同步方式! 我的主要特点就是执着!所有的操作都要完成或出错才会返回,不过偶的执着被大家称之为阻塞,实在是郁闷~~(场下一片嘘声),其实这样 也是有好处的,比如逻辑清晰,编程比较容易。 在服务器端,我会做个socket交给acceptor对象,让它一直等客户端连进来,连上以后再通过这个socket与客户端通信, 而所有的通信都是以阻塞方式进行的,读完或写完才会返回。 在客户端也一样,这时我会拿着socket去连接服务器,当然也是连上或出错了才返回,最后也是以阻塞的方式和服务器通信。 有人认为同步方式没有异步方式高效,其实这是片面的理解。在单线程的情况下可能确实如此,我不能利用耗时的网络操作这段时间做别的事情,不是好的统筹方法。不过这个问题可以通过多线程来避免,比如在服务器端让其中一个线程负责等待客户端连接

How to eliminate crashes when destroying boost::asio entities on fly?

主宰稳场 提交于 2021-02-10 09:22:33
问题 Note!!! The question is for people who are experts in boost::asio library . Unfortunately, I cannot do the code more compact, It contains a minimum amount to describe the problem. The code is example, artificially created. Places where it crashes known and described in comments, it is designed to illustrate the crashes!!! NO need any help with debugging of the code... The question is about how to design the asio server, not about - where it crashes!!! This example is close to "chat server"

C++ boost asynchronous timer to run in parallel with program

匆匆过客 提交于 2021-02-04 06:28:48
问题 Note: This is for C++98 I am trying to develop a simple timer/counter that runs in the background of my main program. I haven't used asynchronous timers before, and I have been trying to follow the boost tutorials on how to do this, but they still seem to block my main function. I've slightly modified Timer.3 from the boost website to experiment. Essentially, with the program below what I want to do is: Run main Execute testRun() which counts to 5 At the same time testRun() is counting, print

How would I include asio library using CMake?

社会主义新天地 提交于 2021-01-28 22:20:20
问题 I am trying to work on a project for a class and I want to use CMake to build the project. My current project looks like |-bin |-CMakeLists.txt |-include |-asio-1.12.2 |-chat_message.hpp |-chat_message.cpp |-CMakeLists.txt |-src |-Server.cpp although my Server.cpp needs asio.hpp that is in /include/asio-1.12.2/include . The professor has a makefile that compiles it with the flags -DASIO_STANDALONE -Wall -O0 -g -std=c++11 -I./include -I./include/asio-1.12.2/include . My CMakeLists files look

Exception throw in boost::asio::spawn not caught by try catch

≡放荡痞女 提交于 2021-01-28 05:14:31
问题 In this convoluted example, two for loops are started by boost::asio::spawn() asynchronously. The first for loop prints an odd number every 1000us and the second one prints an even number every 1000us. I expect the output to be something like 1 2 3 4 5 6 and then the 'Throw an error' message should be printed to stderr by the call to cerr. However, the exception is actually thrown in loop.run() so it is not caught by the try catch block. Can someone point out how to properly catch the runtime

input delay with PortAudio callback and ASIO sdk

徘徊边缘 提交于 2020-12-05 12:47:51
问题 I'm trying to get the input from my guitar to be played through my computer using the portaudio library and the ASIO sdk. I have been following some of the tutorials on the official website to get the basics set up. Currently I got it working so that portaudio is listening to the right input and output device and I have the callback setup to just output the input and do nothing with it like this: static int paTestCallback(const void *inputBuffer, void *outputBuffer, unsigned long

evpp网络库代码分析(二)

江枫思渺然 提交于 2020-08-11 21:04:11
开局一张图! 上图是盗用自《Linux多线程服务端编程,使用muduo C++网络库》一书6.6.2章节(以及下面的时序图也是盗用该书的图)。该图列举出大部分常用的网络编程模型,当然了,这里并没有列出Boost.Asio的proactor模式。其中表中的“互通”是指多个客户端(连接)间是否能方便地交换数据,如chat聊天程序。我们的evpp库实际上是用到了“方案9”,方案9的时序图如下: 可以看出,每一个线程有一个EventLoop处理事件。这种方案是典型的“one loop per thread”流程,有一个“主EventLoop”负责accept连接,然后把连接通过round-robin(轮询调度)挂到底下的多个“子EventLoop”中(EventLoopThreadPool),每个连接都是由一个“子EventLoop”完成的,能保证请求的顺序性,也可以充分利用CPU,防止出现一个reactor的处理能力饱和,而且EventLoop线程池线程数量固定,不会因为连接数过多到达临界点(线程太多导致操作系统线程调度不过来)而性能下降! 另外还有一种网络编程模型比较常用,就是“方案8”,这个模型在muduo中有现成方案,而在evpp中需要自己实现,时序图如下: 这种方案,只有一个EventLoop,但它把计算密集的部分(decode、compute、encode