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