00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef _SOCKETS_Socket_H
00031 #define _SOCKETS_Socket_H
00032 #include "sockets-config.h"
00033
00034 #include <string>
00035 #include <vector>
00036 #include <list>
00037 #ifdef HAVE_OPENSSL
00038 #include <openssl/ssl.h>
00039 #endif
00040
00041 #include "socket_include.h"
00042 #include <time.h>
00043 #include "Utility.h"
00044 #include "SocketAddress.h"
00045 #include "RandomNumber.h"
00046 #include "Thread.h"
00047
00048
00049 #ifdef SOCKETS_NAMESPACE
00050 namespace SOCKETS_NAMESPACE {
00051 #endif
00052
00053
00054 class ISocketHandler;
00055 class SocketAddress;
00056 class IFile;
00057
00058
00062 class Socket
00063 {
00064 friend class ISocketHandler;
00065 protected:
00068 #ifdef ENABLE_DETACH
00069 class SocketThread : public Thread
00070 {
00071 public:
00072 SocketThread(Socket *p);
00073 ~SocketThread();
00074
00075 void Run();
00076
00077 private:
00078 Socket *GetSocket() const { return m_socket; }
00079 SocketThread(const SocketThread& s) : m_socket(s.GetSocket()) {}
00080 SocketThread& operator=(const SocketThread& ) { return *this; }
00081 Socket *m_socket;
00082 };
00083 #endif // ENABLE_DETACH
00084
00085 public:
00087 Socket(ISocketHandler&);
00088 virtual ~Socket();
00089
00094 virtual Socket *Create();
00095
00102 virtual void Init();
00103
00106 void Attach(SOCKET s);
00108 SOCKET GetSocket();
00111 virtual int Close();
00116 SOCKET CreateSocket(int af,int type,const std::string& protocol = "");
00118 void Set(bool bRead,bool bWrite,bool bException = true);
00122 bool Ready();
00123
00125 virtual void OnRead();
00127 virtual void OnWrite();
00129 virtual void OnException();
00131 virtual void OnDelete();
00133 virtual void OnConnect();
00135 virtual void OnAccept();
00138 virtual void OnLine(const std::string& );
00140 virtual void OnConnectFailed();
00142 virtual void OnOptions(int family,int type,int protocol,SOCKET) = 0;
00144 virtual bool OnConnectRetry();
00145 #ifdef ENABLE_RECONNECT
00146
00147 virtual void OnReconnect();
00149 virtual void OnDisconnect();
00150 #endif
00151
00153 virtual bool CheckConnect();
00158 virtual void SetLineProtocol(bool = true);
00161 bool LineProtocol();
00164 void SetDeleteByHandler(bool = true);
00167 bool DeleteByHandler();
00169 void SetCloseAndDelete(bool = true);
00172 bool CloseAndDelete();
00174 void SetConnecting(bool = true);
00177 bool Connecting();
00179 time_t GetConnectTime();
00180
00182 void SetRemoteAddress(SocketAddress&);
00184 std::auto_ptr<SocketAddress> GetRemoteSocketAddress();
00186 ipaddr_t GetRemoteIP4();
00187 #ifdef ENABLE_IPV6
00188
00189 #ifdef IPPROTO_IPV6
00190 struct in6_addr GetRemoteIP6();
00191 #endif
00192 #endif
00193
00194 port_t GetRemotePort();
00196 std::string GetRemoteAddress();
00198 std::string GetRemoteHostname();
00199
00203 ISocketHandler& Handler() const;
00208 ISocketHandler& MasterHandler() const;
00210 bool SetNonblocking(bool);
00212 bool SetNonblocking(bool, SOCKET);
00213
00215 time_t Uptime();
00216
00217
00218
00219
00220
00221
00222 #ifdef ENABLE_IPV6
00223
00224 void SetIpv6(bool x = true);
00227 bool IsIpv6();
00228 #endif
00229
00231 Socket *GetParent();
00234 void SetParent(Socket *);
00236 virtual port_t GetPort();
00237
00239 void SetClientRemoteAddress(SocketAddress&);
00241 std::auto_ptr<SocketAddress> GetClientRemoteAddress();
00242
00244 void SetCallOnConnect(bool x = true);
00247 bool CallOnConnect();
00248
00251 void SetReuse(bool x);
00254 void SetKeepalive(bool x);
00255
00258 void SetConnectTimeout(int x);
00261 int GetConnectTimeout();
00263 void DisableRead(bool x = true);
00266 bool IsDisableRead();
00267
00269 void SetRetryClientConnect(bool x = true);
00272 bool RetryClientConnect();
00273
00275 virtual void SendBuf(const char *,size_t,int = 0);
00277 virtual void Send(const std::string&,int = 0);
00278
00280 void SetConnected(bool = true);
00283 bool IsConnected();
00284
00287 void SetFlushBeforeClose(bool = true);
00290 bool GetFlushBeforeClose();
00291
00296 void SetConnectionRetry(int n);
00298 int GetConnectionRetry();
00300 void IncreaseConnectionRetries();
00302 int GetConnectionRetries();
00304 void ResetConnectionRetries();
00306 void SetErasedByHandler(bool x = true);
00308 bool ErasedByHandler();
00309
00311 time_t TimeSinceClose();
00312
00314 void SetShutdown(int);
00316 int GetShutdown();
00317 virtual uint64_t GetBytesSent(bool clear = false);
00318 virtual uint64_t GetBytesReceived(bool clear = false);
00319
00320 unsigned long int Random();
00321
00322 #ifdef HAVE_OPENSSL
00323
00324 virtual void OnSSLConnect();
00326 virtual void OnSSLAccept();
00328 virtual void OnSSLConnectFailed();
00330 virtual void OnSSLAcceptFailed();
00332 virtual bool SSLNegotiate();
00335 bool IsSSL();
00337 void EnableSSL(bool x = true);
00340 bool IsSSLNegotiate();
00342 void SetSSLNegotiate(bool x = true);
00345 bool IsSSLServer();
00347 void SetSSLServer(bool x = true);
00349 virtual SSL_CTX *GetSslContext() { return NULL; }
00351 virtual SSL *GetSsl() { return NULL; }
00352 #endif // HAVE_OPENSSL
00353
00354 #ifdef ENABLE_POOL
00355
00356 void SetIsClient();
00358 void SetSocketType(int x);
00360 int GetSocketType();
00362 void SetSocketProtocol(const std::string& x);
00364 const std::string& GetSocketProtocol();
00371 void SetRetain();
00374 bool Retain();
00376 void SetLost();
00379 bool Lost();
00381 void CopyConnection(Socket *sock);
00382 #endif // ENABLE_POOL
00383
00384 #ifdef ENABLE_SOCKS4
00385
00386 virtual void OnSocks4Connect();
00388 virtual void OnSocks4ConnectFailed();
00390 virtual bool OnSocks4Read();
00394 bool Socks4();
00396 void SetSocks4(bool x = true);
00397
00399 void SetSocks4Host(ipaddr_t a);
00401 void SetSocks4Host(const std::string& );
00403 void SetSocks4Port(port_t p);
00405 void SetSocks4Userid(const std::string& x);
00408 ipaddr_t GetSocks4Host();
00411 port_t GetSocks4Port();
00414 const std::string& GetSocks4Userid();
00415 #endif // ENABLE_SOCKS4
00416
00417 #ifdef ENABLE_RESOLVER
00418
00422 int Resolve(const std::string& host,port_t port = 0);
00423 #ifdef ENABLE_IPV6
00424 int Resolve6(const std::string& host, port_t port = 0);
00425 #endif
00426
00430 virtual void OnResolved(int id,ipaddr_t a,port_t port);
00431 #ifdef ENABLE_IPV6
00432 virtual void OnResolved(int id,in6_addr& a,port_t port);
00433 #endif
00434
00436 int Resolve(ipaddr_t a);
00437 #ifdef ENABLE_IPV6
00438 int Resolve(in6_addr& a);
00439 #endif
00440
00443 virtual void OnReverseResolved(int id,const std::string& name);
00446 virtual void OnResolveFailed(int id);
00447 #endif // ENABLE_RESOLVER
00448
00449 #ifdef ENABLE_DETACH
00450
00453 virtual void OnDetached();
00455 void SetDetach(bool x = true);
00458 bool IsDetach();
00460 void SetDetached(bool x = true);
00463 const bool IsDetached() const;
00466 bool Detach();
00468 void SetSlaveHandler(ISocketHandler *);
00470 void DetachSocket();
00471 #endif // ENABLE_DETACH
00472
00473 void SetTrafficMonitor(IFile *p) { m_traffic_monitor = p; }
00474
00475 protected:
00476 Socket(const Socket& );
00477 RandomNumber m_prng;
00478 IFile *GetTrafficMonitor() { return m_traffic_monitor; }
00479
00480 private:
00482 Socket() : m_handler(Handler()) {}
00483 #ifdef _WIN32
00484 static WSAInitializer m_winsock_init;
00485 #endif
00486
00487 Socket& operator=(const Socket& ) { return *this; }
00488
00489 ISocketHandler& m_handler;
00490 SOCKET m_socket;
00491 bool m_bDel;
00492 bool m_bClose;
00493 bool m_bConnecting;
00494 time_t m_tConnect;
00495 time_t m_tCreate;
00496 bool m_line_protocol;
00497
00498
00499 #ifdef ENABLE_IPV6
00500 bool m_ipv6;
00501 #endif
00502 Socket *m_parent;
00503 bool m_call_on_connect;
00504 bool m_opt_reuse;
00505 bool m_opt_keepalive;
00506 int m_connect_timeout;
00507 bool m_b_disable_read;
00508 bool m_b_retry_connect;
00509 bool m_connected;
00510 bool m_flush_before_close;
00511 int m_connection_retry;
00512 int m_retries;
00513 bool m_b_erased_by_handler;
00514 time_t m_tClose;
00515 int m_shutdown;
00516 std::auto_ptr<SocketAddress> m_client_remote_address;
00517 std::auto_ptr<SocketAddress> m_remote_address;
00518 IFile *m_traffic_monitor;
00519
00520 #ifdef HAVE_OPENSSL
00521 bool m_b_enable_ssl;
00522 bool m_b_ssl;
00523 bool m_b_ssl_server;
00524 #endif
00525
00526 #ifdef ENABLE_POOL
00527 int m_socket_type;
00528 std::string m_socket_protocol;
00529 bool m_bClient;
00530 bool m_bRetain;
00531 bool m_bLost;
00532 #endif
00533
00534 #ifdef ENABLE_SOCKS4
00535 bool m_bSocks4;
00536 ipaddr_t m_socks4_host;
00537 port_t m_socks4_port;
00538 std::string m_socks4_userid;
00539 #endif
00540
00541 #ifdef ENABLE_DETACH
00542 bool m_detach;
00543 bool m_detached;
00544 SocketThread *m_pThread;
00545 ISocketHandler *m_slave_handler;
00546 #endif
00547
00548 };
00549
00550 #ifdef SOCKETS_NAMESPACE
00551 }
00552 #endif
00553
00554
00555 #endif // _SOCKETS_Socket_H