⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ncbi_namedpipe.cpp

📁 ncbi源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    if (m_LSocket >= 0) {        if ( !x_CloseSocket(m_LSocket) ) {            ERR_POST(s_FormatErrorMessage                     ("Close", string("UNIX socket close() failed: ") +                      strerror(errno)));        }        m_LSocket = -1;    }    return status;}EIO_Status CNamedPipeHandle::Read(void* buf, size_t count, size_t* n_read,                                  const STimeout* timeout){    EIO_Status status = eIO_Closed;    try {        if ( !m_IoSocket ) {            throw string("Pipe is closed");        }        if ( !count ) {            // *n_read == 0            return eIO_Success;        }        status = SOCK_SetTimeout(m_IoSocket, eIO_Read, timeout);        if (status == eIO_Success) {            status = SOCK_Read(m_IoSocket, buf, count, n_read, eIO_ReadPlain);        }    }    catch (string& what) {        ERR_POST(s_FormatErrorMessage("Read", what));    }    return status;}EIO_Status CNamedPipeHandle::Write(const void* buf, size_t count,                                   size_t* n_written, const STimeout* timeout){    EIO_Status status = eIO_Closed;    try {        if ( !m_IoSocket ) {            throw string("Pipe is closed");        }        if ( !count ) {            // *n_written == 0            return eIO_Success;        }        status = SOCK_SetTimeout(m_IoSocket, eIO_Write, timeout);        if (status == eIO_Success) {            status = SOCK_Write(m_IoSocket, buf, count, n_written,                                eIO_WritePlain);        }    }    catch (string& what) {        ERR_POST(s_FormatErrorMessage("Write", what));    }    return status;}EIO_Status CNamedPipeHandle::Wait(EIO_Event event, const STimeout* timeout){    if ( m_IoSocket )        return SOCK_Wait(m_IoSocket, event, timeout);    ERR_POST(s_FormatErrorMessage("Wait", "Pipe is closed"));    return eIO_Closed;}EIO_Status CNamedPipeHandle::Status(EIO_Event direction) const{    if ( !m_IoSocket ) {        return eIO_Closed;    }    return SOCK_Status(m_IoSocket, direction);}bool CNamedPipeHandle::x_CloseSocket(int sock){    if (sock >= 0) {        for (;;) { // Close persistently            if (close(sock) == 0) {                break;            }            if (errno != EINTR) {                return false;            }        }    }    return true;}bool CNamedPipeHandle::x_SetSocketBufSize(int sock, size_t bufsize, int dir){    int       bs_old = 0;    int       bs_new = (int) bufsize;    socklen_t bs_len = (socklen_t) sizeof(bs_old);    if (getsockopt(sock, SOL_SOCKET, dir, &bs_old, &bs_len) == 0  &&        bs_new > bs_old) {        if (setsockopt(sock, SOL_SOCKET, dir, &bs_new, bs_len) != 0) {            return false;        }    }    return true;}#endif  /* NCBI_OS_UNIX | NCBI_OS_MSWIN */////////////////////////////////////////////////////////////////////////////////// CNamedPipe//CNamedPipe::CNamedPipe(void)    : m_PipeName(kEmptyStr), m_PipeBufSize(kDefaultPipeBufSize),      m_OpenTimeout(0), m_ReadTimeout(0), m_WriteTimeout(0){    m_NamedPipeHandle = new CNamedPipeHandle;}CNamedPipe::~CNamedPipe(void){    Close();    delete m_NamedPipeHandle;}EIO_Status CNamedPipe::Close(){    return m_NamedPipeHandle ? m_NamedPipeHandle->Close() : eIO_Unknown;}     EIO_Status CNamedPipe::Read(void* buf, size_t count, size_t* n_read){    if ( n_read ) {        *n_read = 0;    }    if (count  &&  !buf) {        return eIO_InvalidArg;    }    return m_NamedPipeHandle        ? m_NamedPipeHandle->Read(buf, count, n_read, m_ReadTimeout)        : eIO_Unknown;}EIO_Status CNamedPipe::Write(const void* buf, size_t count, size_t* n_written){    if ( n_written ) {        *n_written = 0;    }    if (count  &&  !buf) {        return eIO_InvalidArg;    }    return m_NamedPipeHandle        ? m_NamedPipeHandle->Write(buf, count, n_written, m_WriteTimeout)        : eIO_Unknown;}EIO_Status CNamedPipe::Wait(EIO_Event event, const STimeout* timeout){    switch (event) {    case eIO_Read:    case eIO_Write:    case eIO_ReadWrite:        break;    default:        return eIO_InvalidArg;    }    return m_NamedPipeHandle        ? m_NamedPipeHandle->Wait(event, timeout)        : eIO_Unknown;}EIO_Status CNamedPipe::Status(EIO_Event direction) const{    switch (direction) {    case eIO_Read:    case eIO_Write:        break;    default:        return eIO_InvalidArg;    }    return m_NamedPipeHandle        ? m_NamedPipeHandle->Status(direction)        : eIO_Unknown;}EIO_Status CNamedPipe::SetTimeout(EIO_Event event, const STimeout* timeout){    if (timeout == kDefaultTimeout) {        return eIO_Success;    }    switch ( event ) {    case eIO_Open:        m_OpenTimeout  = s_SetTimeout(timeout, &m_OpenTimeoutValue);        break;    case eIO_Read:        m_ReadTimeout  = s_SetTimeout(timeout, &m_ReadTimeoutValue);        break;    case eIO_Write:        m_WriteTimeout = s_SetTimeout(timeout, &m_WriteTimeoutValue);        break;    case eIO_ReadWrite:        m_ReadTimeout  = s_SetTimeout(timeout, &m_ReadTimeoutValue);        m_WriteTimeout = s_SetTimeout(timeout, &m_WriteTimeoutValue);        break;    default:        return eIO_InvalidArg;    }    return eIO_Success;}const STimeout* CNamedPipe::GetTimeout(EIO_Event event) const{    switch ( event ) {    case eIO_Open:        return m_OpenTimeout;    case eIO_Read:        return m_ReadTimeout;    case eIO_Write:        return m_WriteTimeout;    default:        ;    }    return kDefaultTimeout;}////////////////////////////////////////////////////////////////////////////////// CNamedPipeClient//CNamedPipeClient::CNamedPipeClient(void){    m_IsClientSide = true;}CNamedPipeClient::CNamedPipeClient(const string&   pipename,                                   const STimeout* timeout,                                    size_t          pipebufsize){    m_IsClientSide = true;    Open(pipename, timeout, pipebufsize);}EIO_Status CNamedPipeClient::Open(const string&    pipename,                                  const STimeout*  timeout,                                  size_t           pipebufsize){    if ( !m_NamedPipeHandle ) {        return eIO_Unknown;    }    s_AdjustPipeBufSize(&pipebufsize);    m_PipeName    = pipename;    m_PipeBufSize = pipebufsize;    SetTimeout(eIO_Open, timeout);    return m_NamedPipeHandle->Open(pipename, m_OpenTimeout, m_PipeBufSize);}EIO_Status CNamedPipeClient::Create(const string&, const STimeout*, size_t){    return eIO_Unknown;}////////////////////////////////////////////////////////////////////////////////// CNamedPipeServer//CNamedPipeServer::CNamedPipeServer(void){    m_IsClientSide = false;}CNamedPipeServer::CNamedPipeServer(const string&   pipename,                                   const STimeout* timeout,                                   size_t          pipebufsize){    m_IsClientSide = false;    Create(pipename, timeout, pipebufsize);}EIO_Status CNamedPipeServer::Create(const string&   pipename,                                    const STimeout* timeout,                                    size_t          pipebufsize){    if ( !m_NamedPipeHandle ) {        return eIO_Unknown;    }    s_AdjustPipeBufSize(&pipebufsize);    m_PipeName    = pipename;    m_PipeBufSize = pipebufsize;    SetTimeout(eIO_Open, timeout);    return m_NamedPipeHandle->Create(pipename, pipebufsize);}EIO_Status CNamedPipeServer::Open(const string&, const STimeout*, size_t){    return eIO_Unknown;}EIO_Status CNamedPipeServer::Listen(void){    return m_NamedPipeHandle        ? m_NamedPipeHandle->Listen(m_OpenTimeout)        : eIO_Unknown;}EIO_Status CNamedPipeServer::Disconnect(void){    return m_NamedPipeHandle        ? m_NamedPipeHandle->Disconnect()        : eIO_Unknown;}END_NCBI_SCOPE/* * =========================================================================== * $Log: ncbi_namedpipe.cpp,v $ * Revision 1000.5  2004/06/01 18:45:01  gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.26 * * Revision 1.26  2004/05/17 20:58:13  gorelenk * Added include of PCH ncbi_pch.hpp * * Revision 1.25  2004/03/22 17:03:20  ivanov * Replaced static member CNamedPipe::kDefaultPipeSize with enum values * for default and system pipe buffer size (by Denis Vakatov). * * Revision 1.24  2004/03/17 15:45:30  jcherry * Always record read status in Windows version of * CNamedPipeHandle::Read * * Revision 1.23  2004/03/10 16:09:00  jcherry * Reversed sense of test in unix version of CNamedPipeHandle::Listen * * Revision 1.22  2004/01/23 12:30:42  lavr * More explanatory stuff in posted error messages * * Revision 1.21  2003/12/02 19:16:20  ivanov * Fixed typo -- use x_errno instead of errno * * Revision 1.20  2003/12/02 17:50:46  ivanov * throw/catch strings exceptions instead char* * * Revision 1.19  2003/11/03 17:34:31  lavr * Print strerror() along with most syscall-related errors * * Revision 1.18  2003/10/24 16:52:38  lavr * Check RW bits before E bits in select() * * Revision 1.17  2003/09/25 04:41:22  lavr * Few minor style and performance changes * * Revision 1.16  2003/09/23 21:31:10  ucko * Fix typo (FD_SET for FD_ISSET in two adjacent lines) * * Revision 1.15  2003/09/23 21:07:06  lavr * Slightly reworked to fit in CConn_...Streams better; Wait() methods added * * Revision 1.14  2003/09/16 13:42:36  ivanov * Added deleting OS-specific pipe handle in the destructor * * Revision 1.13  2003/09/05 19:52:37  ivanov * + UNIX CNamedPipeHandle::SetSocketBufSize() * * Revision 1.12  2003/09/03 14:48:32  ivanov * Fix for previous commit * * Revision 1.11  2003/09/03 14:29:58  ivanov * Set r/w status to eIO_Success in the CNamedPipeHandle::Open/Create * * Revision 1.10  2003/09/02 19:51:17  ivanov * Fixed incorrect infinite timeout handling in the CNamedPipeHandle::Open() * * Revision 1.9  2003/08/28 16:03:05  ivanov * Use os-specific Status() function * * Revision 1.8  2003/08/25 14:41:22  lavr * Employ new k..Timeout constants * * Revision 1.7  2003/08/20 14:22:20  ivanov * Get rid of warning -- double variable declaration * * Revision 1.6  2003/08/19 21:02:12  ivanov * Other fix for error messages and comments. * * Revision 1.5  2003/08/19 20:52:45  ivanov * UNIX: Fixed a waiting method for socket connection in the * CNamedPipeHandle::Open() (by Anton Lavrentiev). Fixed some error * messages and comments. UNIX sockets can have a zero value. * * Revision 1.4  2003/08/19 14:34:43  ivanov * + #include <sys/time.h> for UNIX * * Revision 1.3  2003/08/18 22:53:59  vakatov * Fix for the platforms which are missing type 'socklen_t' * * Revision 1.2  2003/08/18 20:51:56  ivanov * Retry 'connect()' syscall if interrupted and allowed to restart * (by Anton Lavrentiev) * * Revision 1.1  2003/08/18 19:18:23  ivanov * Initial revision * * =========================================================================== */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -