diff options
Diffstat (limited to 'libjava/win32.cc')
-rw-r--r-- | libjava/win32.cc | 135 |
1 files changed, 123 insertions, 12 deletions
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 <config.h> #include <platform.h> -#include <jvm.h> #include <sys/timeb.h> #include <stdlib.h> #include <java/lang/ArithmeticException.h> +#include <java/lang/UnsupportedOperationException.h> +#include <java/io/IOException.h> +#include <java/net/SocketException.h> #include <java/util/Properties.h> 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; +} |