PageRenderTime 65ms CodeModel.GetById 14ms app.highlight 44ms RepoModel.GetById 1ms app.codeStats 0ms

/vos/inc/vos/socket.hxx

https://bitbucket.org/mst/ooo340
C++ Header | 1130 lines | 339 code | 180 blank | 611 comment | 1 complexity | cfe1b2aec9c1e4f8dd30c4a96f247aed MD5 | raw file
   1/*************************************************************************
   2 *
   3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4 * 
   5 * Copyright 2000, 2010 Oracle and/or its affiliates.
   6 *
   7 * OpenOffice.org - a multi-platform office productivity suite
   8 *
   9 * This file is part of OpenOffice.org.
  10 *
  11 * OpenOffice.org is free software: you can redistribute it and/or modify
  12 * it under the terms of the GNU Lesser General Public License version 3
  13 * only, as published by the Free Software Foundation.
  14 *
  15 * OpenOffice.org is distributed in the hope that it will be useful,
  16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18 * GNU Lesser General Public License version 3 for more details
  19 * (a copy is included in the LICENSE file that accompanied this code).
  20 *
  21 * You should have received a copy of the GNU Lesser General Public License
  22 * version 3 along with OpenOffice.org.  If not, see
  23 * <http://www.openoffice.org/license.html>
  24 * for a copy of the LGPLv3 License.
  25 *
  26 ************************************************************************/
  27
  28#ifndef _VOS_SOCKET_HXX_
  29#define _VOS_SOCKET_HXX_
  30
  31#   include <vos/types.hxx>
  32#   include <vos/object.hxx>
  33#   include <vos/istream.hxx>
  34#ifndef _VOS_REFERMCE_HXX_
  35#   include <vos/refernce.hxx>
  36#endif
  37#   include <vos/refobj.hxx>
  38#   include <rtl/ustring.hxx>
  39#   include <osl/socket.h>
  40
  41#include <osl/time.h>
  42
  43namespace vos
  44{
  45
  46/** Base data types
  47*/
  48class ISocketTypes
  49{
  50public:
  51
  52    ISocketTypes() { }
  53    virtual ~ISocketTypes() { }
  54
  55    /*
  56        Represents the address-family of a socket
  57    */
  58    enum TAddrFamily {
  59        TFamily_Inet = osl_Socket_FamilyInet,       /* IP */
  60        TFamily_Ipx  = osl_Socket_FamilyIpx,        /* Novell IPX/SPX */
  61        TFamily_Invalid = osl_Socket_FamilyInvalid
  62    };
  63
  64    /*
  65        represent a specific protocol within a address-family
  66    */
  67    enum TProtocol {
  68        TProtocol_Ip    = osl_Socket_ProtocolIp,        /* for all af_inet */
  69        TProtocol_Ipx   = osl_Socket_ProtocolIpx,       /* af_ipx datagram sockets (IPX) */
  70        TProtocol_Spx   = osl_Socket_ProtocolSpx,       /* af_ipx seqpacket or stream for SPX */
  71        TProtocol_SpxII = osl_Socket_ProtocolSpxII,     /* af_ipx seqpacket or stream for SPX II */
  72        TProtocol_Invalid = osl_Socket_ProtocolInvalid
  73    };
  74
  75    /*
  76        Represents the type of a socket
  77    */
  78    enum TSocketType {
  79        TType_Stream    = osl_Socket_TypeStream,
  80        TType_Dgram     = osl_Socket_TypeDgram,
  81        TType_Raw       = osl_Socket_TypeRaw,
  82        TType_Rdm       = osl_Socket_TypeRdm,
  83        TType_SeqPacket = osl_Socket_TypeSeqPacket,
  84        TType_Invalid = osl_Socket_TypeInvalid
  85    };
  86
  87    /*
  88        Represents socket-options
  89    */
  90    enum TSocketOption {
  91        TOption_Debug       = osl_Socket_OptionDebug,
  92        TOption_AcceptConn  = osl_Socket_OptionAcceptConn,
  93        TOption_ReuseAddr   = osl_Socket_OptionReuseAddr,
  94        TOption_KeepAlive   = osl_Socket_OptionKeepAlive,
  95        TOption_DontRoute   = osl_Socket_OptionDontRoute,
  96        TOption_Broadcast   = osl_Socket_OptionBroadcast,
  97        TOption_UseLoopback = osl_Socket_OptionUseLoopback,
  98        TOption_Linger      = osl_Socket_OptionLinger,
  99        TOption_OOBinLine   = osl_Socket_OptionOOBinLine,
 100        TOption_SndBuf      = osl_Socket_OptionSndBuf,
 101        TOption_RcvBuf      = osl_Socket_OptionRcvBuf,
 102        TOption_SndLowat    = osl_Socket_OptionSndLowat,
 103        TOption_RcvLowat    = osl_Socket_OptionRcvLowat,
 104        TOption_SndTimeo    = osl_Socket_OptionSndTimeo,
 105        TOption_RcvTimeo    = osl_Socket_OptionRcvTimeo,
 106        TOption_Error       = osl_Socket_OptionError,
 107        TOption_Type        = osl_Socket_OptionType,
 108        TOption_TcpNoDelay  = osl_Socket_OptionTcpNoDelay,
 109        TOption_Invalid     = osl_Socket_OptionInvalid
 110    };
 111
 112    /*
 113        Represents the different socket-option levels
 114    */
 115    enum TSocketOptionLevel {
 116        TLevel_Socket = osl_Socket_LevelSocket,
 117        TLevel_Tcp    = osl_Socket_LevelTcp,
 118        TLevel_Invalid = osl_Socket_LevelInvalid
 119    };
 120
 121    /*
 122        Represents flags to be used with send/recv-calls.
 123    */
 124    enum TSocketMsgFlag {
 125        TMsg_Normal    = osl_Socket_MsgNormal,
 126        TMsg_OOB       = osl_Socket_MsgOOB,
 127        TMsg_Peek      = osl_Socket_MsgPeek,
 128        TMsg_DontRoute = osl_Socket_MsgDontRoute,
 129        TMsg_MaxIOVLen = osl_Socket_MsgMaxIOVLen,
 130        TMsg_Invalid   = osl_Socket_MsgInvalid
 131    };
 132
 133    /*
 134        Used by shutdown to denote which end of the socket to "close".
 135    */
 136    enum TSocketDirection {
 137        TDirection_Read      = osl_Socket_DirRead,
 138        TDirection_Write     = osl_Socket_DirWrite,
 139        TDirection_ReadWrite = osl_Socket_DirReadWrite,
 140        TDirection_Invalid   = osl_Socket_DirInvalid
 141    };
 142
 143    enum TSocketError {
 144        E_None              = osl_Socket_E_None,              /* no error */
 145        E_NotSocket         = osl_Socket_E_NotSocket,         /* Socket operation on non-socket */
 146        E_DestAddrReq       = osl_Socket_E_DestAddrReq,       /* Destination address required */
 147        E_MsgSize           = osl_Socket_E_MsgSize,           /* Message too sal_Int32 */
 148        E_Prototype         = osl_Socket_E_Prototype,         /* Protocol wrong type for socket */
 149        E_NoProtocol        = osl_Socket_E_NoProtocol,        /* Protocol not available */
 150        E_ProtocolNoSupport = osl_Socket_E_ProtocolNoSupport, /* Protocol not supported */
 151        E_TypeNoSupport     = osl_Socket_E_TypeNoSupport,     /* Socket type not supported */
 152        E_OpNotSupport      = osl_Socket_E_OpNotSupport,      /* Operation not supported on socket */
 153        E_PfNoSupport       = osl_Socket_E_PfNoSupport,       /* Protocol family not supported */
 154        E_AfNoSupport       = osl_Socket_E_AfNoSupport,       /* Address family not supported by */
 155                                                              /* protocol family */
 156        E_AddrInUse         = osl_Socket_E_AddrInUse,         /* Address already in use */
 157        E_AddrNotAvail      = osl_Socket_E_AddrNotAvail,      /* Can't assign requested address */
 158        E_NetDown           = osl_Socket_E_NetDown,           /* Network is down */
 159        E_NetUnreachable    = osl_Socket_E_NetUnreachable,    /* Network is unreachable */
 160        E_NetReset          = osl_Socket_E_NetReset,          /* Network dropped connection because */
 161                                                              /* of reset */
 162        E_ConnAborted       = osl_Socket_E_ConnAborted,       /* Software caused connection abort */
 163        E_ConnReset         = osl_Socket_E_ConnReset,         /* Connection reset by peer */
 164        E_NoBufferSpace     = osl_Socket_E_NoBufferSpace,     /* No buffer space available */
 165        E_IsConnected       = osl_Socket_E_IsConnected,       /* Socket is already connected */
 166        E_NotConnected      = osl_Socket_E_NotConnected,      /* Socket is not connected */
 167        E_Shutdown          = osl_Socket_E_Shutdown,          /* Can't send after socket shutdown */
 168        E_TooManyRefs       = osl_Socket_E_TooManyRefs,       /* Too many references: can't splice */
 169        E_TimedOut          = osl_Socket_E_TimedOut,          /* Connection timed out */
 170        E_ConnRefused       = osl_Socket_E_ConnRefused,       /* Connection refused */
 171        E_HostDown          = osl_Socket_E_HostDown,          /* Host is down */
 172        E_HostUnreachable   = osl_Socket_E_HostUnreachable,   /* No route to host */
 173        E_WouldBlock        = osl_Socket_E_WouldBlock,        /* call would block on non-blocking socket */
 174        E_Already           = osl_Socket_E_Already,           /* operation already in progress */
 175        E_InProgress        = osl_Socket_E_InProgress,        /* operation now in progress */
 176
 177        E_Invalid = osl_Socket_E_InvalidError   /* unmapped error */
 178    };
 179
 180    enum TResult {
 181        TResult_Ok          = osl_Socket_Ok,          /* successful completion */
 182        TResult_Error       = osl_Socket_Error,       /* error occured, check osl_getLastSocketError() for details */
 183        TResult_TimedOut    = osl_Socket_TimedOut,    /* blocking operation timed out */
 184        TResult_Interrupted = osl_Socket_Interrupted, /* blocking operation was interrupted */
 185        TResult_InProgress  = osl_Socket_InProgress   /* nonblocking operation is in progress */
 186    };
 187};
 188
 189
 190/** Base class for socket addresses.
 191*/
 192class ISocketAddr : public NAMESPACE_VOS(ISocketTypes)
 193{
 194public:
 195    virtual ~ISocketAddr() { }
 196
 197
 198    virtual TAddrFamily SAL_CALL getFamily() const= 0;
 199    virtual TResult SAL_CALL getHostname(::rtl::OUString& strHostName) const= 0;
 200    virtual SAL_CALL operator oslSocketAddr() const= 0;
 201    virtual void SAL_CALL operator= (oslSocketAddr Addr)= 0;
 202    virtual sal_Bool SAL_CALL operator== (oslSocketAddr Addr)= 0;
 203};
 204
 205class OSocketAddr : public NAMESPACE_VOS(ISocketAddr),
 206            public NAMESPACE_VOS(OObject)
 207
 208{
 209    VOS_DECLARE_CLASSINFO(NAMESPACE_VOS(OSocketAddr));
 210public:
 211
 212    /** Creates socket address of unknown type.
 213    */
 214    OSocketAddr();
 215
 216    /** Copy constructor.
 217    */
 218    OSocketAddr(const OSocketAddr& Addr);
 219
 220    /**
 221    */
 222    OSocketAddr(oslSocketAddr Addr);
 223
 224    /** destroys underlying oslSocketAddress
 225    */
 226    virtual ~OSocketAddr();
 227
 228    /** Queries the socket for its address family.
 229        @return the address family of the socket.
 230    */
 231    virtual TAddrFamily SAL_CALL getFamily() const;
 232
 233    /** Cast Object to the underlying oslSocketAddr.
 234    */
 235    virtual SAL_CALL operator oslSocketAddr() const;
 236
 237    /** Converts the address to a (human readable) domain-name.
 238        @return the hostname represented by the address.
 239        On failure returns the empty string.
 240    */
 241    virtual TResult SAL_CALL getHostname(::rtl::OUString& strHostName) const;
 242
 243    /** Get the hostname for the local interface.
 244        @return the hostname or an error.
 245    */
 246    static TResult SAL_CALL getLocalHostname(::rtl::OUString& strLocalHostName);
 247
 248    /** Tries to find an address for a host.
 249        @return A new created socket-address or 0 if the name could not be found.
 250    */
 251    static oslSocketAddr SAL_CALL resolveHostname(const ::rtl::OUString& strHostName);
 252
 253    /** Wraps itself around the osl Socket-Address.
 254        The object assumes ownership of the Addr, it
 255        will be destroyed by destructor(). If the socket is already attached to
 256        an oslSocketAddr, the existing one will be destroyed.
 257    */
 258    virtual void SAL_CALL operator= (oslSocketAddr Addr);
 259
 260    /** Compares to Addr
 261    */
 262    virtual sal_Bool SAL_CALL operator== (oslSocketAddr Addr);
 263
 264    /** Makes a copy of Addr.
 265    */
 266    OSocketAddr& SAL_CALL operator= (const OSocketAddr& Addr);
 267
 268
 269protected:
 270
 271    oslSocketAddr m_SockAddr;
 272};
 273
 274
 275/** Represents an internet-address.
 276*/
 277class OInetSocketAddr : public NAMESPACE_VOS(OSocketAddr)
 278{
 279    VOS_DECLARE_CLASSINFO(NAMESPACE_VOS(OInetSocketAddr));
 280public:
 281
 282    /** Creates an empty internet-address (INADDR_ANY).
 283    */
 284    OInetSocketAddr();
 285
 286    /** Wraps itself around the osl Socket-Address.
 287        The object assumes ownership of the Addr, it
 288        will be destroyed by ~OInetSocketAddr().
 289    */
 290    OInetSocketAddr(oslSocketAddr Addr);
 291
 292    /**
 293        Create a socket address either from a dotted decimal address
 294        (e.g. 141.99.128.50) or a hostname (e.g. www.stardiv.de).
 295    */
 296    OInetSocketAddr(const ::rtl::OUString& strAddrOrHostName, sal_Int32 Port);
 297
 298    /**
 299        Copy constructor.
 300    */
 301    OInetSocketAddr(const OInetSocketAddr& Addr);
 302
 303    /**
 304    */
 305    OInetSocketAddr(const OSocketAddr& Addr);
 306
 307
 308    virtual ~OInetSocketAddr();
 309
 310    /**
 311    */
 312    virtual void SAL_CALL operator= (oslSocketAddr Addr);
 313
 314    /**
 315    */
 316    virtual sal_Bool SAL_CALL operator== (oslSocketAddr Addr);
 317
 318    /**
 319    */
 320    OInetSocketAddr& SAL_CALL operator= (const OInetSocketAddr& Addr);
 321
 322    /**
 323    */
 324    OInetSocketAddr& SAL_CALL operator= (const OSocketAddr& Addr);
 325
 326    /**
 327        Tries to find the port associated with the given service/protocol-
 328        pair (e.g. "ftp"/"tcp").
 329        @return the port number in host-byte order or CVOS_PORT_NONE
 330        if no service/protocol pair could be found.
 331    */
 332    static sal_Int32 SAL_CALL getServicePort(const ::rtl::OUString& strServiceName,
 333                              const ::rtl::OUString& strProtocolName= ::rtl::OUString::createFromAscii( "tcp" ) );
 334
 335
 336    /** Delivers the port number of the address.
 337        @return the port in host-byte order or or OSL_INVALID_PORT on errors.
 338    */
 339    sal_Int32   SAL_CALL getPort() const;
 340
 341    /** Sets the port number of the address.
 342        @return False if the port couldn't be set
 343        (e.g because the address is not of family TFamily_Inet).
 344    */
 345    sal_Bool    SAL_CALL setPort(sal_Int32 Port);
 346
 347    /** @return the dotted-address-form (141.99.128.90) of this address.
 348        On failure returns the empty string.
 349    */
 350    TResult SAL_CALL getDottedAddr(::rtl::OUString& strDottedAddr) const;
 351
 352    /** Sets the host-part of the address from the dotted-address-form (141.99.128.90)
 353        or from a hostname.
 354        @param strDottedAddrOrHostname the address in dotted form or a hostname.
 355    */
 356    sal_Bool SAL_CALL setAddr(const ::rtl::OUString& strDottedAddrOrHostname);
 357
 358};
 359
 360/** Represents an IPX/SPX address.
 361*/
 362class OIpxSocketAddr :  public NAMESPACE_VOS(OSocketAddr)
 363{
 364    VOS_DECLARE_CLASSINFO(NAMESPACE_VOS(OIpxSocketAddr));
 365public:
 366
 367    typedef oslSocketIpxNetNumber  TIpxNetNumber;
 368    typedef oslSocketIpxNodeNumber TIpxNodeNumber;
 369
 370    /** Creates an empty ipx-address.
 371    */
 372    OIpxSocketAddr();
 373
 374    /** Wraps itself around the osl Socket-Address.
 375        The object assumes ownership of the Addr, it
 376        will be destroyed by the destructor.
 377    */
 378    OIpxSocketAddr(oslSocketAddr Addr);
 379
 380    /** Create an IPX/SPX socketaddress from native parameters.
 381    */
 382    OIpxSocketAddr(const ::rtl::OUString& strNetNumber,
 383                   const ::rtl::OUString& strNodeNumber,
 384                   sal_uInt32 SocketNumber);
 385
 386    /** Copy constructor.
 387    */
 388    OIpxSocketAddr(const OIpxSocketAddr& Addr);
 389
 390    /**
 391    */
 392    OIpxSocketAddr(const OSocketAddr& Addr);
 393
 394    virtual  ~OIpxSocketAddr();
 395
 396    /**
 397    */
 398    virtual void SAL_CALL operator= (oslSocketAddr Addr);
 399
 400    /**
 401    */
 402    virtual sal_Bool SAL_CALL operator== (oslSocketAddr Addr);
 403
 404    /**
 405    */
 406    OIpxSocketAddr& SAL_CALL operator= (const OIpxSocketAddr& Addr);
 407
 408    /**
 409    */
 410    OIpxSocketAddr& SAL_CALL operator= (const OSocketAddr& Addr);
 411
 412    /**
 413    */
 414    TResult SAL_CALL getNetNumber(TIpxNetNumber& NetNumber) const;
 415
 416    /**
 417    */
 418    TResult SAL_CALL getNodeNumber(TIpxNodeNumber& NodeNumber) const;
 419
 420    /**
 421    */
 422    sal_uInt32 SAL_CALL getSocketNumber() const;
 423
 424    /** Builds a human readable string in the format network.node:socket.
 425        The numbers are given in hexadecimal form.
 426    */
 427    void SAL_CALL getAddressString(::rtl::OUString& strAddressString) const;
 428};
 429
 430
 431/** Represents a socket.
 432*/
 433class OSocket : public NAMESPACE_VOS(ISocketTypes),
 434                public NAMESPACE_VOS(OReference),
 435                public NAMESPACE_VOS(OObject)
 436{
 437    VOS_DECLARE_CLASSINFO(NAMESPACE_VOS(OSocket));
 438
 439protected:
 440    typedef ORefObj<oslSocket> SockRef;
 441
 442    SockRef* m_pSockRef;
 443
 444    TimeValue* m_pSendTimeout;
 445    TimeValue* m_pRecvTimeout;
 446
 447public:
 448
 449    /** Does not create a socket. Use assignment operator to
 450        make this a useable socket.
 451    */
 452    OSocket();
 453
 454    /** Creates a socket.
 455        @param Family
 456        @param Type
 457        @param Protocol
 458    */
 459    OSocket(TSocketType Type,
 460            TAddrFamily Family = TFamily_Inet,
 461            TProtocol   Protocol = TProtocol_Ip);
 462
 463    /** Copy constructor.
 464    */
 465    OSocket(const OSocket& sock);
 466
 467    /** Creates socket as wrapper around the underlying oslSocket.
 468        @param Socket
 469    */
 470    OSocket(oslSocket Socket);
 471
 472    /** Destructor. Destroys the underlying oslSocket.
 473    */
 474    virtual ~OSocket();
 475
 476    /** Create a socket with the given attributes.
 477        If socket was already created, the old one will be discarded.
 478        @param Type
 479        @param Family
 480        @param Protocol
 481        @return True if socket was successfully created.
 482    */
 483    sal_Bool SAL_CALL create(TSocketType Type,
 484                   TAddrFamily Family = TFamily_Inet,
 485                   TProtocol   Protocol = TProtocol_Ip);
 486
 487    /** Assignment operator. If socket was already created, the old one will
 488        be discarded.
 489    */
 490    OSocket& SAL_CALL operator= (const OSocket& sock);
 491
 492    /** Allow cast to underlying oslSocket.
 493    */
 494    SAL_CALL operator oslSocket() const;
 495
 496    /** Checks if the socket is valid.
 497        @return True if the object represents a valid socket.
 498    */
 499    sal_Bool SAL_CALL isValid() const;
 500
 501    sal_Bool SAL_CALL operator==( const OSocket& rSocket )
 502    {
 503        return m_pSockRef == rSocket.m_pSockRef;
 504    }
 505
 506    /** Closes the socket.
 507    */
 508    virtual void SAL_CALL close();
 509
 510    /** Retrieves the address of the local interface of this socket.
 511        @param Addr [out] receives the address.
 512    */
 513    void SAL_CALL getLocalAddr(OSocketAddr& Addr) const;
 514
 515    /** Get the local port of the socket.
 516        @return the port number or OSL_INVALID_PORT on errors.
 517    */
 518    sal_Int32   SAL_CALL getLocalPort() const;
 519
 520    /** Get the hostname for the local interface.
 521        @return the hostname or an empty string ("").
 522    */
 523    TResult SAL_CALL getLocalHost(::rtl::OUString& strLocalHost) const;
 524
 525    /** Retrieves the address of the remote host of this socket.
 526        @param Addr [out] receives the address.
 527    */
 528    void SAL_CALL getPeerAddr(OSocketAddr& Addr) const;
 529
 530    /** Get the remote port of the socket.
 531        @return the port number or OSL_INVALID_PORT on errors.
 532    */
 533    sal_Int32   SAL_CALL getPeerPort() const;
 534
 535    /** Get the hostname for the remote interface.
 536        @return the hostname or an empty string ("").
 537    */
 538    TResult SAL_CALL getPeerHost(::rtl::OUString& strPeerHost) const;
 539
 540    /** Binds the socket to the specified (local) interface.
 541        @param LocalInterface Address of the Interface
 542        @return True if bind was successful.
 543    */
 544    sal_Bool SAL_CALL bind(const OSocketAddr& LocalInterface);
 545
 546
 547    /** Blocking send operations will unblock after the send-timeout.
 548        @return 0 for disables timeout else timeout value.
 549    */
 550    const TimeValue* SAL_CALL getSendTimeout() const
 551        { return m_pSendTimeout; }
 552
 553    /** Blocking receive operations will unblock after the send-timeout.
 554        @return 0 for disables timeout else timeout value.
 555    */
 556    const TimeValue* SAL_CALL getRecvTimeout() const
 557        { return m_pRecvTimeout; }
 558
 559    /** Blocking send operations will unblock after the send-timeout.
 560        @param time pTimeout == 0 disables timeout. pTimeout != 0 sets timeout value.
 561    */
 562    void SAL_CALL setSendTimeout(const TimeValue* pTimeout = 0);
 563
 564    /** Blocking receive operations will unblock after the send-timeout.
 565        @param time pTimeout == 0 disables timeout. pTimeout != 0 sets timeout value.
 566    */
 567    void SAL_CALL setRecvTimeout(const TimeValue* pTimeout = 0);
 568
 569    /** Checks if read operations will block.
 570        You can specify a timeout-value in seconds/nanoseconds that denotes
 571        how sal_Int32 the operation will block if the Socket is not ready.
 572        @return True if read operations (recv, recvFrom, accept) on the Socket
 573        will NOT block; False if it would block or if an error occured.
 574
 575        @param pTimeout if 0, the operation will block without a timeout. Otherwise
 576        the specified amout of time.
 577    */
 578    sal_Bool    SAL_CALL isRecvReady(const TimeValue* pTimeout = 0) const;
 579
 580    /** Checks if send operations will block.
 581        You can specify a timeout-value in seconds/nanoseconds that denotes
 582        how sal_Int32 the operation will block if the Socket is not ready.
 583        @return True if send operations (send, sendTo) on the Socket
 584        will NOT block; False if it would block or if an error occured.
 585
 586        @param pTimeout if 0, the operation will block without a timeout. Otherwise
 587        the specified amout of time.
 588    */
 589    sal_Bool    SAL_CALL isSendReady(const TimeValue* pTimeout = 0) const;
 590
 591    /** Checks if a request for out-of-band data will block.
 592        You can specify a timeout-value in seconds/nanoseconds that denotes
 593        how sal_Int32 the operation will block if the Socket has no pending OOB data.
 594
 595        @return True if OOB-request operations (recv with appropriate flags)
 596        on the Socket will NOT block; False if it would block or if an error occured.
 597
 598        @param pTimeout if 0, the operation will block without a timeout. Otherwise
 599        the specified amout of time.
 600    */
 601    sal_Bool    SAL_CALL isExceptionPending(const TimeValue* pTimeout = 0) const;
 602
 603    /** Retrieves option-attributes associated with the socket.
 604        @param Option The attribute to query.
 605        Valid values (depending on the Level) are:
 606        <ul>
 607        <li> TOption_Debug
 608        <li> TOption_AcceptConn
 609        <li> TOption_ReuseAddr
 610        <li> TOption_KeepAlive
 611        <li> TOption_DontRoute
 612        <li> TOption_Broadcast
 613        <li> TOption_UseLoopback
 614        <li> TOption_Linger
 615        <li> TOption_OOBinLine
 616        <li> TOption_SndBuf
 617        <li> TOption_RcvBuf
 618        <li> TOption_SndLowat
 619        <li> TOption_RcvLowat
 620        <li> TOption_SndTimeo
 621        <li> TOption_RcvTimeo
 622        <li> TOption_Error
 623        <li> TOption_Type
 624        <li> TOption_TcpNoDelay
 625        </ul>
 626        If not above mentioned otherwise, the options are only valid for
 627        level TLevel_Socket.
 628
 629        @param pBuffer The Buffer will be filled with the attribute.
 630
 631        @param BufferSize The size of pBuffer.
 632
 633        @param Level The option level. Valid values are:
 634        <ul>
 635        <li> TLevel_Socket : Socket Level
 636        <li> TLevel_Tcp    : Level of Transmission Control Protocol
 637        </ul>
 638
 639        @return The size of the attribute copied into pBuffer ot -1 if an error
 640        occured.
 641    */
 642    sal_Int32 SAL_CALL getOption(TSocketOption Option,
 643                      void* pBuffer,
 644                      sal_uInt32 BufferLen,
 645                      TSocketOptionLevel Level= TLevel_Socket) const;
 646
 647    /** Sets the sockets attributes.
 648
 649        @param Option denotes the option to modify.
 650        Valid values (depending on the Level) are:
 651        <ul>
 652        <li> TOption_Debug
 653        <li> TOption_AcceptConn
 654        <li> TOption_ReuseAddr
 655        <li> TOption_KeepAlive
 656        <li> TOption_DontRoute
 657        <li> TOption_Broadcast
 658        <li> TOption_UseLoopback
 659        <li> TOption_Linger
 660        <li> TOption_OOBinLine
 661        <li> TOption_SndBuf
 662        <li> TOption_RcvBuf
 663        <li> TOption_SndLowat
 664        <li> TOption_RcvLowat
 665        <li> TOption_SndTimeo
 666        <li> TOption_RcvTimeo
 667        <li> TOption_Error
 668        <li> TOption_Type
 669        <li> TOption_TcpNoDelay
 670        </ul>
 671        If not above mentioned otherwise, the options are only valid for
 672        level TLevel_Socket.
 673
 674        @param pBuffer Pointer to a Buffer which contains the attribute-value.
 675
 676        @param BufferSize contains the length of the Buffer.
 677
 678        @param Level selects the level for which an option should be changed.
 679        Valid values are:
 680        <ul>
 681        <li> TLevel_Socket : Socket Level
 682        <li> TLevel_Tcp    : Level of Transmission Control Protocol
 683        </ul>
 684
 685        @return True if the option could be changed.
 686    */
 687    sal_Bool SAL_CALL setOption(TSocketOption Option,
 688                      void* pBuffer,
 689                      sal_uInt32 BufferLen,
 690                      TSocketOptionLevel Level= TLevel_Socket) const;
 691
 692
 693    /** Enables/disables non-blocking mode of the socket.
 694        @param On If True, non-blocking mode will be switched on, if False
 695        socket will become a blocking socket, which is the default behaviour of a
 696        socket.
 697        @return True if mode could be set.
 698    */
 699    sal_Bool SAL_CALL enableNonBlockingMode(sal_Bool On= sal_True);
 700
 701    /** Query non-blocking mode of the socket.
 702    @return True if non-blocking mode is set.
 703    */
 704    sal_Bool SAL_CALL isNonBlockingMode() const;
 705
 706    /** Queries the socket for its type.
 707        @return one of:
 708        <ul>
 709        <li> TType_Stream
 710        <li> TType_Dgram
 711        <li> TType_Raw
 712        <li> TType_Rdm
 713        <li> TType_SeqPacket
 714        <li> TType_Invalid
 715        </ul>
 716    */
 717    TSocketType SAL_CALL getType() const;
 718
 719
 720    /** Gets and clears the error status of the socket.
 721        @returns the current error state.
 722    */
 723    sal_Int32   SAL_CALL clearError() const;
 724
 725    /** Enables/Disables debugging.
 726        @param opt 1 sets, 0 resets, -1 won't change anything
 727        @return the previous setting
 728    */
 729    sal_Int32   SAL_CALL setDebug(sal_Int32 opt = -1) const;
 730
 731    /** Allow the socket to be bound to an address that is already in use.
 732        @param opt 1 sets, 0 resets, -1 won't change anything
 733        @return the previous setting
 734    */
 735    sal_Int32   SAL_CALL setReuseAddr(sal_Int32 opt = -1) const;
 736
 737    /** Send keepalive-packets.
 738        @param opt 1 sets, 0 resets, -1 won't change anything
 739        @return the previous setting
 740    */
 741    sal_Int32   SAL_CALL setKeepAlive(sal_Int32 opt = -1) const;
 742
 743    /** Do not route: send directly to interface.
 744        @param opt 1 sets, 0 resets, -1 won't change anything
 745        @return the previous setting
 746    */
 747    sal_Int32   SAL_CALL setDontRoute(sal_Int32 opt = -1) const;
 748
 749
 750    /** Allow transmission of broadcast messages on the socket.
 751        @param opt 1 sets, 0 resets, -1 won't change anything
 752        @return the previous setting
 753    */
 754    sal_Int32   SAL_CALL setBroadcast(sal_Int32 opt = -1) const;
 755
 756    /** Receive out-of-band data in the normal data stream.
 757        @param opt 1 sets, 0 resets, -1 won't change anything
 758        @return the previous setting
 759    */
 760    sal_Int32   SAL_CALL setOobinline(sal_Int32 opt = -1) const;
 761
 762    /** Linger on close if unsent data is present.
 763        @param time values > 0 enable lingering with a timeout of time in seconds.
 764        If time is 0, lingering will be disabled. If time is -1 no changes will occur.
 765        @return the previous setting (0 == off, > 0 timeout-value in seconds).
 766    */
 767    sal_Int32   SAL_CALL setLinger(sal_Int32 time = -1) const;
 768
 769    /** Specify buffer size for sends.
 770        You might want to use getOption() to check if the size changes were
 771        really successful.
 772        @param size Size >= 0 sets the size, -1 won't change anything.
 773        @return the previous setting
 774    */
 775    sal_Int32   SAL_CALL setSendBufSize(sal_Int32 size =-1) const;
 776
 777    /** Specify buffer size for receives.
 778        You might want to use getOption() to check if the size changes were
 779        really successful.
 780        @param size Size >= 0 sets the size, -1 won't change anything.
 781        @return the previous setting
 782    */
 783    sal_Int32   SAL_CALL setRecvBufSize(sal_Int32 size =-1) const;
 784
 785    /** Disables the Nagle algorithm for send coalescing. (Do not
 786        collect data until a packet is full, instead send immediatly.
 787        This increases network traffic but might improve response-times.)
 788        @param opt 1 sets, 0 resets, -1 won't change anything
 789        @return the previous setting
 790    */
 791    sal_Int32   SAL_CALL setTcpNoDelay(sal_Int32 sz =-1) const;
 792
 793    /** Builds a string with the last error-message for the socket.
 794        @param pBuffer is filled with the error message.
 795        @param nSize the size of pBuffer. The message will be cut
 796        sal_Int16 if the buffer isn't large enough, but still remains
 797        a valid zero-terminated string.
 798    */
 799    void SAL_CALL getError(::rtl::OUString& strError) const;
 800
 801    /** Delivers a constant decribing the last error for the socket system.
 802        @return ENONE if no error occured, invalid_SocketError if
 803        an unknown (unmapped) error occured, otherwise an enum describing the
 804        error.
 805    */
 806    TSocketError SAL_CALL getError() const;
 807
 808};
 809
 810/** A socket to send or receive a stream of data.
 811*/
 812class OStreamSocket : public NAMESPACE_VOS(OSocket),
 813                      public NAMESPACE_VOS(IStream)
 814{
 815    VOS_DECLARE_CLASSINFO(NAMESPACE_VOS(OStreamSocket));
 816public:
 817
 818    /** Creates an unattached socket. You must attach the socket to an oslSocket
 819        e.g. by using the operator=(oslSocket), before you can use the stream-
 820        functionality of the object.
 821    */
 822    OStreamSocket();
 823
 824
 825
 826    /** Creates socket as wrapper around the underlying oslSocket.
 827        @param Socket
 828    */
 829    OStreamSocket(oslSocket Socket);
 830
 831
 832    /** Copy constructor.
 833        @param Socket
 834    */
 835    OStreamSocket(const OStreamSocket& Socket);
 836
 837
 838    /**
 839    */
 840    OStreamSocket(const OSocket& Socket);
 841
 842    /** Destructor. Calls shutdown(readwrite) and close().
 843    */
 844    virtual ~OStreamSocket();
 845
 846    /** Closes the socket after calling shutdown.
 847    */
 848    virtual void SAL_CALL close();
 849
 850    /** Attaches the oslSocket to this object. If the object
 851        already was attached to an oslSocket, the old one will
 852        be closed and destroyed.
 853        @param Socket.
 854    */
 855    OStreamSocket& SAL_CALL operator=(oslSocket Socket);
 856
 857    /**
 858    */
 859    OStreamSocket& SAL_CALL operator=(const OSocket& Socket);
 860
 861    /**
 862    */
 863    OStreamSocket& SAL_CALL operator=(const OStreamSocket& Socket);
 864
 865    /** Retrieves n bytes from the stream and copies them into pBuffer.
 866        The method avoids incomplete reads due to packet boundaries.
 867        @param pBuffer receives the read data.
 868        @param n the number of bytes to read. pBuffer must be large enough
 869        to hold the n bytes!
 870        @return the number of read bytes. The number will only be smaller than
 871        n if an exceptional condition (e.g. connection closed) occurs.
 872    */
 873    virtual sal_Int32 SAL_CALL read(void* pBuffer, sal_uInt32 n) const;
 874
 875    /** Writes n bytes from pBuffer to the stream. The method avoids
 876        incomplete writes due to packet boundaries.
 877        @param pBuffer contains the data to be written.
 878        @param n the number of bytes to write.
 879        @return the number of written bytes. The number will only be smaller than
 880        n if an exceptional condition (e.g. connection closed) occurs.
 881    */
 882    virtual sal_Int32 SAL_CALL write(const void* pBuffer, sal_uInt32 n);
 883
 884    /** Checks if socket is closed.
 885        @return True if socket is closed.
 886    */
 887    virtual sal_Bool SAL_CALL isEof() const;
 888
 889    /** Tries to receives BytesToRead data from the connected socket,
 890
 891        @param pBuffer [out] Points to a buffer that will be filled with the received
 892        data.
 893        @param BytesToRead [in] The number of bytes to read. pBuffer must have at least
 894        this size.
 895        @param Flag [in] Modifier for the call. Valid values are:
 896        <ul>
 897        <li> TMsg_Normal
 898        <li> TMsg_OOB
 899        <li> TMsg_Peek
 900        <li> TMsg_DontRoute
 901        <li> TMsg_MaxIOVLen
 902        </ul>
 903
 904        @return the number of received bytes.
 905    */
 906    sal_Int32   SAL_CALL recv(void* pBuffer,
 907                 sal_uInt32 BytesToRead,
 908                 TSocketMsgFlag Flag= TMsg_Normal);
 909
 910
 911    /** Tries to sends BytesToSend data from the connected socket.
 912
 913        @param pBuffer [in] Points to a buffer that contains the send-data.
 914        @param BytesToSend [in] The number of bytes to send. pBuffer must have at least
 915        this size.
 916        @param Flag [in] Modifier for the call. Valid values are:
 917        <ul>
 918        <li> TMsg_Normal
 919        <li> TMsg_OOB
 920        <li> TMsg_Peek
 921        <li> TMsg_DontRoute
 922        <li> TMsg_MaxIOVLen
 923        </ul>
 924
 925        @return the number of transfered bytes.
 926    */
 927    sal_Int32 SAL_CALL send(const void* pBuffer,
 928                 sal_uInt32 BytesToSend,
 929                 TSocketMsgFlag Flag= TMsg_Normal);
 930
 931    /** Closes a connection in a controlled manner.
 932        @param Direction Says which "end" of the socket is to be closed.
 933    */
 934    sal_Bool SAL_CALL shutdown(TSocketDirection Direction= TDirection_ReadWrite);
 935protected:
 936
 937    /** Creates a socket. This constructor is used only by derived classes
 938        (e.g. OConnectorSocket).
 939        @param Family
 940        @param Protocol
 941        @param Type For some protocols it might be desirable to
 942                    use a different type than sock_stream (like sock_seqpacket).
 943                    Therefore we do not hide this parameter here.
 944    */
 945    OStreamSocket(TAddrFamily Family,
 946                  TProtocol   Protocol,
 947                  TSocketType Type= TType_Stream);
 948
 949
 950};
 951
 952
 953/** A socket to accept incoming connections.
 954*/
 955class OAcceptorSocket : public NAMESPACE_VOS(OSocket)
 956{
 957    VOS_DECLARE_CLASSINFO(NAMESPACE_VOS(OAcceptorSocket));
 958public:
 959
 960    /** Creates a socket that can accept connections.
 961        @param Type For some protocols it might be desirable to
 962                    use a different type than sock_stream (like sock_seqpacket).
 963                    Therefore we do not hide this parameter here.
 964    */
 965    OAcceptorSocket(TAddrFamily Family= TFamily_Inet,
 966                    TProtocol   Protocol= TProtocol_Ip,
 967                    TSocketType Type= TType_Stream);
 968
 969    /** Copy constructor.
 970    */
 971    OAcceptorSocket(const OAcceptorSocket& Socket);
 972
 973    /** Destructor. Closes the socket and destroys the underlying oslSocket.
 974    */
 975    virtual ~OAcceptorSocket();
 976
 977    /** Closes the socket. Also calls shutdown, needed to unblock
 978        accept on some systems.
 979    */
 980    virtual void SAL_CALL close();
 981
 982    /** Prepare a socket for the accept-call.
 983        @param MaxPendingConnections The maximum number of pending
 984        connections (waiting to be accepted) on this socket. If you use
 985        -1, a system default value is used.
 986        @return True if call was successful.
 987    */
 988    sal_Bool SAL_CALL listen(sal_Int32 MaxPendingConnections= -1);
 989
 990    /** Accepts incoming connections on the socket. You must
 991        precede this call with bind() and listen().
 992        @param Connection receives the incoming connection.
 993        @return result_ok: if a connection has been accepted,
 994                result_timeout: if m_RecvTimeout milliseconds passed without connect,
 995                result_error: on errors.
 996    */
 997    TResult SAL_CALL acceptConnection(OStreamSocket& Connection);
 998
 999    /** Accepts incoming connections on the socket. You must
1000        precede this call with bind() and listen().
1001        @param PeerAddr receives the address of the connecting entity
1002        (your communication partner).
1003        @param Connection receives the incoming connection.
1004        @return True if a connection has been accepted, False on errors.
1005        @return result_ok: if a connection has been accepted,
1006                result_timeout: if m_RecvTimeout milliseconds passed without connect,
1007                result_error: on errors.
1008    */
1009    TResult SAL_CALL acceptConnection(OStreamSocket&    Connection,
1010                             OSocketAddr& PeerAddr);
1011
1012};
1013
1014
1015/** A socket to initiate a conenction.
1016*/
1017class OConnectorSocket : public NAMESPACE_VOS(OStreamSocket)
1018{
1019    VOS_DECLARE_CLASSINFO(NAMESPACE_VOS(OConnectorSocket));
1020public:
1021
1022    /** Creates a socket that can accept connections.
1023        @param Type For some protocols it might be desirable to
1024                    use a different type than sock_stream (like sock_seqpacket).
1025                    Therefore we do not hide this parameter here.
1026    */
1027    OConnectorSocket(TAddrFamily Family= TFamily_Inet,
1028                     TProtocol   Protocol= TProtocol_Ip,
1029                     TSocketType Type= TType_Stream);
1030
1031    /** Copy constructor. Doesn't duplicate oslSocket.
1032    */
1033    OConnectorSocket(const OConnectorSocket& Socket);
1034
1035    /** Destructor. Relies on ~OStreamSocket to close down connection gracefully.
1036    */
1037    virtual ~OConnectorSocket();
1038
1039    /** Connects the socket to a (remote) host.
1040        @param TargetHost The address of the target.
1041        @param msTimeout The timeout in milliseconds. Use -1 to block.
1042        @return result_ok if connected successfully,
1043                result_timeout on timeout,
1044                result_interrupted if unblocked forcefully (by close()),
1045                result_error if connect failed.
1046    */
1047    TResult SAL_CALL connect(const OSocketAddr& TargetHost, const TimeValue* pTimeout = 0);
1048};
1049
1050
1051/** A connectionless socket to send and receive datagrams.
1052*/
1053class ODatagramSocket : public NAMESPACE_VOS(OSocket)
1054{
1055    VOS_DECLARE_CLASSINFO(NAMESPACE_VOS(ODatagramSocket));
1056public:
1057
1058    /** Creates a datagram socket.
1059        @param Type is sock_dgram by default.
1060    */
1061    ODatagramSocket(TAddrFamily Family= TFamily_Inet,
1062                    TProtocol   Protocol= TProtocol_Ip,
1063                    TSocketType Type= TType_Dgram);
1064
1065    /** Copy constructor.
1066    */
1067    ODatagramSocket(const ODatagramSocket& Socket);
1068
1069    /** Destructor. Closes the socket.
1070    */
1071    virtual ~ODatagramSocket();
1072
1073
1074    /** Tries to receives BufferSize data from the socket, if no error occurs.
1075
1076        @param pSenderAddr [out] You must provide pointer to a SocketAddr.
1077        It will be  filled with the address of the datagrams sender.
1078        If pSenderAddr is 0, it is ignored.
1079        @param pBuffer [out] Points to a buffer that will be filled with the received
1080        datagram.
1081        @param BufferSize [in] The size of pBuffer.
1082        @param Flag [in] Modifier for the call. Valid values are:
1083        <ul>
1084        <li> TMsg_Normal
1085        <li> TMsg_OOB
1086        <li> TMsg_Peek
1087        <li> TMsg_DontRoute
1088        <li> TMsg_MaxIOVLen
1089        </ul>
1090
1091        @return the number of received bytes.
1092    */
1093    sal_Int32   SAL_CALL recvFrom(void*  pBuffer,
1094                     sal_uInt32 BufferSize,
1095                     OSocketAddr* pSenderAddr= 0,
1096                     TSocketMsgFlag Flag= TMsg_Normal);
1097
1098    /** Tries to send one datagram with BytesToSend data to the given ReceiverAddr.
1099        Since we only send one packet, we don't need to concern ourselfes here with
1100        incomplete sends due to packet boundaries.
1101
1102        @param ReceiverAddr [in] A SocketAddr that contains
1103        the destination address for this send.
1104
1105        @param pBuffer [in] Points to a buffer that contains the send-data.
1106        @param BufferSize [in] The number of bytes to send. pBuffer must have at least
1107        this size.
1108        @param Flag [in] Modifier for the call. Valid values are:
1109        <ul>
1110        <li> TMsg_Normal
1111        <li> TMsg_OOB
1112        <li> TMsg_Peek
1113        <li> TMsg_DontRoute
1114        <li> TMsg_MaxIOVLen
1115        </ul>
1116
1117        @return the number of transfered bytes.
1118    */
1119    sal_Int32   SAL_CALL sendTo(const OSocketAddr& ReceiverAddr,
1120                   const void* pBuffer,
1121                   sal_uInt32 BufferSize,
1122                   TSocketMsgFlag Flag= TMsg_Normal);
1123};
1124
1125
1126
1127}
1128
1129#endif // _VOS_SOCKET_HXX_
1130