/src/dcmtk/oflog/libsrc/socket.cc
Line | Count | Source |
1 | | // Module: Log4CPLUS |
2 | | // File: socket-win32.cxx |
3 | | // Created: 4/2003 |
4 | | // Author: Tad E. Smith |
5 | | // |
6 | | // |
7 | | // Copyright 2003-2010 Tad E. Smith |
8 | | // |
9 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | // you may not use this file except in compliance with the License. |
11 | | // You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software |
16 | | // distributed under the License is distributed on an "AS IS" BASIS, |
17 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | // See the License for the specific language governing permissions and |
19 | | // limitations under the License. |
20 | | |
21 | | #include "dcmtk/oflog/helpers/loglog.h" |
22 | | #include "dcmtk/oflog/internal/socket.h" |
23 | | |
24 | | |
25 | | namespace dcmtk { |
26 | | namespace log4cplus { namespace helpers { |
27 | | |
28 | | |
29 | | extern DCMTK_LOG4CPLUS_EXPORT SOCKET_TYPE const INVALID_SOCKET_VALUE |
30 | | #if defined(_WIN32) |
31 | | = OFstatic_cast(SOCKET_TYPE, INVALID_SOCKET); |
32 | | #else |
33 | | = OFstatic_cast(SOCKET_TYPE, -1); |
34 | | #endif |
35 | | |
36 | | |
37 | | ////////////////////////////////////////////////////////////////////////////// |
38 | | // AbstractSocket ctors and dtor |
39 | | ////////////////////////////////////////////////////////////////////////////// |
40 | | |
41 | | AbstractSocket::AbstractSocket() |
42 | 0 | : sock(INVALID_SOCKET_VALUE), |
43 | 0 | state(not_opened), |
44 | 0 | err(0) |
45 | 0 | { |
46 | 0 | } |
47 | | |
48 | | |
49 | | |
50 | | AbstractSocket::AbstractSocket(SOCKET_TYPE sock_, |
51 | | SocketState state_, int err_) |
52 | 0 | : sock(sock_), |
53 | 0 | state(state_), |
54 | 0 | err(err_) |
55 | 0 | { |
56 | 0 | } |
57 | | |
58 | | |
59 | | |
60 | | AbstractSocket::AbstractSocket(const AbstractSocket& rhs) |
61 | 0 | : sock(INVALID_SOCKET_VALUE), |
62 | 0 | state(not_opened), |
63 | 0 | err(0) |
64 | 0 | { |
65 | 0 | copy(rhs); |
66 | 0 | } |
67 | | |
68 | | |
69 | | AbstractSocket::~AbstractSocket() |
70 | 0 | { |
71 | 0 | close(); |
72 | 0 | } |
73 | | |
74 | | |
75 | | |
76 | | ////////////////////////////////////////////////////////////////////////////// |
77 | | // AbstractSocket methods |
78 | | ////////////////////////////////////////////////////////////////////////////// |
79 | | |
80 | | void |
81 | | AbstractSocket::close() |
82 | 0 | { |
83 | 0 | if(sock != INVALID_SOCKET_VALUE) { |
84 | 0 | closeSocket(sock); |
85 | 0 | sock = INVALID_SOCKET_VALUE; |
86 | 0 | } |
87 | 0 | } |
88 | | |
89 | | |
90 | | |
91 | | bool |
92 | | AbstractSocket::isOpen() const |
93 | 0 | { |
94 | 0 | return sock != INVALID_SOCKET_VALUE; |
95 | 0 | } |
96 | | |
97 | | |
98 | | |
99 | | |
100 | | AbstractSocket& |
101 | | AbstractSocket::operator=(const AbstractSocket& rhs) |
102 | 0 | { |
103 | 0 | if(&rhs != this) { |
104 | 0 | close(); |
105 | 0 | copy(rhs); |
106 | 0 | } |
107 | |
|
108 | 0 | return *this; |
109 | 0 | } |
110 | | |
111 | | |
112 | | |
113 | | void |
114 | | AbstractSocket::copy(const AbstractSocket& r) |
115 | 0 | { |
116 | 0 | AbstractSocket& rhs = OFconst_cast(AbstractSocket&, r); |
117 | 0 | sock = rhs.sock; |
118 | 0 | state = rhs.state; |
119 | 0 | err = rhs.err; |
120 | 0 | rhs.sock = INVALID_SOCKET_VALUE; |
121 | 0 | rhs.state = not_opened; |
122 | 0 | rhs.err = 0; |
123 | 0 | } |
124 | | |
125 | | |
126 | | |
127 | | ////////////////////////////////////////////////////////////////////////////// |
128 | | // Socket ctors and dtor |
129 | | ////////////////////////////////////////////////////////////////////////////// |
130 | | |
131 | | Socket::Socket() |
132 | 0 | : AbstractSocket() |
133 | 0 | { } |
134 | | |
135 | | |
136 | | Socket::Socket(const tstring& address, unsigned short port, bool udp /*= false*/) |
137 | 0 | : AbstractSocket() |
138 | 0 | { |
139 | 0 | sock = connectSocket(address, port, udp, state); |
140 | 0 | if (sock == INVALID_SOCKET_VALUE) |
141 | 0 | goto error; |
142 | | |
143 | 0 | if (! udp && setTCPNoDelay (sock, true) != 0) |
144 | 0 | goto error; |
145 | | |
146 | 0 | return; |
147 | | |
148 | 0 | error: |
149 | 0 | err = get_last_socket_error (); |
150 | 0 | } |
151 | | |
152 | | |
153 | | Socket::Socket(SOCKET_TYPE sock_, SocketState state_, int err_) |
154 | 0 | : AbstractSocket(sock_, state_, err_) |
155 | 0 | { } |
156 | | |
157 | | |
158 | | Socket::~Socket() |
159 | | { } |
160 | | |
161 | | |
162 | | |
163 | | ////////////////////////////////////////////////////////////////////////////// |
164 | | // Socket methods |
165 | | ////////////////////////////////////////////////////////////////////////////// |
166 | | |
167 | | bool |
168 | | Socket::read(SocketBuffer& buffer) |
169 | 0 | { |
170 | 0 | long retval = helpers::read(sock, buffer); |
171 | 0 | if(retval <= 0) { |
172 | 0 | close(); |
173 | 0 | } |
174 | 0 | else { |
175 | 0 | buffer.setSize(retval); |
176 | 0 | } |
177 | |
|
178 | 0 | return (retval > 0); |
179 | 0 | } |
180 | | |
181 | | |
182 | | |
183 | | bool |
184 | | Socket::write(const SocketBuffer& buffer) |
185 | 0 | { |
186 | 0 | long retval = helpers::write(sock, buffer); |
187 | 0 | if(retval <= 0) { |
188 | 0 | close(); |
189 | 0 | } |
190 | |
|
191 | 0 | return (retval > 0); |
192 | 0 | } |
193 | | |
194 | | |
195 | | bool |
196 | | Socket::write(const STD_NAMESPACE string & buffer) |
197 | 0 | { |
198 | 0 | long retval = helpers::write (sock, buffer); |
199 | 0 | if (retval <= 0) |
200 | 0 | close(); |
201 | |
|
202 | 0 | return retval > 0; |
203 | 0 | } |
204 | | |
205 | | |
206 | | |
207 | | |
208 | | |
209 | | ////////////////////////////////////////////////////////////////////////////// |
210 | | // ServerSocket ctor and dtor |
211 | | ////////////////////////////////////////////////////////////////////////////// |
212 | | |
213 | | ServerSocket::ServerSocket(unsigned short port) |
214 | 0 | { |
215 | 0 | sock = openSocket(port, state); |
216 | 0 | if(sock == INVALID_SOCKET_VALUE) { |
217 | 0 | err = get_last_socket_error (); |
218 | 0 | } |
219 | 0 | } |
220 | | |
221 | | |
222 | | |
223 | | ServerSocket::~ServerSocket() |
224 | | { |
225 | | } |
226 | | |
227 | | |
228 | | |
229 | | ////////////////////////////////////////////////////////////////////////////// |
230 | | // ServerSocket methods |
231 | | ////////////////////////////////////////////////////////////////////////////// |
232 | | |
233 | | Socket |
234 | | ServerSocket::accept() |
235 | 0 | { |
236 | 0 | SocketState st = not_opened; |
237 | 0 | SOCKET_TYPE clientSock = acceptSocket(sock, st); |
238 | 0 | return Socket(clientSock, st, 0); |
239 | 0 | } |
240 | | |
241 | | |
242 | | } } // namespace log4cplus { namespace helpers { |
243 | | } // end namespace dcmtk |