LibreOffice
LibreOffice 4.3 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 #ifndef INCLUDED_OSL_SOCKET_DECL_HXX
21 #define INCLUDED_OSL_SOCKET_DECL_HXX
22 
23 #include <osl/socket.h>
24 #include <rtl/ustring.hxx>
25 #include <rtl/byteseq.hxx>
26 
27 namespace osl
28 {
30 
35  class SocketAddr
36  {
37  protected:
39  public:
40 
43  inline SocketAddr();
44 
47  inline SocketAddr(const SocketAddr& Addr);
48 
54  inline SocketAddr(const oslSocketAddr Addr, __osl_socket_NoCopy nocopy );
55 
58  inline SocketAddr(oslSocketAddr Addr);
59 
65  inline SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort );
66 
69  inline ~SocketAddr();
70 
75  inline bool is() const;
76 
85  inline ::rtl::OUString SAL_CALL getHostname( oslSocketResult *pResult = 0 ) const;
86 
89  inline bool SAL_CALL setHostname( const ::rtl::OUString &sDottedIpOrHostname );
90 
94  inline sal_Int32 SAL_CALL getPort() const;
95 
99  inline bool SAL_CALL setPort( sal_Int32 nPort );
100 
104  inline bool SAL_CALL setAddr( const ::rtl::ByteSequence & address );
105 
108  inline ::rtl::ByteSequence SAL_CALL getAddr( oslSocketResult *pResult = 0 ) const;
109 
112  inline SocketAddr & SAL_CALL operator= (oslSocketAddr Addr);
113 
116  inline SocketAddr & SAL_CALL operator= (const SocketAddr& Addr);
117 
122  inline SocketAddr & SAL_CALL assign( oslSocketAddr Addr, __osl_socket_NoCopy nocopy );
123 
126  inline bool SAL_CALL operator== (oslSocketAddr Addr) const;
127 
130  inline bool SAL_CALL operator== (const SocketAddr & Addr) const;
131 
134  inline oslSocketAddr SAL_CALL getHandle() const;
135 
141  static inline ::rtl::OUString SAL_CALL getLocalHostname( oslSocketResult *pResult = 0);
142 
147  static inline void SAL_CALL resolveHostname(
148  const ::rtl::OUString & strHostName , SocketAddr & Addr );
149 
156  static inline sal_Int32 SAL_CALL getServicePort(
157  const ::rtl::OUString& strServiceName,
158  const ::rtl::OUString & strProtocolName= ::rtl::OUString("tcp") );
159  };
160 
161 
162  class Socket
163  {
164  protected:
166  protected:
172  inline Socket(oslSocketType Type,
175  public:
176  inline Socket( );
177 
178  inline Socket( const Socket & socket );
179 
180  inline Socket( oslSocket socketHandle );
181 
187  inline Socket( oslSocket socketHandle, __sal_NoAcquire noacquire );
188 
191  inline ~Socket();
192 
196  inline Socket& SAL_CALL operator= ( oslSocket socketHandle);
197 
201  inline Socket& SAL_CALL operator= (const Socket& sock);
202 
207  inline bool SAL_CALL operator==( const Socket& rSocket ) const ;
208 
213  inline bool SAL_CALL operator==( const oslSocket socketHandle ) const;
214 
220  inline void SAL_CALL shutdown( oslSocketDirection Direction = osl_Socket_DirReadWrite );
221 
227  inline void SAL_CALL close();
228 
233  inline void SAL_CALL getLocalAddr( SocketAddr &Addr ) const;
234 
238  inline sal_Int32 SAL_CALL getLocalPort() const;
239 
243  inline ::rtl::OUString SAL_CALL getLocalHost() const;
244 
248  inline void SAL_CALL getPeerAddr( SocketAddr & Addr) const;
249 
253  inline sal_Int32 SAL_CALL getPeerPort() const;
254 
258  inline ::rtl::OUString SAL_CALL getPeerHost() const;
259 
264  inline bool SAL_CALL bind(const SocketAddr& LocalInterface);
265 
276  inline bool SAL_CALL isRecvReady(const TimeValue *pTimeout = 0) const;
277 
288  inline bool SAL_CALL isSendReady(const TimeValue *pTimeout = 0) const;
289 
290 
303  inline bool SAL_CALL isExceptionPending(const TimeValue *pTimeout = 0) const;
304 
305 
317  inline oslSocketType SAL_CALL getType() const;
318 
400  inline sal_Int32 SAL_CALL getOption(
401  oslSocketOption Option,
402  void* pBuffer,
403  sal_uInt32 BufferLen,
405 
447  inline bool SAL_CALL setOption( oslSocketOption Option,
448  void* pBuffer,
449  sal_uInt32 BufferLen,
451 
455  inline bool setOption( oslSocketOption option, sal_Int32 nValue );
456 
460  inline sal_Int32 getOption( oslSocketOption option ) const;
461 
468  inline bool SAL_CALL enableNonBlockingMode( bool bNonBlockingMode);
469 
473  inline bool SAL_CALL isNonBlockingMode() const;
474 
475 
478  inline void SAL_CALL clearError() const;
479 
487  inline oslSocketError getError() const;
488 
491  inline ::rtl::OUString getErrorAsString( ) const;
492 
495  inline oslSocket getHandle() const;
496  };
497 
498 
499  class StreamSocket : public Socket
500  {
501  public:
513 
514  inline StreamSocket( const StreamSocket & );
515 
516  inline StreamSocket( oslSocket Socket , __sal_NoAcquire noacquire );
517 
518  inline StreamSocket( oslSocket Socket );
519 
529  inline sal_Int32 SAL_CALL read(void* pBuffer, sal_uInt32 n);
530 
538  inline sal_Int32 SAL_CALL write(const void* pBuffer, sal_uInt32 n);
539 
540 
557  inline sal_Int32 SAL_CALL recv(void* pBuffer,
558  sal_uInt32 BytesToRead,
560 
577  sal_Int32 SAL_CALL send(const void* pBuffer,
578  sal_uInt32 BytesToSend,
580  };
581 
583  {
584  public:
596 
597 
607  oslSocketResult SAL_CALL connect(const SocketAddr& TargetHost, const TimeValue* pTimeout = 0);
608  };
609 
612  class AcceptorSocket : public Socket
613  {
614  public:
618 
626  inline bool SAL_CALL listen(sal_Int32 MaxPendingConnections= -1);
627 
635  inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection);
636 
646  inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection, SocketAddr & PeerAddr);
647  };
648 
649 
650 
653  class DatagramSocket : public Socket
654  {
655  public:
656 
665 
685  inline sal_Int32 SAL_CALL recvFrom(void* pBuffer,
686  sal_uInt32 BufferSize,
687  SocketAddr* pSenderAddr= 0,
689 
712  inline sal_Int32 SAL_CALL sendTo( const SocketAddr& ReceiverAddr,
713  const void* pBuffer,
714  sal_uInt32 BufferSize,
716  };
717 
718 }
719 
720 #endif
721 
722 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AcceptorSocket(oslAddrFamily Family=osl_Socket_FamilyInet, oslProtocol Protocol=osl_Socket_ProtocolIp, oslSocketType Type=osl_Socket_TypeStream)
Definition: socket.hxx:471
Definition: socket.h:71
struct oslSocketAddrImpl * oslSocketAddr
Opaque datatype SocketAddr.
Definition: socket.h:41
bool bind(const SocketAddr &LocalInterface)
Binds the socket to the specified (local) interface.
Definition: socket.hxx:304
Definition: socket_decl.hxx:499
oslSocket m_handle
Definition: socket_decl.hxx:165
Definition: socket.h:137
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:441
bool listen(sal_Int32 MaxPendingConnections=-1)
Prepare a socket for the accept-call.
Definition: socket.hxx:478
static inline::rtl::OUString getLocalHostname(oslSocketResult *pResult=0)
Get the hostname for the local interface.
Definition: socket.hxx:164
bool isExceptionPending(const TimeValue *pTimeout=0) const
Checks if a request for out-of-band data will block.
Definition: socket.hxx:322
sal_Int32 getPeerPort() const
Get the remote port of the socket.
Definition: socket.hxx:288
inline::rtl::OUString getLocalHost() const
Get the hostname for the local interface.
Definition: socket.hxx:274
Definition: socket.h:58
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:334
inline::rtl::OUString getHostname(oslSocketResult *pResult=0) const
Converts the address to a (human readable) domain-name.
Definition: socket.hxx:78
bool isRecvReady(const TimeValue *pTimeout=0) const
Checks if read operations will block.
Definition: socket.hxx:310
SocketAddr & operator=(oslSocketAddr Addr)
assign the handle to this reference.
Definition: socket.hxx:122
bool operator==(const Socket &rSocket) const
Definition: socket.hxx:236
__osl_socket_NoCopy
Definition: socket_decl.hxx:29
oslSocketType
Represents the type of a socket.
Definition: socket.h:70
void getLocalAddr(SocketAddr &Addr) const
Retrieves the address of the local interface of this socket.
Definition: socket.hxx:260
bool is() const
checks, if the SocketAddr was created successful.
Definition: socket.hxx:158
oslSocketDirection
Used by shutdown to denote which end of the socket to "close".
Definition: socket.h:134
bool setHostname(const ::rtl::OUString &sDottedIpOrHostname)
Sets the ipaddress or hostname of the SocketAddress.
Definition: socket.hxx:99
inline::rtl::OUString getErrorAsString() const
Builds a string with the last error-message for the socket.
Definition: socket.hxx:392
sal_Int32 write(const void *pBuffer, sal_uInt32 n)
Writes n bytes from pBuffer to the stream.
Definition: socket.hxx:434
SocketAddr()
Creates socket address of unknown type.
Definition: socket.hxx:27
struct oslSocketImpl * oslSocket
Definition: socket.h:403
oslSocketResult connect(const SocketAddr &TargetHost, const TimeValue *pTimeout=0)
Connects the socket to a (remote) host.
Definition: socket.hxx:464
Definition: socket.h:111
oslSocketMsgFlag
Represents flags to be used with send/recv-calls.
Definition: socket.h:121
bool isSendReady(const TimeValue *pTimeout=0) const
Checks if send operations will block.
Definition: socket.hxx:316
__sal_NoAcquire
Definition: types.h:384
bool operator==(oslSocketAddr Addr) const
Returns true if the underlying handle is identical to the Addr handle.
Definition: socket.hxx:147
sal_Int32 getLocalPort() const
Get the local port of the socket.
Definition: socket.hxx:266
oslSocketError
Describes the various error socket error conditions, which may occur.
Definition: socket.h:144
SocketAddr & assign(oslSocketAddr Addr, __osl_socket_NoCopy nocopy)
Assigns the socket addr without copyconstructing it.
Definition: socket.hxx:138
Definition: time.h:42
The class should be understood as a reference to a socket address handle ( struct sockaddr )...
Definition: socket_decl.hxx:35
bool setAddr(const ::rtl::ByteSequence &address)
Sets the address of the underlying socket address struct in network byte order.
Definition: socket.hxx:106
sal_Int32 recvFrom(void *pBuffer, sal_uInt32 BufferSize, SocketAddr *pSenderAddr=0, oslSocketMsgFlag Flag=osl_Socket_MsgNormal)
Tries to receives BufferSize data from the socket, if no error occurs.
Definition: socket.hxx:527
void close()
Closes a socket.
Definition: socket.hxx:254
static void resolveHostname(const ::rtl::OUString &strHostName, SocketAddr &Addr)
Tries to find an address for a host.
Definition: socket.hxx:174
oslSocketOption
Represents socket-options.
Definition: socket.h:84
oslSocket getHandle() const
Returns the underlying handle unacquired (The caller must acquire it to keep it). ...
Definition: socket.hxx:400
~Socket()
Destructor.
Definition: socket.hxx:215
Socket & operator=(oslSocket socketHandle)
Assignment operator.
Definition: socket.hxx:221
Definition: conditn.hxx:30
oslSocketAddr getHandle() const
Returns the underlying SocketAddr handle without copyconstructing it.
Definition: socket.hxx:152
oslSocketResult acceptConnection(StreamSocket &Connection)
Accepts incoming connections on the socket.
Definition: socket.hxx:484
oslAddrFamily
Represents the address-family of a socket.
Definition: socket.h:47
oslSocketOptionLevel
Represents the different socket-option levels.
Definition: socket.h:110
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:449
Definition: socket.h:48
DatagramSocket(oslAddrFamily Family=osl_Socket_FamilyInet, oslProtocol Protocol=osl_Socket_ProtocolIp, oslSocketType Type=osl_Socket_TypeDgram)
Creates a datagram socket.
Definition: socket.hxx:520
sal_Int32 getPort() const
Returns the port number of the address.
Definition: socket.hxx:88
bool enableNonBlockingMode(bool bNonBlockingMode)
Enables/disables non-blocking mode of the socket.
Definition: socket.hxx:367
void clearError() const
clears the error status
Definition: socket.hxx:379
oslSocketAddr m_handle
Definition: socket_decl.hxx:38
void shutdown(oslSocketDirection Direction=osl_Socket_DirReadWrite)
Closes a definite or both directions of the bidirectional stream.
Definition: socket.hxx:248
bool setOption(oslSocketOption Option, void *pBuffer, sal_uInt32 BufferLen, oslSocketOptionLevel Level=osl_Socket_LevelSocket) const
Sets the sockets attributes.
Definition: socket.hxx:344
sal_Int32 read(void *pBuffer, sal_uInt32 n)
Retrieves n bytes from the stream and copies them into pBuffer.
Definition: socket.hxx:428
oslProtocol
represent a specific protocol within a address-family
Definition: socket.h:57
Definition: socket_decl.hxx:162
Definition: socket.h:122
A connectionless socket to send and receive datagrams.
Definition: socket_decl.hxx:653
Allows to accept socket connections.
Definition: socket_decl.hxx:612
StreamSocket(oslAddrFamily Family=osl_Socket_FamilyInet, oslProtocol Protocol=osl_Socket_ProtocolIp, oslSocketType Type=osl_Socket_TypeStream)
Creates a socket.
Definition: socket.hxx:406
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:181
bool setPort(sal_Int32 nPort)
Sets the port number of the address.
Definition: socket.hxx:94
oslSocketError getError() const
returns a constant describing the last error for the socket system.
Definition: socket.hxx:386
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:457
bool isNonBlockingMode() const
Query blocking mode of the socket.
Definition: socket.hxx:373
Definition: socket_decl.hxx:582
oslSocketResult
Common return codes of socket related functions.
Definition: socket.h:183
Definition: socket.h:72
This String class provides base functionality for C++ like Unicode character array handling...
Definition: ustring.hxx:82
oslSocketType getType() const
Queries the socket for its type.
Definition: socket.hxx:328
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:549
inline::rtl::OUString getPeerHost() const
Get the hostname for the remote interface.
Definition: socket.hxx:296
void getPeerAddr(SocketAddr &Addr) const
Retrieves the address of the remote host of this socket.
Definition: socket.hxx:282
~SocketAddr()
destroys underlying oslSocketAddress
Definition: socket.hxx:71
inline::rtl::ByteSequence getAddr(oslSocketResult *pResult=0) const
Returns the address of the underlying socket in network byte order.
Definition: socket.hxx:112
Definition: socket_decl.hxx:29