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 .
21 * This file is part of LibreOffice published API.
24 #ifndef INCLUDED_OSL_SOCKET_HXX
25 #define INCLUDED_OSL_SOCKET_HXX
27 #include "osl/socket_decl.hxx"
32 inline SocketAddr::SocketAddr()
33 : m_handle( osl_createEmptySocketAddr( osl_Socket_FamilyInet
) )
37 inline SocketAddr::SocketAddr(const SocketAddr
& Addr
)
38 : m_handle( osl_copySocketAddr( Addr
.m_handle
) )
42 #if defined LIBO_INTERNAL_ONLY
43 SocketAddr::SocketAddr(SocketAddr
&& other
) noexcept
: m_handle(other
.m_handle
) {
44 other
.m_handle
= nullptr;
48 inline SocketAddr::SocketAddr(oslSocketAddr Addr
)
49 : m_handle( osl_copySocketAddr( Addr
) )
54 inline SocketAddr::SocketAddr(oslSocketAddr Addr
, __osl_socket_NoCopy
)
60 inline SocketAddr::SocketAddr( const ::rtl::OUString
& strAddrOrHostName
, sal_Int32 nPort
)
61 : m_handle( osl_createInetSocketAddr( strAddrOrHostName
.pData
, nPort
) )
65 m_handle
= osl_resolveHostname(strAddrOrHostName
.pData
);
70 osl_setInetPortOfSocketAddr(m_handle
, nPort
);
74 osl_destroySocketAddr( m_handle
);
81 inline SocketAddr::~SocketAddr()
84 osl_destroySocketAddr( m_handle
);
88 inline ::rtl::OUString
SocketAddr::getHostname( oslSocketResult
*pResult
) const
90 ::rtl::OUString hostname
;
91 oslSocketResult result
= osl_getHostnameOfSocketAddr( m_handle
, &(hostname
.pData
) );
98 inline sal_Int32 SAL_CALL
SocketAddr::getPort() const
100 return osl_getInetPortOfSocketAddr(m_handle
);
104 inline bool SAL_CALL
SocketAddr::setPort( sal_Int32 nPort
)
106 return osl_setInetPortOfSocketAddr(m_handle
, nPort
);
109 inline bool SAL_CALL
SocketAddr::setHostname( const ::rtl::OUString
&sDottedIpOrHostname
)
111 *this = SocketAddr( sDottedIpOrHostname
, getPort() );
116 inline bool SAL_CALL
SocketAddr::setAddr( const ::rtl::ByteSequence
& address
)
118 return osl_setAddrOfSocketAddr( m_handle
, address
.getHandle() )
122 inline ::rtl::ByteSequence SAL_CALL
SocketAddr::getAddr( oslSocketResult
*pResult
) const
124 ::rtl::ByteSequence sequence
;
125 oslSocketResult result
= osl_getAddrOfSocketAddr( m_handle
, reinterpret_cast<sal_Sequence
**>(&sequence
) );
132 inline SocketAddr
& SAL_CALL
SocketAddr::operator= (oslSocketAddr Addr
)
134 oslSocketAddr pNewAddr
= osl_copySocketAddr( Addr
);
136 osl_destroySocketAddr( m_handle
);
142 inline SocketAddr
& SAL_CALL
SocketAddr::operator= (const SocketAddr
& Addr
)
144 *this = Addr
.getHandle();
148 #if defined LIBO_INTERNAL_ONLY
149 SocketAddr
& SocketAddr::operator =(SocketAddr
&& other
) noexcept
{
150 if (m_handle
!= nullptr) {
151 osl_destroySocketAddr(m_handle
);
153 m_handle
= other
.m_handle
;
154 other
.m_handle
= nullptr;
159 inline SocketAddr
& SAL_CALL
SocketAddr::assign( oslSocketAddr Addr
, __osl_socket_NoCopy
)
162 osl_destroySocketAddr( m_handle
);
168 inline bool SAL_CALL
SocketAddr::operator== (oslSocketAddr Addr
) const
170 return osl_isEqualSocketAddr( m_handle
, Addr
);
173 inline oslSocketAddr
SocketAddr::getHandle() const
179 inline bool SocketAddr::is() const
181 return m_handle
!= NULL
;
184 inline ::rtl::OUString SAL_CALL
SocketAddr::getLocalHostname( oslSocketResult
*pResult
)
186 ::rtl::OUString hostname
;
187 oslSocketResult result
= osl_getLocalHostname( &(hostname
.pData
) );
193 inline void SAL_CALL
SocketAddr::resolveHostname(
194 const ::rtl::OUString
& strHostName
, SocketAddr
&Addr
)
196 Addr
= SocketAddr( osl_resolveHostname( strHostName
.pData
) , SAL_NO_COPY
);
199 inline sal_Int32 SAL_CALL
SocketAddr::getServicePort(
200 const ::rtl::OUString
& strServiceName
,
201 const ::rtl::OUString
& strProtocolName
)
203 return osl_getServicePort( strServiceName
.pData
, strProtocolName
.pData
);
207 inline Socket::Socket(oslSocketType Type
,
208 oslAddrFamily Family
,
209 oslProtocol Protocol
)
210 : m_handle( osl_createSocket(Family
, Type
, Protocol
) )
214 inline Socket::Socket( oslSocket socketHandle
, __sal_NoAcquire
)
215 : m_handle( socketHandle
)
219 inline Socket::Socket( oslSocket socketHandle
)
220 : m_handle( socketHandle
)
222 osl_acquireSocket( m_handle
);
226 inline Socket::Socket( const Socket
& socket
)
227 : m_handle( socket
.getHandle() )
229 osl_acquireSocket( m_handle
);
233 inline Socket::~Socket()
235 osl_releaseSocket( m_handle
);
239 inline Socket
& Socket::operator= ( oslSocket socketHandle
)
241 osl_acquireSocket( socketHandle
);
242 osl_releaseSocket( m_handle
);
243 m_handle
= socketHandle
;
248 inline Socket
& Socket::operator= (const Socket
& sock
)
250 *this = sock
.getHandle();
255 inline bool Socket::operator==( const Socket
& rSocket
) const
257 return m_handle
== rSocket
.getHandle();
261 inline bool Socket::operator==( const oslSocket socketHandle
) const
263 return m_handle
== socketHandle
;
267 inline void Socket::shutdown( oslSocketDirection Direction
)
269 osl_shutdownSocket( m_handle
, Direction
);
273 inline void Socket::close()
275 osl_closeSocket( m_handle
);
279 inline void Socket::getLocalAddr( SocketAddr
& addr
) const
281 addr
.assign( osl_getLocalAddrOfSocket( m_handle
) , SAL_NO_COPY
);
285 inline sal_Int32
Socket::getLocalPort() const
287 SocketAddr
addr( NULL
);
288 getLocalAddr( addr
);
289 return addr
.getPort();
293 inline ::rtl::OUString
Socket::getLocalHost() const
295 SocketAddr
addr( NULL
);
296 getLocalAddr( addr
);
297 return addr
.getHostname();
301 inline void Socket::getPeerAddr( SocketAddr
&addr
) const
303 addr
.assign( osl_getPeerAddrOfSocket( m_handle
), SAL_NO_COPY
);
307 inline sal_Int32
Socket::getPeerPort() const
309 SocketAddr
addr( NULL
);
311 return addr
.getPort();
315 inline ::rtl::OUString
Socket::getPeerHost() const
317 SocketAddr
addr( NULL
);
319 return addr
.getHostname();
323 inline bool Socket::bind(const SocketAddr
& LocalInterface
)
325 return osl_bindAddrToSocket( m_handle
, LocalInterface
.getHandle() );
329 inline bool Socket::isRecvReady(const TimeValue
*pTimeout
) const
331 return osl_isReceiveReady( m_handle
, pTimeout
);
335 inline bool Socket::isSendReady(const TimeValue
*pTimeout
) const
337 return osl_isSendReady( m_handle
, pTimeout
);
341 inline bool Socket::isExceptionPending(const TimeValue
*pTimeout
) const
343 return osl_isExceptionPending( m_handle
, pTimeout
);
347 inline oslSocketType
Socket::getType() const
349 return osl_getSocketType( m_handle
);
353 inline sal_Int32
Socket::getOption(
354 oslSocketOption Option
,
356 sal_uInt32 BufferLen
,
357 oslSocketOptionLevel Level
) const
359 return osl_getSocketOption( m_handle
, Level
, Option
, pBuffer
, BufferLen
);
363 inline bool Socket::setOption( oslSocketOption Option
,
365 sal_uInt32 BufferLen
,
366 oslSocketOptionLevel Level
) const
368 return osl_setSocketOption( m_handle
, Level
, Option
, pBuffer
, BufferLen
);
372 inline bool Socket::setOption( oslSocketOption option
, sal_Int32 nValue
)
374 return setOption( option
, &nValue
, sizeof( nValue
) );
378 inline sal_Int32
Socket::getOption( oslSocketOption option
) const
381 getOption( option
, &n
, sizeof( n
) );
386 inline bool Socket::enableNonBlockingMode( bool bNonBlockingMode
)
388 return osl_enableNonBlockingMode( m_handle
, bNonBlockingMode
);
392 inline bool Socket::isNonBlockingMode() const
394 return osl_isNonBlockingMode( m_handle
);
398 inline void SAL_CALL
Socket::clearError() const
401 getOption(osl_Socket_OptionError
, &err
, sizeof(err
));
405 inline oslSocketError
Socket::getError() const
407 return osl_getLastSocketError( m_handle
);
411 inline ::rtl::OUString
Socket::getErrorAsString( ) const
413 ::rtl::OUString error
;
414 osl_getLastSocketErrorDescription( m_handle
, &(error
.pData
) );
419 inline oslSocket
Socket::getHandle() const
425 inline StreamSocket::StreamSocket(oslAddrFamily Family
,
426 oslProtocol Protocol
,
428 : Socket( Type
, Family
, Protocol
)
432 inline StreamSocket::StreamSocket( oslSocket socketHandle
, __sal_NoAcquire noacquire
)
433 : Socket( socketHandle
, noacquire
)
437 inline StreamSocket::StreamSocket( oslSocket socketHandle
)
438 : Socket( socketHandle
)
442 inline sal_Int32
StreamSocket::read(void* pBuffer
, sal_uInt32 n
)
444 return osl_readSocket( m_handle
, pBuffer
, n
);
448 inline sal_Int32
StreamSocket::write(const void* pBuffer
, sal_uInt32 n
)
450 return osl_writeSocket( m_handle
, pBuffer
, n
);
454 inline sal_Int32
StreamSocket::recv(void* pBuffer
,
455 sal_uInt32 BytesToRead
,
456 oslSocketMsgFlag Flag
)
458 return osl_receiveSocket( m_handle
, pBuffer
,BytesToRead
, Flag
);
462 inline sal_Int32
StreamSocket::send(const void* pBuffer
,
463 sal_uInt32 BytesToSend
,
464 oslSocketMsgFlag Flag
)
466 return osl_sendSocket( m_handle
, pBuffer
, BytesToSend
, Flag
);
470 inline ConnectorSocket::ConnectorSocket(oslAddrFamily Family
,
471 oslProtocol Protocol
,
473 : StreamSocket( Family
, Protocol
,Type
)
477 inline oslSocketResult
ConnectorSocket::connect( const SocketAddr
& TargetHost
,
478 const TimeValue
* pTimeout
)
480 return osl_connectSocketTo( m_handle
, TargetHost
.getHandle(), pTimeout
);
484 inline AcceptorSocket::AcceptorSocket(oslAddrFamily Family
,
485 oslProtocol Protocol
,
487 : Socket( Type
, Family
, Protocol
)
491 inline bool AcceptorSocket::listen(sal_Int32 MaxPendingConnections
)
493 return osl_listenOnSocket( m_handle
, MaxPendingConnections
);
497 inline oslSocketResult
AcceptorSocket::acceptConnection( StreamSocket
& Connection
)
499 oslSocket o
= osl_acceptConnectionOnSocket( m_handle
, NULL
);
500 oslSocketResult status
= osl_Socket_Ok
;
503 Connection
= StreamSocket( o
, SAL_NO_ACQUIRE
);
507 Connection
= StreamSocket();
508 status
= osl_Socket_Error
;
514 inline oslSocketResult
AcceptorSocket::acceptConnection(
515 StreamSocket
& Connection
, SocketAddr
& PeerAddr
)
517 // TODO change in/OUT parameter
518 oslSocket o
= osl_acceptConnectionOnSocket(
519 m_handle
, reinterpret_cast<oslSocketAddr
*>(&PeerAddr
));
520 oslSocketResult status
= osl_Socket_Ok
;
523 Connection
= StreamSocket( o
, SAL_NO_ACQUIRE
);
527 Connection
= StreamSocket();
528 status
= osl_Socket_Error
;
534 inline DatagramSocket::DatagramSocket(oslAddrFamily Family
,
535 oslProtocol Protocol
,
537 : Socket( Type
, Family
, Protocol
)
541 inline sal_Int32
DatagramSocket::recvFrom(void* pBuffer
,
542 sal_uInt32 BufferSize
,
543 SocketAddr
* pSenderAddr
,
544 oslSocketMsgFlag Flag
)
549 // TODO : correct the out-parameter pSenderAddr outparameter
550 nByteRead
= osl_receiveFromSocket( m_handle
, pSenderAddr
->getHandle() , pBuffer
,
555 nByteRead
= osl_receiveFromSocket( m_handle
, NULL
, pBuffer
, BufferSize
, Flag
);
561 inline sal_Int32
DatagramSocket::sendTo( const SocketAddr
& ReceiverAddr
,
563 sal_uInt32 BufferSize
,
564 oslSocketMsgFlag Flag
)
566 return osl_sendToSocket( m_handle
, ReceiverAddr
.getHandle(), pBuffer
, BufferSize
, Flag
);
571 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */