Table of contents
1.
Introduction
2.
Asio
3.
Asio Pipe
3.1.
basic_writable_pipe
3.1.1.
Member Functions
3.2.
basic_readable_pipe
3.2.1.
Member Function
3.3.
connect_pipe
3.4.
readable_pipe
3.5.
writable_pipe
4.
Example of Asio Pipe
4.1.
Output
4.1.1.
Explanation
5.
Frequently Asked Questions
5.1.
What is the use of the basic_channel class?
5.2.
What are the advantages of the Asio library?
5.3.
What does asynchronous mean?
5.4.
What is the use of the Asio library?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Asio-Pipes

Author Kanak Rana
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hello there, Ninjas!! Have you heard about Asio C++ Library? Do you know what Asio-Pipes are? If not, then don't worry. We will help you to clear all your doubts. Asio is for those who want to know how to connect a language to networking and have input and output of connections in C++.

asio pipes

In this article, we will discuss what Asio-Pipes are. We will also discuss how we can use them. But first, just have a brief introduction about Asio, and we recommend you freshen up your C++ fundamental knowledge.

Asio

Asio is an open-source asynchronous input-output and cross-platform C++ library. It is designed for input, output, and networking. It was developed in 2003 by Christopher M. Kohlhoff and was released under Boost Software License. It provides an asynchronous model to the developers using a modern approach of C++. Many platforms, like Windows, macOS, Linux, etc., support it.

c++ asio

Applications ranging from mobile apps to the quickest stock markets in the world are using Asio. There are two types of Asio: 

  • Boost 
  • Non-Boost

So the difference between Boost and Non-Boost or normal Asio are mentioned below:

Difference between Boost and Non-Boost

Let's now understand the Asio pipes. Also read about, Abstract Data Types in C++

Asio Pipe

The pipe is a method where we try to use the output of one program process as input to another. Asio pipes are a means of communicating between processes using the Asio library in C++. They allow you to send data between processes using a pipe, which is a unidirectional data channel that can be used to pass information from the output of one process to the input of another. Asio pipes can be used to create a pipeline of processes, where the output of one process is fed as the input to the next, and so on. This can be useful for creating complex processing flows or for enabling interprocess communication. There are five ways by which we can provide different types of functionality.

  1. basic_writable_pipe
  2. basic_readable_pipe
  3. connect_pipe
  4. readable_pipe
  5. writable_pipe

basic_writable_pipe

It provides different functionality to Asio Pipes and has the same types as basic_readable_pipe. The basic_writable_pipe class provides a wrapper over Pipe functionality. 

Member Functions

These are some functions, and some of them are common for every type of Asio Pipe.

member function

basic_readable_pipe

basic_readable_pipe provides different types of functionality in the Asio Pipes. The basic_readable_pipe class wraps around pipe functionality.

There are four types of basic_readable_pipe:

⭕rebind_executor rebinds the types of Pipe to another executor.

⭕executor_type is used for the types of executors that are associated or linked with the object.

⭕lowest_layer_type is like the other name of basic_readable_pipe, which is the lowest layer.

⭕native_handle_type is used to represent Pipes like 

typedef implementation_defined native_handle_type;

Member Function

These are some member functions used in basic_readable_pipes in Asio Pipe: 

member function

connect_pipe

An anonymous pipe is used to connect two Asio Pipe ends in the connecting pipe. They have two parameters:

⭕read_end to read the end of the pipe.

⭕write_end to write the end of the pipe.

The way to include connecting pipes is as follows-

template<
    typename Executor1,
    typename Executor2>
void connect_pipe(
    basic_readable_pipe< Executor1 > & read_end,
    basic_writable_pipe< Executor2 > & write_end,
    asio::error_code & ec);

Where ec is set to indicate what error occurred, if any.

readable_pipe

A readable_pipe is a type of name assigned to a basic_readable_pipe. A typical definition of typedef for the readable_pipe is

typedef basic_readable_pipe readable_pipe;

Note: All types and member functions are the same as basic_readable_pipe.

writable_pipe

A writable_pipe is a type of name assigned to a basic_writable_pipe. A typical definition of typedef for the writable_pipe is

typedef basic_writable_pipe writable_pipe;

Note:  All types and member functions are the same as basic_writable_pipe.

Example of Asio Pipe

This is an implementation of Asio Pipe, which will help you understand the Pipe concept more clearly.

#include<iostream>
#include<asio.hpp>
#include<asio/basic_readable_pipe.hpp>
#include<asio/basic_writable_pipe.hpp>
#include <asio/error_code.hpp>

using asio::error_code;
using namespace std;

int main() {

    asio::io_context io;
    
    cout << "Using writable_pipe class" << endl;
    asio::writable_pipe pipe1(io);
    asio::writable_pipe::native_handle_type native_pipe1 = pipe1.native_handle();
    std:cout << "Printing executor " << pipe1.get_executor() << endl << "The native representation of pipe " << native_pipe1 <<"\n" << endl;

    cout << "Using readable_pipe class" << endl;
    asio::readable_pipe pipe(io);
    asio::readable_pipe::native_handle_type native_pipe = pipe.native_handle();
    cout << "Printing executor" << pipe.get_executor() << endl << "The native representation of pipe " << native_pipe << endl;


    return 0;
}

Output

output

Explanation

In the above example, we created an instance(io) of asio::io_context. Then we passed this instance(io) to the pipe1 instance of asio::writable_pipe. After this, we called the native_handle function(native_handle()) on the asio::writable_pipe::native_handle_type. We used this function to obtain the Native Pipe representation. Similarly, we worked on the readable_pipe.

Also see, Application of Oops

Frequently Asked Questions

What is the use of the basic_channel class?

The basic_channel class is used for sending messages across different parts of the same application.

What are the advantages of the Asio library?

Asio is used in projects such as the WebSocketPP library and the DDT3 remote debugger for the Lua programming language. Asio is free and open-source software licensed under the Boost Software License, and it runs on Linux, Windows, macOS, and FreeBSD.

What does asynchronous mean?

Asynchronous means that a process is independent of the execution of any other process. The execution is not blocked for operations if more than one is in progress.

What is the use of the Asio library?

Asio helps in doing network programming using C++ language. It allows the processing of data asynchronously by providing asynchronous I/O models.

Conclusion

We have discussed Asio Pipes in this article. We have covered Asio, Asio pipes, basic_readable_pipe, basic_writable_pipe, etc. We hope this article helps you enhance your knowledge of Asio pipe. We recommend you read some more articles on Asio.

We hope you have gained a better understanding of these topics now! Are you planning to ace the interviews of reputed product-based companies like AmazonGoogleMicrosoft, and more? Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Happy learning!

Live masterclass