| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 |
|
| 6 |
/* |
| 7 |
** File: pprio.h |
| 8 |
** |
| 9 |
** Description: Private definitions for I/O related structures |
| 10 |
*/ |
| 11 |
|
| 12 |
#ifndef pprio_h___ |
| 13 |
#define pprio_h___ |
| 14 |
|
| 15 |
#include "prtypes.h" |
| 16 |
#include "prio.h" |
| 17 |
|
| 18 |
PR_BEGIN_EXTERN_C |
| 19 |
|
| 20 |
/************************************************************************/ |
| 21 |
/************************************************************************/ |
| 22 |
|
| 23 |
#ifdef _WIN64 |
| 24 |
typedef __int64 PROsfd; |
| 25 |
#else |
| 26 |
typedef PRInt32 PROsfd; |
| 27 |
#endif |
| 28 |
|
| 29 |
/* Return the method tables for files, tcp sockets and udp sockets */ |
| 30 |
NSPR_API(const PRIOMethods*) PR_GetFileMethods(void); |
| 31 |
NSPR_API(const PRIOMethods*) PR_GetTCPMethods(void); |
| 32 |
NSPR_API(const PRIOMethods*) PR_GetUDPMethods(void); |
| 33 |
NSPR_API(const PRIOMethods*) PR_GetPipeMethods(void); |
| 34 |
|
| 35 |
/* |
| 36 |
** Convert a NSPR socket handle to a native socket handle. |
| 37 |
** |
| 38 |
** Using this function makes your code depend on the properties of the |
| 39 |
** current NSPR implementation, which may change (although extremely |
| 40 |
** unlikely because of NSPR's backward compatibility requirement). Avoid |
| 41 |
** using it if you can. |
| 42 |
** |
| 43 |
** If you use this function, you need to understand what NSPR does to |
| 44 |
** the native handle. For example, NSPR puts native socket handles in |
| 45 |
** non-blocking mode or associates them with an I/O completion port (the |
| 46 |
** WINNT build configuration only). Your use of the native handle should |
| 47 |
** not interfere with NSPR's use of the native handle. If your code |
| 48 |
** changes the configuration of the native handle, (e.g., changes it to |
| 49 |
** blocking or closes it), NSPR will not work correctly. |
| 50 |
*/ |
| 51 |
NSPR_API(PROsfd) PR_FileDesc2NativeHandle(PRFileDesc *); |
| 52 |
NSPR_API(void) PR_ChangeFileDescNativeHandle(PRFileDesc *, PROsfd); |
| 53 |
NSPR_API(PRFileDesc*) PR_AllocFileDesc(PROsfd osfd, |
| 54 |
const PRIOMethods *methods); |
| 55 |
NSPR_API(void) PR_FreeFileDesc(PRFileDesc *fd); |
| 56 |
/* |
| 57 |
** Import an existing OS file to NSPR. |
| 58 |
*/ |
| 59 |
NSPR_API(PRFileDesc*) PR_ImportFile(PROsfd osfd); |
| 60 |
NSPR_API(PRFileDesc*) PR_ImportPipe(PROsfd osfd); |
| 61 |
NSPR_API(PRFileDesc*) PR_ImportTCPSocket(PROsfd osfd); |
| 62 |
NSPR_API(PRFileDesc*) PR_ImportUDPSocket(PROsfd osfd); |
| 63 |
|
| 64 |
|
| 65 |
/* |
| 66 |
************************************************************************* |
| 67 |
* FUNCTION: PR_CreateSocketPollFd |
| 68 |
* DESCRIPTION: |
| 69 |
* Create a PRFileDesc wrapper for a native socket handle, for use with |
| 70 |
* PR_Poll only |
| 71 |
* INPUTS: |
| 72 |
* None |
| 73 |
* OUTPUTS: |
| 74 |
* None |
| 75 |
* RETURN: PRFileDesc* |
| 76 |
* Upon successful completion, PR_CreateSocketPollFd returns a pointer |
| 77 |
* to the PRFileDesc created for the native socket handle |
| 78 |
* Returns a NULL pointer if the create of a new PRFileDesc failed |
| 79 |
* |
| 80 |
************************************************************************** |
| 81 |
*/ |
| 82 |
|
| 83 |
NSPR_API(PRFileDesc*) PR_CreateSocketPollFd(PROsfd osfd); |
| 84 |
|
| 85 |
/* |
| 86 |
************************************************************************* |
| 87 |
* FUNCTION: PR_DestroySocketPollFd |
| 88 |
* DESCRIPTION: |
| 89 |
* Destroy the PRFileDesc wrapper created by PR_CreateSocketPollFd |
| 90 |
* INPUTS: |
| 91 |
* None |
| 92 |
* OUTPUTS: |
| 93 |
* None |
| 94 |
* RETURN: PRFileDesc* |
| 95 |
* Upon successful completion, PR_DestroySocketPollFd returns |
| 96 |
* PR_SUCCESS, else PR_FAILURE |
| 97 |
* |
| 98 |
************************************************************************** |
| 99 |
*/ |
| 100 |
|
| 101 |
NSPR_API(PRStatus) PR_DestroySocketPollFd(PRFileDesc *fd); |
| 102 |
|
| 103 |
|
| 104 |
/* |
| 105 |
** Macros for PR_Socket |
| 106 |
** |
| 107 |
** Socket types: PR_SOCK_STREAM, PR_SOCK_DGRAM |
| 108 |
*/ |
| 109 |
|
| 110 |
#ifdef WIN32 |
| 111 |
|
| 112 |
#define PR_SOCK_STREAM 1 |
| 113 |
#define PR_SOCK_DGRAM 2 |
| 114 |
|
| 115 |
#else /* WIN32 */ |
| 116 |
|
| 117 |
#define PR_SOCK_STREAM SOCK_STREAM |
| 118 |
#define PR_SOCK_DGRAM SOCK_DGRAM |
| 119 |
|
| 120 |
#endif /* WIN32 */ |
| 121 |
|
| 122 |
/* |
| 123 |
** Create a new Socket; this function is obsolete. |
| 124 |
*/ |
| 125 |
NSPR_API(PRFileDesc*) PR_Socket(PRInt32 domain, PRInt32 type, PRInt32 proto); |
| 126 |
|
| 127 |
/* FUNCTION: PR_LockFile |
| 128 |
** DESCRIPTION: |
| 129 |
** Lock a file for exclusive access. |
| 130 |
** RETURNS: |
| 131 |
** PR_SUCCESS when the lock is held |
| 132 |
** PR_FAILURE otherwise |
| 133 |
*/ |
| 134 |
NSPR_API(PRStatus) PR_LockFile(PRFileDesc *fd); |
| 135 |
|
| 136 |
/* FUNCTION: PR_TLockFile |
| 137 |
** DESCRIPTION: |
| 138 |
** Test and Lock a file for exclusive access. Do not block if the |
| 139 |
** file cannot be locked immediately. |
| 140 |
** RETURNS: |
| 141 |
** PR_SUCCESS when the lock is held |
| 142 |
** PR_FAILURE otherwise |
| 143 |
*/ |
| 144 |
NSPR_API(PRStatus) PR_TLockFile(PRFileDesc *fd); |
| 145 |
|
| 146 |
/* FUNCTION: PR_UnlockFile |
| 147 |
** DESCRIPTION: |
| 148 |
** Unlock a file which has been previously locked successfully by this |
| 149 |
** process. |
| 150 |
** RETURNS: |
| 151 |
** PR_SUCCESS when the lock is released |
| 152 |
** PR_FAILURE otherwise |
| 153 |
*/ |
| 154 |
NSPR_API(PRStatus) PR_UnlockFile(PRFileDesc *fd); |
| 155 |
|
| 156 |
/* |
| 157 |
** Emulate acceptread by accept and recv. |
| 158 |
*/ |
| 159 |
NSPR_API(PRInt32) PR_EmulateAcceptRead(PRFileDesc *sd, PRFileDesc **nd, |
| 160 |
PRNetAddr **raddr, void *buf, PRInt32 amount, PRIntervalTime timeout); |
| 161 |
|
| 162 |
/* |
| 163 |
** Emulate sendfile by reading from the file and writing to the socket. |
| 164 |
** The file is memory-mapped if memory-mapped files are supported. |
| 165 |
*/ |
| 166 |
NSPR_API(PRInt32) PR_EmulateSendFile( |
| 167 |
PRFileDesc *networkSocket, PRSendFileData *sendData, |
| 168 |
PRTransmitFileFlags flags, PRIntervalTime timeout); |
| 169 |
|
| 170 |
#ifdef WIN32 |
| 171 |
/* FUNCTION: PR_NTFast_AcceptRead |
| 172 |
** DESCRIPTION: |
| 173 |
** NT has the notion of an "accept context", which is only needed in |
| 174 |
** order to make certain calls. By default, a socket connected via |
| 175 |
** AcceptEx can only do a limited number of things without updating |
| 176 |
** the acceptcontext. The generic version of PR_AcceptRead always |
| 177 |
** updates the accept context. This version does not. |
| 178 |
**/ |
| 179 |
NSPR_API(PRInt32) PR_NTFast_AcceptRead(PRFileDesc *sd, PRFileDesc **nd, |
| 180 |
PRNetAddr **raddr, void *buf, PRInt32 amount, PRIntervalTime t); |
| 181 |
|
| 182 |
typedef void (*_PR_AcceptTimeoutCallback)(void *); |
| 183 |
|
| 184 |
/* FUNCTION: PR_NTFast_AcceptRead_WithTimeoutCallback |
| 185 |
** DESCRIPTION: |
| 186 |
** The AcceptEx call combines the accept with the read function. However, |
| 187 |
** our daemon threads need to be able to wakeup and reliably flush their |
| 188 |
** log buffers if the Accept times out. However, with the current blocking |
| 189 |
** interface to AcceptRead, there is no way for us to timeout the Accept; |
| 190 |
** this is because when we timeout the Read, we can close the newly |
| 191 |
** socket and continue; but when we timeout the accept itself, there is no |
| 192 |
** new socket to timeout. So instead, this version of the function is |
| 193 |
** provided. After the initial timeout period elapses on the accept() |
| 194 |
** portion of the function, it will call the callback routine and then |
| 195 |
** continue the accept. If the timeout occurs on the read, it will |
| 196 |
** close the connection and return error. |
| 197 |
*/ |
| 198 |
NSPR_API(PRInt32) PR_NTFast_AcceptRead_WithTimeoutCallback( |
| 199 |
PRFileDesc *sd, |
| 200 |
PRFileDesc **nd, |
| 201 |
PRNetAddr **raddr, |
| 202 |
void *buf, |
| 203 |
PRInt32 amount, |
| 204 |
PRIntervalTime t, |
| 205 |
_PR_AcceptTimeoutCallback callback, |
| 206 |
void *callback_arg); |
| 207 |
|
| 208 |
/* FUNCTION: PR_NTFast_Accept |
| 209 |
** DESCRIPTION: |
| 210 |
** NT has the notion of an "accept context", which is only needed in |
| 211 |
** order to make certain calls. By default, a socket connected via |
| 212 |
** AcceptEx can only do a limited number of things without updating |
| 213 |
** the acceptcontext. The generic version of PR_Accept always |
| 214 |
** updates the accept context. This version does not. |
| 215 |
**/ |
| 216 |
NSPR_API(PRFileDesc*) PR_NTFast_Accept(PRFileDesc *fd, PRNetAddr *addr, |
| 217 |
PRIntervalTime timeout); |
| 218 |
|
| 219 |
/* FUNCTION: PR_NTFast_Update |
| 220 |
** DESCRIPTION: |
| 221 |
** For sockets accepted with PR_NTFast_Accept or PR_NTFastAcceptRead, |
| 222 |
** this function will update the accept context for those sockets, |
| 223 |
** so that the socket can make general purpose socket calls. |
| 224 |
** Without calling this, the only operations supported on the socket |
| 225 |
** Are PR_Read, PR_Write, PR_Transmitfile, and PR_Close. |
| 226 |
*/ |
| 227 |
NSPR_API(void) PR_NTFast_UpdateAcceptContext(PRFileDesc *acceptSock, |
| 228 |
PRFileDesc *listenSock); |
| 229 |
|
| 230 |
|
| 231 |
/* FUNCTION: PR_NT_CancelIo |
| 232 |
** DESCRIPTION: |
| 233 |
** Cancel IO operations on fd. |
| 234 |
*/ |
| 235 |
NSPR_API(PRStatus) PR_NT_CancelIo(PRFileDesc *fd); |
| 236 |
|
| 237 |
|
| 238 |
#endif /* WIN32 */ |
| 239 |
|
| 240 |
PR_END_EXTERN_C |
| 241 |
|
| 242 |
#endif /* pprio_h___ */ |