📄 ncbi_socket_cxx.cpp
字号:
/* * =========================================================================== * PRODUCTION $Log: ncbi_socket_cxx.cpp,v $ * PRODUCTION Revision 1000.3 2004/06/01 18:45:29 gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R6.24 * PRODUCTION * =========================================================================== *//* $Id: ncbi_socket_cxx.cpp,v 1000.3 2004/06/01 18:45:29 gouriano Exp $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Anton Lavrentiev * * File Description: * C++ wrappers for the C "SOCK" API (UNIX, MS-Win, MacOS, Darwin) * Implementation of out-of-line methods * */#include <ncbi_pch.hpp>#include <connect/ncbi_socket.hpp>#include <limits.h> // for PATH_MAX#if defined(NCBI_OS_MSWIN) && !defined(PATH_MAX)# define PATH_MAX 512 // will actually use less than 32 chars#endifBEGIN_NCBI_SCOPE/////////////////////////////////////////////////////////////////////////////// CSocket:://CSocket::CSocket(void) : m_Socket(0), m_IsOwned(eTakeOwnership), o_timeout(0), r_timeout(0), w_timeout(0), c_timeout(0){ return;}CSocket::CSocket(const string& host, unsigned short port, const STimeout* timeout, ESwitch log) : m_IsOwned(eTakeOwnership), r_timeout(0), w_timeout(0), c_timeout(0){ const char* x_host = host.c_str(); if (timeout && timeout != kDefaultTimeout) { oo_timeout = *timeout; o_timeout = &oo_timeout; } else o_timeout = 0; if (SOCK_CreateEx(x_host, port, o_timeout, &m_Socket, 0, 0, log) != eIO_Success) { m_Socket = 0; }}CSocket::~CSocket(void){ if (m_Socket && m_IsOwned != eNoOwnership) SOCK_Close(m_Socket);}void CSocket::Reset(SOCK sock, EOwnership if_to_own, ECopyTimeout whence){ if (m_Socket && m_IsOwned != eNoOwnership) SOCK_Close(m_Socket); m_Socket = sock; m_IsOwned = if_to_own; if ( sock ) { if (whence == eCopyTimeoutsFromSOCK) { const STimeout* timeout; timeout = SOCK_GetTimeout(sock, eIO_Read); if ( timeout ) { rr_timeout = *timeout; r_timeout = &rr_timeout; } else r_timeout = 0; timeout = SOCK_GetTimeout(sock, eIO_Write); if ( timeout ) { ww_timeout = *timeout; w_timeout = &ww_timeout; } else w_timeout = 0; timeout = SOCK_GetTimeout(sock, eIO_Close); if ( timeout ) { cc_timeout = *timeout; c_timeout = &cc_timeout; } else c_timeout = 0; } else { SOCK_SetTimeout(sock, eIO_Read, r_timeout); SOCK_SetTimeout(sock, eIO_Write, w_timeout); SOCK_SetTimeout(sock, eIO_Close, c_timeout); } }}EIO_Status CSocket::Connect(const string& host, unsigned short port, const STimeout* timeout, ESwitch log){ if ( m_Socket ) { if (SOCK_Status(m_Socket, eIO_Open) != eIO_Closed) return eIO_Unknown; SOCK_Close(m_Socket); } const char* x_host = host.c_str(); if (timeout != kDefaultTimeout) { if ( timeout ) { oo_timeout = *timeout; o_timeout = &oo_timeout; } else o_timeout = 0; } EIO_Status status = SOCK_CreateEx(x_host, port, o_timeout, &m_Socket, 0, 0, log); if (status == eIO_Success) { SOCK_SetTimeout(m_Socket, eIO_Read, r_timeout); SOCK_SetTimeout(m_Socket, eIO_Write, w_timeout); SOCK_SetTimeout(m_Socket, eIO_Close, c_timeout); } else m_Socket = 0; return status;}EIO_Status CSocket::Reconnect(const STimeout* timeout){ if (timeout != kDefaultTimeout) { if ( timeout ) { oo_timeout = *timeout; o_timeout = &oo_timeout; } else o_timeout = 0; } return m_Socket ? SOCK_Reconnect(m_Socket, 0, 0, o_timeout) : eIO_Closed;}EIO_Status CSocket::Close(void){ if ( !m_Socket ) return eIO_Success; EIO_Status status = m_IsOwned != eNoOwnership ? SOCK_CloseEx(m_Socket, 0) : eIO_Success; return status;}EIO_Status CSocket::SetTimeout(EIO_Event event, const STimeout* timeout){ if (timeout == kDefaultTimeout) return eIO_Success; switch (event) { case eIO_Open: if ( timeout ) { oo_timeout = *timeout; o_timeout = &oo_timeout; } else o_timeout = 0; break; case eIO_Read: if ( timeout ) { rr_timeout = *timeout; r_timeout = &rr_timeout; } else r_timeout = 0; break; case eIO_Write: if ( timeout ) { ww_timeout = *timeout; w_timeout = &ww_timeout; } else w_timeout = 0; break; case eIO_ReadWrite: if ( timeout ) { rr_timeout = *timeout; ww_timeout = *timeout; r_timeout = &rr_timeout; w_timeout = &ww_timeout; } else { r_timeout = 0; w_timeout = 0; } break; case eIO_Close: if ( timeout ) { cc_timeout = *timeout; c_timeout = &cc_timeout; } else c_timeout = 0; break; default: return eIO_InvalidArg; } return m_Socket ? SOCK_SetTimeout(m_Socket, event, timeout) : eIO_Success;}const STimeout* CSocket::GetTimeout(EIO_Event event) const{ switch (event) { case eIO_Open: return o_timeout; case eIO_Read: return r_timeout; case eIO_Write: return w_timeout; case eIO_ReadWrite: if ( !r_timeout ) return w_timeout; if ( !w_timeout ) return r_timeout; return ((unsigned long) r_timeout->sec*1000000 + r_timeout->usec > (unsigned long) w_timeout->sec*1000000 + w_timeout->usec) ? w_timeout : r_timeout; case eIO_Close: return c_timeout; default: break; } return kDefaultTimeout;}EIO_Status CSocket::Read(void* buf, size_t size, size_t* n_read, EIO_ReadMethod how){ if ( m_Socket ) return SOCK_Read(m_Socket, buf, size, n_read, how); if ( n_read ) *n_read = 0; return eIO_Closed;}EIO_Status CSocket::Write(const void* buf, size_t size, size_t* n_written, EIO_WriteMethod how){ if ( m_Socket ) return SOCK_Write(m_Socket, buf, size, n_written, how); if ( n_written ) *n_written = 0; return eIO_Closed;}void CSocket::GetPeerAddress(unsigned int* host, unsigned short* port, ENH_ByteOrder byte_order) const{ if ( !m_Socket ) { if ( host ) *host = 0; if ( port ) *port = 0; } else SOCK_GetPeerAddress(m_Socket, host, port, byte_order);}string CSocket::GetPeerAddress(void) const{ char buf[PATH_MAX + 1]; if (m_Socket && SOCK_GetPeerAddressString(m_Socket, buf, sizeof(buf)) != 0) { return string(buf); } return "";}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -