andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public
andre@0:  * License, v. 2.0. If a copy of the MPL was not distributed with this
andre@0:  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
andre@0: 
andre@0: /*
andre@0: ** File:	pprio.h
andre@0: **
andre@0: ** Description:	Private definitions for I/O related structures
andre@0: */
andre@0: 
andre@0: #ifndef pprio_h___
andre@0: #define pprio_h___
andre@0: 
andre@0: #include "prtypes.h"
andre@0: #include "prio.h"
andre@0: 
andre@0: PR_BEGIN_EXTERN_C
andre@0: 
andre@0: /************************************************************************/
andre@0: /************************************************************************/
andre@0: 
andre@0: #ifdef _WIN64
andre@0: typedef __int64 PROsfd;
andre@0: #else
andre@0: typedef PRInt32 PROsfd;
andre@0: #endif
andre@0: 
andre@0: /* Return the method tables for files, tcp sockets and udp sockets */
andre@0: NSPR_API(const PRIOMethods*)    PR_GetFileMethods(void);
andre@0: NSPR_API(const PRIOMethods*)    PR_GetTCPMethods(void);
andre@0: NSPR_API(const PRIOMethods*)    PR_GetUDPMethods(void);
andre@0: NSPR_API(const PRIOMethods*)    PR_GetPipeMethods(void);
andre@0: 
andre@0: /*
andre@0: ** Convert a NSPR socket handle to a native socket handle.
andre@0: **
andre@0: ** Using this function makes your code depend on the properties of the
andre@0: ** current NSPR implementation, which may change (although extremely
andre@0: ** unlikely because of NSPR's backward compatibility requirement).  Avoid
andre@0: ** using it if you can.
andre@0: **
andre@0: ** If you use this function, you need to understand what NSPR does to
andre@0: ** the native handle.  For example, NSPR puts native socket handles in
andre@0: ** non-blocking mode or associates them with an I/O completion port (the
andre@0: ** WINNT build configuration only).  Your use of the native handle should
andre@0: ** not interfere with NSPR's use of the native handle.  If your code
andre@0: ** changes the configuration of the native handle, (e.g., changes it to
andre@0: ** blocking or closes it), NSPR will not work correctly.
andre@0: */
andre@0: NSPR_API(PROsfd)       PR_FileDesc2NativeHandle(PRFileDesc *);
andre@0: NSPR_API(void)         PR_ChangeFileDescNativeHandle(PRFileDesc *, PROsfd);
andre@0: NSPR_API(PRFileDesc*)  PR_AllocFileDesc(PROsfd osfd,
andre@0:                                          const PRIOMethods *methods);
andre@0: NSPR_API(void)         PR_FreeFileDesc(PRFileDesc *fd);
andre@0: /*
andre@0: ** Import an existing OS file to NSPR. 
andre@0: */
andre@0: NSPR_API(PRFileDesc*)  PR_ImportFile(PROsfd osfd);
andre@0: NSPR_API(PRFileDesc*)  PR_ImportPipe(PROsfd osfd);
andre@0: NSPR_API(PRFileDesc*)  PR_ImportTCPSocket(PROsfd osfd);
andre@0: NSPR_API(PRFileDesc*)  PR_ImportUDPSocket(PROsfd osfd);
andre@0: 
andre@0: 
andre@0: /*
andre@0:  *************************************************************************
andre@0:  * FUNCTION: PR_CreateSocketPollFd
andre@0:  * DESCRIPTION:
andre@0:  *     Create a PRFileDesc wrapper for a native socket handle, for use with
andre@0:  *	   PR_Poll only
andre@0:  * INPUTS:
andre@0:  *     None
andre@0:  * OUTPUTS:
andre@0:  *     None
andre@0:  * RETURN: PRFileDesc*
andre@0:  *     Upon successful completion, PR_CreateSocketPollFd returns a pointer
andre@0:  *     to the PRFileDesc created for the native socket handle
andre@0:  *     Returns a NULL pointer if the create of a new PRFileDesc failed
andre@0:  *
andre@0:  **************************************************************************
andre@0:  */
andre@0: 
andre@0: NSPR_API(PRFileDesc*)	PR_CreateSocketPollFd(PROsfd osfd);
andre@0: 
andre@0: /*
andre@0:  *************************************************************************
andre@0:  * FUNCTION: PR_DestroySocketPollFd
andre@0:  * DESCRIPTION:
andre@0:  *     Destroy the PRFileDesc wrapper created by PR_CreateSocketPollFd
andre@0:  * INPUTS:
andre@0:  *     None
andre@0:  * OUTPUTS:
andre@0:  *     None
andre@0:  * RETURN: PRFileDesc*
andre@0:  *     Upon successful completion, PR_DestroySocketPollFd returns
andre@0:  *	   PR_SUCCESS, else PR_FAILURE
andre@0:  *
andre@0:  **************************************************************************
andre@0:  */
andre@0: 
andre@0: NSPR_API(PRStatus) PR_DestroySocketPollFd(PRFileDesc *fd);
andre@0: 
andre@0: 
andre@0: /*
andre@0: ** Macros for PR_Socket
andre@0: **
andre@0: ** Socket types: PR_SOCK_STREAM, PR_SOCK_DGRAM
andre@0: */
andre@0: 
andre@0: #ifdef WIN32
andre@0: 
andre@0: #define PR_SOCK_STREAM 1
andre@0: #define PR_SOCK_DGRAM 2
andre@0: 
andre@0: #else /* WIN32 */
andre@0: 
andre@0: #define PR_SOCK_STREAM SOCK_STREAM
andre@0: #define PR_SOCK_DGRAM SOCK_DGRAM
andre@0: 
andre@0: #endif /* WIN32 */
andre@0: 
andre@0: /*
andre@0: ** Create a new Socket; this function is obsolete.
andre@0: */
andre@0: NSPR_API(PRFileDesc*)	PR_Socket(PRInt32 domain, PRInt32 type, PRInt32 proto);
andre@0: 
andre@0: /* FUNCTION: PR_LockFile
andre@0: ** DESCRIPTION:
andre@0: **    Lock a file for exclusive access.
andre@0: ** RETURNS:
andre@0: **    PR_SUCCESS when the lock is held
andre@0: **    PR_FAILURE otherwise
andre@0: */
andre@0: NSPR_API(PRStatus) PR_LockFile(PRFileDesc *fd);
andre@0: 
andre@0: /* FUNCTION: PR_TLockFile
andre@0: ** DESCRIPTION:
andre@0: **    Test and Lock a file for exclusive access.  Do not block if the
andre@0: **    file cannot be locked immediately.
andre@0: ** RETURNS:
andre@0: **    PR_SUCCESS when the lock is held
andre@0: **    PR_FAILURE otherwise
andre@0: */
andre@0: NSPR_API(PRStatus) PR_TLockFile(PRFileDesc *fd);
andre@0: 
andre@0: /* FUNCTION: PR_UnlockFile
andre@0: ** DESCRIPTION:
andre@0: **    Unlock a file which has been previously locked successfully by this
andre@0: **    process.
andre@0: ** RETURNS:
andre@0: **    PR_SUCCESS when the lock is released
andre@0: **    PR_FAILURE otherwise
andre@0: */
andre@0: NSPR_API(PRStatus) PR_UnlockFile(PRFileDesc *fd);
andre@0: 
andre@0: /*
andre@0: ** Emulate acceptread by accept and recv.
andre@0: */
andre@0: NSPR_API(PRInt32) PR_EmulateAcceptRead(PRFileDesc *sd, PRFileDesc **nd,
andre@0:     PRNetAddr **raddr, void *buf, PRInt32 amount, PRIntervalTime timeout);
andre@0: 
andre@0: /*
andre@0: ** Emulate sendfile by reading from the file and writing to the socket.
andre@0: ** The file is memory-mapped if memory-mapped files are supported.
andre@0: */
andre@0: NSPR_API(PRInt32) PR_EmulateSendFile(
andre@0:     PRFileDesc *networkSocket, PRSendFileData *sendData,
andre@0:     PRTransmitFileFlags flags, PRIntervalTime timeout);
andre@0: 
andre@0: #ifdef WIN32
andre@0: /* FUNCTION: PR_NTFast_AcceptRead
andre@0: ** DESCRIPTION:
andre@0: **    NT has the notion of an "accept context", which is only needed in
andre@0: **    order to make certain calls.  By default, a socket connected via
andre@0: **    AcceptEx can only do a limited number of things without updating
andre@0: **    the acceptcontext.  The generic version of PR_AcceptRead always
andre@0: **    updates the accept context.  This version does not.
andre@0: **/
andre@0: NSPR_API(PRInt32) PR_NTFast_AcceptRead(PRFileDesc *sd, PRFileDesc **nd,
andre@0:               PRNetAddr **raddr, void *buf, PRInt32 amount, PRIntervalTime t);
andre@0: 
andre@0: typedef void (*_PR_AcceptTimeoutCallback)(void *);
andre@0: 
andre@0: /* FUNCTION: PR_NTFast_AcceptRead_WithTimeoutCallback
andre@0: ** DESCRIPTION:
andre@0: **    The AcceptEx call combines the accept with the read function.  However,
andre@0: **    our daemon threads need to be able to wakeup and reliably flush their
andre@0: **    log buffers if the Accept times out.  However, with the current blocking
andre@0: **    interface to AcceptRead, there is no way for us to timeout the Accept;
andre@0: **    this is because when we timeout the Read, we can close the newly 
andre@0: **    socket and continue; but when we timeout the accept itself, there is no
andre@0: **    new socket to timeout.  So instead, this version of the function is
andre@0: **    provided.  After the initial timeout period elapses on the accept()
andre@0: **    portion of the function, it will call the callback routine and then
andre@0: **    continue the accept.   If the timeout occurs on the read, it will 
andre@0: **    close the connection and return error.
andre@0: */
andre@0: NSPR_API(PRInt32) PR_NTFast_AcceptRead_WithTimeoutCallback(
andre@0:               PRFileDesc *sd, 
andre@0:               PRFileDesc **nd,
andre@0:               PRNetAddr **raddr, 
andre@0:               void *buf, 
andre@0:               PRInt32 amount, 
andre@0:               PRIntervalTime t,
andre@0:               _PR_AcceptTimeoutCallback callback, 
andre@0:               void *callback_arg);
andre@0: 
andre@0: /* FUNCTION: PR_NTFast_Accept
andre@0: ** DESCRIPTION:
andre@0: **    NT has the notion of an "accept context", which is only needed in
andre@0: **    order to make certain calls.  By default, a socket connected via
andre@0: **    AcceptEx can only do a limited number of things without updating
andre@0: **    the acceptcontext.  The generic version of PR_Accept always
andre@0: **    updates the accept context.  This version does not.
andre@0: **/
andre@0: NSPR_API(PRFileDesc*)	PR_NTFast_Accept(PRFileDesc *fd, PRNetAddr *addr,
andre@0:                                                 PRIntervalTime timeout);
andre@0: 
andre@0: /* FUNCTION: PR_NTFast_Update
andre@0: ** DESCRIPTION:
andre@0: **    For sockets accepted with PR_NTFast_Accept or PR_NTFastAcceptRead,
andre@0: **    this function will update the accept context for those sockets,
andre@0: **    so that the socket can make general purpose socket calls.
andre@0: **    Without calling this, the only operations supported on the socket
andre@0: **    Are PR_Read, PR_Write, PR_Transmitfile, and PR_Close.
andre@0: */
andre@0: NSPR_API(void) PR_NTFast_UpdateAcceptContext(PRFileDesc *acceptSock, 
andre@0:                                         PRFileDesc *listenSock);
andre@0: 
andre@0: 
andre@0: /* FUNCTION: PR_NT_CancelIo
andre@0: ** DESCRIPTION:
andre@0: **    Cancel IO operations on fd.
andre@0: */
andre@0: NSPR_API(PRStatus) PR_NT_CancelIo(PRFileDesc *fd);
andre@0: 
andre@0: 
andre@0: #endif /* WIN32 */
andre@0: 
andre@0: PR_END_EXTERN_C
andre@0: 
andre@0: #endif /* pprio_h___ */