Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenstartup.c
Go to the documentation of this file.
00001 /* 00002 * COPYRIGHT: See COPYING in the top level directory 00003 * PROJECT: ReactOS WinSock 2 API 00004 * FILE: startup.c 00005 * PURPOSE: Startup/Cleanup Support 00006 * PROGRAMMER: Alex Ionescu (alex@relsoft.net) 00007 */ 00008 00009 /* INCLUDES ******************************************************************/ 00010 #include "ws2_32.h" 00011 00012 //#define NDEBUG 00013 #include <debug.h> 00014 00015 /* DATA **********************************************************************/ 00016 00017 PWS_SOCK_POST_ROUTINE WsSockPostRoutine = NULL; 00018 CRITICAL_SECTION WsStartupLock; 00019 00020 #define WsStartupLock() EnterCriticalSection(&WsStartupLock); 00021 #define WsStartupUnlock() LeaveCriticalSection(&WsStartupLock); 00022 00023 /* FUNCTIONS *****************************************************************/ 00024 00025 VOID 00026 WSAAPI 00027 WsCreateStartupSynchronization(VOID) 00028 { 00029 /* Initialize the startup lock */ 00030 InitializeCriticalSection(&WsStartupLock); 00031 } 00032 00033 VOID 00034 WSAAPI 00035 WsDestroyStartupSynchronization(VOID) 00036 { 00037 /* Destroy the startup lock */ 00038 DeleteCriticalSection(&WsStartupLock); 00039 } 00040 00041 /* 00042 * @implemented 00043 */ 00044 BOOL 00045 WSAAPI 00046 WSApSetPostRoutine(PVOID Routine) 00047 { 00048 /* Set the post routine */ 00049 DPRINT("WSApSetPostRoutine: %p\n", Routine); 00050 WsSockPostRoutine = (PWS_SOCK_POST_ROUTINE)Routine; 00051 return ERROR_SUCCESS; 00052 } 00053 00054 /* 00055 * @implemented 00056 */ 00057 INT 00058 WSAAPI 00059 WSACleanup(VOID) 00060 { 00061 PWSPROCESS Process; 00062 PWSTHREAD Thread; 00063 INT ErrorCode; 00064 LONG RefCount; 00065 DPRINT("WSACleanup\n"); 00066 00067 /* Enter startup lock */ 00068 WsStartupLock(); 00069 00070 /* Enter prolog */ 00071 if ((ErrorCode = WsApiProlog(&Process, &Thread)) == ERROR_SUCCESS) 00072 { 00073 /* Decrement process reference count and check if it's zero */ 00074 if (!(RefCount = InterlockedDecrement(&Process->RefCount))) 00075 { 00076 /* It's zero, destroy the process structure */ 00077 WsProcDelete(Process); 00078 } 00079 else if (RefCount == 1 && WsAsyncThreadInitialized) 00080 { 00081 /* Kill async thread */ 00082 WsAsyncTerminateThread(); 00083 } 00084 00085 /* Return success */ 00086 ErrorCode = ERROR_SUCCESS; 00087 } 00088 else 00089 { 00090 /* Weren't initialized */ 00091 SetLastError(ErrorCode); 00092 ErrorCode = SOCKET_ERROR; 00093 } 00094 00095 /* Release startup lock */ 00096 WsStartupUnlock(); 00097 00098 /* Done */ 00099 return ErrorCode; 00100 } 00101 00102 /* 00103 * @implemented 00104 */ 00105 INT 00106 WINAPI 00107 WSAStartup(IN WORD wVersionRequested, 00108 OUT LPWSADATA lpWSAData) 00109 { 00110 WORD VersionReturned = 0; 00111 DWORD ErrorCode = ERROR_SUCCESS; 00112 PWSPROCESS CurrentProcess; 00113 DPRINT("WSAStartup: %wx\n", wVersionRequested); 00114 00115 /* Make sure that we went through DLL Init */ 00116 if (!WsDllHandle) return WSASYSNOTREADY; 00117 00118 /* Check which version is being requested */ 00119 switch (LOBYTE(wVersionRequested)) 00120 { 00121 case 0: 00122 00123 /* We don't support this unknown version */ 00124 ErrorCode = WSAVERNOTSUPPORTED; 00125 break; 00126 00127 case 1: 00128 /* We support only 1.0 and 1.1 */ 00129 if (HIBYTE(wVersionRequested) == 0) 00130 { 00131 /* Caller wants 1.0, return it */ 00132 VersionReturned = wVersionRequested; 00133 } 00134 else 00135 { 00136 /* The only other version we support is 1.1 */ 00137 VersionReturned = MAKEWORD(1, 1); 00138 } 00139 break; 00140 00141 case 2: 00142 /* We support only 2.0, 2.1 and 2.2 */ 00143 if (HIBYTE(wVersionRequested) <= 2) 00144 { 00145 /* Caller wants 2.0-2.2, return it */ 00146 VersionReturned = MAKEWORD(2, HIBYTE(wVersionRequested)); 00147 } 00148 else 00149 { 00150 /* The highest version we support is 2.2 */ 00151 VersionReturned = MAKEWORD(2, 2); 00152 } 00153 break; 00154 00155 default: 00156 00157 /* Return 2.2 */ 00158 VersionReturned = MAKEWORD(2, 2);; 00159 break; 00160 } 00161 00162 /* Return the Version Requsted, unless error */ 00163 lpWSAData->wVersion = VersionReturned; 00164 00165 /* We support Winsock 2.2 */ 00166 lpWSAData->wHighVersion = MAKEWORD(2,2); 00167 lstrcpy(lpWSAData->szDescription, "WinSock 2.2"); 00168 lstrcpy(lpWSAData->szSystemStatus, "Running"); 00169 00170 /* 00171 * On Winsock 1, the following values are returned. 00172 * Taken straight from a Winsock Test app on Windows. 00173 */ 00174 if (LOBYTE(wVersionRequested) == 1) 00175 { 00176 lpWSAData->iMaxSockets = 32767; 00177 lpWSAData->iMaxUdpDg = 65467; 00178 } 00179 else 00180 { 00181 lpWSAData->iMaxSockets = 0; 00182 lpWSAData->iMaxUdpDg = 0; 00183 } 00184 00185 /* Enter the startup syncronization lock */ 00186 WsStartupLock(); 00187 00188 /* Now setup all our objects */ 00189 while (TRUE) 00190 { 00191 /* Make sure we don't already have a process */ 00192 CurrentProcess = WsGetProcess(); 00193 if (CurrentProcess) break; 00194 00195 /* Setup the process object support */ 00196 ErrorCode = WsProcStartup(); 00197 if (ErrorCode != ERROR_SUCCESS) break; 00198 00199 /* Setup the process object support */ 00200 ErrorCode = WsSockStartup(); 00201 if (ErrorCode != ERROR_SUCCESS) break; 00202 00203 /* Setup the process object support */ 00204 ErrorCode = WsThreadStartup(); 00205 if (ErrorCode != ERROR_SUCCESS) break; 00206 00207 /* Try getting the process now */ 00208 CurrentProcess = WsGetProcess(); 00209 if (!CurrentProcess) 00210 { 00211 /* Something is weird... */ 00212 ErrorCode = WSASYSNOTREADY; 00213 break; 00214 } 00215 } 00216 00217 /* Check if all worked */ 00218 if (ErrorCode == ERROR_SUCCESS) 00219 { 00220 /* Set the requested version */ 00221 WsProcSetVersion(CurrentProcess, wVersionRequested); 00222 00223 /* Increase the reference count */ 00224 InterlockedIncrement(&CurrentProcess->RefCount); 00225 } 00226 00227 /* Leave the startup lock */ 00228 WsStartupUnlock(); 00229 00230 /* Return any Error */ 00231 return ErrorCode; 00232 } Generated on Sun May 27 2012 04:18:31 for ReactOS by
1.7.6.1
|