tars源码漫谈第35篇------tc_fifo.h/tc_fifo.cpp(命名管道fifo操作封装)

     tc_fifo是对基本fifo操作的封装,  先看看mkfifo函数, man一下:

MKFIFO(1)                        User Commands                        MKFIFO(1)

NAME
       mkfifo - make FIFOs (named pipes)

SYNOPSIS
       mkfifo [OPTION]... NAME...

DESCRIPTION
       Create named pipes (FIFOs) with the given NAMEs.

       Mandatory arguments to long options are mandatory for short options too.

       -m, --mode=MODE
              set file permission bits to MODE, not a=rw - umask

       -Z     set the SELinux security context to default type

       --context[=CTX]
              like  -Z,  or  if  CTX is specified then set the SELinux or SMACK
              security context to CTX

       --help display this help and exit

       --version
              output version information and exit

AUTHOR
       Written by David MacKenzie.

REPORTING BUGS
       GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
       Report mkfifo translation bugs to <http://translationproject.org/team/>

COPYRIGHT
       Copyright © 2016 Free Software Foundation, Inc.  License GPLv3+: GNU GPL
       version 3 or later <http://gnu.org/licenses/gpl.html>.
       This  is  free  software:  you  are  free to change and redistribute it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       mkfifo(3)

       Full documentation at: <http://www.gnu.org/software/coreutils/mkfifo>
       or available locally via: info '(coreutils) mkfifo invocation'

      可见, 就是创建命名管道, 也可以读写。 来看看TC_Fifo对fifo的封装:

/**
 * Tencent is pleased to support the open source community by making Tars available.
 *
 * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
 *
 * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 
 * in compliance with the License. You may obtain a copy of the License at
 *
 * https://opensource.org/licenses/BSD-3-Clause
 *
 * Unless required by applicable law or agreed to in writing, software distributed 
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 
 * specific language governing permissions and limitations under the License.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>

#include "util/tc_fifo.h"

namespace tars
{

///
// 
TC_Fifo::TC_Fifo(bool bOwner) : _bOwner(bOwner), _enRW(EM_READ), _fd(-1)
{

}

TC_Fifo::~TC_Fifo()
{
    if (_bOwner) close();
}

void TC_Fifo::close()
{
    if (_fd >= 0) ::close(_fd); 
        
    _fd = -1;
}

int TC_Fifo::open(const std::string & sPathName, ENUM_RW_SET enRW, mode_t mode)
{
    _enRW       = enRW;
    _sPathName    = sPathName;

    if (_enRW != EM_READ && _enRW != EM_WRITE)
    {
        return -1;
    }

    if (::mkfifo(_sPathName.c_str(), mode) == -1 && errno != EEXIST)
    {
        return -1;
    }

    if (_enRW == EM_READ  && (_fd = ::open(_sPathName.c_str(), O_NONBLOCK|O_RDONLY, 0664)) < 0)
    {
        return -1;
    }

    if (_enRW == EM_WRITE && (_fd = ::open(_sPathName.c_str(), O_NONBLOCK|O_WRONLY, 0664)) < 0)
    {
        return -1;
    }

    return 0;
}

int TC_Fifo::read(char * szBuff, const size_t sizeMax)
{
    return ::read(_fd, szBuff, sizeMax);
}

int TC_Fifo::write(const char * szBuff, const size_t sizeBuffLen)
{
    if (sizeBuffLen == 0) return 0;

    return ::write(_fd, szBuff, sizeBuffLen);
}

}


       不得不说, fifo在实际开发中很少使用。 看了一下tars源码, 还是有使用的, 后面我们会继续涉及。

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值