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 _OSL_SOCKET_HXX_
20 #define _OSL_SOCKET_HXX_
22 #include <osl/socket_decl.hxx>
26 //______________________________________________________________________________
27 inline SocketAddr::SocketAddr()
28 : m_handle( osl_createEmptySocketAddr( osl_Socket_FamilyInet
) )
31 //______________________________________________________________________________
32 inline SocketAddr::SocketAddr(const SocketAddr
& Addr
)
33 : m_handle( osl_copySocketAddr( Addr
.m_handle
) )
37 //______________________________________________________________________________
38 inline SocketAddr::SocketAddr(oslSocketAddr Addr
)
39 : m_handle( osl_copySocketAddr( Addr
) )
43 //______________________________________________________________________________
44 inline SocketAddr::SocketAddr(oslSocketAddr Addr
, __osl_socket_NoCopy
)
49 //______________________________________________________________________________
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
);
70 //______________________________________________________________________________
71 inline SocketAddr::~SocketAddr()
74 osl_destroySocketAddr( m_handle
);
77 //______________________________________________________________________________
78 inline ::rtl::OUString
SocketAddr::getHostname( oslSocketResult
*pResult
) const
80 ::rtl::OUString hostname
;
81 oslSocketResult result
= osl_getHostnameOfSocketAddr( m_handle
, &(hostname
.pData
) );
87 //______________________________________________________________________________
88 inline sal_Int32 SAL_CALL
SocketAddr::getPort() const
90 return osl_getInetPortOfSocketAddr(m_handle
);
93 //______________________________________________________________________________
94 inline sal_Bool SAL_CALL
SocketAddr::setPort( sal_Int32 nPort
)
96 return osl_setInetPortOfSocketAddr(m_handle
, nPort
);
99 inline sal_Bool SAL_CALL
SocketAddr::setHostname( const ::rtl::OUString
&sDottedIpOrHostname
)
101 *this = SocketAddr( sDottedIpOrHostname
, getPort() );
105 //______________________________________________________________________________
106 inline sal_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
,(sal_Sequence
**) &sequence
);
121 //______________________________________________________________________________
122 inline SocketAddr
& SAL_CALL
SocketAddr::operator= (oslSocketAddr Addr
)
124 oslSocketAddr pNewAddr
= osl_copySocketAddr( Addr
);
126 osl_destroySocketAddr( m_handle
);
131 //______________________________________________________________________________
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
);
146 //______________________________________________________________________________
147 inline sal_Bool SAL_CALL
SocketAddr::operator== (oslSocketAddr Addr
) const
149 return osl_isEqualSocketAddr( m_handle
, Addr
);
152 inline oslSocketAddr
SocketAddr::getHandle() const
157 //______________________________________________________________________________
158 inline sal_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
);
188 //______________________________________________________________________________
189 inline Socket::Socket(oslSocketType Type
,
190 oslAddrFamily Family
,
191 oslProtocol Protocol
)
192 : m_handle( osl_createSocket(Family
, Type
, Protocol
) )
195 //______________________________________________________________________________
196 inline Socket::Socket( oslSocket socketHandle
, __sal_NoAcquire
)
197 : m_handle( socketHandle
)
200 //______________________________________________________________________________
201 inline Socket::Socket( oslSocket socketHandle
)
202 : m_handle( socketHandle
)
204 osl_acquireSocket( m_handle
);
207 //______________________________________________________________________________
208 inline Socket::Socket( const Socket
& socket
)
209 : m_handle( socket
.getHandle() )
211 osl_acquireSocket( m_handle
);
214 //______________________________________________________________________________
215 inline Socket::~Socket()
217 osl_releaseSocket( m_handle
);
220 //______________________________________________________________________________
221 inline Socket
& Socket::operator= ( oslSocket socketHandle
)
223 osl_acquireSocket( socketHandle
);
224 osl_releaseSocket( m_handle
);
225 m_handle
= socketHandle
;
229 //______________________________________________________________________________
230 inline Socket
& Socket::operator= (const Socket
& sock
)
232 return (*this) = sock
.getHandle();
235 //______________________________________________________________________________
236 inline sal_Bool
Socket::operator==( const Socket
& rSocket
) const
238 return m_handle
== rSocket
.getHandle();
241 //______________________________________________________________________________
242 inline sal_Bool
Socket::operator==( const oslSocket socketHandle
) const
244 return m_handle
== socketHandle
;
247 //______________________________________________________________________________
248 inline void Socket::shutdown( oslSocketDirection Direction
)
250 osl_shutdownSocket( m_handle
, Direction
);
253 //______________________________________________________________________________
254 inline void Socket::close()
256 osl_closeSocket( m_handle
);
259 //______________________________________________________________________________
260 inline void Socket::getLocalAddr( SocketAddr
& addr
) const
262 addr
.assign( osl_getLocalAddrOfSocket( m_handle
) , SAL_NO_COPY
);
265 //______________________________________________________________________________
266 inline sal_Int32
Socket::getLocalPort() const
268 SocketAddr
addr( 0 );
269 getLocalAddr( addr
);
270 return addr
.getPort();
273 //______________________________________________________________________________
274 inline ::rtl::OUString
Socket::getLocalHost() const
276 SocketAddr
addr( 0 );
277 getLocalAddr( addr
);
278 return addr
.getHostname();
281 //______________________________________________________________________________
282 inline void Socket::getPeerAddr( SocketAddr
&addr
) const
284 addr
.assign( osl_getPeerAddrOfSocket( m_handle
), SAL_NO_COPY
);
287 //______________________________________________________________________________
288 inline sal_Int32
Socket::getPeerPort() const
290 SocketAddr
addr( 0 );
292 return addr
.getPort();
295 //______________________________________________________________________________
296 inline ::rtl::OUString
Socket::getPeerHost() const
298 SocketAddr
addr( 0 );
300 return addr
.getHostname();
303 //______________________________________________________________________________
304 inline sal_Bool
Socket::bind(const SocketAddr
& LocalInterface
)
306 return osl_bindAddrToSocket( m_handle
, LocalInterface
.getHandle() );
309 //______________________________________________________________________________
310 inline sal_Bool
Socket::isRecvReady(const TimeValue
*pTimeout
) const
312 return osl_isReceiveReady( m_handle
, pTimeout
);
315 //______________________________________________________________________________
316 inline sal_Bool
Socket::isSendReady(const TimeValue
*pTimeout
) const
318 return osl_isSendReady( m_handle
, pTimeout
);
321 //______________________________________________________________________________
322 inline sal_Bool
Socket::isExceptionPending(const TimeValue
*pTimeout
) const
324 return osl_isExceptionPending( m_handle
, pTimeout
);
327 //______________________________________________________________________________
328 inline oslSocketType
Socket::getType() const
330 return osl_getSocketType( m_handle
);
333 //______________________________________________________________________________
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
);
343 //______________________________________________________________________________
344 inline sal_Bool
Socket::setOption( oslSocketOption Option
,
346 sal_uInt32 BufferLen
,
347 oslSocketOptionLevel Level
) const
349 return osl_setSocketOption( m_handle
, Level
, Option
, pBuffer
, BufferLen
);
352 //______________________________________________________________________________
353 inline sal_Bool
Socket::setOption( oslSocketOption option
, sal_Int32 nValue
)
355 return setOption( option
, &nValue
, sizeof( nValue
) );
358 //______________________________________________________________________________
359 inline sal_Int32
Socket::getOption( oslSocketOption option
) const
362 getOption( option
, &n
, sizeof( n
) );
366 //______________________________________________________________________________
367 inline sal_Bool
Socket::enableNonBlockingMode( sal_Bool bNonBlockingMode
)
369 return osl_enableNonBlockingMode( m_handle
, bNonBlockingMode
);
372 //______________________________________________________________________________
373 inline sal_Bool
Socket::isNonBlockingMode() const
375 return osl_isNonBlockingMode( m_handle
);
378 //______________________________________________________________________________
379 inline void SAL_CALL
Socket::clearError() const
382 getOption(osl_Socket_OptionError
, &err
, sizeof(err
));
385 //______________________________________________________________________________
386 inline oslSocketError
Socket::getError() const
388 return osl_getLastSocketError( m_handle
);
391 //______________________________________________________________________________
392 inline ::rtl::OUString
Socket::getErrorAsString( ) const
394 ::rtl::OUString error
;
395 osl_getLastSocketErrorDescription( m_handle
, &(error
.pData
) );
399 //______________________________________________________________________________
400 inline oslSocket
Socket::getHandle() const
405 //______________________________________________________________________________
406 inline StreamSocket::StreamSocket(oslAddrFamily Family
,
407 oslProtocol Protocol
,
409 : Socket( Type
, Family
, Protocol
)
412 //______________________________________________________________________________
413 inline StreamSocket::StreamSocket( oslSocket socketHandle
, __sal_NoAcquire noacquire
)
414 : Socket( socketHandle
, noacquire
)
417 //______________________________________________________________________________
418 inline StreamSocket::StreamSocket( oslSocket socketHandle
)
419 : Socket( socketHandle
)
422 //______________________________________________________________________________
423 inline StreamSocket::StreamSocket( const StreamSocket
& socket
)
427 //______________________________________________________________________________
428 inline sal_Int32
StreamSocket::read(void* pBuffer
, sal_uInt32 n
)
430 return osl_readSocket( m_handle
, pBuffer
, n
);
433 //______________________________________________________________________________
434 inline sal_Int32
StreamSocket::write(const void* pBuffer
, sal_uInt32 n
)
436 return osl_writeSocket( m_handle
, pBuffer
, n
);
440 //______________________________________________________________________________
441 inline sal_Int32
StreamSocket::recv(void* pBuffer
,
442 sal_uInt32 BytesToRead
,
443 oslSocketMsgFlag Flag
)
445 return osl_receiveSocket( m_handle
, pBuffer
,BytesToRead
, Flag
);
448 //______________________________________________________________________________
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
);
456 //______________________________________________________________________________
457 inline ConnectorSocket::ConnectorSocket(oslAddrFamily Family
,
458 oslProtocol Protocol
,
460 : StreamSocket( Family
, Protocol
,Type
)
463 //______________________________________________________________________________
464 inline oslSocketResult
ConnectorSocket::connect( const SocketAddr
& TargetHost
,
465 const TimeValue
* pTimeout
)
467 return osl_connectSocketTo( m_handle
, TargetHost
.getHandle(), pTimeout
);
470 //______________________________________________________________________________
471 inline AcceptorSocket::AcceptorSocket(oslAddrFamily Family
,
472 oslProtocol Protocol
,
474 : Socket( Type
, Family
, Protocol
)
477 //______________________________________________________________________________
478 inline sal_Bool
AcceptorSocket::listen(sal_Int32 MaxPendingConnections
)
480 return osl_listenOnSocket( m_handle
, MaxPendingConnections
);
483 //______________________________________________________________________________
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
;
500 //______________________________________________________________________________
501 inline oslSocketResult
AcceptorSocket::acceptConnection(
502 StreamSocket
& Connection
, SocketAddr
& PeerAddr
)
504 // TODO change in/OUT parameter
505 oslSocket o
= osl_acceptConnectionOnSocket( m_handle
, (oslSocketAddr
*)&PeerAddr
);
506 oslSocketResult status
= osl_Socket_Ok
;
509 Connection
= StreamSocket( o
, SAL_NO_ACQUIRE
);
513 Connection
= StreamSocket();
514 status
= osl_Socket_Error
;
519 //______________________________________________________________________________
520 inline DatagramSocket::DatagramSocket(oslAddrFamily Family
,
521 oslProtocol Protocol
,
523 : Socket( Type
, Family
, Protocol
)
526 //______________________________________________________________________________
527 inline sal_Int32
DatagramSocket::recvFrom(void* pBuffer
,
528 sal_uInt32 BufferSize
,
529 SocketAddr
* pSenderAddr
,
530 oslSocketMsgFlag Flag
)
535 // TODO : correct the out-parameter pSenderAddr outparameter
536 nByteRead
= osl_receiveFromSocket( m_handle
, pSenderAddr
->getHandle() , pBuffer
,
538 // nByteRead = osl_receiveFromSocket( m_handle, *(oslSocketAddr**) &pSenderAddr , pBuffer,
539 // BufferSize, Flag);
543 nByteRead
= osl_receiveFromSocket( m_handle
, 0 , pBuffer
, BufferSize
, Flag
);
548 //______________________________________________________________________________
549 inline sal_Int32
DatagramSocket::sendTo( const SocketAddr
& ReceiverAddr
,
551 sal_uInt32 BufferSize
,
552 oslSocketMsgFlag Flag
)
554 return osl_sendToSocket( m_handle
, ReceiverAddr
.getHandle(), pBuffer
, BufferSize
, Flag
);
559 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */