PageRenderTime 106ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/Sockets-2.3.3/Socket.h

https://bitbucket.org/cugraphics/flight
C Header | 746 lines | 346 code | 90 blank | 310 comment | 0 complexity | 76e1aed03e7666246cca446f152e602d MD5 | raw file
Possible License(s): GPL-2.0
  1. /** \file Socket.h
  2. ** \date 2004-02-13
  3. ** \author grymse@alhem.net
  4. **/
  5. /*
  6. Copyright (C) 2004-2008 Anders Hedstrom
  7. This library is made available under the terms of the GNU GPL.
  8. If you would like to use this library in a closed-source application,
  9. a separate license agreement is available. For information about
  10. the closed-source license agreement for the C++ sockets library,
  11. please visit http://www.alhem.net/Sockets/license.html and/or
  12. email license@alhem.net.
  13. This program is free software; you can redistribute it and/or
  14. modify it under the terms of the GNU General Public License
  15. as published by the Free Software Foundation; either version 2
  16. of the License, or (at your option) any later version.
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. */
  25. #ifndef _SOCKETS_Socket_H
  26. #define _SOCKETS_Socket_H
  27. #include "sockets-config.h"
  28. #include <string>
  29. #include <vector>
  30. #include <list>
  31. #ifdef HAVE_OPENSSL
  32. #include <openssl/ssl.h>
  33. #endif
  34. #include "socket_include.h"
  35. #include <time.h>
  36. #include "SocketAddress.h"
  37. #include "Thread.h"
  38. #ifdef SOCKETS_NAMESPACE
  39. namespace SOCKETS_NAMESPACE {
  40. #endif
  41. class ISocketHandler;
  42. class SocketAddress;
  43. class IFile;
  44. /** \defgroup basic Basic sockets */
  45. /** Socket base class.
  46. \ingroup basic */
  47. class Socket
  48. {
  49. // friend class ISocketHandler;
  50. #ifdef ENABLE_DETACH
  51. /** Detached socket run thread.
  52. \ingroup internal */
  53. class SocketThread : public Thread
  54. {
  55. public:
  56. SocketThread(Socket *p);
  57. ~SocketThread();
  58. void Run();
  59. private:
  60. Socket *GetSocket() const { return m_socket; }
  61. SocketThread(const SocketThread& s) : m_socket(s.GetSocket()) {}
  62. SocketThread& operator=(const SocketThread& ) { return *this; }
  63. Socket *m_socket;
  64. };
  65. #endif // ENABLE_DETACH
  66. #ifdef ENABLE_TRIGGERS
  67. public:
  68. /** Data pass class from source to destination. */
  69. class TriggerData
  70. {
  71. public:
  72. TriggerData() : m_src(NULL) {}
  73. virtual ~TriggerData() {}
  74. Socket *GetSource() const { return m_src; }
  75. void SetSource(Socket *x) { m_src = x; }
  76. private:
  77. Socket *m_src;
  78. };
  79. #endif // ENABLE_TRIGGERS
  80. /** Socket mode flags. */
  81. /*
  82. enum {
  83. // Socket
  84. SOCK_DEL = 0x01, ///< Delete by handler flag
  85. SOCK_CLOSE = 0x02, ///< Close and delete flag
  86. SOCK_DISABLE_READ = 0x04, ///< Disable checking for read events
  87. SOCK_CONNECTED = 0x08, ///< Socket is connected (tcp/udp)
  88. SOCK_ERASED_BY_HANDLER = 0x10, ///< Set by handler before delete
  89. // HAVE_OPENSSL
  90. SOCK_ENABLE_SSL = 0x20, ///< Enable SSL for this TcpSocket
  91. SOCK_SSL = 0x40, ///< ssl negotiation mode (TcpSocket)
  92. SOCK_SSL_SERVER = 0x80, ///< True if this is an incoming ssl TcpSocket connection
  93. // ENABLE_IPV6
  94. SOCK_IPV6 = 0x0100, ///< This is an ipv6 socket if this one is true
  95. // ENABLE_POOL
  96. SOCK_CLIENT = 0x0200, ///< only client connections are pooled
  97. SOCK_RETAIN = 0x0400, ///< keep connection on close
  98. SOCK_LOST = 0x0800, ///< connection lost
  99. // ENABLE_SOCKS4
  100. SOCK_SOCKS4 = 0x1000, ///< socks4 negotiation mode (TcpSocket)
  101. // ENABLE_DETACH
  102. SOCK_DETACH = 0x2000, ///< Socket ordered to detach flag
  103. SOCK_DETACHED = 0x4000, ///< Socket has been detached
  104. // StreamSocket
  105. STREAMSOCK_CONNECTING = 0x8000, ///< Flag indicating connection in progress
  106. STREAMSOCK_FLUSH_BEFORE_CLOSE = 0x010000L, ///< Send all data before closing (default true)
  107. STREAMSOCK_CALL_ON_CONNECT = 0x020000L, ///< OnConnect will be called next ISocketHandler cycle if true
  108. STREAMSOCK_RETRY_CONNECT = 0x040000L, ///< Try another connection attempt next ISocketHandler cycle
  109. STREAMSOCK_LINE_PROTOCOL = 0x080000L, ///< Line protocol mode flag
  110. };
  111. */
  112. public:
  113. /** "Default" constructor */
  114. Socket(ISocketHandler&);
  115. virtual ~Socket();
  116. /** Socket class instantiation method. Used when a "non-standard" constructor
  117. * needs to be used for the socket class. Note: the socket class still needs
  118. * the "default" constructor with one ISocketHandler& as input parameter.
  119. */
  120. virtual Socket *Create() { return NULL; }
  121. /** Returns reference to sockethandler that owns the socket.
  122. If the socket is detached, this is a reference to the slave sockethandler.
  123. */
  124. ISocketHandler& Handler() const;
  125. /** Returns reference to sockethandler that owns the socket.
  126. This one always returns the reference to the original sockethandler,
  127. even if the socket is detached.
  128. */
  129. ISocketHandler& MasterHandler() const;
  130. /** Called by ListenSocket after accept but before socket is added to handler.
  131. * CTcpSocket uses this to create its ICrypt member variable.
  132. * The ICrypt member variable is created by a virtual method, therefore
  133. * it can't be called directly from the CTcpSocket constructor.
  134. * Also used to determine if incoming HTTP connection is normal (port 80)
  135. * or ssl (port 443).
  136. */
  137. virtual void Init();
  138. /** Create a socket file descriptor.
  139. \param af Address family AF_INET / AF_INET6 / ...
  140. \param type SOCK_STREAM / SOCK_DGRAM / ...
  141. \param protocol "tcp" / "udp" / ... */
  142. SOCKET CreateSocket(int af,int type,const std::string& protocol = "");
  143. /** Assign this socket a file descriptor created
  144. by a call to socket() or otherwise. */
  145. void Attach(SOCKET s);
  146. /** Return file descriptor assigned to this socket. */
  147. SOCKET GetSocket();
  148. /** Close connection immediately - internal use.
  149. \sa SetCloseAndDelete */
  150. virtual int Close();
  151. /** Add file descriptor to sockethandler fd_set's. */
  152. void Set(bool bRead,bool bWrite,bool bException = true);
  153. /** Returns true when socket file descriptor is valid
  154. and socket is not about to be closed. */
  155. virtual bool Ready();
  156. /** Returns pointer to ListenSocket that created this instance
  157. * on an incoming connection. */
  158. Socket *GetParent();
  159. /** Used by ListenSocket to set parent pointer of newly created
  160. * socket instance. */
  161. void SetParent(Socket *);
  162. /** Get listening port from ListenSocket<>. */
  163. virtual port_t GetPort();
  164. /** Set socket non-block operation. */
  165. bool SetNonblocking(bool);
  166. /** Set socket non-block operation. */
  167. bool SetNonblocking(bool, SOCKET);
  168. /** Total lifetime of instance. */
  169. time_t Uptime();
  170. /** Set address/port of last connect() call. */
  171. void SetClientRemoteAddress(SocketAddress&);
  172. /** Get address/port of last connect() call. */
  173. std::auto_ptr<SocketAddress> GetClientRemoteAddress();
  174. /** Common interface for SendBuf used by Tcp and Udp sockets. */
  175. virtual void SendBuf(const char *,size_t,int = 0);
  176. /** Common interface for Send used by Tcp and Udp sockets. */
  177. virtual void Send(const std::string&,int = 0);
  178. /** Outgoing traffic counter. */
  179. virtual uint64_t GetBytesSent(bool clear = false);
  180. /** Incoming traffic counter. */
  181. virtual uint64_t GetBytesReceived(bool clear = false);
  182. // LIST_TIMEOUT
  183. /** Enable timeout control. 0=disable timeout check. */
  184. void SetTimeout(time_t secs);
  185. /** Check timeout. \return true if time limit reached */
  186. bool Timeout(time_t tnow);
  187. /** Used by ListenSocket. ipv4 and ipv6 */
  188. void SetRemoteAddress(SocketAddress&);
  189. /** \name Event callbacks */
  190. //@{
  191. /** Called when there is something to be read from the file descriptor. */
  192. virtual void OnRead();
  193. /** Called when there is room for another write on the file descriptor. */
  194. virtual void OnWrite();
  195. /** Called on socket exception. */
  196. virtual void OnException();
  197. /** Called before a socket class is deleted by the ISocketHandler. */
  198. virtual void OnDelete();
  199. /** Called when a connection has completed. */
  200. virtual void OnConnect();
  201. /** Called when an incoming connection has been completed. */
  202. virtual void OnAccept();
  203. /** Called when a complete line has been read and the socket is in
  204. * line protocol mode. */
  205. virtual void OnLine(const std::string& );
  206. /** Called on connect timeout (5s). */
  207. virtual void OnConnectFailed();
  208. /** Called when a client socket is created, to set socket options.
  209. \param family AF_INET, AF_INET6, etc
  210. \param type SOCK_STREAM, SOCK_DGRAM, etc
  211. \param protocol Protocol number (tcp, udp, sctp, etc)
  212. \param s Socket file descriptor
  213. */
  214. virtual void OnOptions(int family,int type,int protocol,SOCKET s) = 0;
  215. /** Connection retry callback - return false to abort connection attempts */
  216. virtual bool OnConnectRetry();
  217. #ifdef ENABLE_RECONNECT
  218. /** a reconnect has been made */
  219. virtual void OnReconnect();
  220. #endif
  221. /** TcpSocket: When a disconnect has been detected (recv/SSL_read returns 0 bytes). */
  222. virtual void OnDisconnect();
  223. /** TcpSocket: When a disconnect has been detected (recv/SSL_read returns 0 bytes).
  224. \param info bit 0 read(0)/write(1)
  225. bit 1 normal(read or write returned 0)/error(r/w returned -1)
  226. bit 2 ssl
  227. \param code error code from read/write call (errno / ssl error)
  228. */
  229. virtual void OnDisconnect(short info, int code);
  230. /** Timeout callback. */
  231. virtual void OnTimeout();
  232. /** Connection timeout. */
  233. virtual void OnConnectTimeout();
  234. //@}
  235. /** \name Socket mode flags, set/reset */
  236. //@{
  237. /** Set delete by handler true when you want the sockethandler to
  238. delete the socket instance after use. */
  239. void SetDeleteByHandler(bool = true);
  240. /** Check delete by handler flag.
  241. \return true if this instance should be deleted by the sockethandler */
  242. bool DeleteByHandler();
  243. // LIST_CLOSE - conditional event queue
  244. /** Set close and delete to terminate the connection. */
  245. void SetCloseAndDelete(bool = true);
  246. /** Check close and delete flag.
  247. \return true if this socket should be closed and the instance removed */
  248. bool CloseAndDelete();
  249. /** Return number of seconds since socket was ordered to close. \sa SetCloseAndDelete */
  250. time_t TimeSinceClose();
  251. /** Ignore read events for an output only socket. */
  252. void DisableRead(bool x = true);
  253. /** Check ignore read events flag.
  254. \return true if read events should be ignored */
  255. bool IsDisableRead();
  256. /** Set connected status. */
  257. void SetConnected(bool = true);
  258. /** Check connected status.
  259. \return true if connected */
  260. bool IsConnected();
  261. /** Connection lost - error while reading/writing from a socket - TcpSocket only. */
  262. void SetLost();
  263. /** Check connection lost status flag, used by TcpSocket only.
  264. \return true if there was an error while r/w causing the socket to close */
  265. bool Lost();
  266. /** Set flag indicating the socket is being actively deleted by the sockethandler. */
  267. void SetErasedByHandler(bool x = true);
  268. /** Get value of flag indicating socket is deleted by sockethandler. */
  269. bool ErasedByHandler();
  270. //@}
  271. /** \name Information about remote connection */
  272. //@{
  273. /** Returns address of remote end. */
  274. std::auto_ptr<SocketAddress> GetRemoteSocketAddress();
  275. /** Returns address of remote end: ipv4. */
  276. ipaddr_t GetRemoteIP4();
  277. #ifdef ENABLE_IPV6
  278. /** Returns address of remote end: ipv6. */
  279. #ifdef IPPROTO_IPV6
  280. struct in6_addr GetRemoteIP6();
  281. #endif
  282. #endif
  283. /** Returns remote port number: ipv4 and ipv6. */
  284. port_t GetRemotePort();
  285. /** Returns remote ip as string? ipv4 and ipv6. */
  286. std::string GetRemoteAddress();
  287. /** ipv4 and ipv6(not implemented) */
  288. std::string GetRemoteHostname();
  289. //@}
  290. /** Returns local port number for bound socket file descriptor. */
  291. port_t GetSockPort();
  292. /** Returns local ipv4 address for bound socket file descriptor. */
  293. ipaddr_t GetSockIP4();
  294. /** Returns local ipv4 address as text for bound socket file descriptor. */
  295. std::string GetSockAddress();
  296. #ifdef ENABLE_IPV6
  297. #ifdef IPPROTO_IPV6
  298. /** Returns local ipv6 address for bound socket file descriptor. */
  299. struct in6_addr GetSockIP6();
  300. /** Returns local ipv6 address as text for bound socket file descriptor. */
  301. std::string GetSockAddress6();
  302. #endif
  303. #endif
  304. // --------------------------------------------------------------------------
  305. /** @name IP options
  306. When an ip or socket option is available on all of the operating systems
  307. I'm testing on (linux 2.4.x, _win32, macosx, solaris9 intel) they are not
  308. checked with an #ifdef below.
  309. This might cause a compile error on other operating systems. */
  310. // --------------------------------------------------------------------------
  311. // IP options
  312. //@{
  313. bool SetIpOptions(const void *p, socklen_t len);
  314. bool SetIpTOS(unsigned char tos);
  315. unsigned char IpTOS();
  316. bool SetIpTTL(int ttl);
  317. int IpTTL();
  318. bool SetIpHdrincl(bool x = true);
  319. bool SetIpMulticastTTL(int);
  320. int IpMulticastTTL();
  321. bool SetMulticastLoop(bool x = true);
  322. bool IpAddMembership(struct ip_mreq&);
  323. bool IpDropMembership(struct ip_mreq&);
  324. #ifdef IP_PKTINFO
  325. bool SetIpPktinfo(bool x = true);
  326. #endif
  327. #ifdef IP_RECVTOS
  328. bool SetIpRecvTOS(bool x = true);
  329. #endif
  330. #ifdef IP_RECVTTL
  331. bool SetIpRecvTTL(bool x = true);
  332. #endif
  333. #ifdef IP_RECVOPTS
  334. bool SetIpRecvopts(bool x = true);
  335. #endif
  336. #ifdef IP_RETOPTS
  337. bool SetIpRetopts(bool x = true);
  338. #endif
  339. #ifdef IP_RECVERR
  340. bool SetIpRecverr(bool x = true);
  341. #endif
  342. #ifdef IP_MTU_DISCOVER
  343. bool SetIpMtudiscover(bool x = true);
  344. #endif
  345. #ifdef IP_MTU
  346. int IpMtu();
  347. #endif
  348. #ifdef IP_ROUTER_ALERT
  349. bool SetIpRouterAlert(bool x = true);
  350. #endif
  351. #ifdef LINUX
  352. bool IpAddMembership(struct ip_mreqn&);
  353. #endif
  354. #ifdef LINUX
  355. bool IpDropMembership(struct ip_mreqn&);
  356. #endif
  357. //@}
  358. // SOCKET options
  359. /** @name Socket Options */
  360. //@{
  361. bool SoAcceptconn();
  362. bool SetSoBroadcast(bool x = true);
  363. bool SetSoDebug(bool x = true);
  364. int SoError();
  365. bool SetSoDontroute(bool x = true);
  366. bool SetSoLinger(int onoff, int linger);
  367. bool SetSoOobinline(bool x = true);
  368. bool SetSoRcvlowat(int);
  369. bool SetSoSndlowat(int);
  370. bool SetSoRcvtimeo(struct timeval&);
  371. bool SetSoSndtimeo(struct timeval&);
  372. bool SetSoRcvbuf(int);
  373. int SoRcvbuf();
  374. bool SetSoSndbuf(int);
  375. int SoSndbuf();
  376. int SoType();
  377. bool SetSoReuseaddr(bool x = true);
  378. bool SetSoKeepalive(bool x = true);
  379. #ifdef SO_BSDCOMPAT
  380. bool SetSoBsdcompat(bool x = true);
  381. #endif
  382. #ifdef SO_BINDTODEVICE
  383. bool SetSoBindtodevice(const std::string& intf);
  384. #endif
  385. #ifdef SO_PASSCRED
  386. bool SetSoPasscred(bool x = true);
  387. #endif
  388. #ifdef SO_PEERCRED
  389. bool SoPeercred(struct ucred& );
  390. #endif
  391. #ifdef SO_PRIORITY
  392. bool SetSoPriority(int);
  393. #endif
  394. #ifdef SO_RCVBUFFORCE
  395. bool SetSoRcvbufforce(int);
  396. #endif
  397. #ifdef SO_SNDBUFFORCE
  398. bool SetSoSndbufforce(int);
  399. #endif
  400. #ifdef SO_TIMESTAMP
  401. bool SetSoTimestamp(bool x = true);
  402. #endif
  403. #ifdef SO_NOSIGPIPE
  404. bool SetSoNosigpipe(bool x = true);
  405. #endif
  406. //@}
  407. // TCP options in TcpSocket.h/TcpSocket.cpp
  408. #ifdef HAVE_OPENSSL
  409. /** @name SSL Support */
  410. //@{
  411. /** SSL client/server support - internal use. \sa TcpSocket */
  412. virtual void OnSSLConnect();
  413. /** SSL client/server support - internal use. \sa TcpSocket */
  414. virtual void OnSSLAccept();
  415. /** SSL negotiation failed for client connect. */
  416. virtual void OnSSLConnectFailed();
  417. /** SSL negotiation failed for server accept. */
  418. virtual void OnSSLAcceptFailed();
  419. /** new SSL support */
  420. virtual bool SSLNegotiate();
  421. /** Check if SSL is Enabled for this TcpSocket.
  422. \return true if this is a TcpSocket with SSL enabled */
  423. bool IsSSL();
  424. /** Enable SSL operation for a TcpSocket. */
  425. void EnableSSL(bool x = true);
  426. /** Still negotiating ssl connection.
  427. \return true if ssl negotiating is still in progress */
  428. bool IsSSLNegotiate();
  429. /** Set flag indicating ssl handshaking still in progress. */
  430. void SetSSLNegotiate(bool x = true);
  431. /** OnAccept called with SSL Enabled.
  432. \return true if this is a TcpSocket with an incoming SSL connection */
  433. bool IsSSLServer();
  434. /** Set flag indicating that this is a TcpSocket with incoming SSL connection. */
  435. void SetSSLServer(bool x = true);
  436. /** SSL; Get pointer to ssl context structure. */
  437. virtual SSL_CTX *GetSslContext() { return NULL; }
  438. /** SSL; Get pointer to ssl structure. */
  439. virtual SSL *GetSsl() { return NULL; }
  440. //@}
  441. #endif // HAVE_OPENSSL
  442. #ifdef ENABLE_IPV6
  443. /** Enable ipv6 for this socket. */
  444. void SetIpv6(bool x = true);
  445. /** Check ipv6 socket.
  446. \return true if this is an ipv6 socket */
  447. bool IsIpv6();
  448. #endif
  449. #ifdef ENABLE_POOL
  450. /** @name Connection Pool */
  451. //@{
  452. /** Client = connecting TcpSocket. */
  453. void SetIsClient();
  454. /** Socket type from socket() call. */
  455. void SetSocketType(int x);
  456. /** Socket type from socket() call. */
  457. int GetSocketType();
  458. /** Protocol type from socket() call. */
  459. void SetSocketProtocol(const std::string& x);
  460. /** Protocol type from socket() call. */
  461. const std::string& GetSocketProtocol();
  462. /** Instruct a client socket to stay open in the connection pool after use.
  463. If you have connected to a server using tcp, you can call SetRetain
  464. to leave the connection open after your socket instance has been deleted.
  465. The next connection you make to the same server will reuse the already
  466. opened connection, if it is still available.
  467. */
  468. void SetRetain();
  469. /** Check retain flag.
  470. \return true if the socket should be moved to connection pool after use */
  471. bool Retain();
  472. /** Copy connection parameters from sock. */
  473. void CopyConnection(Socket *sock);
  474. //@}
  475. #endif // ENABLE_POOL
  476. #ifdef ENABLE_SOCKS4
  477. /** \name Socks4 support */
  478. //@{
  479. /** Socks4 client support internal use. \sa TcpSocket */
  480. virtual void OnSocks4Connect();
  481. /** Socks4 client support internal use. \sa TcpSocket */
  482. virtual void OnSocks4ConnectFailed();
  483. /** Socks4 client support internal use. \sa TcpSocket */
  484. virtual bool OnSocks4Read();
  485. /** Called when the last write caused the tcp output buffer to
  486. * become empty. */
  487. /** socket still in socks4 negotiation mode */
  488. bool Socks4();
  489. /** Set flag indicating Socks4 handshaking in progress */
  490. void SetSocks4(bool x = true);
  491. /** Set socks4 server host address to use */
  492. void SetSocks4Host(ipaddr_t a);
  493. /** Set socks4 server hostname to use. */
  494. void SetSocks4Host(const std::string& );
  495. /** Socks4 server port to use. */
  496. void SetSocks4Port(port_t p);
  497. /** Provide a socks4 userid if required by the socks4 server. */
  498. void SetSocks4Userid(const std::string& x);
  499. /** Get the ip address of socks4 server to use.
  500. \return socks4 server host address */
  501. ipaddr_t GetSocks4Host();
  502. /** Get the socks4 server port to use.
  503. \return socks4 server port */
  504. port_t GetSocks4Port();
  505. /** Get socks4 userid.
  506. \return Socks4 userid */
  507. const std::string& GetSocks4Userid();
  508. //@}
  509. #endif // ENABLE_SOCKS4
  510. #ifdef ENABLE_RESOLVER
  511. /** \name Asynchronous Resolver */
  512. //@{
  513. /** Request an asynchronous dns resolution.
  514. \param host hostname to be resolved
  515. \param port port number passed along for the ride
  516. \return Resolve ID */
  517. int Resolve(const std::string& host,port_t port = 0);
  518. #ifdef ENABLE_IPV6
  519. int Resolve6(const std::string& host, port_t port = 0);
  520. #endif
  521. /** Callback returning a resolved address.
  522. \param id Resolve ID from Resolve call
  523. \param a resolved ip address
  524. \param port port number passed to Resolve */
  525. virtual void OnResolved(int id,ipaddr_t a,port_t port);
  526. #ifdef ENABLE_IPV6
  527. virtual void OnResolved(int id,in6_addr& a,port_t port);
  528. #endif
  529. /** Request asynchronous reverse dns lookup.
  530. \param a in_addr to be translated */
  531. int Resolve(ipaddr_t a);
  532. #ifdef ENABLE_IPV6
  533. int Resolve(in6_addr& a);
  534. #endif
  535. /** Callback returning reverse resolve results.
  536. \param id Resolve ID
  537. \param name Resolved hostname */
  538. virtual void OnReverseResolved(int id,const std::string& name);
  539. /** Callback indicating failed dns lookup.
  540. \param id Resolve ID */
  541. virtual void OnResolveFailed(int id);
  542. //@}
  543. #endif // ENABLE_RESOLVER
  544. #ifdef ENABLE_DETACH
  545. /** \name Thread Support */
  546. //@{
  547. /** Callback fires when a new socket thread has started and this
  548. socket is ready for operation again.
  549. \sa ResolvSocket */
  550. virtual void OnDetached();
  551. // LIST_DETACH
  552. /** Internal use. */
  553. void SetDetach(bool x = true);
  554. /** Check detach flag.
  555. \return true if the socket should detach to its own thread */
  556. bool IsDetach();
  557. /** Internal use. */
  558. void SetDetached(bool x = true);
  559. /** Check detached flag.
  560. \return true if the socket runs in its own thread. */
  561. const bool IsDetached() const;
  562. /** Order this socket to start its own thread and call OnDetached
  563. when ready for operation. */
  564. bool Detach();
  565. /** Store the slave sockethandler pointer. */
  566. void SetSlaveHandler(ISocketHandler *);
  567. /** Create new thread for this socket to run detached in. */
  568. void DetachSocket();
  569. //@}
  570. #endif // ENABLE_DETACH
  571. /** Write traffic to an IFile. Socket will not delete this object. */
  572. void SetTrafficMonitor(IFile *p) { m_traffic_monitor = p; }
  573. #ifdef ENABLE_TRIGGERS
  574. /** \name Triggers */
  575. //@{
  576. /** Subscribe to trigger id. */
  577. void Subscribe(int id);
  578. /** Unsubscribe from trigger id. */
  579. void Unsubscribe(int id);
  580. /** Trigger callback, with data passed from source to destination. */
  581. virtual void OnTrigger(int id, const TriggerData& data);
  582. /** Trigger cancelled because source has been deleted (as in delete). */
  583. virtual void OnCancelled(int id);
  584. //@}
  585. #endif
  586. protected:
  587. /** default constructor not available */
  588. Socket() : m_handler(m_handler) {}
  589. /** copy constructor not available */
  590. Socket(const Socket& s) : m_handler(s.m_handler) {}
  591. /** assignment operator not available. */
  592. Socket& operator=(const Socket& ) { return *this; }
  593. /** All traffic will be written to this IFile, if set. */
  594. IFile *GetTrafficMonitor() { return m_traffic_monitor; }
  595. // unsigned long m_flags; ///< boolean flags, replacing old 'bool' members
  596. private:
  597. ISocketHandler& m_handler; ///< Reference of ISocketHandler in control of this socket
  598. SOCKET m_socket; ///< File descriptor
  599. bool m_bDel; ///< Delete by handler flag
  600. bool m_bClose; ///< Close and delete flag
  601. time_t m_tCreate; ///< Time in seconds when this socket was created
  602. Socket *m_parent; ///< Pointer to ListenSocket class, valid for incoming sockets
  603. bool m_b_disable_read; ///< Disable checking for read events
  604. bool m_connected; ///< Socket is connected (tcp/udp)
  605. bool m_b_erased_by_handler; ///< Set by handler before delete
  606. time_t m_tClose; ///< Time in seconds when ordered to close
  607. std::auto_ptr<SocketAddress> m_client_remote_address; ///< Address of last connect()
  608. std::auto_ptr<SocketAddress> m_remote_address; ///< Remote end address
  609. IFile *m_traffic_monitor;
  610. time_t m_timeout_start; ///< Set by SetTimeout
  611. time_t m_timeout_limit; ///< Defined by SetTimeout
  612. bool m_bLost; ///< connection lost
  613. #ifdef _WIN32
  614. static WSAInitializer m_winsock_init; ///< Winsock initialization singleton class
  615. #endif
  616. #ifdef HAVE_OPENSSL
  617. bool m_b_enable_ssl; ///< Enable SSL for this TcpSocket
  618. bool m_b_ssl; ///< ssl negotiation mode (TcpSocket)
  619. bool m_b_ssl_server; ///< True if this is an incoming ssl TcpSocket connection
  620. #endif
  621. #ifdef ENABLE_IPV6
  622. bool m_ipv6; ///< This is an ipv6 socket if this one is true
  623. #endif
  624. #ifdef ENABLE_POOL
  625. int m_socket_type; ///< Type of socket, from socket() call
  626. std::string m_socket_protocol; ///< Protocol, from socket() call
  627. bool m_bClient; ///< only client connections are pooled
  628. bool m_bRetain; ///< keep connection on close
  629. #endif
  630. #ifdef ENABLE_SOCKS4
  631. bool m_bSocks4; ///< socks4 negotiation mode (TcpSocket)
  632. ipaddr_t m_socks4_host; ///< socks4 server address
  633. port_t m_socks4_port; ///< socks4 server port number
  634. std::string m_socks4_userid; ///< socks4 server usedid
  635. #endif
  636. #ifdef ENABLE_DETACH
  637. bool m_detach; ///< Socket ordered to detach flag
  638. bool m_detached; ///< Socket has been detached
  639. SocketThread *m_pThread; ///< Detach socket thread class pointer
  640. ISocketHandler *m_slave_handler; ///< Actual sockethandler while detached
  641. #endif
  642. };
  643. #ifdef SOCKETS_NAMESPACE
  644. }
  645. #endif
  646. #endif // _SOCKETS_Socket_H