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