From 5c14415811b48f1b486be91ea41661381f08a3fe Mon Sep 17 00:00:00 2001 From: Mohan Embar Date: Fri, 29 Aug 2003 04:21:01 +0000 Subject: win32.cc: fixed tab... * win32.cc: fixed tab, indentation and whitespace inconsistencies removed jvm.h include added includes java/lang/UnsupportedOperationException.h, java/io/IOException.h, java/net/SocketException.h (WSAEventWrapper): class implementation (_Jv_WinStrError): implemented both overloads (_Jv_ThrowIOException): implemented both overloads (_Jv_ThrowSocketException): implemented both overloads (_Jv_select): implemented * include/win32.h: fixed tab, indentation and whitespace inconsistencies wrapped include with #define WIN32_LEAN_AND_MEAN added jvm.h include (WSAEventWrapper): added class declaration (_Jv_WinStrError): added both overload declarations (_Jv_ThrowIOException): added both overload declarations (_Jv_ThrowSocketException): added both overload declarations removed ENOTCONN, ECONNRESET and ENOPROTOOPT defines (_Jv_select): added declaration (_Jv_socket): removed (_Jv_connect): removed (_Jv_close): removed (_Jv_bind): removed (_Jv_accept): removed (_Jv_listen): removed (_Jv_write): removed (_Jv_read): removed * java/io/natFileDescriptorWin32.cc: fixed tab, indentation and whitespace inconsistencies replaced #include with removed jvm.h include (testCanUseGetHandleInfo): new function which tests whether Win32 GetHandleInformation() call can be used with console buffer handles (only supported on >=WinNT 5.0) (winerr): removed (superseded by _Jv_WinStrError in include/win32.h) (valid): rewrote implementation using GetHandleInformation() (sync): changed exception throwing to use error string and exception helper methods declared in include/win32.h (open): likewise (write): likewise (setLength): likewise (close): likewise (seek): likewise (getFilePointer): likewise (read): likewise * java/io/natFileWin32.cc: fixed tab, indentation and whitespace inconsistencies replaced #include with removed jvm.h include (_access): use JV_TEMP_UTF_STRING (_stat): likewise (performMkDir): use JV_TEMP_UTF_STRING (performRenameTo): likewise (performDelete): likewise (performCreate): likewise (performSetReadOnly): likewise (performSetLastModified): likewise * java/lang/natWin32Process.cc: fixed tab, indentation and whitespace inconsistencies replaced #include with removed includes gcj/cni.h, jvm.h (new_string): removed (startProcess): use JV_TEMP_UTF_STRING, changed exception throwing to use error string and exception helper methods declared in include/win32.h * java/net/natInetAddressWin32.cc: fixed tab, indentation and whitespace inconsistencies replaced #include with removed jvm.h include removed DISABLE_JAVA_NET conditional code removed POSIX conditional code not relevant to Win32 (aton): use JV_TEMP_UTF_STRING removed POSIX conditional code not relevant to Win32 (lookup): likewise (getLocalHostName): likewise * java/net/natNetworkInterfaceWin32.cc: fixed tab, indentation and whitespace inconsistencies removed unnecessary windows.h, winsock.h and gcj/cni.h includes removed DISABLE_JAVA_NET conditional code removed POSIX conditional code not relevant to Win32 (winsock2GetRealNetworkInterfaces): new function to compute network interfaces via Winsock2 API (determineGetRealNetworkInterfacesFN): new function for returning a function pointer to the function used to compute network interfaces. (getRealNetworkInterfaces): implemented * java/net/natPlainDatagramSocketImplWin32.cc: fixed tab, indentation and whitespace inconsistencies removed gcj/cni.h include removed DISABLE_JAVA_NET conditional code removed POSIX conditional code not relevant to Win32 changed net POSIXisms to Win32isms replaced _Jv socket-related calls with their real Win32 equivalents changed exception throwing to use error string and exception helper methods declared in include/win32.h (peekData): implemented timeout support (receive): likewise * java/net/natPlainSocketImplWin32.cc: fixed tab, indentation and whitespace inconsistencies removed gcj/cni.h and gcj/javaprims.h includes removed DISABLE_JAVA_NET conditional code removed POSIX conditional code not relevant to Win32 changed net POSIXisms to Win32isms replaced _Jv socket-related calls with their real Win32 equivalents changed exception throwing to use error string and exception helper methods declared in include/win32.h (throwConnectException): helper function for connect() (connect): implemented timeout support (accept): likewise (doRead): new helper function common to both read() method overloads, includes timeout support (read): implemented both overloads in terms of doRead() (available): implemented using ioctlsocket() From-SVN: r70904 --- libjava/win32.cc | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 123 insertions(+), 12 deletions(-) (limited to 'libjava/win32.cc') diff --git a/libjava/win32.cc b/libjava/win32.cc index 9597dc8..abe768a 100644 --- a/libjava/win32.cc +++ b/libjava/win32.cc @@ -10,11 +10,13 @@ details. */ #include #include -#include #include #include #include +#include +#include +#include #include static LONG CALLBACK @@ -37,6 +39,102 @@ const char *_Jv_ThisExecutable (void) return exec_name; } +// Helper classes and methods implementation + +// class WSAEventWrapper +WSAEventWrapper::WSAEventWrapper (int fd, DWORD dwSelFlags): + m_hEvent(0), + m_fd(fd), + m_dwSelFlags(dwSelFlags) +{ + m_hEvent = WSACreateEvent (); + if (dwSelFlags) + WSAEventSelect(fd, m_hEvent, dwSelFlags); +} + +WSAEventWrapper::~WSAEventWrapper () +{ + if (m_dwSelFlags) + { + WSAEventSelect(m_fd, m_hEvent, 0); + if (m_dwSelFlags & (FD_ACCEPT | FD_CONNECT)) + { + // Set the socket back to non-blocking mode. + // Ignore any error since we're in a destructor. + unsigned long lSockOpt = 0L; + // blocking mode + ::ioctlsocket (m_fd, FIONBIO, &lSockOpt); + } + } + WSACloseEvent (m_hEvent); +} + +// Error string text. +jstring +_Jv_WinStrError (LPCTSTR lpszPrologue, int nErrorCode) +{ + LPTSTR lpMsgBuf = 0; + + DWORD dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS; + + FormatMessage (dwFlags, + NULL, + (DWORD) nErrorCode, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR) &lpMsgBuf, + 0, + NULL); + + jstring ret; + if (lpszPrologue) + { + LPTSTR lpszTemp = + (LPTSTR) _Jv_Malloc (strlen (lpszPrologue) + + strlen (lpMsgBuf) + 3); + strcpy (lpszTemp, lpszPrologue); + strcat (lpszTemp, ": "); + strcat (lpszTemp, lpMsgBuf); + ret = JvNewStringLatin1 (lpszTemp); + } + else + { + ret = JvNewStringLatin1 (lpMsgBuf); + } + + LocalFree(lpMsgBuf); + return ret; +} + +jstring +_Jv_WinStrError (int nErrorCode) +{ + return _Jv_WinStrError (0, nErrorCode); +} + +void _Jv_ThrowIOException (DWORD dwErrorCode) +{ + throw new java::io::IOException (_Jv_WinStrError (dwErrorCode)); +} + +void _Jv_ThrowIOException() +{ + DWORD dwErrorCode = WSAGetLastError (); + _Jv_ThrowIOException (dwErrorCode); +} + +void _Jv_ThrowSocketException (DWORD dwErrorCode) +{ + throw new java::net::SocketException (_Jv_WinStrError (dwErrorCode)); +} + +void _Jv_ThrowSocketException() +{ + DWORD dwErrorCode = WSAGetLastError (); + _Jv_ThrowSocketException (dwErrorCode); +} + // Platform-specific VM initialization. void _Jv_platform_initialize (void) @@ -45,11 +143,11 @@ _Jv_platform_initialize (void) WSADATA data; if (WSAStartup (MAKEWORD (1, 1), &data)) MessageBox (NULL, "Error initialising winsock library.", "Error", - MB_OK | MB_ICONEXCLAMATION); - + MB_OK | MB_ICONEXCLAMATION); + // Install exception handler SetUnhandledExceptionFilter (win32_exception_handler); - + // Initialize our executable name GetModuleFileName(NULL, exec_name, sizeof(exec_name)); } @@ -96,14 +194,14 @@ _Jv_platform_initProperties (java::util::Properties* newprops) if (buffer != NULL) { if (GetCurrentDirectory (buflen, buffer)) - SET ("user.dir", buffer); + SET ("user.dir", buffer); if (GetTempPath (buflen, buffer)) - SET ("java.io.tmpdir", buffer); + SET ("java.io.tmpdir", buffer); _Jv_Free (buffer); } - + // Use GetUserName to set 'user.name'. buflen = 257; // UNLEN + 1 buffer = (char *) _Jv_MallocUnchecked (buflen); @@ -114,8 +212,8 @@ _Jv_platform_initProperties (java::util::Properties* newprops) _Jv_Free (buffer); } - // According to the api documentation for 'GetWindowsDirectory()', the - // environmental variable HOMEPATH always specifies the user's home + // According to the api documentation for 'GetWindowsDirectory()', the + // environmental variable HOMEPATH always specifies the user's home // directory or a default directory. On the 3 windows machines I checked // only 1 had it set. If it's not set, JDK1.3.1 seems to set it to // the windows directory, so we'll do the same. @@ -130,7 +228,7 @@ _Jv_platform_initProperties (java::util::Properties* newprops) if (winHome != NULL) { if (GetWindowsDirectory (winHome, MAX_PATH)) - SET ("user.home", winHome); + SET ("user.home", winHome); _Jv_Free (winHome); } } @@ -148,7 +246,7 @@ _Jv_platform_initProperties (java::util::Properties* newprops) if (buffer != NULL) { sprintf (buffer, "%d.%d", (int) osvi.dwMajorVersion, - (int) osvi.dwMinorVersion); + (int) osvi.dwMinorVersion); SET ("os.version", buffer); _Jv_Free (buffer); } @@ -163,7 +261,7 @@ _Jv_platform_initProperties (java::util::Properties* newprops) else if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90) SET ("os.name", "Windows Me"); else - SET ("os.name", "Windows ??"); + SET ("os.name", "Windows ??"); break; case VER_PLATFORM_WIN32_NT: @@ -231,3 +329,16 @@ backtrace (void **__array, int __size) } return i; } + +int +_Jv_select (int n, fd_set *readfds, fd_set *writefds, + fd_set *exceptfds, struct timeval *timeout) +{ + int r = ::select (n, readfds, writefds, exceptfds, timeout); + if (r == SOCKET_ERROR) + { + DWORD dwErrorCode = WSAGetLastError (); + throw new java::io::IOException (_Jv_WinStrError (dwErrorCode)); + } + return r; +} -- cgit v1.1