1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifndef INCLUDED_OSL_SOCKET_HXX
20 #define INCLUDED_OSL_SOCKET_HXX
22 #include <osl/socket_decl.hxx>
27 inline SocketAddr::SocketAddr()
28 : m_handle( osl_createEmptySocketAddr( osl_Socket_FamilyInet
) )
32 inline SocketAddr::SocketAddr(const SocketAddr
& Addr
)
33 : m_handle( osl_copySocketAddr( Addr
.m_handle
) )
38 inline SocketAddr::SocketAddr(oslSocketAddr Addr
)
39 : m_handle( osl_copySocketAddr( Addr
) )
44 inline SocketAddr::SocketAddr(oslSocketAddr Addr
, __osl_socket_NoCopy
)
50 inline SocketAddr::SocketAddr( const ::rtl::OUString
& strAddrOrHostName
, sal_Int32 nPort
)
51 : m_handle( osl_createInetSocketAddr( strAddrOrHostName
.pData
, nPort
) )
55 m_handle
= osl_resolveHostname(strAddrOrHostName
.pData
);
60 osl_setInetPortOfSocketAddr(m_handle
, nPort
);
64 osl_destroySocketAddr( m_handle
);
71 inline SocketAddr::~SocketAddr()
74 osl_destroySocketAddr( m_handle
);
78 inline ::rtl::OUString
SocketAddr::getHostname( oslSocketResult
*pResult
) const
80 ::rtl::OUString hostname
;
81 oslSocketResult result
= osl_getHostnameOfSocketAddr( m_handle
, &(hostname
.pData
) );
88 inline sal_Int32 SAL_CALL
SocketAddr::getPort() const
90 return osl_getInetPortOfSocketAddr(m_handle
);
94 inline bool SAL_CALL
SocketAddr::setPort( sal_Int32 nPort
)
96 return osl_setInetPortOfSocketAddr(m_handle
, nPort
);
99 inline bool SAL_CALL
SocketAddr::setHostname( const ::rtl::OUString
&sDottedIpOrHostname
)
101 *this = SocketAddr( sDottedIpOrHostname
, getPort() );
106 inline bool SAL_CALL
SocketAddr::setAddr( const ::rtl::ByteSequence
& address
)
108 return osl_setAddrOfSocketAddr( m_handle
, address
.getHandle() )
112 inline ::rtl::ByteSequence SAL_CALL
SocketAddr::getAddr( oslSocketResult
*pResult
) const
114 ::rtl::ByteSequence sequence
;
115 oslSocketResult result
= osl_getAddrOfSocketAddr( m_handle
, reinterpret_cast<sal_Sequence
**>(&sequence
) );
122 inline SocketAddr
& SAL_CALL
SocketAddr::operator= (oslSocketAddr Addr
)
124 oslSocketAddr pNewAddr
= osl_copySocketAddr( Addr
);
126 osl_destroySocketAddr( m_handle
);
132 inline SocketAddr
& SAL_CALL
SocketAddr::operator= (const SocketAddr
& Addr
)
134 *this = (Addr
.getHandle());
138 inline SocketAddr
& SAL_CALL
SocketAddr::assign( oslSocketAddr Addr
, __osl_socket_NoCopy
)
141 osl_destroySocketAddr( m_handle
);
147 inline bool SAL_CALL
SocketAddr::operator== (oslSocketAddr Addr
) const
149 return osl_isEqualSocketAddr( m_handle
, Addr
);
152 inline oslSocketAddr
SocketAddr::getHandle() const
158 inline bool SocketAddr::is() const
160 return m_handle
!= 0;
163 // (static method)______________________________________________________________
164 inline ::rtl::OUString SAL_CALL
SocketAddr::getLocalHostname( oslSocketResult
*pResult
)
166 ::rtl::OUString hostname
;
167 oslSocketResult result
= osl_getLocalHostname( &(hostname
.pData
) );
173 // (static method)______________________________________________________________
174 inline void SAL_CALL
SocketAddr::resolveHostname(
175 const ::rtl::OUString
& strHostName
, SocketAddr
&Addr
)
177 Addr
= SocketAddr( osl_resolveHostname( strHostName
.pData
) , SAL_NO_COPY
);
180 // (static method)______________________________________________________________
181 inline sal_Int32 SAL_CALL
SocketAddr::getServicePort(
182 const ::rtl::OUString
& strServiceName
,
183 const ::rtl::OUString
& strProtocolName
)
185 return osl_getServicePort( strServiceName
.pData
, strProtocolName
.pData
);
189 inline Socket::Socket(oslSocketType Type
,
190 oslAddrFamily Family
,
191 oslProtocol Protocol
)
192 : m_handle( osl_createSocket(Family
, Type
, Protocol
) )
196 inline Socket::Socket( oslSocket socketHandle
, __sal_NoAcquire
)
197 : m_handle( socketHandle
)
201 inline Socket::Socket( oslSocket socketHandle
)
202 : m_handle( socketHandle
)
204 osl_acquireSocket( m_handle
);
208 inline Socket::Socket( const Socket
& socket
)
209 : m_handle( socket
.getHandle() )
211 osl_acquireSocket( m_handle
);
215 inline Socket::~Socket()
217 osl_releaseSocket( m_handle
);
221 inline Socket
& Socket::operator= ( oslSocket socketHandle
)
223 osl_acquireSocket( socketHandle
);
224 osl_releaseSocket( m_handle
);
225 m_handle
= socketHandle
;
230 inline Socket
& Socket::operator= (const Socket
& sock
)
232 return (*this) = sock
.getHandle();
236 inline bool Socket::operator==( const Socket
& rSocket
) const
238 return m_handle
== rSocket
.getHandle();
242 inline bool Socket::operator==( const oslSocket socketHandle
) const
244 return m_handle
== socketHandle
;
248 inline void Socket::shutdown( oslSocketDirection Direction
)
250 osl_shutdownSocket( m_handle
, Direction
);
254 inline void Socket::close()
256 osl_closeSocket( m_handle
);
260 inline void Socket::getLocalAddr( SocketAddr
& addr
) const
262 addr
.assign( osl_getLocalAddrOfSocket( m_handle
) , SAL_NO_COPY
);
266 inline sal_Int32
Socket::getLocalPort() const
268 SocketAddr
addr( 0 );
269 getLocalAddr( addr
);
270 return addr
.getPort();
274 inline ::rtl::OUString
Socket::getLocalHost() const
276 SocketAddr
addr( 0 );
277 getLocalAddr( addr
);
278 return addr
.getHostname();
282 inline void Socket::getPeerAddr( SocketAddr
&addr
) const
284 addr
.assign( osl_getPeerAddrOfSocket( m_handle
), SAL_NO_COPY
);
288 inline sal_Int32
Socket::getPeerPort() const
290 SocketAddr
addr( 0 );
292 return addr
.getPort();
296 inline ::rtl::OUString
Socket::getPeerHost() const
298 SocketAddr
addr( 0 );
300 return addr
.getHostname();
304 inline bool Socket::bind(const SocketAddr
& LocalInterface
)
306 return osl_bindAddrToSocket( m_handle
, LocalInterface
.getHandle() );
310 inline bool Socket::isRecvReady(const TimeValue
*pTimeout
) const
312 return osl_isReceiveReady( m_handle
, pTimeout
);
316 inline bool Socket::isSendReady(const TimeValue
*pTimeout
) const
318 return osl_isSendReady( m_handle
, pTimeout
);
322 inline bool Socket::isExceptionPending(const TimeValue
*pTimeout
) const
324 return osl_isExceptionPending( m_handle
, pTimeout
);
328 inline oslSocketType
Socket::getType() const
330 return osl_getSocketType( m_handle
);
334 inline sal_Int32
Socket::getOption(
335 oslSocketOption Option
,
337 sal_uInt32 BufferLen
,
338 oslSocketOptionLevel Level
) const
340 return osl_getSocketOption( m_handle
, Level
, Option
, pBuffer
, BufferLen
);
344 inline bool Socket::setOption( oslSocketOption Option
,
346 sal_uInt32 BufferLen
,
347 oslSocketOptionLevel Level
) const
349 return osl_setSocketOption( m_handle
, Level
, Option
, pBuffer
, BufferLen
);
353 inline bool Socket::setOption( oslSocketOption option
, sal_Int32 nValue
)
355 return setOption( option
, &nValue
, sizeof( nValue
) );
359 inline sal_Int32
Socket::getOption( oslSocketOption option
) const
362 getOption( option
, &n
, sizeof( n
) );
367 inline bool Socket::enableNonBlockingMode( bool bNonBlockingMode
)
369 return osl_enableNonBlockingMode( m_handle
, bNonBlockingMode
);
373 inline bool Socket::isNonBlockingMode() const
375 return osl_isNonBlockingMode( m_handle
);
379 inline void SAL_CALL
Socket::clearError() const
382 getOption(osl_Socket_OptionError
, &err
, sizeof(err
));
386 inline oslSocketError
Socket::getError() const
388 return osl_getLastSocketError( m_handle
);
392 inline ::rtl::OUString
Socket::getErrorAsString( ) const
394 ::rtl::OUString error
;
395 osl_getLastSocketErrorDescription( m_handle
, &(error
.pData
) );
400 inline oslSocket
Socket::getHandle() const
406 inline StreamSocket::StreamSocket(oslAddrFamily Family
,
407 oslProtocol Protocol
,
409 : Socket( Type
, Family
, Protocol
)
413 inline StreamSocket::StreamSocket( oslSocket socketHandle
, __sal_NoAcquire noacquire
)
414 : Socket( socketHandle
, noacquire
)
418 inline StreamSocket::StreamSocket( oslSocket socketHandle
)
419 : Socket( socketHandle
)
423 inline StreamSocket::StreamSocket( const StreamSocket
& socket
)
428 inline sal_Int32
StreamSocket::read(void* pBuffer
, sal_uInt32 n
)
430 return osl_readSocket( m_handle
, pBuffer
, n
);
434 inline sal_Int32
StreamSocket::write(const void* pBuffer
, sal_uInt32 n
)
436 return osl_writeSocket( m_handle
, pBuffer
, n
);
441 inline sal_Int32
StreamSocket::recv(void* pBuffer
,
442 sal_uInt32 BytesToRead
,
443 oslSocketMsgFlag Flag
)
445 return osl_receiveSocket( m_handle
, pBuffer
,BytesToRead
, Flag
);
449 inline sal_Int32
StreamSocket::send(const void* pBuffer
,
450 sal_uInt32 BytesToSend
,
451 oslSocketMsgFlag Flag
)
453 return osl_sendSocket( m_handle
, pBuffer
, BytesToSend
, Flag
);
457 inline ConnectorSocket::ConnectorSocket(oslAddrFamily Family
,
458 oslProtocol Protocol
,
460 : StreamSocket( Family
, Protocol
,Type
)
464 inline oslSocketResult
ConnectorSocket::connect( const SocketAddr
& TargetHost
,
465 const TimeValue
* pTimeout
)
467 return osl_connectSocketTo( m_handle
, TargetHost
.getHandle(), pTimeout
);
471 inline AcceptorSocket::AcceptorSocket(oslAddrFamily Family
,
472 oslProtocol Protocol
,
474 : Socket( Type
, Family
, Protocol
)
478 inline bool AcceptorSocket::listen(sal_Int32 MaxPendingConnections
)
480 return osl_listenOnSocket( m_handle
, MaxPendingConnections
);
484 inline oslSocketResult
AcceptorSocket::acceptConnection( StreamSocket
& Connection
)
486 oslSocket o
= osl_acceptConnectionOnSocket( m_handle
, 0 );
487 oslSocketResult status
= osl_Socket_Ok
;
490 Connection
= StreamSocket( o
, SAL_NO_ACQUIRE
);
494 Connection
= StreamSocket();
495 status
= osl_Socket_Error
;
501 inline oslSocketResult
AcceptorSocket::acceptConnection(
502 StreamSocket
& Connection
, SocketAddr
& PeerAddr
)
504 // TODO change in/OUT parameter
505 oslSocket o
= osl_acceptConnectionOnSocket(
506 m_handle
, reinterpret_cast<oslSocketAddr
*>(&PeerAddr
));
507 oslSocketResult status
= osl_Socket_Ok
;
510 Connection
= StreamSocket( o
, SAL_NO_ACQUIRE
);
514 Connection
= StreamSocket();
515 status
= osl_Socket_Error
;
521 inline DatagramSocket::DatagramSocket(oslAddrFamily Family
,
522 oslProtocol Protocol
,
524 : Socket( Type
, Family
, Protocol
)
528 inline sal_Int32
DatagramSocket::recvFrom(void* pBuffer
,
529 sal_uInt32 BufferSize
,
530 SocketAddr
* pSenderAddr
,
531 oslSocketMsgFlag Flag
)
536 // TODO : correct the out-parameter pSenderAddr outparameter
537 nByteRead
= osl_receiveFromSocket( m_handle
, pSenderAddr
->getHandle() , pBuffer
,
539 // nByteRead = osl_receiveFromSocket( m_handle, *(oslSocketAddr**) &pSenderAddr , pBuffer,
540 // BufferSize, Flag);
544 nByteRead
= osl_receiveFromSocket( m_handle
, 0 , pBuffer
, BufferSize
, Flag
);
550 inline sal_Int32
DatagramSocket::sendTo( const SocketAddr
& ReceiverAddr
,
552 sal_uInt32 BufferSize
,
553 oslSocketMsgFlag Flag
)
555 return osl_sendToSocket( m_handle
, ReceiverAddr
.getHandle(), pBuffer
, BufferSize
, Flag
);
560 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */