blocxx
|
00001 /******************************************************************************* 00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved. 00003 * Copyright (C) 2006, Novell, Inc. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of 00014 * Vintela, Inc., 00015 * nor Novell, Inc., 00016 * nor the names of its contributors or employees may be used to 00017 * endorse or promote products derived from this software without 00018 * specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00030 * POSSIBILITY OF SUCH DAMAGE. 00031 *******************************************************************************/ 00032 00033 00039 #include "blocxx/BLOCXX_config.h" 00040 #include "blocxx/Socket.hpp" 00041 #include "blocxx/UnnamedPipe.hpp" 00042 #include "blocxx/Assertion.hpp" 00043 #include "blocxx/MutexLock.hpp" 00044 #include "blocxx/Exception.hpp" 00045 #include "blocxx/IOException.hpp" 00046 #include "blocxx/ExceptionIds.hpp" 00047 #include "blocxx/SocketImpl.hpp" 00048 #include "blocxx/GlobalMutex.hpp" 00049 00050 00051 namespace BLOCXX_NAMESPACE 00052 { 00053 00054 BLOCXX_DEFINE_EXCEPTION_WITH_ID(Socket); 00055 BLOCXX_DEFINE_EXCEPTION_WITH_ID(SocketTimeout); 00056 00057 LazyGlobal<Socket::ShutDownMechanism_t, int, Socket::ShutDownMechanismFactory> Socket::s_shutDownMechanism = BLOCXX_LAZY_GLOBAL_INIT(0); 00058 00060 Socket::Socket() 00061 : m_impl(new SocketImpl) 00062 { 00063 } 00064 00065 static bool b_gotShutDown = false; 00066 static GlobalMutex shutdownMutex = BLOCXX_GLOBAL_MUTEX_INIT(); 00067 00069 // STATIC 00070 void 00071 Socket::shutdownAllSockets() 00072 { 00073 MutexLock mlock(shutdownMutex); 00074 00075 BLOCXX_ASSERT(s_shutDownMechanism.get() != 0); 00076 if (!s_shutDownMechanism.get()) 00077 { 00078 return; 00079 } 00080 00081 b_gotShutDown = true; 00082 #if defined(BLOCXX_WIN32) 00083 ::SetEvent(s_shutDownMechanism); 00084 #else 00085 if ((s_shutDownMechanism.get())->writeString("die!") == -1) 00086 { 00087 BLOCXX_THROW_ERRNO_MSG(IOException, "Failed writing to socket shutdown pipe"); 00088 } 00089 #endif 00090 } 00091 00093 // STATIC 00094 void 00095 Socket::createShutDownMechanism() 00096 { 00097 MutexLock mlock(shutdownMutex); 00098 if (!s_shutDownMechanism.get()) 00099 { 00100 #if defined(BLOCXX_WIN32) 00101 s_shutDownMechanism = ::CreateEvent(0, TRUE, FALSE, 0); 00102 BLOCXX_ASSERT(s_shutDownMechanism.get() != 0); 00103 #else 00104 s_shutDownMechanism = UnnamedPipe::createUnnamedPipe(); 00105 (s_shutDownMechanism.get())->setBlocking(UnnamedPipe::E_NONBLOCKING); 00106 #endif 00107 b_gotShutDown = false; 00108 } 00109 } 00111 // STATIC 00112 void 00113 Socket::deleteShutDownMechanism() 00114 { 00115 MutexLock mlock(shutdownMutex); 00116 if (s_shutDownMechanism.get()) 00117 { 00118 #if defined(BLOCXX_WIN32) 00119 ::CloseHandle(s_shutDownMechanism); 00120 #endif 00121 s_shutDownMechanism = static_cast<Socket::ShutDownMechanism_t>(0); 00122 } 00123 } 00125 // STATIC 00126 bool 00127 Socket::gotShutDown() 00128 { 00129 MutexLock mlock(shutdownMutex); 00130 return b_gotShutDown; 00131 } 00132 00133 } // end namespace BLOCXX_NAMESPACE 00134