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
): 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
) {
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 return (*this) = sock
.getHandle();
249 inline bool Socket::operator==( const Socket
& rSocket
) const
251 return m_handle
== rSocket
.getHandle();
255 inline bool Socket::operator==( const oslSocket socketHandle
) const
257 return m_handle
== socketHandle
;
261 inline void Socket::shutdown( oslSocketDirection Direction
)
263 osl_shutdownSocket( m_handle
, Direction
);
267 inline void Socket::close()
269 osl_closeSocket( m_handle
);
273 inline void Socket::getLocalAddr( SocketAddr
& addr
) const
275 addr
.assign( osl_getLocalAddrOfSocket( m_handle
) , SAL_NO_COPY
);
279 inline sal_Int32
Socket::getLocalPort() const
281 SocketAddr
addr( NULL
);
282 getLocalAddr( addr
);
283 return addr
.getPort();
287 inline ::rtl::OUString
Socket::getLocalHost() const
289 SocketAddr
addr( NULL
);
290 getLocalAddr( addr
);
291 return addr
.getHostname();
295 inline void Socket::getPeerAddr( SocketAddr
&addr
) const
297 addr
.assign( osl_getPeerAddrOfSocket( m_handle
), SAL_NO_COPY
);
301 inline sal_Int32
Socket::getPeerPort() const
303 SocketAddr
addr( NULL
);
305 return addr
.getPort();
309 inline ::rtl::OUString
Socket::getPeerHost() const
311 SocketAddr
addr( NULL
);
313 return addr
.getHostname();
317 inline bool Socket::bind(const SocketAddr
& LocalInterface
)
319 return osl_bindAddrToSocket( m_handle
, LocalInterface
.getHandle() );
323 inline bool Socket::isRecvReady(const TimeValue
*pTimeout
) const
325 return osl_isReceiveReady( m_handle
, pTimeout
);
329 inline bool Socket::isSendReady(const TimeValue
*pTimeout
) const
331 return osl_isSendReady( m_handle
, pTimeout
);
335 inline bool Socket::isExceptionPending(const TimeValue
*pTimeout
) const
337 return osl_isExceptionPending( m_handle
, pTimeout
);
341 inline oslSocketType
Socket::getType() const
343 return osl_getSocketType( m_handle
);
347 inline sal_Int32
Socket::getOption(
348 oslSocketOption Option
,
350 sal_uInt32 BufferLen
,
351 oslSocketOptionLevel Level
) const
353 return osl_getSocketOption( m_handle
, Level
, Option
, pBuffer
, BufferLen
);
357 inline bool Socket::setOption( oslSocketOption Option
,
359 sal_uInt32 BufferLen
,
360 oslSocketOptionLevel Level
) const
362 return osl_setSocketOption( m_handle
, Level
, Option
, pBuffer
, BufferLen
);
366 inline bool Socket::setOption( oslSocketOption option
, sal_Int32 nValue
)
368 return setOption( option
, &nValue
, sizeof( nValue
) );
372 inline sal_Int32
Socket::getOption( oslSocketOption option
) const
375 getOption( option
, &n
, sizeof( n
) );
380 inline bool Socket::enableNonBlockingMode( bool bNonBlockingMode
)
382 return osl_enableNonBlockingMode( m_handle
, bNonBlockingMode
);
386 inline bool Socket::isNonBlockingMode() const
388 return osl_isNonBlockingMode( m_handle
);
392 inline void SAL_CALL
Socket::clearError() const
395 getOption(osl_Socket_OptionError
, &err
, sizeof(err
));
399 inline oslSocketError
Socket::getError() const
401 return osl_getLastSocketError( m_handle
);
405 inline ::rtl::OUString
Socket::getErrorAsString( ) const
407 ::rtl::OUString error
;
408 osl_getLastSocketErrorDescription( m_handle
, &(error
.pData
) );
413 inline oslSocket
Socket::getHandle() const
419 inline StreamSocket::StreamSocket(oslAddrFamily Family
,
420 oslProtocol Protocol
,
422 : Socket( Type
, Family
, Protocol
)
426 inline StreamSocket::StreamSocket( oslSocket socketHandle
, __sal_NoAcquire noacquire
)
427 : Socket( socketHandle
, noacquire
)
431 inline StreamSocket::StreamSocket( oslSocket socketHandle
)
432 : Socket( socketHandle
)
436 inline StreamSocket::StreamSocket( const StreamSocket
& socket
)
441 inline sal_Int32
StreamSocket::read(void* pBuffer
, sal_uInt32 n
)
443 return osl_readSocket( m_handle
, pBuffer
, n
);
447 inline sal_Int32
StreamSocket::write(const void* pBuffer
, sal_uInt32 n
)
449 return osl_writeSocket( m_handle
, pBuffer
, n
);
453 inline sal_Int32
StreamSocket::recv(void* pBuffer
,
454 sal_uInt32 BytesToRead
,
455 oslSocketMsgFlag Flag
)
457 return osl_receiveSocket( m_handle
, pBuffer
,BytesToRead
, Flag
);
461 inline sal_Int32
StreamSocket::send(const void* pBuffer
,
462 sal_uInt32 BytesToSend
,
463 oslSocketMsgFlag Flag
)
465 return osl_sendSocket( m_handle
, pBuffer
, BytesToSend
, Flag
);
469 inline ConnectorSocket::ConnectorSocket(oslAddrFamily Family
,
470 oslProtocol Protocol
,
472 : StreamSocket( Family
, Protocol
,Type
)
476 inline oslSocketResult
ConnectorSocket::connect( const SocketAddr
& TargetHost
,
477 const TimeValue
* pTimeout
)
479 return osl_connectSocketTo( m_handle
, TargetHost
.getHandle(), pTimeout
);
483 inline AcceptorSocket::AcceptorSocket(oslAddrFamily Family
,
484 oslProtocol Protocol
,
486 : Socket( Type
, Family
, Protocol
)
490 inline bool AcceptorSocket::listen(sal_Int32 MaxPendingConnections
)
492 return osl_listenOnSocket( m_handle
, MaxPendingConnections
);
496 inline oslSocketResult
AcceptorSocket::acceptConnection( StreamSocket
& Connection
)
498 oslSocket o
= osl_acceptConnectionOnSocket( m_handle
, NULL
);
499 oslSocketResult status
= osl_Socket_Ok
;
502 Connection
= StreamSocket( o
, SAL_NO_ACQUIRE
);
506 Connection
= StreamSocket();
507 status
= osl_Socket_Error
;
513 inline oslSocketResult
AcceptorSocket::acceptConnection(
514 StreamSocket
& Connection
, SocketAddr
& PeerAddr
)
516 // TODO change in/OUT parameter
517 oslSocket o
= osl_acceptConnectionOnSocket(
518 m_handle
, reinterpret_cast<oslSocketAddr
*>(&PeerAddr
));
519 oslSocketResult status
= osl_Socket_Ok
;
522 Connection
= StreamSocket( o
, SAL_NO_ACQUIRE
);
526 Connection
= StreamSocket();
527 status
= osl_Socket_Error
;
533 inline DatagramSocket::DatagramSocket(oslAddrFamily Family
,
534 oslProtocol Protocol
,
536 : Socket( Type
, Family
, Protocol
)
540 inline sal_Int32
DatagramSocket::recvFrom(void* pBuffer
,
541 sal_uInt32 BufferSize
,
542 SocketAddr
* pSenderAddr
,
543 oslSocketMsgFlag Flag
)
548 // TODO : correct the out-parameter pSenderAddr outparameter
549 nByteRead
= osl_receiveFromSocket( m_handle
, pSenderAddr
->getHandle() , pBuffer
,
554 nByteRead
= osl_receiveFromSocket( m_handle
, NULL
, pBuffer
, BufferSize
, Flag
);
560 inline sal_Int32
DatagramSocket::sendTo( const SocketAddr
& ReceiverAddr
,
562 sal_uInt32 BufferSize
,
563 oslSocketMsgFlag Flag
)
565 return osl_sendToSocket( m_handle
, ReceiverAddr
.getHandle(), pBuffer
, BufferSize
, Flag
);
570 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */