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;    }}
原创粉丝点击