PageRenderTime 40ms CodeModel.GetById 2ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 1ms

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