Linux pipe 封装实现
来源:互联网 发布:已备案域名转入阿里云 编辑:程序博客网 时间:2024/06/12 00:21
头文件duye_pipe.h
/*************************************************************************************** * @copyright (c) 2013-2100, Technology Co., LTD. All Right Reserved.**************************************************************************************//*** @file duye_pipe.h* @version * @brief * @autho duye* @date 2013-11-15* @note ** 1. 2013-11-15 Created this file* */#pragma once#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <duye_type.h>namespace duye {/** * @brief pipe base class, be inherited by WritePipe and ReadPipe class */class Pipe {public: Pipe() {} virtual ~Pipe() {} /** * @brief Open the pipe * @param [in] pipeName : the GPipe name * @return true/false * @note */ virtual bool open(const int8* pipeName) = 0;protected: /** * @brief Open the pipe * @param [in] pipeName : the GPipe name * @param [in] mode : open mode * @return true/false * @note */ bool orgOpen(const int8* pipeName, const int32 mode);protected: /** * @brief pipe descriptor */ int32 m_pipefd;};/** * @brief Be used to write GPipe */class WritePipe : public Pipe {public: WritePipe() {} virtual ~WritePipe() {} /** * @brief Open the pipe * @param [in] GPipeName : the GPipe name * @return true/false * @note */ virtual bool open(const int8* pipeName); /** * @brief Write data to pipe * @param [in] data : write data * @param [in] length : data length * @return size/-1 * @note */ int64 write(const int8* data, const uint64 length);private: /** * @brief prevent copying * @note */ WritePipe(const WritePipe&); /** * prevent copying * @note */ void operator=(const WritePipe&);};/** * @brief be used to read GPipe */class ReadPipe : public Pipe {public: ReadPipe() {} virtual ~ReadPipe() {} /** * @brief open pipe * @param [in] pipeName : GPipe name * @return true/false * @note */ virtual bool open(const int8* pipeName); /** * @brief read data from pipe * @param [out] buffer : output buffer * @param [in] size : output buffer size * @return size/-1 * @note */ int64 read(int8* buffer, const uint64 size); private: /** * @brief prevent copying * @note */ ReadPipe(const ReadPipe&); /** * @brief prevent copying * @note */ void operator=(const ReadPipe&); };}
cpp文件duye_pipe.cpp
/************************************************************************************** * @copyright (c) 2013-2100, Technology Co., LTD. All Right Reserved.**************************************************************************************//*** @file duye_pipe.cpp* @version * @brief * @author* @date 2013-12-1* @note ** 2. 2014-06-21 duye move to gohoop * 1. 2013-12-01 Created this file* */#include <duye_pipe.h>namespace duye {bool Pipe::orgOpen(const int8* pipeName, const int32 mode) { if (pipeName == NULL) { return false; } if (access(pipeName, F_OK) == -1) { if (mkfifo(pipeName, 0777) != 0) { return false; } } m_pipefd = ::open(pipeName, mode); if (m_pipefd == -1) { return false; } return true; }bool WritePipe::open(const int8* pipeName) { return orgOpen(pipeName, O_WRONLY | O_NONBLOCK); }int64 WritePipe::write(const int8* data, const uint64 length) { if (m_pipefd == -1) { return -1; } if (data == NULL) { return -1; } if (length == 0) { return -1; } int64 bytes = -1; if ((bytes = ::write(m_pipefd, data, length)) == -1) { return -1; } return bytes; }bool ReadPipe::open(const int8* pipeName) { return orgOpen(pipeName, O_RDONLY); }int64 ReadPipe::read(int8* buffer, const uint64 size) { if (m_pipefd == -1) { return -1; } if (buffer == NULL) { return -1; } if (size == 0) { return -1; } int32 bytes = -1; if ((bytes = ::read(m_pipefd, buffer, size)) == -1) { return -1; } return bytes; }}
阅读全文
0 0
- Linux pipe 封装实现
- linux内核管道pipe实现详解
- Linux pipe
- Linux Pipe
- Linux pipe
- Linux pipe
- Linux pipe
- Linux pipe
- linux pipe
- linux pipe
- linux pipe
- Linux epoll 实现封装
- Linux semaphore 封装实现
- Linux thread 封装实现
- Linux进程间通信分类 以及 pipe的原理实现
- Linux进程间通信分类 以及 pipe的原理实现
- Linux进程间通信分类 以及 pipe的原理实现
- Linux进程间通信分类 以及 pipe的原理实现
- HDU 6052 To my boyfriend 分情况处理 DP 容斥
- C# 托管资源和非托管资源
- Retrofit,Rxjava网络请求是 baseUrl 报错 IllegalArgumentException
- CSS尺寸单位
- 1、MYSQL
- Linux pipe 封装实现
- mysql取字段逗号分隔的第一个
- 115个Java面试题和答案
- Python日期的加减等操作
- JVM常用调优参数
- LCA转RMQ学习、
- TCP通讯例程一:固定数据长度的简单数据服务器/客户端通讯
- 2017中国互联网安全大会-安全训练营之iOS技术及.NET培训
- Cesium中的Entity API和Primitive API的区别(1)