update dev300-m58
[ooovba.git] / sal / inc / osl / socket.hxx
blob50d014c994c3dfe6125a7c844f937ef367a480f0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: socket.hxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #ifndef _OSL_SOCKET_HXX_
31 #define _OSL_SOCKET_HXX_
33 #include <osl/socket_decl.hxx>
35 namespace osl
37 //______________________________________________________________________________
38 inline SocketAddr::SocketAddr()
39 : m_handle( osl_createEmptySocketAddr( osl_Socket_FamilyInet ) )
42 //______________________________________________________________________________
43 inline SocketAddr::SocketAddr(const SocketAddr& Addr)
44 : m_handle( osl_copySocketAddr( Addr.m_handle ) )
48 //______________________________________________________________________________
49 inline SocketAddr::SocketAddr(oslSocketAddr Addr)
50 : m_handle( osl_copySocketAddr( Addr ) )
54 //______________________________________________________________________________
55 inline SocketAddr::SocketAddr(oslSocketAddr Addr, __osl_socket_NoCopy )
56 : m_handle( Addr )
60 //______________________________________________________________________________
61 inline SocketAddr::SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort)
62 : m_handle( osl_createInetSocketAddr( strAddrOrHostName.pData, nPort ) )
64 if(! m_handle )
66 m_handle = osl_resolveHostname(strAddrOrHostName.pData);
68 // host found?
69 if(m_handle)
71 osl_setInetPortOfSocketAddr(m_handle, nPort);
73 else
75 osl_destroySocketAddr( m_handle );
76 m_handle = 0;
81 //______________________________________________________________________________
82 inline SocketAddr::~SocketAddr()
84 if( m_handle )
85 osl_destroySocketAddr( m_handle );
88 //______________________________________________________________________________
89 inline ::rtl::OUString SocketAddr::getHostname( oslSocketResult *pResult ) const
91 ::rtl::OUString hostname;
92 oslSocketResult result = osl_getHostnameOfSocketAddr( m_handle, &(hostname.pData) );
93 if( pResult )
94 *pResult = result;
95 return hostname;
98 //______________________________________________________________________________
99 inline sal_Int32 SAL_CALL SocketAddr::getPort() const
101 return osl_getInetPortOfSocketAddr(m_handle);
104 //______________________________________________________________________________
105 inline sal_Bool SAL_CALL SocketAddr::setPort( sal_Int32 nPort )
107 return osl_setInetPortOfSocketAddr(m_handle, nPort );
110 inline sal_Bool SAL_CALL SocketAddr::setHostname( const ::rtl::OUString &sDottedIpOrHostname )
112 *this = SocketAddr( sDottedIpOrHostname , getPort() );
113 return is();
116 //______________________________________________________________________________
117 inline sal_Bool SAL_CALL SocketAddr::setAddr( const ::rtl::ByteSequence & address )
119 return osl_setAddrOfSocketAddr( m_handle, address.getHandle() )
120 == osl_Socket_Ok;
123 inline ::rtl::ByteSequence SAL_CALL SocketAddr::getAddr( oslSocketResult *pResult ) const
125 ::rtl::ByteSequence sequence;
126 oslSocketResult result = osl_getAddrOfSocketAddr( m_handle,(sal_Sequence **) &sequence );
127 if( pResult )
128 *pResult = result;
129 return sequence;
132 //______________________________________________________________________________
133 inline SocketAddr & SAL_CALL SocketAddr::operator= (oslSocketAddr Addr)
135 oslSocketAddr pNewAddr = osl_copySocketAddr( Addr );
136 if( m_handle )
137 osl_destroySocketAddr( m_handle );
138 m_handle = pNewAddr;
139 return *this;
142 //______________________________________________________________________________
143 inline SocketAddr & SAL_CALL SocketAddr::operator= (const SocketAddr& Addr)
145 *this = (Addr.getHandle());
146 return *this;
149 inline SocketAddr & SAL_CALL SocketAddr::assign( oslSocketAddr Addr, __osl_socket_NoCopy )
151 if( m_handle )
152 osl_destroySocketAddr( m_handle );
153 m_handle = Addr;
154 return *this;
157 //______________________________________________________________________________
158 inline sal_Bool SAL_CALL SocketAddr::operator== (oslSocketAddr Addr) const
160 return osl_isEqualSocketAddr( m_handle, Addr );
163 inline oslSocketAddr SocketAddr::getHandle() const
165 return m_handle;
168 //______________________________________________________________________________
169 inline sal_Bool SocketAddr::is() const
171 return m_handle != 0;
174 // (static method)______________________________________________________________
175 inline ::rtl::OUString SAL_CALL SocketAddr::getLocalHostname( oslSocketResult *pResult )
177 ::rtl::OUString hostname;
178 oslSocketResult result = osl_getLocalHostname( &(hostname.pData) );
179 if(pResult )
180 *pResult = result;
181 return hostname;
184 // (static method)______________________________________________________________
185 inline void SAL_CALL SocketAddr::resolveHostname(
186 const ::rtl::OUString & strHostName, SocketAddr &Addr)
188 Addr = SocketAddr( osl_resolveHostname( strHostName.pData ) , SAL_NO_COPY );
191 // (static method)______________________________________________________________
192 inline sal_Int32 SAL_CALL SocketAddr::getServicePort(
193 const ::rtl::OUString& strServiceName,
194 const ::rtl::OUString & strProtocolName )
196 return osl_getServicePort( strServiceName.pData, strProtocolName.pData );
199 //______________________________________________________________________________
200 inline Socket::Socket(oslSocketType Type,
201 oslAddrFamily Family,
202 oslProtocol Protocol)
203 : m_handle( osl_createSocket(Family, Type, Protocol) )
206 //______________________________________________________________________________
207 inline Socket::Socket( oslSocket socketHandle, __sal_NoAcquire )
208 : m_handle( socketHandle )
211 //______________________________________________________________________________
212 inline Socket::Socket( oslSocket socketHandle )
213 : m_handle( socketHandle )
215 osl_acquireSocket( m_handle );
218 //______________________________________________________________________________
219 inline Socket::Socket( const Socket & socket )
220 : m_handle( socket.getHandle() )
222 osl_acquireSocket( m_handle );
225 //______________________________________________________________________________
226 inline Socket::~Socket()
228 osl_releaseSocket( m_handle );
231 //______________________________________________________________________________
232 inline Socket& Socket::operator= ( oslSocket socketHandle)
234 osl_acquireSocket( socketHandle );
235 osl_releaseSocket( m_handle );
236 m_handle = socketHandle;
237 return *this;
240 //______________________________________________________________________________
241 inline Socket& Socket::operator= (const Socket& sock)
243 return (*this) = sock.getHandle();
246 //______________________________________________________________________________
247 inline sal_Bool Socket::operator==( const Socket& rSocket ) const
249 return m_handle == rSocket.getHandle();
252 //______________________________________________________________________________
253 inline sal_Bool Socket::operator==( const oslSocket socketHandle ) const
255 return m_handle == socketHandle;
258 //______________________________________________________________________________
259 inline void Socket::shutdown( oslSocketDirection Direction )
261 osl_shutdownSocket( m_handle , Direction );
264 //______________________________________________________________________________
265 inline void Socket::close()
267 osl_closeSocket( m_handle );
270 //______________________________________________________________________________
271 inline void Socket::getLocalAddr( SocketAddr & addr) const
273 addr.assign( osl_getLocalAddrOfSocket( m_handle ) , SAL_NO_COPY );
276 //______________________________________________________________________________
277 inline sal_Int32 Socket::getLocalPort() const
279 SocketAddr addr( 0 );
280 getLocalAddr( addr );
281 return addr.getPort();
284 //______________________________________________________________________________
285 inline ::rtl::OUString Socket::getLocalHost() const
287 SocketAddr addr( 0 );
288 getLocalAddr( addr );
289 return addr.getHostname();
292 //______________________________________________________________________________
293 inline void Socket::getPeerAddr( SocketAddr &addr ) const
295 addr.assign( osl_getPeerAddrOfSocket( m_handle ), SAL_NO_COPY );
298 //______________________________________________________________________________
299 inline sal_Int32 Socket::getPeerPort() const
301 SocketAddr addr( 0 );
302 getPeerAddr( addr );
303 return addr.getPort();
306 //______________________________________________________________________________
307 inline ::rtl::OUString Socket::getPeerHost() const
309 SocketAddr addr( 0 );
310 getPeerAddr( addr );
311 return addr.getHostname();
314 //______________________________________________________________________________
315 inline sal_Bool Socket::bind(const SocketAddr& LocalInterface)
317 return osl_bindAddrToSocket( m_handle , LocalInterface.getHandle() );
320 //______________________________________________________________________________
321 inline sal_Bool Socket::isRecvReady(const TimeValue *pTimeout ) const
323 return osl_isReceiveReady( m_handle , pTimeout );
326 //______________________________________________________________________________
327 inline sal_Bool Socket::isSendReady(const TimeValue *pTimeout ) const
329 return osl_isSendReady( m_handle, pTimeout );
332 //______________________________________________________________________________
333 inline sal_Bool Socket::isExceptionPending(const TimeValue *pTimeout ) const
335 return osl_isExceptionPending( m_handle, pTimeout );
338 //______________________________________________________________________________
339 inline oslSocketType Socket::getType() const
341 return osl_getSocketType( m_handle );
344 //______________________________________________________________________________
345 inline sal_Int32 Socket::getOption(
346 oslSocketOption Option,
347 void* pBuffer,
348 sal_uInt32 BufferLen,
349 oslSocketOptionLevel Level) const
351 return osl_getSocketOption( m_handle, Level, Option, pBuffer , BufferLen );
354 //______________________________________________________________________________
355 inline sal_Bool Socket::setOption( oslSocketOption Option,
356 void* pBuffer,
357 sal_uInt32 BufferLen,
358 oslSocketOptionLevel Level ) const
360 return osl_setSocketOption( m_handle, Level, Option , pBuffer, BufferLen );
363 //______________________________________________________________________________
364 inline sal_Bool Socket::setOption( oslSocketOption option, sal_Int32 nValue )
366 return setOption( option, &nValue, sizeof( nValue ) );
369 //______________________________________________________________________________
370 inline sal_Int32 Socket::getOption( oslSocketOption option ) const
372 sal_Int32 n;
373 getOption( option, &n, sizeof( n ) );
374 return n;
377 //______________________________________________________________________________
378 inline sal_Bool Socket::enableNonBlockingMode( sal_Bool bNonBlockingMode)
380 return osl_enableNonBlockingMode( m_handle , bNonBlockingMode );
383 //______________________________________________________________________________
384 inline sal_Bool Socket::isNonBlockingMode() const
386 return osl_isNonBlockingMode( m_handle );
389 //______________________________________________________________________________
390 inline void SAL_CALL Socket::clearError() const
392 sal_Int32 err = 0;
393 getOption(osl_Socket_OptionError, &err, sizeof(err));
396 //______________________________________________________________________________
397 inline oslSocketError Socket::getError() const
399 return osl_getLastSocketError( m_handle );
402 //______________________________________________________________________________
403 inline ::rtl::OUString Socket::getErrorAsString( ) const
405 ::rtl::OUString error;
406 osl_getLastSocketErrorDescription( m_handle, &(error.pData) );
407 return error;
410 //______________________________________________________________________________
411 inline oslSocket Socket::getHandle() const
413 return m_handle;
416 //______________________________________________________________________________
417 inline StreamSocket::StreamSocket(oslAddrFamily Family,
418 oslProtocol Protocol,
419 oslSocketType Type )
420 : Socket( Type, Family, Protocol )
423 //______________________________________________________________________________
424 inline StreamSocket::StreamSocket( oslSocket socketHandle, __sal_NoAcquire noacquire )
425 : Socket( socketHandle, noacquire )
428 //______________________________________________________________________________
429 inline StreamSocket::StreamSocket( oslSocket socketHandle )
430 : Socket( socketHandle )
433 //______________________________________________________________________________
434 inline StreamSocket::StreamSocket( const StreamSocket & socket )
435 : Socket( socket )
438 //______________________________________________________________________________
439 inline sal_Int32 StreamSocket::read(void* pBuffer, sal_uInt32 n)
441 return osl_readSocket( m_handle, pBuffer, n );
444 //______________________________________________________________________________
445 inline sal_Int32 StreamSocket::write(const void* pBuffer, sal_uInt32 n)
447 return osl_writeSocket( m_handle, pBuffer, n );
451 //______________________________________________________________________________
452 inline sal_Int32 StreamSocket::recv(void* pBuffer,
453 sal_uInt32 BytesToRead,
454 oslSocketMsgFlag Flag)
456 return osl_receiveSocket( m_handle, pBuffer,BytesToRead, Flag );
459 //______________________________________________________________________________
460 inline sal_Int32 StreamSocket::send(const void* pBuffer,
461 sal_uInt32 BytesToSend,
462 oslSocketMsgFlag Flag)
464 return osl_sendSocket( m_handle, pBuffer, BytesToSend, Flag );
467 //______________________________________________________________________________
468 inline ConnectorSocket::ConnectorSocket(oslAddrFamily Family,
469 oslProtocol Protocol,
470 oslSocketType Type)
471 : StreamSocket( Family, Protocol ,Type )
474 //______________________________________________________________________________
475 inline oslSocketResult ConnectorSocket::connect( const SocketAddr& TargetHost,
476 const TimeValue* pTimeout )
478 return osl_connectSocketTo( m_handle , TargetHost.getHandle(), pTimeout );
481 //______________________________________________________________________________
482 inline AcceptorSocket::AcceptorSocket(oslAddrFamily Family ,
483 oslProtocol Protocol ,
484 oslSocketType Type )
485 : Socket( Type, Family, Protocol )
488 //______________________________________________________________________________
489 inline sal_Bool AcceptorSocket::listen(sal_Int32 MaxPendingConnections)
491 return osl_listenOnSocket( m_handle, MaxPendingConnections );
494 //______________________________________________________________________________
495 inline oslSocketResult AcceptorSocket::acceptConnection( StreamSocket& Connection)
497 oslSocket o = osl_acceptConnectionOnSocket( m_handle, 0 );
498 oslSocketResult status = osl_Socket_Ok;
499 if( o )
501 Connection = StreamSocket( o , SAL_NO_ACQUIRE );
503 else
505 Connection = StreamSocket();
506 status = osl_Socket_Error;
508 return status;
511 //______________________________________________________________________________
512 inline oslSocketResult AcceptorSocket::acceptConnection(
513 StreamSocket& Connection, SocketAddr & PeerAddr)
515 // TODO change in/OUT parameter
516 oslSocket o = osl_acceptConnectionOnSocket( m_handle, (oslSocketAddr *)&PeerAddr );
517 oslSocketResult status = osl_Socket_Ok;
518 if( o )
520 Connection = StreamSocket( o , SAL_NO_ACQUIRE );
522 else
524 Connection = StreamSocket();
525 status = osl_Socket_Error;
527 return status;
530 //______________________________________________________________________________
531 inline DatagramSocket::DatagramSocket(oslAddrFamily Family,
532 oslProtocol Protocol,
533 oslSocketType Type)
534 : Socket( Type, Family, Protocol )
537 //______________________________________________________________________________
538 inline sal_Int32 DatagramSocket::recvFrom(void* pBuffer,
539 sal_uInt32 BufferSize,
540 SocketAddr* pSenderAddr,
541 oslSocketMsgFlag Flag )
543 sal_Int32 nByteRead;
544 if( pSenderAddr )
546 // TODO : correct the out-parameter pSenderAddr outparameter
547 nByteRead = osl_receiveFromSocket( m_handle, pSenderAddr->getHandle() , pBuffer,
548 BufferSize, Flag);
549 // nByteRead = osl_receiveFromSocket( m_handle, *(oslSocketAddr**) &pSenderAddr , pBuffer,
550 // BufferSize, Flag);
552 else
554 nByteRead = osl_receiveFromSocket( m_handle, 0 , pBuffer , BufferSize , Flag );
556 return nByteRead;
559 //______________________________________________________________________________
560 inline sal_Int32 DatagramSocket::sendTo( const SocketAddr& ReceiverAddr,
561 const void* pBuffer,
562 sal_uInt32 BufferSize,
563 oslSocketMsgFlag Flag )
565 return osl_sendToSocket( m_handle, ReceiverAddr.getHandle(), pBuffer, BufferSize, Flag );
568 #endif