LibreOffice
LibreOffice 24.2 SDK C/C++ API Reference
socket_decl.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 /*
21  * This file is part of LibreOffice published API.
22  */
23 
24 #ifndef INCLUDED_OSL_SOCKET_DECL_HXX
25 #define INCLUDED_OSL_SOCKET_DECL_HXX
26 
27 #include "sal/config.h"
28 
29 #include <cstddef>
30 
31 #include "osl/socket.h"
32 #include "rtl/ustring.hxx"
33 #include "rtl/byteseq.hxx"
34 
35 namespace osl
36 {
38 
43  class SocketAddr
44  {
45  protected:
47  public:
48 
51  inline SocketAddr();
52 
55  inline SocketAddr(const SocketAddr& Addr);
56 
57 #if defined LIBO_INTERNAL_ONLY
58  inline SocketAddr(SocketAddr && other) noexcept;
59 #endif
60 
67  inline SocketAddr(const oslSocketAddr Addr, __osl_socket_NoCopy nocopy );
68 
73  inline SocketAddr(oslSocketAddr Addr);
74 
81  inline SocketAddr(const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort);
82 
85  inline ~SocketAddr();
86 
92  inline bool is() const;
93 
104  inline ::rtl::OUString SAL_CALL getHostname(oslSocketResult *pResult = NULL) const;
105 
113  inline bool SAL_CALL setHostname(const ::rtl::OUString &sDottedIpOrHostname);
114 
119  inline sal_Int32 SAL_CALL getPort() const;
120 
128  inline bool SAL_CALL setPort(sal_Int32 nPort);
129 
135  inline bool SAL_CALL setAddr(const ::rtl::ByteSequence & address);
136 
139  inline ::rtl::ByteSequence SAL_CALL getAddr(oslSocketResult *pResult = NULL) const;
140 
143  inline SocketAddr & SAL_CALL operator= (oslSocketAddr Addr);
144 
145  inline SocketAddr & SAL_CALL operator= (const SocketAddr& Addr);
146 
147 #if defined LIBO_INTERNAL_ONLY
148  inline SocketAddr & operator =(SocketAddr && other) noexcept;
149 #endif
150 
155  inline SocketAddr & SAL_CALL assign( oslSocketAddr Addr, __osl_socket_NoCopy nocopy );
156 
159  inline bool SAL_CALL operator== (oslSocketAddr Addr) const;
160 
163  inline bool SAL_CALL operator== (const SocketAddr & Addr) const;
164 
167  inline oslSocketAddr SAL_CALL getHandle() const;
168 
174  static inline ::rtl::OUString SAL_CALL getLocalHostname( oslSocketResult *pResult = NULL);
175 
182  static inline void SAL_CALL resolveHostname(
183  const ::rtl::OUString & strHostName , SocketAddr & Addr );
184 
191  static inline sal_Int32 SAL_CALL getServicePort(
192  const ::rtl::OUString& strServiceName,
193  const ::rtl::OUString & strProtocolName= ::rtl::OUString("tcp") );
194  };
195 
196 
197  class Socket
198  {
199  protected:
201  protected:
207  inline Socket(oslSocketType Type,
210  public:
211  inline Socket( );
212 
213  inline Socket( const Socket & socket );
214 
215  inline Socket( oslSocket socketHandle );
216 
222  inline Socket( oslSocket socketHandle, __sal_NoAcquire noacquire );
223 
226  inline ~Socket();
227 
231  inline Socket& SAL_CALL operator= ( oslSocket socketHandle);
232 
236  inline Socket& SAL_CALL operator= (const Socket& sock);
237 
242  inline bool SAL_CALL operator==( const Socket& rSocket ) const ;
243 
248  inline bool SAL_CALL operator==( const oslSocket socketHandle ) const;
249 
255  inline void SAL_CALL shutdown( oslSocketDirection Direction = osl_Socket_DirReadWrite );
256 
262  inline void SAL_CALL close();
263 
268  inline void SAL_CALL getLocalAddr( SocketAddr &Addr ) const;
269 
273  inline sal_Int32 SAL_CALL getLocalPort() const;
274 
278  inline ::rtl::OUString SAL_CALL getLocalHost() const;
279 
283  inline void SAL_CALL getPeerAddr( SocketAddr & Addr) const;
284 
288  inline sal_Int32 SAL_CALL getPeerPort() const;
289 
293  inline ::rtl::OUString SAL_CALL getPeerHost() const;
294 
299  inline bool SAL_CALL bind(const SocketAddr& LocalInterface);
300 
311  inline bool SAL_CALL isRecvReady(const TimeValue *pTimeout = NULL) const;
312 
323  inline bool SAL_CALL isSendReady(const TimeValue *pTimeout = NULL) const;
324 
325 
338  inline bool SAL_CALL isExceptionPending(const TimeValue *pTimeout = NULL) const;
339 
340 
350  inline oslSocketType SAL_CALL getType() const;
351 
433  inline sal_Int32 SAL_CALL getOption(
434  oslSocketOption Option,
435  void* pBuffer,
436  sal_uInt32 BufferLen,
438 
480  inline bool SAL_CALL setOption( oslSocketOption Option,
481  void* pBuffer,
482  sal_uInt32 BufferLen,
484 
488  inline bool setOption( oslSocketOption option, sal_Int32 nValue );
489 
493  inline sal_Int32 getOption( oslSocketOption option ) const;
494 
501  inline bool SAL_CALL enableNonBlockingMode( bool bNonBlockingMode);
502 
506  inline bool SAL_CALL isNonBlockingMode() const;
507 
508 
511  inline void SAL_CALL clearError() const;
512 
520  inline oslSocketError getError() const;
521 
524  inline ::rtl::OUString getErrorAsString( ) const;
525 
528  inline oslSocket getHandle() const;
529  };
530 
531 
532  class StreamSocket : public Socket
533  {
534  public:
546 
547  inline StreamSocket( oslSocket Socket , __sal_NoAcquire noacquire );
548 
549  inline StreamSocket( oslSocket Socket );
550 
560  inline sal_Int32 SAL_CALL read(void* pBuffer, sal_uInt32 n);
561 
569  inline sal_Int32 SAL_CALL write(const void* pBuffer, sal_uInt32 n);
570 
571 
588  inline sal_Int32 SAL_CALL recv(void* pBuffer,
589  sal_uInt32 BytesToRead,
591 
608  sal_Int32 SAL_CALL send(const void* pBuffer,
609  sal_uInt32 BytesToSend,
611  };
612 
614  {
615  public:
627 
628 
638  oslSocketResult SAL_CALL connect(const SocketAddr& TargetHost, const TimeValue* pTimeout = NULL);
639  };
640 
643  class AcceptorSocket : public Socket
644  {
645  public:
649 
657  inline bool SAL_CALL listen(sal_Int32 MaxPendingConnections= -1);
658 
666  inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection);
667 
677  inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection, SocketAddr & PeerAddr);
678  };
679 
680 
683  class DatagramSocket : public Socket
684  {
685  public:
686 
695 
715  inline sal_Int32 SAL_CALL recvFrom(void* pBuffer,
716  sal_uInt32 BufferSize,
717  SocketAddr* pSenderAddr= NULL,
719 
742  inline sal_Int32 SAL_CALL sendTo( const SocketAddr& ReceiverAddr,
743  const void* pBuffer,
744  sal_uInt32 BufferSize,
746  };
747 
748 }
749 
750 #endif
751 
752 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void close()
Closes a socket.
Definition: socket.hxx:273
SocketAddr()
Creates socket address of unknown type.
Definition: socket.hxx:32
inline ::rtl::OUString getPeerHost() const
Get the hostname for the remote interface.
Definition: socket.hxx:315
bool setHostname(const ::rtl::OUString &sDottedIpOrHostname)
Sets the IP address or hostname of the SocketAddress.
Definition: socket.hxx:109
Definition: socket.h:72
bool setOption(oslSocketOption Option, void *pBuffer, sal_uInt32 BufferLen, oslSocketOptionLevel Level=osl_Socket_LevelSocket) const
Sets the sockets attributes.
Definition: socket.hxx:363
bool isSendReady(const TimeValue *pTimeout=NULL) const
Checks if send operations will block.
Definition: socket.hxx:335
oslProtocol
represent a specific protocol within an address-family
Definition: socket.h:59
inline ::rtl::OUString getLocalHost() const
Get the hostname for the local interface.
Definition: socket.hxx:293
oslSocket getHandle() const
Returns the underlying handle unacquired (The caller must acquire it to keep it). ...
Definition: socket.hxx:419
Definition: socket.h:151
oslSocketOption
Represents socket-options.
Definition: socket.h:89
bool enableNonBlockingMode(bool bNonBlockingMode)
Enables/disables non-blocking mode of the socket.
Definition: socket.hxx:386
oslSocketType getType() const
Queries the socket for its type.
Definition: socket.hxx:347
Definition: socket.h:60
bool isRecvReady(const TimeValue *pTimeout=NULL) const
Checks if read operations will block.
Definition: socket.hxx:329
sal_Int32 getPeerPort() const
Get the remote port of the socket.
Definition: socket.hxx:307
oslSocketResult acceptConnection(StreamSocket &Connection)
Accepts incoming connections on the socket.
Definition: socket.hxx:497
inline ::rtl::OUString getHostname(oslSocketResult *pResult=NULL) const
Converts the address to a (human readable) domain-name.
Definition: socket.hxx:88
Definition: condition.hxx:31
oslSocketResult
Common return codes of socket related functions.
Definition: socket.h:197
sal_Int32 write(const void *pBuffer, sal_uInt32 n)
Writes n bytes from pBuffer to the stream.
Definition: socket.hxx:448
Definition: socket_decl.hxx:532
oslAddrFamily
Represents the address-family of a socket.
Definition: socket.h:49
Definition: socket.h:126
bool isExceptionPending(const TimeValue *pTimeout=NULL) const
Checks if a request for out-of-band data will block.
Definition: socket.hxx:341
__osl_socket_NoCopy
Definition: socket_decl.hxx:37
SocketAddr & operator=(oslSocketAddr Addr)
assign the handle to this reference.
Definition: socket.hxx:132
oslSocketOptionLevel
Represents the different socket-option levels.
Definition: socket.h:125
Allows to accept socket connections.
Definition: socket_decl.hxx:643
oslSocketResult connect(const SocketAddr &TargetHost, const TimeValue *pTimeout=NULL)
Connects the socket to a (remote) host.
Definition: socket.hxx:477
DatagramSocket(oslAddrFamily Family=osl_Socket_FamilyInet, oslProtocol Protocol=osl_Socket_ProtocolIp, oslSocketType Type=osl_Socket_TypeDgram)
Creates a datagram socket.
Definition: socket.hxx:534
oslSocketType
Represents the type of a socket.
Definition: socket.h:71
bool setAddr(const ::rtl::ByteSequence &address)
Sets the address of the underlying socket address struct in network byte order.
Definition: socket.hxx:116
AcceptorSocket(oslAddrFamily Family=osl_Socket_FamilyInet, oslProtocol Protocol=osl_Socket_ProtocolIp, oslSocketType Type=osl_Socket_TypeStream)
Definition: socket.hxx:484
bool operator==(const Socket &rSocket) const
Definition: socket.hxx:255
sal_Int32 getLocalPort() const
Get the local port of the socket.
Definition: socket.hxx:285
bool isNonBlockingMode() const
Query blocking mode of the socket.
Definition: socket.hxx:392
__sal_NoAcquire
Definition: types.h:352
Definition: socket_decl.hxx:197
sal_Int32 read(void *pBuffer, sal_uInt32 n)
Retrieves n bytes from the stream and copies them into pBuffer.
Definition: socket.hxx:442
Definition: socket.h:50
StreamSocket(oslAddrFamily Family=osl_Socket_FamilyInet, oslProtocol Protocol=osl_Socket_ProtocolIp, oslSocketType Type=osl_Socket_TypeStream)
Creates a socket.
Definition: socket.hxx:425
oslSocket m_handle
Definition: socket_decl.hxx:200
static inline ::rtl::OUString getLocalHostname(oslSocketResult *pResult=NULL)
Get the hostname for the local interface.
Definition: socket.hxx:184
oslSocketMsgFlag
Represents flags to be used with send/recv-calls.
Definition: socket.h:135
sal_Int32 sendTo(const SocketAddr &ReceiverAddr, const void *pBuffer, sal_uInt32 BufferSize, oslSocketMsgFlag Flag=osl_Socket_MsgNormal)
Tries to send one datagram with BytesToSend size to the given ReceiverAddr.
Definition: socket.hxx:561
inline ::rtl::ByteSequence getAddr(oslSocketResult *pResult=NULL) const
Returns the address of the underlying socket in network byte order.
Definition: socket.hxx:122
static void resolveHostname(const ::rtl::OUString &strHostName, SocketAddr &Addr)
Tries to find an address for a host.
Definition: socket.hxx:193
sal_Int32 recv(void *pBuffer, sal_uInt32 BytesToRead, oslSocketMsgFlag flags=osl_Socket_MsgNormal)
Tries to receive BytesToRead data from the connected socket,.
Definition: socket.hxx:454
Definition: socket_decl.hxx:613
bool listen(sal_Int32 MaxPendingConnections=-1)
Prepare a socket for the accept-call.
Definition: socket.hxx:491
void shutdown(oslSocketDirection Direction=osl_Socket_DirReadWrite)
Closes a definite or both directions of the bidirectional stream.
Definition: socket.hxx:267
sal_Int32 getOption(oslSocketOption Option, void *pBuffer, sal_uInt32 BufferLen, oslSocketOptionLevel Level=osl_Socket_LevelSocket) const
Retrieves option-attributes associated with the socket.
Definition: socket.hxx:353
The class should be understood as a reference to a socket address handle (struct sockaddr).
Definition: socket_decl.hxx:43
This String class provides base functionality for C++ like Unicode character array handling...
Definition: ustring.hxx:170
Socket & operator=(oslSocket socketHandle)
Assignment operator.
Definition: socket.hxx:239
sal_Int32 send(const void *pBuffer, sal_uInt32 BytesToSend, oslSocketMsgFlag=osl_Socket_MsgNormal)
Tries to send BytesToSend data to the connected socket.
Definition: socket.hxx:462
oslSocketError
Describes the various error socket error conditions, which may occur.
Definition: socket.h:158
void getLocalAddr(SocketAddr &Addr) const
Retrieves the address of the local interface of this socket.
Definition: socket.hxx:279
bool is() const
Checks if the SocketAddr was created successful.
Definition: socket.hxx:179
oslSocketAddr m_handle
Definition: socket_decl.hxx:46
oslSocketError getError() const
returns a constant describing the last error for the socket system.
Definition: socket.hxx:405
Time since Jan-01-1970.
Definition: time.h:76
Definition: socket.h:136
inline ::rtl::OUString getErrorAsString() const
Builds a string with the last error-message for the socket.
Definition: socket.hxx:411
sal_Int32 recvFrom(void *pBuffer, sal_uInt32 BufferSize, SocketAddr *pSenderAddr=NULL, oslSocketMsgFlag Flag=osl_Socket_MsgNormal)
Tries to receives BufferSize data from the socket, if no error occurs.
Definition: socket.hxx:541
SocketAddr & assign(oslSocketAddr Addr, __osl_socket_NoCopy nocopy)
Assigns the socket addr without copyconstructing it.
Definition: socket.hxx:159
A connectionless socket to send and receive datagrams.
Definition: socket_decl.hxx:683
ConnectorSocket(oslAddrFamily Family=osl_Socket_FamilyInet, oslProtocol Protocol=osl_Socket_ProtocolIp, oslSocketType Type=osl_Socket_TypeStream)
Creates a socket that can connect to a (remote) host.
Definition: socket.hxx:470
struct oslSocketAddrImpl * oslSocketAddr
Opaque datatype SocketAddr.
Definition: socket.h:44
Definition: socket_decl.hxx:37
sal_Int32 getPort() const
Returns the port number of the address.
Definition: socket.hxx:98
void clearError() const
clears the error status
Definition: socket.hxx:398
oslSocketAddr getHandle() const
Returns the underlying SocketAddr handle without copyconstructing it.
Definition: socket.hxx:173
oslSocketDirection
Used by shutdown to denote which end of the socket to "close".
Definition: socket.h:148
bool operator==(oslSocketAddr Addr) const
Returns true if the underlying handle is identical to the Addr handle.
Definition: socket.hxx:168
Definition: socket.h:73
struct oslSocketImpl * oslSocket
Definition: socket.h:215
~SocketAddr()
destroys underlying oslSocketAddress
Definition: socket.hxx:81
~Socket()
Destructor.
Definition: socket.hxx:233
bool setPort(sal_Int32 nPort)
Sets the port number of the address.
Definition: socket.hxx:104
void getPeerAddr(SocketAddr &Addr) const
Retrieves the address of the remote host of this socket.
Definition: socket.hxx:301
bool bind(const SocketAddr &LocalInterface)
Binds the socket to the specified (local) interface.
Definition: socket.hxx:323
static sal_Int32 getServicePort(const ::rtl::OUString &strServiceName, const ::rtl::OUString &strProtocolName=::rtl::OUString("tcp"))
Tries to find the port associated with the given service/protocol- pair (e.g.
Definition: socket.hxx:199