📄 ncbi_socket.h
字号:
/* * =========================================================================== * PRODUCTION $Log: ncbi_socket.h,v $ * PRODUCTION Revision 1000.4 2004/04/12 17:06:00 gouriano * PRODUCTION PRODUCTION: UPGRADED [CATCHUP_003] Dev-tree R6.49 * PRODUCTION * =========================================================================== */#ifndef CONNECT___NCBI_SOCKET__H#define CONNECT___NCBI_SOCKET__H/* $Id: ncbi_socket.h,v 1000.4 2004/04/12 17:06:00 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: Denis Vakatov * * File Description: * Plain portable TCP/IP socket API for: UNIX, MS-Win, MacOS * Platform-specific library requirements: * [UNIX ] -DNCBI_OS_UNIX -lresolv -lsocket -lnsl * [MSWIN] -DNCBI_OS_MSWIN wsock32.lib * [MacOS] -DNCBI_OS_MAC NCSASOCK -- BSD-style socket emulation lib * ********************************* * Generic: * * SOCK, LSOCK * * SOCK_AllowSigPipeAPI * * SOCK_InitializeAPI * SOCK_ShutdownAPI * * Listening socket (handle LSOCK): * * LSOCK_Create * LSOCK_Accept * LSOCK_Close * * I/O Socket (handle SOCK): * * SOCK_Create[Ex] (see also LSOCK_Accept) * SOCK_CreateOnTop[Ex] * SOCK_Reconnect * SOCK_Shutdown * SOCK_Close * SOCK_Wait * SOCK_Poll * SOCK_SetTimeout * SOCK_GetReadTimeout * SOCK_GetWriteTimeout * SOCK_Read (including "peek" and "persistent read") * SOCK_PushBack * SOCK_Status * SOCK_Write * SOCK_Abort * SOCK_GetPeerAddress * SOCK_GetPeerAddressString * * SOCK_SetReadOnWriteAPI * SOCK_SetReadOnWrite * SOCK_SetInterruptOnSignalAPI * SOCK_SetInterruptOnSignal * SOCK_SetReuseAddressAPI * SOCK_SetReuseAddress * * Datagram Socket: * * DSOCK_Create[Ex] * DSOCK_Bind * DSOCK_Connect * DSOCK_WaitMsg * DSOCK_SendMsg * DSOCK_RecvMsg * DSOCK_WipeMsg * DSOCK_SetBroadcast * * Socket classification: * * SOCK_IsDatagram * SOCK_IsClientSide * SOCK_IsServerSide * * Data logging: * * SOCK_SetDataLoggingAPI * SOCK_SetDataLogging * * Auxiliary: * * SOCK_gethostname * SOCK_ntoa * SOCK_HostToNetShort * SOCK_HostToNetLong * SOCK_NetToHostShort * SOCK_NetToHostLong * SOCK_gethostbyname * SOCK_gethostbyaddr * */#if defined(NCBISOCK__H)# error "<ncbisock.h> and <ncbi_socket.h> must never be #include'd together"#endif#include <connect/ncbi_core.h>/** @addtogroup Sockets * * @{ */#ifdef __cplusplusextern "C" {#endif/****************************************************************************** * TYPEDEFS & MACROS *//* Network and host byte order enumeration type */typedef enum { eNH_HostByteOrder, eNH_NetworkByteOrder} ENH_ByteOrder;/* Forward declarations of the hidden socket internal structure, and * their upper-level handles to use by the LSOCK_*() and SOCK_*() API */struct LSOCK_tag; /* listening socket: internal storage */typedef struct LSOCK_tag* LSOCK; /* listening socket: handle */struct SOCK_tag; /* socket: internal storage */typedef struct SOCK_tag* SOCK; /* socket: handle *//****************************************************************************** * Multi-Thread safety * * If you are using this API in a multi-thread application, and there is * more than one thread using this API, it is safe to call SOCK_InitializeAPI() * explicitly in the beginning of your main thread, before you run any other * threads, and to call SOCK_ShutdownAPI() after all threads are exited. * * As soon as the API is initialized it becomes relatively MT-safe, however * you still must not operate with the same LSOCK or SOCK objects from * different threads simultaneously. * * A MUCH BETTER WAY of dealing with this issue is to provide your own MT * locking callback (see CORE_SetLOCK in "ncbi_core.h"). This will also * guarantee the proper MT protection should some other SOCK functions * start to access any static data in the future. *//****************************************************************************** * Error & Data Logging * * NOTE: Use CORE_SetLOG() from "ncbi_core.h" to setup the log handler. *//* By default ("log" == eDefault, which is eOff), the data is not logged. * To start logging the data, call this func with "log" == eOn. * To stop logging the data, call this func with "log" == eOff. * Return prior setting. */extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetDataLoggingAPI(ESwitch log );/* Control the data logging for socket "sock" individually. * To reset to the global default behavior (as set by SOCK_SetDataLoggingAPI), * call this function with "log" == eDefault. * Return prior setting. */extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetDataLogging(SOCK sock, ESwitch log );/****************************************************************************** * I/O restart on signals *//* By default ("on_off" == eDefault,eOff), I/O is restartable if interrupted. * Return prior setting. */extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetInterruptOnSignalAPI(ESwitch on_off );/* Control sockets individually. eDefault causes the use of global API flag. * Return prior setting. */extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetInterruptOnSignal(SOCK sock, ESwitch on_off );/****************************************************************************** * Address reuse: EXPERIMENTAL and may be removed in the upcoming releases! *//* By default ("on_off" == eDefault,eOff), address is not marked for reuse * in SOCK, but is always reused for LSOCK. * Return prior setting. */extern NCBI_XCONNECT_EXPORT ESwitch SOCK_SetReuseAddressAPI(ESwitch on_off );/* Control sockets individually (Note: only boolean value is available here.) * No return value is available. */extern NCBI_XCONNECT_EXPORT void SOCK_SetReuseAddress(SOCK sock, int/*bool*/ on_off);/****************************************************************************** * API Initialization and Shutdown/Cleanup *//* */extern NCBI_XCONNECT_EXPORT const STimeout*SOCK_SetSelectInternalRestartTimeout(const STimeout* timeout);/* By default (on UNIX platforms) the SOCK API functions automagically call * "signal(SIGPIPE, SIG_IGN)" on initialization. To prohibit this feature, * you must call SOCK_AllowSigPipeAPI() before you call any other * function from the SOCK API. */extern NCBI_XCONNECT_EXPORT void SOCK_AllowSigPipeAPI(void);/* Initialize all internal/system data & resources to be used by the SOCK API. * NOTE: * You can safely call it more than once; just, all calls after the first * one will have no result. * NOTE: * Usually, SOCK API does not require an explicit initialization -- as it is * guaranteed to initialize itself automagically, in one of API functions, * when necessary. Yet, see the "Multi Thread safety" remark above. * NOTE: * This call, when used for the very first time in the application, enqueues * SOCK_ShutdownAPI() to be called upon application exit on plaftorms that * provide this functionality. In any case, the application can opt for * explicit SOCK_ShutdownAPI() call when it is done with all sockets. */extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_InitializeAPI(void);/* Cleanup; destroy all internal/system data & resources used by the SOCK API. * ATTENTION: no function from the SOCK API should be called after this call! * NOTE: you can safely call it more than once; just, all calls after the first * one will have no result. */extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_ShutdownAPI(void);/****************************************************************************** * LISTENING SOCKET *//* [SERVER-side] Create and initialize the server-side(listening) socket * (socket() + bind() + listen()) * NOTE: on some systems, "backlog" can be silently limited down to 128 (or 5). */extern NCBI_XCONNECT_EXPORT EIO_Status LSOCK_CreateEx(unsigned short port, /* [in] the port to listen at */ unsigned short backlog, /* [in] maximal # of pending connections */ LSOCK* lsock, /* [out] handle of the created listening socket */ ESwitch log /* [in] whether to log activity (inherited) */ );extern NCBI_XCONNECT_EXPORT EIO_Status LSOCK_Create(unsigned short port, /* [in] the port to listen at */ unsigned short backlog, /* [in] maximal # of pending connections */ LSOCK* lsock /* [out] handle of the created listening socket */ );/* [SERVER-side] Accept connection from a client. * NOTE: the "*timeout" is for this accept() only. To set I/O timeout, * use SOCK_SetTimeout(); all I/O timeouts are infinite by default. */extern NCBI_XCONNECT_EXPORT EIO_Status LSOCK_Accept(LSOCK lsock, /* [in] handle of a listening socket */ const STimeout* timeout, /* [in] timeout (infinite if NULL) */ SOCK* sock /* [out] handle of the created socket */ );/* [SERVER-side] Close the listening socket, destroy relevant internal data. */extern NCBI_XCONNECT_EXPORT EIO_Status LSOCK_Close(LSOCK lsock);/* Get an OS-dependent native socket handle to use by platform-specific API. * FYI: on MS-Windows it will be "SOCKET", on other platforms -- "int". */extern NCBI_XCONNECT_EXPORT EIO_Status LSOCK_GetOSHandle(LSOCK lsock, void* handle_buf, /* pointer to a memory area to put the OS handle at */ size_t handle_size /* the exact(!) size of the expected OS handle */ );/****************************************************************************** * SOCKET *//* [CLIENT-side] Connect client to another(server-side, listening) socket * (socket() + connect() [+ select()]) *//* SOCK_CreateEx(host, port, timeout, sock, 0, 0, eDefault) */extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_Create(const char* host, /* [in] host to connect to */ unsigned short port, /* [in] port to connect to */ const STimeout* timeout, /* [in] the connect timeout (infinite if NULL) */ SOCK* sock /* [out] handle of the created socket */ );extern NCBI_XCONNECT_EXPORT EIO_Status SOCK_CreateEx(const char* host, /* [in] host to connect to */ unsigned short port, /* [in] port to connect to */ const STimeout* timeout, /* [in] the connect timeout (infinite if NULL) */ SOCK* sock, /* [out] handle of the created socket */ const void* init_data,/* [in] initial output data segment (may be NULL)*/ size_t init_size,/* [in] size of initial data segment (may be 0) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -