blocxx
Socket.cpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (C) 2005, Vintela, Inc. All rights reserved.
3* Copyright (C) 2006, Novell, Inc. All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are met:
7*
8* * Redistributions of source code must retain the above copyright notice,
9* this list of conditions and the following disclaimer.
10* * Redistributions in binary form must reproduce the above copyright
11* notice, this list of conditions and the following disclaimer in the
12* documentation and/or other materials provided with the distribution.
13* * Neither the name of
14* Vintela, Inc.,
15* nor Novell, Inc.,
16* nor the names of its contributors or employees may be used to
17* endorse or promote products derived from this software without
18* specific prior written permission.
19*
20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30* POSSIBILITY OF SUCH DAMAGE.
31*******************************************************************************/
32
33
39#include "blocxx/BLOCXX_config.h"
40#include "blocxx/Socket.hpp"
42#include "blocxx/Assertion.hpp"
43#include "blocxx/MutexLock.hpp"
44#include "blocxx/Exception.hpp"
47#include "blocxx/SocketImpl.hpp"
49
50
51namespace BLOCXX_NAMESPACE
52{
53
56
58
61 : m_impl(new SocketImpl)
62{
63}
64
65static bool b_gotShutDown = false;
67
69// STATIC
70void
72{
74
76 if (!s_shutDownMechanism.get())
77 {
78 return;
79 }
80
81 b_gotShutDown = true;
82#if defined(BLOCXX_WIN32)
83 ::SetEvent(s_shutDownMechanism);
84#else
85 if ((s_shutDownMechanism.get())->writeString("die!") == -1)
86 {
87 BLOCXX_THROW_ERRNO_MSG(IOException, "Failed writing to socket shutdown pipe");
88 }
89#endif
90}
91
93// STATIC
94void
96{
98 if (!s_shutDownMechanism.get())
99 {
100#if defined(BLOCXX_WIN32)
101 s_shutDownMechanism = ::CreateEvent(0, TRUE, FALSE, 0);
103#else
106#endif
107 b_gotShutDown = false;
108 }
109}
111// STATIC
112void
114{
116 if (s_shutDownMechanism.get())
117 {
118#if defined(BLOCXX_WIN32)
119 ::CloseHandle(s_shutDownMechanism);
120#endif
122 }
123}
125// STATIC
126bool
128{
130 return b_gotShutDown;
131}
132
133} // end namespace BLOCXX_NAMESPACE
134
#define BLOCXX_ASSERT(CON)
BLOCXX_ASSERT works similar to the assert() macro, but instead of calling abort(),...
Definition Assertion.hpp:57
#define BLOCXX_DEFINE_EXCEPTION_WITH_ID(NAME)
Define a new exception class named <NAME>Exception that derives from Exception.
#define BLOCXX_THROW_ERRNO_MSG(exType, msg)
Throw an exception using FILE, LINE, errno and strerror(errno)
#define BLOCXX_GLOBAL_MUTEX_INIT()
#define BLOCXX_LAZY_GLOBAL_INIT(...)
Statically initialize a LazyGlobal instance.
ProcessImplRef m_impl
Definition Process.cpp:317
This class can be used to store a global variable that is lazily initialized in a thread safe manner.
Socket()
Construct a non-SSL Socket.
Definition Socket.cpp:60
static LazyGlobal< Socket::ShutDownMechanism_t, int, ShutDownMechanismFactory > s_shutDownMechanism
Definition Socket.hpp:345
static void shutdownAllSockets()
Call this to shutdown all sockets.
Definition Socket.cpp:71
static void createShutDownMechanism()
Definition Socket.cpp:95
static void deleteShutDownMechanism()
Definition Socket.cpp:113
static bool gotShutDown() BLOCXX_DEPRECATED
Have the sockets been shutdown?
Definition Socket.cpp:127
static UnnamedPipeRef createUnnamedPipe(EOpen doOpen=E_OPEN)
Create an instance of the concrete class that implements the UnnamedPipe interface.
Taken from RFC 1321.
static bool b_gotShutDown
Definition Socket.cpp:65
static GlobalMutex shutdownMutex
Definition Socket.cpp:66