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
) )
37 #if defined LIBO_INTERNAL_ONLY
38 SocketAddr::SocketAddr(SocketAddr
&& other
) noexcept
: m_handle(other
.m_handle
) {
39 other
.m_handle
= nullptr;
43 inline SocketAddr::SocketAddr(oslSocketAddr Addr
)
44 : m_handle( osl_copySocketAddr( Addr
) )
49 inline SocketAddr::SocketAddr(oslSocketAddr Addr
, __osl_socket_NoCopy
)
55 inline SocketAddr::SocketAddr( const ::rtl::OUString
& strAddrOrHostName
, sal_Int32 nPort
)
56 : m_handle( osl_createInetSocketAddr( strAddrOrHostName
.pData
, nPort
) )
60 m_handle
= osl_resolveHostname(strAddrOrHostName
.pData
);
65 osl_setInetPortOfSocketAddr(m_handle
, nPort
);
69 osl_destroySocketAddr( m_handle
);
76 inline SocketAddr::~SocketAddr()
79 osl_destroySocketAddr( m_handle
);
83 inline ::rtl::OUString
SocketAddr::getHostname( oslSocketResult
*pResult
) const
85 ::rtl::OUString hostname
;
86 oslSocketResult result
= osl_getHostnameOfSocketAddr( m_handle
, &(hostname
.pData
) );
93 inline sal_Int32 SAL_CALL
SocketAddr::getPort() const
95 return osl_getInetPortOfSocketAddr(m_handle
);
99 inline bool SAL_CALL
SocketAddr::setPort( sal_Int32 nPort
)
101 return osl_setInetPortOfSocketAddr(m_handle
, nPort
);
104 inline bool SAL_CALL
SocketAddr::setHostname( const ::rtl::OUString
&sDottedIpOrHostname
)
106 *this = SocketAddr( sDottedIpOrHostname
, getPort() );
111 inline bool SAL_CALL
SocketAddr::setAddr( const ::rtl::ByteSequence
& address
)
113 return osl_setAddrOfSocketAddr( m_handle
, address
.getHandle() )
117 inline ::rtl::ByteSequence SAL_CALL
SocketAddr::getAddr( oslSocketResult
*pResult
) const
119 ::rtl::ByteSequence sequence
;
120 oslSocketResult result
= osl_getAddrOfSocketAddr( m_handle
, reinterpret_cast<sal_Sequence
**>(&sequence
) );
127 inline SocketAddr
& SAL_CALL
SocketAddr::operator= (oslSocketAddr Addr
)
129 oslSocketAddr pNewAddr
= osl_copySocketAddr( Addr
);
131 osl_destroySocketAddr( m_handle
);
137 inline SocketAddr
& SAL_CALL
SocketAddr::operator= (const SocketAddr
& Addr
)
139 *this = Addr
.getHandle();
143 #if defined LIBO_INTERNAL_ONLY
144 SocketAddr
& SocketAddr::operator =(SocketAddr
&& other
) noexcept
{
145 if (m_handle
!= nullptr) {
146 osl_destroySocketAddr(m_handle
);
148 m_handle
= other
.m_handle
;
149 other
.m_handle
= nullptr;
154 inline SocketAddr
& SAL_CALL
SocketAddr::assign( oslSocketAddr Addr
, __osl_socket_NoCopy
)
157 osl_destroySocketAddr( m_handle
);
163 inline bool SAL_CALL
SocketAddr::operator== (oslSocketAddr Addr
) const
165 return osl_isEqualSocketAddr( m_handle
, Addr
);
168 inline oslSocketAddr
SocketAddr::getHandle() const
174 inline bool SocketAddr::is() const
176 return m_handle
!= NULL
;
179 inline ::rtl::OUString SAL_CALL
SocketAddr::getLocalHostname( oslSocketResult
*pResult
)
181 ::rtl::OUString hostname
;
182 oslSocketResult result
= osl_getLocalHostname( &(hostname
.pData
) );
188 inline void SAL_CALL
SocketAddr::resolveHostname(
189 const ::rtl::OUString
& strHostName
, SocketAddr
&Addr
)
191 Addr
= SocketAddr( osl_resolveHostname( strHostName
.pData
) , SAL_NO_COPY
);
194 inline sal_Int32 SAL_CALL
SocketAddr::getServicePort(
195 const ::rtl::OUString
& strServiceName
,
196 const ::rtl::OUString
& strProtocolName
)
198 return osl_getServicePort( strServiceName
.pData
, strProtocolName
.pData
);
202 inline Socket::Socket(oslSocketType Type
,
203 oslAddrFamily Family
,
204 oslProtocol Protocol
)
205 : m_handle( osl_createSocket(Family
, Type
, Protocol
) )
209 inline Socket::Socket( oslSocket socketHandle
, __sal_NoAcquire
)
210 : m_handle( socketHandle
)
214 inline Socket::Socket( oslSocket socketHandle
)
215 : m_handle( socketHandle
)
217 osl_acquireSocket( m_handle
);
221 inline Socket::Socket( const Socket
& socket
)
222 : m_handle( socket
.getHandle() )
224 osl_acquireSocket( m_handle
);
228 inline Socket::~Socket()
230 osl_releaseSocket( m_handle
);
234 inline Socket
& Socket::operator= ( oslSocket socketHandle
)
236 osl_acquireSocket( socketHandle
);
237 osl_releaseSocket( m_handle
);
238 m_handle
= socketHandle
;
243 inline Socket
& Socket::operator= (const Socket
& sock
)
245 *this = sock
.getHandle();
250 inline bool Socket::operator==( const Socket
& rSocket
) const
252 return m_handle
== rSocket
.getHandle();
256 inline bool Socket::operator==( const oslSocket socketHandle
) const
258 return m_handle
== socketHandle
;
262 inline void Socket::shutdown( oslSocketDirection Direction
)
264 osl_shutdownSocket( m_handle
, Direction
);
268 inline void Socket::close()
270 osl_closeSocket( m_handle
);
274 inline void Socket::getLocalAddr( SocketAddr
& addr
) const
276 addr
.assign( osl_getLocalAddrOfSocket( m_handle
) , SAL_NO_COPY
);
280 inline sal_Int32
Socket::getLocalPort() const
282 SocketAddr
addr( NULL
);
283 getLocalAddr( addr
);
284 return addr
.getPort();
288 inline ::rtl::OUString
Socket::getLocalHost() const
290 SocketAddr
addr( NULL
);
291 getLocalAddr( addr
);
292 return addr
.getHostname();
296 inline void Socket::getPeerAddr( SocketAddr
&addr
) const
298 addr
.assign( osl_getPeerAddrOfSocket( m_handle
), SAL_NO_COPY
);
302 inline sal_Int32
Socket::getPeerPort() const
304 SocketAddr
addr( NULL
);
306 return addr
.getPort();
310 inline ::rtl::OUString
Socket::getPeerHost() const
312 SocketAddr
addr( NULL
);
314 return addr
.getHostname();
318 inline bool Socket::bind(const SocketAddr
& LocalInterface
)
320 return osl_bindAddrToSocket( m_handle
, LocalInterface
.getHandle() );
324 inline bool Socket::isRecvReady(const TimeValue
*pTimeout
) const
326 return osl_isReceiveReady( m_handle
, pTimeout
);
330 inline bool Socket::isSendReady(const TimeValue
*pTimeout
) const
332 return osl_isSendReady( m_handle
, pTimeout
);
336 inline bool Socket::isExceptionPending(const TimeValue
*pTimeout
) const
338 return osl_isExceptionPending( m_handle
, pTimeout
);
342 inline oslSocketType
Socket::getType() const
344 return osl_getSocketType( m_handle
);
348 inline sal_Int32
Socket::getOption(
349 oslSocketOption Option
,
351 sal_uInt32 BufferLen
,
352 oslSocketOptionLevel Level
) const
354 return osl_getSocketOption( m_handle
, Level
, Option
, pBuffer
, BufferLen
);
358 inline bool Socket::setOption( oslSocketOption Option
,
360 sal_uInt32 BufferLen
,
361 oslSocketOptionLevel Level
) const
363 return osl_setSocketOption( m_handle
, Level
, Option
, pBuffer
, BufferLen
);
367 inline bool Socket::setOption( oslSocketOption option
, sal_Int32 nValue
)
369 return setOption( option
, &nValue
, sizeof( nValue
) );
373 inline sal_Int32
Socket::getOption( oslSocketOption option
) const
376 getOption( option
, &n
, sizeof( n
) );
381 inline bool Socket::enableNonBlockingMode( bool bNonBlockingMode
)
383 return osl_enableNonBlockingMode( m_handle
, bNonBlockingMode
);
387 inline bool Socket::isNonBlockingMode() const
389 return osl_isNonBlockingMode( m_handle
);
393 inline void SAL_CALL
Socket::clearError() const
396 getOption(osl_Socket_OptionError
, &err
, sizeof(err
));
400 inline oslSocketError
Socket::getError() const
402 return osl_getLastSocketError( m_handle
);
406 inline ::rtl::OUString
Socket::getErrorAsString( ) const
408 ::rtl::OUString error
;
409 osl_getLastSocketErrorDescription( m_handle
, &(error
.pData
) );
414 inline oslSocket
Socket::getHandle() const
420 inline StreamSocket::StreamSocket(oslAddrFamily Family
,
421 oslProtocol Protocol
,
423 : Socket( Type
, Family
, Protocol
)
427 inline StreamSocket::StreamSocket( oslSocket socketHandle
, __sal_NoAcquire noacquire
)
428 : Socket( socketHandle
, noacquire
)
432 inline StreamSocket::StreamSocket( oslSocket socketHandle
)
433 : Socket( socketHandle
)
437 inline sal_Int32
StreamSocket::read(void* pBuffer
, sal_uInt32 n
)
439 return osl_readSocket( m_handle
, pBuffer
, n
);
443 inline sal_Int32
StreamSocket::write(const void* pBuffer
, sal_uInt32 n
)
445 return osl_writeSocket( m_handle
, pBuffer
, n
);
449 inline sal_Int32
StreamSocket::recv(void* pBuffer
,
450 sal_uInt32 BytesToRead
,
451 oslSocketMsgFlag Flag
)
453 return osl_receiveSocket( m_handle
, pBuffer
,BytesToRead
, Flag
);
457 inline sal_Int32
StreamSocket::send(const void* pBuffer
,
458 sal_uInt32 BytesToSend
,
459 oslSocketMsgFlag Flag
)
461 return osl_sendSocket( m_handle
, pBuffer
, BytesToSend
, Flag
);
465 inline ConnectorSocket::ConnectorSocket(oslAddrFamily Family
,
466 oslProtocol Protocol
,
468 : StreamSocket( Family
, Protocol
,Type
)
472 inline oslSocketResult
ConnectorSocket::connect( const SocketAddr
& TargetHost
,
473 const TimeValue
* pTimeout
)
475 return osl_connectSocketTo( m_handle
, TargetHost
.getHandle(), pTimeout
);
479 inline AcceptorSocket::AcceptorSocket(oslAddrFamily Family
,
480 oslProtocol Protocol
,
482 : Socket( Type
, Family
, Protocol
)
486 inline bool AcceptorSocket::listen(sal_Int32 MaxPendingConnections
)
488 return osl_listenOnSocket( m_handle
, MaxPendingConnections
);
492 inline oslSocketResult
AcceptorSocket::acceptConnection( StreamSocket
& Connection
)
494 oslSocket o
= osl_acceptConnectionOnSocket( m_handle
, NULL
);
495 oslSocketResult status
= osl_Socket_Ok
;
498 Connection
= StreamSocket( o
, SAL_NO_ACQUIRE
);
502 Connection
= StreamSocket();
503 status
= osl_Socket_Error
;
509 inline oslSocketResult
AcceptorSocket::acceptConnection(
510 StreamSocket
& Connection
, SocketAddr
& PeerAddr
)
512 // TODO change in/OUT parameter
513 oslSocket o
= osl_acceptConnectionOnSocket(
514 m_handle
, reinterpret_cast<oslSocketAddr
*>(&PeerAddr
));
515 oslSocketResult status
= osl_Socket_Ok
;
518 Connection
= StreamSocket( o
, SAL_NO_ACQUIRE
);
522 Connection
= StreamSocket();
523 status
= osl_Socket_Error
;
529 inline DatagramSocket::DatagramSocket(oslAddrFamily Family
,
530 oslProtocol Protocol
,
532 : Socket( Type
, Family
, Protocol
)
536 inline sal_Int32
DatagramSocket::recvFrom(void* pBuffer
,
537 sal_uInt32 BufferSize
,
538 SocketAddr
* pSenderAddr
,
539 oslSocketMsgFlag Flag
)
544 // TODO : correct the out-parameter pSenderAddr outparameter
545 nByteRead
= osl_receiveFromSocket( m_handle
, pSenderAddr
->getHandle() , pBuffer
,
550 nByteRead
= osl_receiveFromSocket( m_handle
, NULL
, pBuffer
, BufferSize
, Flag
);
556 inline sal_Int32
DatagramSocket::sendTo( const SocketAddr
& ReceiverAddr
,
558 sal_uInt32 BufferSize
,
559 oslSocketMsgFlag Flag
)
561 return osl_sendToSocket( m_handle
, ReceiverAddr
.getHandle(), pBuffer
, BufferSize
, Flag
);
566 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */