blocxx
Main Page
Namespaces
Classes
Files
File List
File Members
src
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
"
41
#include "
blocxx/UnnamedPipe.hpp
"
42
#include "
blocxx/Assertion.hpp
"
43
#include "
blocxx/MutexLock.hpp
"
44
#include "
blocxx/Exception.hpp
"
45
#include "
blocxx/IOException.hpp
"
46
#include "
blocxx/ExceptionIds.hpp
"
47
#include "
blocxx/SocketImpl.hpp
"
48
#include "
blocxx/GlobalMutex.hpp
"
49
50
51
namespace
BLOCXX_NAMESPACE
52
{
53
54
BLOCXX_DEFINE_EXCEPTION_WITH_ID
(
Socket
);
55
BLOCXX_DEFINE_EXCEPTION_WITH_ID
(SocketTimeout);
56
57
LazyGlobal<Socket::ShutDownMechanism_t, int, Socket::ShutDownMechanismFactory>
Socket::s_shutDownMechanism
=
BLOCXX_LAZY_GLOBAL_INIT
(0);
58
60
Socket::Socket
()
61
:
m_impl
(new
SocketImpl
)
62
{
63
}
64
65
static
bool
b_gotShutDown
=
false
;
66
static
GlobalMutex
shutdownMutex
=
BLOCXX_GLOBAL_MUTEX_INIT
();
67
69
// STATIC
70
void
71
Socket::shutdownAllSockets
()
72
{
73
MutexLock
mlock(
shutdownMutex
);
74
75
BLOCXX_ASSERT
(
s_shutDownMechanism
.
get
() != 0);
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
94
void
95
Socket::createShutDownMechanism
()
96
{
97
MutexLock
mlock(
shutdownMutex
);
98
if
(!
s_shutDownMechanism
.
get
())
99
{
100
#if defined(BLOCXX_WIN32)
101
s_shutDownMechanism
= ::CreateEvent(0, TRUE, FALSE, 0);
102
BLOCXX_ASSERT
(
s_shutDownMechanism
.
get
() != 0);
103
#else
104
s_shutDownMechanism
=
UnnamedPipe::createUnnamedPipe
();
105
(s_shutDownMechanism.get())->setBlocking(
UnnamedPipe::E_NONBLOCKING
);
106
#endif
107
b_gotShutDown
=
false
;
108
}
109
}
111
// STATIC
112
void
113
Socket::deleteShutDownMechanism
()
114
{
115
MutexLock
mlock(
shutdownMutex
);
116
if
(
s_shutDownMechanism
.
get
())
117
{
118
#if defined(BLOCXX_WIN32)
119
::CloseHandle(
s_shutDownMechanism
);
120
#endif
121
s_shutDownMechanism
=
static_cast<
Socket::ShutDownMechanism_t
>
(0);
122
}
123
}
125
// STATIC
126
bool
127
Socket::gotShutDown
()
128
{
129
MutexLock
mlock(
shutdownMutex
);
130
return
b_gotShutDown
;
131
}
132
133
}
// end namespace BLOCXX_NAMESPACE
134
Generated by
1.8.2