Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : : #ifndef _OSL_SOCKET_HXX_
29 : : #define _OSL_SOCKET_HXX_
30 : :
31 : : #include <osl/socket_decl.hxx>
32 : :
33 : : namespace osl
34 : : {
35 : : //______________________________________________________________________________
36 : 0 : inline SocketAddr::SocketAddr()
37 : 0 : : m_handle( osl_createEmptySocketAddr( osl_Socket_FamilyInet ) )
38 : 0 : {}
39 : :
40 : : //______________________________________________________________________________
41 : : inline SocketAddr::SocketAddr(const SocketAddr& Addr)
42 : : : m_handle( osl_copySocketAddr( Addr.m_handle ) )
43 : : {
44 : : }
45 : :
46 : : //______________________________________________________________________________
47 : 0 : inline SocketAddr::SocketAddr(oslSocketAddr Addr)
48 : 0 : : m_handle( osl_copySocketAddr( Addr ) )
49 : : {
50 : 0 : }
51 : :
52 : : //______________________________________________________________________________
53 : 0 : inline SocketAddr::SocketAddr(oslSocketAddr Addr, __osl_socket_NoCopy )
54 : 0 : : m_handle( Addr )
55 : : {
56 : 0 : }
57 : :
58 : : //______________________________________________________________________________
59 : 0 : inline SocketAddr::SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort)
60 : 0 : : m_handle( osl_createInetSocketAddr( strAddrOrHostName.pData, nPort ) )
61 : : {
62 [ # # ]: 0 : if(! m_handle )
63 : : {
64 : 0 : m_handle = osl_resolveHostname(strAddrOrHostName.pData);
65 : :
66 : : // host found?
67 [ # # ]: 0 : if(m_handle)
68 : : {
69 : 0 : osl_setInetPortOfSocketAddr(m_handle, nPort);
70 : : }
71 : : else
72 : : {
73 : 0 : osl_destroySocketAddr( m_handle );
74 : 0 : m_handle = 0;
75 : : }
76 : : }
77 : 0 : }
78 : :
79 : : //______________________________________________________________________________
80 : 0 : inline SocketAddr::~SocketAddr()
81 : : {
82 [ # # ]: 0 : if( m_handle )
83 : 0 : osl_destroySocketAddr( m_handle );
84 : 0 : }
85 : :
86 : : //______________________________________________________________________________
87 : 0 : inline ::rtl::OUString SocketAddr::getHostname( oslSocketResult *pResult ) const
88 : : {
89 : 0 : ::rtl::OUString hostname;
90 [ # # ]: 0 : oslSocketResult result = osl_getHostnameOfSocketAddr( m_handle, &(hostname.pData) );
91 [ # # ]: 0 : if( pResult )
92 : 0 : *pResult = result;
93 : 0 : return hostname;
94 : : }
95 : :
96 : : //______________________________________________________________________________
97 : 0 : inline sal_Int32 SAL_CALL SocketAddr::getPort() const
98 : : {
99 : 0 : return osl_getInetPortOfSocketAddr(m_handle);
100 : : }
101 : :
102 : : //______________________________________________________________________________
103 : 0 : inline sal_Bool SAL_CALL SocketAddr::setPort( sal_Int32 nPort )
104 : : {
105 : 0 : return osl_setInetPortOfSocketAddr(m_handle, nPort );
106 : : }
107 : :
108 : 0 : inline sal_Bool SAL_CALL SocketAddr::setHostname( const ::rtl::OUString &sDottedIpOrHostname )
109 : : {
110 [ # # ]: 0 : *this = SocketAddr( sDottedIpOrHostname , getPort() );
111 : 0 : return is();
112 : : }
113 : :
114 : : //______________________________________________________________________________
115 : : inline sal_Bool SAL_CALL SocketAddr::setAddr( const ::rtl::ByteSequence & address )
116 : : {
117 : : return osl_setAddrOfSocketAddr( m_handle, address.getHandle() )
118 : : == osl_Socket_Ok;
119 : : }
120 : :
121 : : inline ::rtl::ByteSequence SAL_CALL SocketAddr::getAddr( oslSocketResult *pResult ) const
122 : : {
123 : : ::rtl::ByteSequence sequence;
124 : : oslSocketResult result = osl_getAddrOfSocketAddr( m_handle,(sal_Sequence **) &sequence );
125 : : if( pResult )
126 : : *pResult = result;
127 : : return sequence;
128 : : }
129 : :
130 : : //______________________________________________________________________________
131 : 0 : inline SocketAddr & SAL_CALL SocketAddr::operator= (oslSocketAddr Addr)
132 : : {
133 : 0 : oslSocketAddr pNewAddr = osl_copySocketAddr( Addr );
134 [ # # ]: 0 : if( m_handle )
135 : 0 : osl_destroySocketAddr( m_handle );
136 : 0 : m_handle = pNewAddr;
137 : 0 : return *this;
138 : : }
139 : :
140 : : //______________________________________________________________________________
141 : 0 : inline SocketAddr & SAL_CALL SocketAddr::operator= (const SocketAddr& Addr)
142 : : {
143 : 0 : *this = (Addr.getHandle());
144 : 0 : return *this;
145 : : }
146 : :
147 : 0 : inline SocketAddr & SAL_CALL SocketAddr::assign( oslSocketAddr Addr, __osl_socket_NoCopy )
148 : : {
149 [ # # ]: 0 : if( m_handle )
150 : 0 : osl_destroySocketAddr( m_handle );
151 : 0 : m_handle = Addr;
152 : 0 : return *this;
153 : : }
154 : :
155 : : //______________________________________________________________________________
156 : : inline sal_Bool SAL_CALL SocketAddr::operator== (oslSocketAddr Addr) const
157 : : {
158 : : return osl_isEqualSocketAddr( m_handle, Addr );
159 : : }
160 : :
161 : 0 : inline oslSocketAddr SocketAddr::getHandle() const
162 : : {
163 : 0 : return m_handle;
164 : : }
165 : :
166 : : //______________________________________________________________________________
167 : 0 : inline sal_Bool SocketAddr::is() const
168 : : {
169 : 0 : return m_handle != 0;
170 : : }
171 : :
172 : : // (static method)______________________________________________________________
173 : 182 : inline ::rtl::OUString SAL_CALL SocketAddr::getLocalHostname( oslSocketResult *pResult )
174 : : {
175 : 182 : ::rtl::OUString hostname;
176 [ + - ]: 182 : oslSocketResult result = osl_getLocalHostname( &(hostname.pData) );
177 [ - + ]: 182 : if(pResult )
178 : 0 : *pResult = result;
179 : 182 : return hostname;
180 : : }
181 : :
182 : : // (static method)______________________________________________________________
183 : 0 : inline void SAL_CALL SocketAddr::resolveHostname(
184 : : const ::rtl::OUString & strHostName, SocketAddr &Addr)
185 : : {
186 [ # # ]: 0 : Addr = SocketAddr( osl_resolveHostname( strHostName.pData ) , SAL_NO_COPY );
187 : 0 : }
188 : :
189 : : // (static method)______________________________________________________________
190 : : inline sal_Int32 SAL_CALL SocketAddr::getServicePort(
191 : : const ::rtl::OUString& strServiceName,
192 : : const ::rtl::OUString & strProtocolName )
193 : : {
194 : : return osl_getServicePort( strServiceName.pData, strProtocolName.pData );
195 : : }
196 : :
197 : : //______________________________________________________________________________
198 : 0 : inline Socket::Socket(oslSocketType Type,
199 : : oslAddrFamily Family,
200 : : oslProtocol Protocol)
201 : 0 : : m_handle( osl_createSocket(Family, Type, Protocol) )
202 : 0 : {}
203 : :
204 : : //______________________________________________________________________________
205 : 0 : inline Socket::Socket( oslSocket socketHandle, __sal_NoAcquire )
206 : 0 : : m_handle( socketHandle )
207 : 0 : {}
208 : :
209 : : //______________________________________________________________________________
210 : : inline Socket::Socket( oslSocket socketHandle )
211 : : : m_handle( socketHandle )
212 : : {
213 : : osl_acquireSocket( m_handle );
214 : : }
215 : :
216 : : //______________________________________________________________________________
217 : 0 : inline Socket::Socket( const Socket & socket )
218 : 0 : : m_handle( socket.getHandle() )
219 : : {
220 : 0 : osl_acquireSocket( m_handle );
221 : 0 : }
222 : :
223 : : //______________________________________________________________________________
224 : 0 : inline Socket::~Socket()
225 : : {
226 : 0 : osl_releaseSocket( m_handle );
227 : 0 : }
228 : :
229 : : //______________________________________________________________________________
230 : 0 : inline Socket& Socket::operator= ( oslSocket socketHandle)
231 : : {
232 : 0 : osl_acquireSocket( socketHandle );
233 : 0 : osl_releaseSocket( m_handle );
234 : 0 : m_handle = socketHandle;
235 : 0 : return *this;
236 : : }
237 : :
238 : : //______________________________________________________________________________
239 : 0 : inline Socket& Socket::operator= (const Socket& sock)
240 : : {
241 : 0 : return (*this) = sock.getHandle();
242 : : }
243 : :
244 : : //______________________________________________________________________________
245 : : inline sal_Bool Socket::operator==( const Socket& rSocket ) const
246 : : {
247 : : return m_handle == rSocket.getHandle();
248 : : }
249 : :
250 : : //______________________________________________________________________________
251 : : inline sal_Bool Socket::operator==( const oslSocket socketHandle ) const
252 : : {
253 : : return m_handle == socketHandle;
254 : : }
255 : :
256 : : //______________________________________________________________________________
257 : 0 : inline void Socket::shutdown( oslSocketDirection Direction )
258 : : {
259 : 0 : osl_shutdownSocket( m_handle , Direction );
260 : 0 : }
261 : :
262 : : //______________________________________________________________________________
263 : 0 : inline void Socket::close()
264 : : {
265 : 0 : osl_closeSocket( m_handle );
266 : 0 : }
267 : :
268 : : //______________________________________________________________________________
269 : 0 : inline void Socket::getLocalAddr( SocketAddr & addr) const
270 : : {
271 : 0 : addr.assign( osl_getLocalAddrOfSocket( m_handle ) , SAL_NO_COPY );
272 : 0 : }
273 : :
274 : : //______________________________________________________________________________
275 : 0 : inline sal_Int32 Socket::getLocalPort() const
276 : : {
277 [ # # ]: 0 : SocketAddr addr( 0 );
278 [ # # ]: 0 : getLocalAddr( addr );
279 [ # # ][ # # ]: 0 : return addr.getPort();
280 : : }
281 : :
282 : : //______________________________________________________________________________
283 : 0 : inline ::rtl::OUString Socket::getLocalHost() const
284 : : {
285 [ # # ]: 0 : SocketAddr addr( 0 );
286 [ # # ]: 0 : getLocalAddr( addr );
287 [ # # ][ # # ]: 0 : return addr.getHostname();
288 : : }
289 : :
290 : : //______________________________________________________________________________
291 : 0 : inline void Socket::getPeerAddr( SocketAddr &addr ) const
292 : : {
293 : 0 : addr.assign( osl_getPeerAddrOfSocket( m_handle ), SAL_NO_COPY );
294 : 0 : }
295 : :
296 : : //______________________________________________________________________________
297 : 0 : inline sal_Int32 Socket::getPeerPort() const
298 : : {
299 [ # # ]: 0 : SocketAddr addr( 0 );
300 [ # # ]: 0 : getPeerAddr( addr );
301 [ # # ][ # # ]: 0 : return addr.getPort();
302 : : }
303 : :
304 : : //______________________________________________________________________________
305 : 0 : inline ::rtl::OUString Socket::getPeerHost() const
306 : : {
307 [ # # ]: 0 : SocketAddr addr( 0 );
308 [ # # ]: 0 : getPeerAddr( addr );
309 [ # # ][ # # ]: 0 : return addr.getHostname();
310 : : }
311 : :
312 : : //______________________________________________________________________________
313 : 0 : inline sal_Bool Socket::bind(const SocketAddr& LocalInterface)
314 : : {
315 : 0 : return osl_bindAddrToSocket( m_handle , LocalInterface.getHandle() );
316 : : }
317 : :
318 : : //______________________________________________________________________________
319 : : inline sal_Bool Socket::isRecvReady(const TimeValue *pTimeout ) const
320 : : {
321 : : return osl_isReceiveReady( m_handle , pTimeout );
322 : : }
323 : :
324 : : //______________________________________________________________________________
325 : : inline sal_Bool Socket::isSendReady(const TimeValue *pTimeout ) const
326 : : {
327 : : return osl_isSendReady( m_handle, pTimeout );
328 : : }
329 : :
330 : : //______________________________________________________________________________
331 : : inline sal_Bool Socket::isExceptionPending(const TimeValue *pTimeout ) const
332 : : {
333 : : return osl_isExceptionPending( m_handle, pTimeout );
334 : : }
335 : :
336 : : //______________________________________________________________________________
337 : : inline oslSocketType Socket::getType() const
338 : : {
339 : : return osl_getSocketType( m_handle );
340 : : }
341 : :
342 : : //______________________________________________________________________________
343 : : inline sal_Int32 Socket::getOption(
344 : : oslSocketOption Option,
345 : : void* pBuffer,
346 : : sal_uInt32 BufferLen,
347 : : oslSocketOptionLevel Level) const
348 : : {
349 : : return osl_getSocketOption( m_handle, Level, Option, pBuffer , BufferLen );
350 : : }
351 : :
352 : : //______________________________________________________________________________
353 : 0 : inline sal_Bool Socket::setOption( oslSocketOption Option,
354 : : void* pBuffer,
355 : : sal_uInt32 BufferLen,
356 : : oslSocketOptionLevel Level ) const
357 : : {
358 : 0 : return osl_setSocketOption( m_handle, Level, Option , pBuffer, BufferLen );
359 : : }
360 : :
361 : : //______________________________________________________________________________
362 : 0 : inline sal_Bool Socket::setOption( oslSocketOption option, sal_Int32 nValue )
363 : : {
364 : 0 : return setOption( option, &nValue, sizeof( nValue ) );
365 : : }
366 : :
367 : : //______________________________________________________________________________
368 : : inline sal_Int32 Socket::getOption( oslSocketOption option ) const
369 : : {
370 : : sal_Int32 n;
371 : : getOption( option, &n, sizeof( n ) );
372 : : return n;
373 : : }
374 : :
375 : : //______________________________________________________________________________
376 : : inline sal_Bool Socket::enableNonBlockingMode( sal_Bool bNonBlockingMode)
377 : : {
378 : : return osl_enableNonBlockingMode( m_handle , bNonBlockingMode );
379 : : }
380 : :
381 : : //______________________________________________________________________________
382 : : inline sal_Bool Socket::isNonBlockingMode() const
383 : : {
384 : : return osl_isNonBlockingMode( m_handle );
385 : : }
386 : :
387 : : //______________________________________________________________________________
388 : : inline void SAL_CALL Socket::clearError() const
389 : : {
390 : : sal_Int32 err = 0;
391 : : getOption(osl_Socket_OptionError, &err, sizeof(err));
392 : : }
393 : :
394 : : //______________________________________________________________________________
395 : 0 : inline oslSocketError Socket::getError() const
396 : : {
397 : 0 : return osl_getLastSocketError( m_handle );
398 : : }
399 : :
400 : : //______________________________________________________________________________
401 : 0 : inline ::rtl::OUString Socket::getErrorAsString( ) const
402 : : {
403 : 0 : ::rtl::OUString error;
404 [ # # ]: 0 : osl_getLastSocketErrorDescription( m_handle, &(error.pData) );
405 : 0 : return error;
406 : : }
407 : :
408 : : //______________________________________________________________________________
409 : 0 : inline oslSocket Socket::getHandle() const
410 : : {
411 : 0 : return m_handle;
412 : : }
413 : :
414 : : //______________________________________________________________________________
415 : 0 : inline StreamSocket::StreamSocket(oslAddrFamily Family,
416 : : oslProtocol Protocol,
417 : : oslSocketType Type )
418 : 0 : : Socket( Type, Family, Protocol )
419 : 0 : {}
420 : :
421 : : //______________________________________________________________________________
422 : 0 : inline StreamSocket::StreamSocket( oslSocket socketHandle, __sal_NoAcquire noacquire )
423 : 0 : : Socket( socketHandle, noacquire )
424 : 0 : {}
425 : :
426 : : //______________________________________________________________________________
427 : : inline StreamSocket::StreamSocket( oslSocket socketHandle )
428 : : : Socket( socketHandle )
429 : : {}
430 : :
431 : : //______________________________________________________________________________
432 : 0 : inline StreamSocket::StreamSocket( const StreamSocket & socket )
433 : 0 : : Socket( socket )
434 : 0 : {}
435 : :
436 : : //______________________________________________________________________________
437 : 0 : inline sal_Int32 StreamSocket::read(void* pBuffer, sal_uInt32 n)
438 : : {
439 : 0 : return osl_readSocket( m_handle, pBuffer, n );
440 : : }
441 : :
442 : : //______________________________________________________________________________
443 : 0 : inline sal_Int32 StreamSocket::write(const void* pBuffer, sal_uInt32 n)
444 : : {
445 : 0 : return osl_writeSocket( m_handle, pBuffer, n );
446 : : }
447 : :
448 : :
449 : : //______________________________________________________________________________
450 : 0 : inline sal_Int32 StreamSocket::recv(void* pBuffer,
451 : : sal_uInt32 BytesToRead,
452 : : oslSocketMsgFlag Flag)
453 : : {
454 : 0 : return osl_receiveSocket( m_handle, pBuffer,BytesToRead, Flag );
455 : : }
456 : :
457 : : //______________________________________________________________________________
458 : : inline sal_Int32 StreamSocket::send(const void* pBuffer,
459 : : sal_uInt32 BytesToSend,
460 : : oslSocketMsgFlag Flag)
461 : : {
462 : : return osl_sendSocket( m_handle, pBuffer, BytesToSend, Flag );
463 : : }
464 : :
465 : : //______________________________________________________________________________
466 : 0 : inline ConnectorSocket::ConnectorSocket(oslAddrFamily Family,
467 : : oslProtocol Protocol,
468 : : oslSocketType Type)
469 : 0 : : StreamSocket( Family, Protocol ,Type )
470 : 0 : {}
471 : :
472 : : //______________________________________________________________________________
473 : 0 : inline oslSocketResult ConnectorSocket::connect( const SocketAddr& TargetHost,
474 : : const TimeValue* pTimeout )
475 : : {
476 : 0 : return osl_connectSocketTo( m_handle , TargetHost.getHandle(), pTimeout );
477 : : }
478 : :
479 : : //______________________________________________________________________________
480 : 0 : inline AcceptorSocket::AcceptorSocket(oslAddrFamily Family ,
481 : : oslProtocol Protocol ,
482 : : oslSocketType Type )
483 : 0 : : Socket( Type, Family, Protocol )
484 : 0 : {}
485 : :
486 : : //______________________________________________________________________________
487 : 0 : inline sal_Bool AcceptorSocket::listen(sal_Int32 MaxPendingConnections)
488 : : {
489 : 0 : return osl_listenOnSocket( m_handle, MaxPendingConnections );
490 : : }
491 : :
492 : : //______________________________________________________________________________
493 : 0 : inline oslSocketResult AcceptorSocket::acceptConnection( StreamSocket& Connection)
494 : : {
495 : 0 : oslSocket o = osl_acceptConnectionOnSocket( m_handle, 0 );
496 : 0 : oslSocketResult status = osl_Socket_Ok;
497 [ # # ]: 0 : if( o )
498 : : {
499 [ # # ]: 0 : Connection = StreamSocket( o , SAL_NO_ACQUIRE );
500 : : }
501 : : else
502 : : {
503 [ # # ]: 0 : Connection = StreamSocket();
504 : 0 : status = osl_Socket_Error;
505 : : }
506 : 0 : return status;
507 : : }
508 : :
509 : : //______________________________________________________________________________
510 : : inline oslSocketResult AcceptorSocket::acceptConnection(
511 : : StreamSocket& Connection, SocketAddr & PeerAddr)
512 : : {
513 : : // TODO change in/OUT parameter
514 : : oslSocket o = osl_acceptConnectionOnSocket( m_handle, (oslSocketAddr *)&PeerAddr );
515 : : oslSocketResult status = osl_Socket_Ok;
516 : : if( o )
517 : : {
518 : : Connection = StreamSocket( o , SAL_NO_ACQUIRE );
519 : : }
520 : : else
521 : : {
522 : : Connection = StreamSocket();
523 : : status = osl_Socket_Error;
524 : : }
525 : : return status;
526 : : }
527 : :
528 : : //______________________________________________________________________________
529 : : inline DatagramSocket::DatagramSocket(oslAddrFamily Family,
530 : : oslProtocol Protocol,
531 : : oslSocketType Type)
532 : : : Socket( Type, Family, Protocol )
533 : : {}
534 : :
535 : : //______________________________________________________________________________
536 : : inline sal_Int32 DatagramSocket::recvFrom(void* pBuffer,
537 : : sal_uInt32 BufferSize,
538 : : SocketAddr* pSenderAddr,
539 : : oslSocketMsgFlag Flag )
540 : : {
541 : : sal_Int32 nByteRead;
542 : : if( pSenderAddr )
543 : : {
544 : : // TODO : correct the out-parameter pSenderAddr outparameter
545 : : nByteRead = osl_receiveFromSocket( m_handle, pSenderAddr->getHandle() , pBuffer,
546 : : BufferSize, Flag);
547 : : // nByteRead = osl_receiveFromSocket( m_handle, *(oslSocketAddr**) &pSenderAddr , pBuffer,
548 : : // BufferSize, Flag);
549 : : }
550 : : else
551 : : {
552 : : nByteRead = osl_receiveFromSocket( m_handle, 0 , pBuffer , BufferSize , Flag );
553 : : }
554 : : return nByteRead;
555 : : }
556 : :
557 : : //______________________________________________________________________________
558 : : inline sal_Int32 DatagramSocket::sendTo( const SocketAddr& ReceiverAddr,
559 : : const void* pBuffer,
560 : : sal_uInt32 BufferSize,
561 : : oslSocketMsgFlag Flag )
562 : : {
563 : : return osl_sendToSocket( m_handle, ReceiverAddr.getHandle(), pBuffer, BufferSize, Flag );
564 : : }
565 : : }
566 : : #endif
567 : :
568 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|