diff options
author | Benoit Foucher <benoit@zeroc.com> | 2008-01-23 11:31:53 +0100 |
---|---|---|
committer | Benoit Foucher <benoit@zeroc.com> | 2008-01-23 11:31:53 +0100 |
commit | c7fb26230801e62f3e690a8948d37c33517c4c13 (patch) | |
tree | f17689f60e13fbbd20d12473272a6f0652f39a78 /cpp/src/IceUtil/Random.cpp | |
parent | removing EventHandler in C# (diff) | |
download | ice-c7fb26230801e62f3e690a8948d37c33517c4c13.tar.bz2 ice-c7fb26230801e62f3e690a8948d37c33517c4c13.tar.xz ice-c7fb26230801e62f3e690a8948d37c33517c4c13.zip |
- Added IceUtil::SyscallException and cleaned up few IceUtil exceptions
- Added errorToString() and lastErrorToString() functions to IceUtil/StringUtil.h
- Replaced multiple implementations of errorToString methods with the IceUtil one.
- Fixed bug 2641.
Diffstat (limited to 'cpp/src/IceUtil/Random.cpp')
-rw-r--r-- | cpp/src/IceUtil/Random.cpp | 76 |
1 files changed, 7 insertions, 69 deletions
diff --git a/cpp/src/IceUtil/Random.cpp b/cpp/src/IceUtil/Random.cpp index d884dbb9a35..1712bb4394f 100644 --- a/cpp/src/IceUtil/Random.cpp +++ b/cpp/src/IceUtil/Random.cpp @@ -20,14 +20,6 @@ using namespace std; using namespace IceUtil; -IceUtilInternal::RandomGeneratorException::RandomGeneratorException(const char* file, int line, int error) : - Exception(file, line), - _error(error) -{ -} - -const char* IceUtilInternal::RandomGeneratorException::_name = "IceUtilInternal::RandomGeneratorException"; - // // The static mutex is required to lazy initialize the file // descriptor for /dev/urandom (Unix) or the cryptographic @@ -78,60 +70,6 @@ public: static RandomCleanup uuidCleanup; } -string -IceUtilInternal::RandomGeneratorException::ice_name() const -{ - return _name; -} - -void -IceUtilInternal::RandomGeneratorException::ice_print(ostream& os) const -{ - Exception::ice_print(os); - if(_error != 0) - { - os << ":\nrandom generator exception: "; -#ifdef _WIN32 - LPVOID lpMsgBuf = 0; - DWORD ok = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - _error, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language - (LPTSTR)&lpMsgBuf, - 0, - NULL); - - if(ok) - { - LPCTSTR msg = (LPCTSTR)lpMsgBuf; - assert(msg && strlen((char*)msg) > 0); - os << msg; - LocalFree(lpMsgBuf); - } - else - { - os << "unknown random generator error"; - } -#else - os << strerror(_error); -#endif - } -} - -Exception* -IceUtilInternal::RandomGeneratorException::ice_clone() const -{ - return new RandomGeneratorException(*this); -} - -void -IceUtilInternal::RandomGeneratorException::ice_throw() const -{ - throw *this; -} - void IceUtilInternal::generateRandom(char* buffer, int size) { @@ -148,13 +86,13 @@ IceUtilInternal::generateRandom(char* buffer, int size) { if(!CryptAcquireContext(&context, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { - throw RandomGeneratorException(__FILE__, __LINE__, GetLastError()); + throw SyscallException(__FILE__, __LINE__, GetLastError()); } } if(!CryptGenRandom(context, size, reinterpret_cast<unsigned char*>(buffer))) { - throw RandomGeneratorException(__FILE__, __LINE__, GetLastError()); + throw SyscallException(__FILE__, __LINE__, GetLastError()); } #else @@ -168,7 +106,7 @@ IceUtilInternal::generateRandom(char* buffer, int size) if(fd == -1) { assert(0); - throw RandomGeneratorException(__FILE__, __LINE__); + throw SyscallException(__FILE__, __LINE__, errno); } } @@ -184,10 +122,10 @@ IceUtilInternal::generateRandom(char* buffer, int size) if(bytesRead == -1 && errno != EINTR) { - int err = errno; - cerr << "Reading /dev/urandom returned " << strerror(err) << endl; + SyscallException ex(__FILE__, __LINE__, errno); + cerr << "Reading /dev/urandom failed:\n" << ex << endl; assert(0); - throw RandomGeneratorException(__FILE__, __LINE__, errno); + throw ex; } else { @@ -199,7 +137,7 @@ IceUtilInternal::generateRandom(char* buffer, int size) if(index != static_cast<size_t>(size)) { assert(0); - throw RandomGeneratorException(__FILE__, __LINE__); + throw SyscallException(__FILE__, __LINE__, 0); } #endif } |