UDK 3.2.7 C/C++ API Reference

osl/socket.hxx

Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /*
00003  * This file is part of the LibreOffice project.
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this
00007  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
00008  *
00009  * This file incorporates work covered by the following license notice:
00010  *
00011  *   Licensed to the Apache Software Foundation (ASF) under one or more
00012  *   contributor license agreements. See the NOTICE file distributed
00013  *   with this work for additional information regarding copyright
00014  *   ownership. The ASF licenses this file to you under the Apache
00015  *   License, Version 2.0 (the "License"); you may not use this file
00016  *   except in compliance with the License. You may obtain a copy of
00017  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
00018  */
00019 #ifndef _OSL_SOCKET_HXX_
00020 #define _OSL_SOCKET_HXX_
00021 
00022 #include <osl/socket_decl.hxx>
00023 
00024 namespace osl
00025 {
00026     //______________________________________________________________________________
00027     inline SocketAddr::SocketAddr()
00028         : m_handle( osl_createEmptySocketAddr( osl_Socket_FamilyInet ) )
00029     {}
00030 
00031     //______________________________________________________________________________
00032     inline SocketAddr::SocketAddr(const SocketAddr& Addr)
00033         : m_handle( osl_copySocketAddr( Addr.m_handle ) )
00034     {
00035     }
00036 
00037     //______________________________________________________________________________
00038     inline SocketAddr::SocketAddr(oslSocketAddr Addr)
00039         : m_handle( osl_copySocketAddr( Addr ) )
00040     {
00041     }
00042 
00043     //______________________________________________________________________________
00044     inline SocketAddr::SocketAddr(oslSocketAddr Addr, __osl_socket_NoCopy )
00045         : m_handle( Addr )
00046     {
00047     }
00048 
00049     //______________________________________________________________________________
00050     inline SocketAddr::SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort)
00051         : m_handle( osl_createInetSocketAddr( strAddrOrHostName.pData, nPort ) )
00052     {
00053         if(! m_handle )
00054         {
00055             m_handle = osl_resolveHostname(strAddrOrHostName.pData);
00056 
00057             // host found?
00058             if(m_handle)
00059             {
00060                 osl_setInetPortOfSocketAddr(m_handle, nPort);
00061             }
00062             else
00063             {
00064                 osl_destroySocketAddr( m_handle );
00065                 m_handle = 0;
00066             }
00067         }
00068     }
00069 
00070     //______________________________________________________________________________
00071     inline SocketAddr::~SocketAddr()
00072     {
00073         if( m_handle )
00074             osl_destroySocketAddr( m_handle );
00075     }
00076 
00077     //______________________________________________________________________________
00078     inline ::rtl::OUString SocketAddr::getHostname( oslSocketResult *pResult ) const
00079     {
00080         ::rtl::OUString hostname;
00081         oslSocketResult result = osl_getHostnameOfSocketAddr( m_handle, &(hostname.pData) );
00082         if( pResult )
00083             *pResult = result;
00084         return hostname;
00085     }
00086 
00087     //______________________________________________________________________________
00088     inline sal_Int32 SAL_CALL SocketAddr::getPort() const
00089     {
00090         return osl_getInetPortOfSocketAddr(m_handle);
00091     }
00092 
00093     //______________________________________________________________________________
00094     inline sal_Bool SAL_CALL SocketAddr::setPort( sal_Int32 nPort )
00095     {
00096         return osl_setInetPortOfSocketAddr(m_handle, nPort );
00097     }
00098 
00099     inline sal_Bool SAL_CALL SocketAddr::setHostname( const ::rtl::OUString &sDottedIpOrHostname )
00100     {
00101         *this = SocketAddr( sDottedIpOrHostname , getPort() );
00102         return is();
00103     }
00104 
00105     //______________________________________________________________________________
00106     inline sal_Bool SAL_CALL SocketAddr::setAddr( const ::rtl::ByteSequence & address )
00107     {
00108         return osl_setAddrOfSocketAddr( m_handle, address.getHandle() )
00109             == osl_Socket_Ok;
00110     }
00111 
00112     inline ::rtl::ByteSequence SAL_CALL SocketAddr::getAddr( oslSocketResult *pResult ) const
00113     {
00114         ::rtl::ByteSequence sequence;
00115         oslSocketResult result = osl_getAddrOfSocketAddr( m_handle,(sal_Sequence **) &sequence );
00116         if( pResult )
00117             *pResult = result;
00118         return sequence;
00119     }
00120 
00121     //______________________________________________________________________________
00122     inline SocketAddr & SAL_CALL SocketAddr::operator= (oslSocketAddr Addr)
00123     {
00124         oslSocketAddr pNewAddr = osl_copySocketAddr( Addr );
00125         if( m_handle )
00126             osl_destroySocketAddr( m_handle );
00127         m_handle = pNewAddr;
00128         return *this;
00129     }
00130 
00131     //______________________________________________________________________________
00132     inline SocketAddr & SAL_CALL SocketAddr::operator= (const SocketAddr& Addr)
00133     {
00134         *this = (Addr.getHandle());
00135         return *this;
00136     }
00137 
00138     inline SocketAddr & SAL_CALL SocketAddr::assign( oslSocketAddr Addr, __osl_socket_NoCopy )
00139     {
00140         if( m_handle )
00141             osl_destroySocketAddr( m_handle );
00142         m_handle = Addr;
00143         return *this;
00144     }
00145 
00146     //______________________________________________________________________________
00147     inline sal_Bool SAL_CALL SocketAddr::operator== (oslSocketAddr Addr) const
00148     {
00149         return osl_isEqualSocketAddr( m_handle, Addr );
00150     }
00151 
00152     inline oslSocketAddr SocketAddr::getHandle() const
00153     {
00154         return m_handle;
00155     }
00156 
00157     //______________________________________________________________________________
00158     inline sal_Bool SocketAddr::is() const
00159     {
00160         return m_handle != 0;
00161     }
00162 
00163     // (static method)______________________________________________________________
00164     inline ::rtl::OUString SAL_CALL SocketAddr::getLocalHostname( oslSocketResult *pResult )
00165     {
00166         ::rtl::OUString hostname;
00167         oslSocketResult result = osl_getLocalHostname( &(hostname.pData) );
00168         if(pResult )
00169             *pResult = result;
00170         return hostname;
00171     }
00172 
00173     // (static method)______________________________________________________________
00174     inline void SAL_CALL SocketAddr::resolveHostname(
00175         const ::rtl::OUString & strHostName, SocketAddr &Addr)
00176     {
00177         Addr = SocketAddr( osl_resolveHostname( strHostName.pData ) , SAL_NO_COPY );
00178     }
00179 
00180     // (static method)______________________________________________________________
00181     inline sal_Int32 SAL_CALL SocketAddr::getServicePort(
00182             const ::rtl::OUString& strServiceName,
00183             const ::rtl::OUString & strProtocolName )
00184     {
00185         return osl_getServicePort( strServiceName.pData, strProtocolName.pData );
00186     }
00187 
00188     //______________________________________________________________________________
00189     inline Socket::Socket(oslSocketType Type,
00190                           oslAddrFamily Family,
00191                           oslProtocol   Protocol)
00192         : m_handle( osl_createSocket(Family, Type, Protocol) )
00193     {}
00194 
00195     //______________________________________________________________________________
00196     inline Socket::Socket( oslSocket socketHandle, __sal_NoAcquire )
00197         : m_handle( socketHandle )
00198     {}
00199 
00200     //______________________________________________________________________________
00201     inline Socket::Socket( oslSocket socketHandle )
00202         : m_handle( socketHandle )
00203     {
00204         osl_acquireSocket( m_handle );
00205     }
00206 
00207     //______________________________________________________________________________
00208     inline Socket::Socket( const Socket & socket )
00209         : m_handle( socket.getHandle() )
00210     {
00211         osl_acquireSocket( m_handle );
00212     }
00213 
00214     //______________________________________________________________________________
00215     inline Socket::~Socket()
00216     {
00217         osl_releaseSocket( m_handle );
00218     }
00219 
00220     //______________________________________________________________________________
00221     inline Socket& Socket::operator= ( oslSocket socketHandle)
00222     {
00223         osl_acquireSocket( socketHandle );
00224         osl_releaseSocket( m_handle );
00225         m_handle = socketHandle;
00226         return *this;
00227     }
00228 
00229     //______________________________________________________________________________
00230     inline Socket&  Socket::operator= (const Socket& sock)
00231     {
00232         return (*this) = sock.getHandle();
00233     }
00234 
00235     //______________________________________________________________________________
00236     inline sal_Bool Socket::operator==( const Socket& rSocket ) const
00237     {
00238         return m_handle == rSocket.getHandle();
00239     }
00240 
00241     //______________________________________________________________________________
00242     inline sal_Bool Socket::operator==( const oslSocket socketHandle ) const
00243     {
00244         return m_handle == socketHandle;
00245     }
00246 
00247     //______________________________________________________________________________
00248     inline void Socket::shutdown( oslSocketDirection Direction )
00249     {
00250         osl_shutdownSocket( m_handle , Direction );
00251     }
00252 
00253     //______________________________________________________________________________
00254     inline void Socket::close()
00255     {
00256         osl_closeSocket( m_handle );
00257     }
00258 
00259     //______________________________________________________________________________
00260     inline void Socket::getLocalAddr( SocketAddr & addr) const
00261     {
00262         addr.assign( osl_getLocalAddrOfSocket( m_handle ) , SAL_NO_COPY );
00263     }
00264 
00265     //______________________________________________________________________________
00266     inline sal_Int32 Socket::getLocalPort() const
00267     {
00268         SocketAddr addr( 0 );
00269         getLocalAddr( addr );
00270         return addr.getPort();
00271     }
00272 
00273     //______________________________________________________________________________
00274     inline ::rtl::OUString Socket::getLocalHost() const
00275     {
00276         SocketAddr addr( 0 );
00277         getLocalAddr( addr );
00278         return addr.getHostname();
00279     }
00280 
00281     //______________________________________________________________________________
00282     inline void Socket::getPeerAddr( SocketAddr &addr ) const
00283     {
00284         addr.assign( osl_getPeerAddrOfSocket( m_handle ), SAL_NO_COPY );
00285     }
00286 
00287     //______________________________________________________________________________
00288     inline sal_Int32 Socket::getPeerPort() const
00289     {
00290         SocketAddr addr( 0 );
00291         getPeerAddr( addr );
00292         return addr.getPort();
00293     }
00294 
00295     //______________________________________________________________________________
00296     inline ::rtl::OUString Socket::getPeerHost() const
00297     {
00298         SocketAddr addr( 0 );
00299         getPeerAddr( addr );
00300         return addr.getHostname();
00301     }
00302 
00303     //______________________________________________________________________________
00304     inline sal_Bool Socket::bind(const SocketAddr& LocalInterface)
00305     {
00306         return osl_bindAddrToSocket( m_handle , LocalInterface.getHandle() );
00307     }
00308 
00309     //______________________________________________________________________________
00310     inline sal_Bool Socket::isRecvReady(const TimeValue *pTimeout ) const
00311     {
00312         return osl_isReceiveReady( m_handle , pTimeout );
00313     }
00314 
00315     //______________________________________________________________________________
00316     inline sal_Bool Socket::isSendReady(const TimeValue *pTimeout ) const
00317     {
00318         return osl_isSendReady( m_handle, pTimeout );
00319     }
00320 
00321     //______________________________________________________________________________
00322     inline sal_Bool Socket::isExceptionPending(const TimeValue *pTimeout ) const
00323     {
00324         return osl_isExceptionPending( m_handle, pTimeout );
00325     }
00326 
00327     //______________________________________________________________________________
00328     inline oslSocketType Socket::getType() const
00329     {
00330         return osl_getSocketType( m_handle );
00331     }
00332 
00333     //______________________________________________________________________________
00334     inline sal_Int32  Socket::getOption(
00335         oslSocketOption Option,
00336         void* pBuffer,
00337         sal_uInt32 BufferLen,
00338         oslSocketOptionLevel Level) const
00339     {
00340         return osl_getSocketOption( m_handle, Level, Option, pBuffer , BufferLen );
00341     }
00342 
00343     //______________________________________________________________________________
00344     inline sal_Bool Socket::setOption(  oslSocketOption Option,
00345                                         void* pBuffer,
00346                                         sal_uInt32 BufferLen,
00347                                         oslSocketOptionLevel Level ) const
00348     {
00349         return osl_setSocketOption( m_handle, Level, Option , pBuffer, BufferLen );
00350     }
00351 
00352     //______________________________________________________________________________
00353     inline sal_Bool Socket::setOption( oslSocketOption option, sal_Int32 nValue  )
00354     {
00355         return setOption( option, &nValue, sizeof( nValue ) );
00356     }
00357 
00358     //______________________________________________________________________________
00359     inline sal_Int32 Socket::getOption( oslSocketOption option ) const
00360     {
00361         sal_Int32 n;
00362         getOption( option, &n, sizeof( n ) );
00363         return n;
00364     }
00365 
00366     //______________________________________________________________________________
00367     inline sal_Bool Socket::enableNonBlockingMode( sal_Bool bNonBlockingMode)
00368     {
00369         return osl_enableNonBlockingMode( m_handle , bNonBlockingMode );
00370     }
00371 
00372     //______________________________________________________________________________
00373     inline sal_Bool Socket::isNonBlockingMode() const
00374     {
00375         return osl_isNonBlockingMode( m_handle );
00376     }
00377 
00378     //______________________________________________________________________________
00379     inline void SAL_CALL Socket::clearError() const
00380     {
00381         sal_Int32 err = 0;
00382         getOption(osl_Socket_OptionError, &err, sizeof(err));
00383     }
00384 
00385     //______________________________________________________________________________
00386     inline oslSocketError Socket::getError() const
00387     {
00388         return osl_getLastSocketError( m_handle );
00389     }
00390 
00391     //______________________________________________________________________________
00392     inline ::rtl::OUString Socket::getErrorAsString( ) const
00393     {
00394         ::rtl::OUString error;
00395         osl_getLastSocketErrorDescription( m_handle, &(error.pData) );
00396         return error;
00397     }
00398 
00399     //______________________________________________________________________________
00400     inline oslSocket Socket::getHandle() const
00401     {
00402         return m_handle;
00403     }
00404 
00405     //______________________________________________________________________________
00406     inline StreamSocket::StreamSocket(oslAddrFamily Family,
00407                                       oslProtocol Protocol,
00408                                       oslSocketType Type )
00409         : Socket( Type, Family, Protocol )
00410     {}
00411 
00412     //______________________________________________________________________________
00413     inline StreamSocket::StreamSocket( oslSocket socketHandle, __sal_NoAcquire noacquire )
00414         : Socket( socketHandle, noacquire )
00415     {}
00416 
00417     //______________________________________________________________________________
00418     inline StreamSocket::StreamSocket( oslSocket socketHandle )
00419         : Socket( socketHandle )
00420     {}
00421 
00422     //______________________________________________________________________________
00423     inline StreamSocket::StreamSocket( const StreamSocket & socket )
00424         : Socket( socket )
00425     {}
00426 
00427     //______________________________________________________________________________
00428     inline sal_Int32 StreamSocket::read(void* pBuffer, sal_uInt32 n)
00429     {
00430         return osl_readSocket( m_handle, pBuffer, n );
00431     }
00432 
00433     //______________________________________________________________________________
00434     inline sal_Int32 StreamSocket::write(const void* pBuffer, sal_uInt32 n)
00435     {
00436         return osl_writeSocket( m_handle, pBuffer, n );
00437     }
00438 
00439 
00440     //______________________________________________________________________________
00441     inline sal_Int32 StreamSocket::recv(void* pBuffer,
00442                                         sal_uInt32 BytesToRead,
00443                                         oslSocketMsgFlag Flag)
00444     {
00445         return osl_receiveSocket( m_handle, pBuffer,BytesToRead, Flag );
00446     }
00447 
00448     //______________________________________________________________________________
00449     inline sal_Int32 StreamSocket::send(const void* pBuffer,
00450                                         sal_uInt32 BytesToSend,
00451                                         oslSocketMsgFlag Flag)
00452     {
00453         return osl_sendSocket( m_handle, pBuffer, BytesToSend, Flag );
00454     }
00455 
00456     //______________________________________________________________________________
00457       inline ConnectorSocket::ConnectorSocket(oslAddrFamily Family,
00458                                             oslProtocol Protocol,
00459                                             oslSocketType   Type)
00460         : StreamSocket( Family, Protocol ,Type )
00461     {}
00462 
00463     //______________________________________________________________________________
00464     inline oslSocketResult ConnectorSocket::connect( const SocketAddr& TargetHost,
00465                                                      const TimeValue* pTimeout )
00466     {
00467         return osl_connectSocketTo( m_handle , TargetHost.getHandle(), pTimeout );
00468     }
00469 
00470     //______________________________________________________________________________
00471     inline AcceptorSocket::AcceptorSocket(oslAddrFamily Family ,
00472                                           oslProtocol   Protocol ,
00473                                           oslSocketType Type )
00474         : Socket( Type, Family, Protocol )
00475     {}
00476 
00477     //______________________________________________________________________________
00478     inline sal_Bool AcceptorSocket::listen(sal_Int32 MaxPendingConnections)
00479     {
00480         return osl_listenOnSocket( m_handle, MaxPendingConnections );
00481     }
00482 
00483     //______________________________________________________________________________
00484     inline oslSocketResult AcceptorSocket::acceptConnection( StreamSocket& Connection)
00485     {
00486         oslSocket o = osl_acceptConnectionOnSocket( m_handle, 0 );
00487         oslSocketResult status = osl_Socket_Ok;
00488         if( o )
00489         {
00490             Connection = StreamSocket( o , SAL_NO_ACQUIRE );
00491         }
00492         else
00493         {
00494             Connection = StreamSocket();
00495             status = osl_Socket_Error;
00496         }
00497         return status;
00498     }
00499 
00500     //______________________________________________________________________________
00501     inline oslSocketResult AcceptorSocket::acceptConnection(
00502         StreamSocket&   Connection, SocketAddr & PeerAddr)
00503     {
00504         // TODO change in/OUT parameter
00505         oslSocket o = osl_acceptConnectionOnSocket( m_handle, (oslSocketAddr *)&PeerAddr );
00506         oslSocketResult status = osl_Socket_Ok;
00507         if( o )
00508         {
00509             Connection = StreamSocket( o , SAL_NO_ACQUIRE );
00510         }
00511         else
00512         {
00513             Connection = StreamSocket();
00514             status = osl_Socket_Error;
00515         }
00516         return status;
00517     }
00518 
00519     //______________________________________________________________________________
00520     inline DatagramSocket::DatagramSocket(oslAddrFamily Family,
00521                                           oslProtocol   Protocol,
00522                                           oslSocketType Type)
00523         : Socket( Type, Family, Protocol )
00524     {}
00525 
00526     //______________________________________________________________________________
00527     inline sal_Int32 DatagramSocket::recvFrom(void*  pBuffer,
00528                                               sal_uInt32 BufferSize,
00529                                               SocketAddr* pSenderAddr,
00530                                               oslSocketMsgFlag Flag )
00531     {
00532         sal_Int32 nByteRead;
00533         if( pSenderAddr )
00534         {
00535             // TODO : correct the out-parameter pSenderAddr outparameter
00536               nByteRead = osl_receiveFromSocket( m_handle, pSenderAddr->getHandle() , pBuffer,
00537                                                  BufferSize, Flag);
00538 //              nByteRead = osl_receiveFromSocket( m_handle, *(oslSocketAddr**) &pSenderAddr , pBuffer,
00539 //                                                 BufferSize, Flag);
00540         }
00541         else
00542         {
00543             nByteRead = osl_receiveFromSocket( m_handle, 0 , pBuffer , BufferSize ,  Flag );
00544         }
00545         return nByteRead;
00546     }
00547 
00548     //______________________________________________________________________________
00549     inline sal_Int32  DatagramSocket::sendTo( const SocketAddr& ReceiverAddr,
00550                                               const void* pBuffer,
00551                                               sal_uInt32 BufferSize,
00552                                               oslSocketMsgFlag Flag )
00553     {
00554         return osl_sendToSocket( m_handle, ReceiverAddr.getHandle(), pBuffer, BufferSize, Flag );
00555     }
00556 }
00557 #endif
00558 
00559 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines