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 #include "Socket.h"
00031 #ifdef _WIN32
00032 #pragma warning(disable:4786)
00033 #include <stdlib.h>
00034 #else
00035 #include <errno.h>
00036 #include <netdb.h>
00037 #endif
00038 #include <ctype.h>
00039 #include <fcntl.h>
00040
00041 #include "ISocketHandler.h"
00042 #include "Utility.h"
00043
00044 #include "TcpSocket.h"
00045 #include "SocketAddress.h"
00046 #include "SocketHandler.h"
00047
00048 #ifdef _DEBUG
00049 #define DEB(x) x; fflush(stderr);
00050 #else
00051 #define DEB(x)
00052 #endif
00053
00054 #ifdef SOCKETS_NAMESPACE
00055 namespace SOCKETS_NAMESPACE {
00056 #endif
00057
00058
00059
00060 #ifdef _WIN32
00061 WSAInitializer Socket::m_winsock_init;
00062 #endif
00063
00064
00065 Socket::Socket(ISocketHandler& h)
00066 :m_prng( true )
00067 ,m_handler(h)
00068 ,m_socket( INVALID_SOCKET )
00069 ,m_bDel(false)
00070 ,m_bClose(false)
00071 ,m_bConnecting(false)
00072 ,m_tCreate(time(NULL))
00073 ,m_line_protocol(false)
00074 #ifdef ENABLE_IPV6
00075 ,m_ipv6(false)
00076 #endif
00077 ,m_parent(NULL)
00078 ,m_call_on_connect(false)
00079 ,m_opt_reuse(true)
00080 ,m_opt_keepalive(true)
00081 ,m_connect_timeout(5)
00082 ,m_b_disable_read(false)
00083 ,m_b_retry_connect(false)
00084 ,m_connected(false)
00085 ,m_flush_before_close(true)
00086 ,m_connection_retry(0)
00087 ,m_retries(0)
00088 ,m_b_erased_by_handler(false)
00089 ,m_tClose(0)
00090 ,m_shutdown(0)
00091 ,m_client_remote_address(NULL)
00092 ,m_remote_address(NULL)
00093 ,m_traffic_monitor(NULL)
00094 #ifdef HAVE_OPENSSL
00095 ,m_b_enable_ssl(false)
00096 ,m_b_ssl(false)
00097 ,m_b_ssl_server(false)
00098 #endif
00099 #ifdef ENABLE_POOL
00100 ,m_socket_type(0)
00101 ,m_bClient(false)
00102 ,m_bRetain(false)
00103 ,m_bLost(false)
00104 #endif
00105 #ifdef ENABLE_SOCKS4
00106 ,m_bSocks4(false)
00107 ,m_socks4_host(h.GetSocks4Host())
00108 ,m_socks4_port(h.GetSocks4Port())
00109 ,m_socks4_userid(h.GetSocks4Userid())
00110 #endif
00111 #ifdef ENABLE_DETACH
00112 ,m_detach(false)
00113 ,m_detached(false)
00114 ,m_pThread(NULL)
00115 ,m_slave_handler(NULL)
00116 #endif
00117 {
00118 }
00119
00120
00121 Socket::~Socket()
00122 {
00123 Handler().Remove(this);
00124 if (m_socket != INVALID_SOCKET
00125 #ifdef ENABLE_POOL
00126 && !m_bRetain
00127 #endif
00128 )
00129 {
00130 Close();
00131 }
00132 }
00133
00134
00135 void Socket::Init()
00136 {
00137 }
00138
00139
00140 void Socket::OnRead()
00141 {
00142 }
00143
00144
00145 void Socket::OnWrite()
00146 {
00147 }
00148
00149
00150 void Socket::OnException()
00151 {
00152 #ifdef _WIN32
00153 if (Connecting())
00154 {
00155 #ifdef ENABLE_SOCKS4
00156 if (Socks4())
00157 OnSocks4ConnectFailed();
00158 else
00159 #endif
00160 if (GetConnectionRetry() == -1 ||
00161 (GetConnectionRetry() &&
00162 GetConnectionRetries() < GetConnectionRetry() ))
00163 {
00164
00165
00166
00167
00168 }
00169 else
00170 {
00171 SetConnecting(false);
00172 SetCloseAndDelete();
00173 OnConnectFailed();
00174 }
00175 return;
00176 }
00177 #endif
00178
00179 int err;
00180 socklen_t errlen = sizeof(err);
00181 #ifdef _WIN32
00182 getsockopt(m_socket, SOL_SOCKET, SO_ERROR, (char *)&err, &errlen);
00183 #else
00184 getsockopt(m_socket, SOL_SOCKET, SO_ERROR, &err, &errlen);
00185 #endif
00186 Handler().LogError(this, "exception on select", err, StrError(err), LOG_LEVEL_FATAL);
00187 SetCloseAndDelete();
00188 }
00189
00190
00191 void Socket::OnDelete()
00192 {
00193 }
00194
00195
00196 void Socket::OnConnect()
00197 {
00198 }
00199
00200
00201 bool Socket::CheckConnect()
00202 {
00203 int err;
00204 socklen_t errlen = sizeof(err);
00205 bool r = true;
00206 #ifdef _WIN32
00207 getsockopt(m_socket, SOL_SOCKET, SO_ERROR, (char *)&err, &errlen);
00208 #else
00209 getsockopt(m_socket, SOL_SOCKET, SO_ERROR, &err, &errlen);
00210 #endif
00211 if (err)
00212 {
00213 Handler().LogError(this, "connect failed", err, StrError(err), LOG_LEVEL_FATAL);
00214 r = false;
00215 }
00216
00218 if (r)
00219 {
00220 Set(!IsDisableRead(), false);
00221 SetConnecting(false);
00222 }
00223 else
00224 {
00225 Set(false, false);
00226 }
00227 return r;
00228 }
00229
00230
00231 void Socket::OnAccept()
00232 {
00233 }
00234
00235
00236 int Socket::Close()
00237 {
00238 DEB( fprintf(stderr, " fd %d\n", m_socket);)
00239 if (m_socket == INVALID_SOCKET)
00240 {
00241 Handler().LogError(this, "Socket::Close", 0, "file descriptor invalid", LOG_LEVEL_WARNING);
00242 return 0;
00243 }
00244 int n;
00245 SetNonblocking(true);
00246 if (IsConnected() && !(GetShutdown() & SHUT_WR))
00247 {
00248 if (shutdown(m_socket, SHUT_WR) == -1)
00249 {
00250
00251 Handler().LogError(this, "shutdown", Errno, StrError(Errno), LOG_LEVEL_ERROR);
00252 }
00253 }
00254
00255 char tmp[100];
00256 if ((n = recv(m_socket,tmp,100,0)) == -1)
00257 {
00258
00259 }
00260 else
00261 {
00262 if (n)
00263 {
00264 Handler().LogError(this, "read() after shutdown", n, "bytes read", LOG_LEVEL_WARNING);
00265 }
00266 }
00267 if ((n = closesocket(m_socket)) == -1)
00268 {
00269
00270 Handler().LogError(this, "close", Errno, StrError(Errno), LOG_LEVEL_ERROR);
00271 }
00272 Set(false, false, false);
00273 Handler().AddList(m_socket, LIST_CALLONCONNECT, false);
00274 #ifdef ENABLE_DETACH
00275 Handler().AddList(m_socket, LIST_DETACH, false);
00276 #endif
00277 Handler().AddList(m_socket, LIST_CONNECTING, false);
00278 Handler().AddList(m_socket, LIST_RETRY, false);
00279 Handler().AddList(m_socket, LIST_CLOSE, false);
00280 m_socket = INVALID_SOCKET;
00281 DEB( fprintf(stderr, " fd %d\n", m_socket);)
00282 return n;
00283 }
00284
00285
00286 SOCKET Socket::CreateSocket(int af,int type, const std::string& protocol)
00287 {
00288 struct protoent *p = NULL;
00289 int optval;
00290 SOCKET s;
00291
00292 #ifdef ENABLE_POOL
00293 m_socket_type = type;
00294 m_socket_protocol = protocol;
00295 #endif
00296 if (protocol.size())
00297 {
00298 p = getprotobyname( protocol.c_str() );
00299 if (!p)
00300 {
00301 Handler().LogError(this, "getprotobyname", Errno, StrError(Errno), LOG_LEVEL_FATAL);
00302 SetCloseAndDelete();
00303 return INVALID_SOCKET;
00304 }
00305 }
00306 int protno = p ? p -> p_proto : 0;
00307
00308 s = socket(af, type, protno);
00309 if (s == INVALID_SOCKET)
00310 {
00311 Handler().LogError(this, "socket", Errno, StrError(Errno), LOG_LEVEL_FATAL);
00312 SetCloseAndDelete();
00313 return INVALID_SOCKET;
00314 }
00315 OnOptions(af, type, protno, s);
00316 #ifdef SO_NOSIGPIPE
00317 {
00318 optval = 1;
00319 if (setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (char *)&optval, sizeof(optval)) == -1)
00320 {
00321 Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_NOSIGPIPE)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
00322 closesocket(s);
00323 SetCloseAndDelete();
00324 return INVALID_SOCKET;
00325 }
00326 }
00327 #endif
00328 if (type == SOCK_STREAM)
00329 {
00330 optval = m_opt_reuse ? 1 : 0;
00331 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&optval, sizeof(optval)) == -1)
00332 {
00333 Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_REUSEADDR)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
00334 closesocket(s);
00335 SetCloseAndDelete();
00336 return INVALID_SOCKET;
00337 }
00338
00339 optval = m_opt_keepalive ? 1 : 0;
00340 if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (char *)&optval, sizeof(optval)) == -1)
00341 {
00342 Handler().LogError(this, "setsockopt(SOL_SOCKET, SO_KEEPALIVE)", Errno, StrError(Errno), LOG_LEVEL_FATAL);
00343 closesocket(s);
00344 SetCloseAndDelete();
00345 return INVALID_SOCKET;
00346 }
00347 }
00348 return s;
00349 }
00350
00351
00352 void Socket::Attach(SOCKET s)
00353 {
00354 m_socket = s;
00355 }
00356
00357
00358 SOCKET Socket::GetSocket()
00359 {
00360 return m_socket;
00361 }
00362
00363
00364 void Socket::SetDeleteByHandler(bool x)
00365 {
00366 m_bDel = x;
00367 }
00368
00369
00370 bool Socket::DeleteByHandler()
00371 {
00372 return m_bDel;
00373 }
00374
00375
00376 void Socket::SetCloseAndDelete(bool x)
00377 {
00378 if (x != m_bClose)
00379 {
00380 Handler().AddList(m_socket, LIST_CLOSE, x);
00381 m_bClose = x;
00382 if (x)
00383 {
00384 m_tClose = time(NULL);
00385 }
00386 }
00387 }
00388
00389
00390 bool Socket::CloseAndDelete()
00391 {
00392 return m_bClose;
00393 }
00394
00395
00396 void Socket::SetConnecting(bool x)
00397 {
00398 if (x != m_bConnecting)
00399 {
00400 Handler().AddList(m_socket, LIST_CONNECTING, x);
00401 m_bConnecting = x;
00402 if (x)
00403 {
00404 m_tConnect = time(NULL);
00405 }
00406 }
00407 }
00408
00409
00410 bool Socket::Connecting()
00411 {
00412 return m_bConnecting;
00413 }
00414
00415
00416 void Socket::SetRemoteAddress(SocketAddress& ad)
00417 {
00418 m_remote_address = ad.GetCopy();
00419 }
00420
00421
00422 std::auto_ptr<SocketAddress> Socket::GetRemoteSocketAddress()
00423 {
00424 return std::auto_ptr<SocketAddress>(m_remote_address -> GetCopy());
00425 }
00426
00427
00428 ISocketHandler& Socket::Handler() const
00429 {
00430 #ifdef ENABLE_DETACH
00431 if (IsDetached())
00432 return *m_slave_handler;
00433 #endif
00434 return m_handler;
00435 }
00436
00437
00438 ISocketHandler& Socket::MasterHandler() const
00439 {
00440 return m_handler;
00441 }
00442
00443
00444 ipaddr_t Socket::GetRemoteIP4()
00445 {
00446 ipaddr_t l = 0;
00447 #ifdef ENABLE_IPV6
00448 if (m_ipv6)
00449 {
00450 Handler().LogError(this, "GetRemoteIP4", 0, "get ipv4 address for ipv6 socket", LOG_LEVEL_WARNING);
00451 }
00452 #endif
00453 if (m_remote_address.get() != NULL)
00454 {
00455 struct sockaddr *p = *m_remote_address;
00456 struct sockaddr_in *sa = (struct sockaddr_in *)p;
00457 memcpy(&l, &sa -> sin_addr, sizeof(struct in_addr));
00458 }
00459 return l;
00460 }
00461
00462
00463 #ifdef ENABLE_IPV6
00464 #ifdef IPPROTO_IPV6
00465 struct in6_addr Socket::GetRemoteIP6()
00466 {
00467 if (!m_ipv6)
00468 {
00469 Handler().LogError(this, "GetRemoteIP6", 0, "get ipv6 address for ipv4 socket", LOG_LEVEL_WARNING);
00470 }
00471 struct sockaddr_in6 fail;
00472 if (m_remote_address.get() != NULL)
00473 {
00474 struct sockaddr *p = *m_remote_address;
00475 memcpy(&fail, p, sizeof(struct sockaddr_in6));
00476 }
00477 else
00478 {
00479 memset(&fail, 0, sizeof(struct sockaddr_in6));
00480 }
00481 return fail.sin6_addr;
00482 }
00483 #endif
00484 #endif
00485
00486
00487 port_t Socket::GetRemotePort()
00488 {
00489 if (!m_remote_address.get())
00490 {
00491 return 0;
00492 }
00493 return m_remote_address -> GetPort();
00494 }
00495
00496
00497 std::string Socket::GetRemoteAddress()
00498 {
00499 if (!m_remote_address.get())
00500 {
00501 return "";
00502 }
00503 return m_remote_address -> Convert(false);
00504 }
00505
00506
00507 std::string Socket::GetRemoteHostname()
00508 {
00509 if (!m_remote_address.get())
00510 {
00511 return "";
00512 }
00513 return m_remote_address -> Reverse();
00514 }
00515
00516
00517 bool Socket::SetNonblocking(bool bNb)
00518 {
00519 #ifdef _WIN32
00520 unsigned long l = bNb ? 1 : 0;
00521 int n = ioctlsocket(m_socket, FIONBIO, &l);
00522 if (n != 0)
00523 {
00524 Handler().LogError(this, "ioctlsocket(FIONBIO)", Errno, "");
00525 return false;
00526 }
00527 return true;
00528 #else
00529 if (bNb)
00530 {
00531 if (fcntl(m_socket, F_SETFL, O_NONBLOCK) == -1)
00532 {
00533 Handler().LogError(this, "fcntl(F_SETFL, O_NONBLOCK)", Errno, StrError(Errno), LOG_LEVEL_ERROR);
00534 return false;
00535 }
00536 }
00537 else
00538 {
00539 if (fcntl(m_socket, F_SETFL, 0) == -1)
00540 {
00541 Handler().LogError(this, "fcntl(F_SETFL, 0)", Errno, StrError(Errno), LOG_LEVEL_ERROR);
00542 return false;
00543 }
00544 }
00545 return true;
00546 #endif
00547 }
00548
00549
00550 bool Socket::SetNonblocking(bool bNb, SOCKET s)
00551 {
00552 #ifdef _WIN32
00553 unsigned long l = bNb ? 1 : 0;
00554 int n = ioctlsocket(s, FIONBIO, &l);
00555 if (n != 0)
00556 {
00557 Handler().LogError(this, "ioctlsocket(FIONBIO)", Errno, "");
00558 return false;
00559 }
00560 return true;
00561 #else
00562 if (bNb)
00563 {
00564 if (fcntl(s, F_SETFL, O_NONBLOCK) == -1)
00565 {
00566 Handler().LogError(this, "fcntl(F_SETFL, O_NONBLOCK)", Errno, StrError(Errno), LOG_LEVEL_ERROR);
00567 return false;
00568 }
00569 }
00570 else
00571 {
00572 if (fcntl(s, F_SETFL, 0) == -1)
00573 {
00574 Handler().LogError(this, "fcntl(F_SETFL, 0)", Errno, StrError(Errno), LOG_LEVEL_ERROR);
00575 return false;
00576 }
00577 }
00578 return true;
00579 #endif
00580 }
00581
00582
00583 void Socket::Set(bool bRead, bool bWrite, bool bException)
00584 {
00585 Handler().Set(m_socket, bRead, bWrite, bException);
00586 }
00587
00588
00589 time_t Socket::GetConnectTime()
00590 {
00591 return time(NULL) - m_tConnect;
00592 }
00593
00594
00595 bool Socket::Ready()
00596 {
00597 if (m_socket != INVALID_SOCKET && !Connecting() && !CloseAndDelete())
00598 return true;
00599 return false;
00600 }
00601
00602
00603 void Socket::OnLine(const std::string& )
00604 {
00605 }
00606
00607
00608 void Socket::SetLineProtocol(bool x)
00609 {
00610 m_line_protocol = x;
00611 }
00612
00613
00614 bool Socket::LineProtocol()
00615 {
00616 return m_line_protocol;
00617 }
00618
00619
00620 void Socket::OnConnectFailed()
00621 {
00622 }
00623
00624
00625 Socket::Socket(const Socket& s) : m_handler(s.Handler())
00626 {
00627 }
00628
00629
00630 Socket *Socket::GetParent()
00631 {
00632 return m_parent;
00633 }
00634
00635
00636 void Socket::SetParent(Socket *x)
00637 {
00638 m_parent = x;
00639 }
00640
00641
00642 port_t Socket::GetPort()
00643 {
00644 Handler().LogError(this, "GetPort", 0, "GetPort only implemented for ListenSocket", LOG_LEVEL_WARNING);
00645 return 0;
00646 }
00647
00648
00649 Socket *Socket::Create()
00650 {
00651 return NULL;
00652 }
00653
00654
00655 bool Socket::OnConnectRetry()
00656 {
00657 return true;
00658 }
00659
00660
00661 #ifdef ENABLE_RECONNECT
00662 void Socket::OnReconnect()
00663 {
00664 }
00665 #endif
00666
00667
00668 time_t Socket::Uptime()
00669 {
00670 return time(NULL) - m_tCreate;
00671 }
00672
00673
00674 #ifdef ENABLE_IPV6
00675 void Socket::SetIpv6(bool x)
00676 {
00677 m_ipv6 = x;
00678 }
00679
00680
00681 bool Socket::IsIpv6()
00682 {
00683 return m_ipv6;
00684 }
00685 #endif
00686
00687
00688 void Socket::SetCallOnConnect(bool x)
00689 {
00690 Handler().AddList(m_socket, LIST_CALLONCONNECT, x);
00691 m_call_on_connect = x;
00692 }
00693
00694
00695 bool Socket::CallOnConnect()
00696 {
00697 return m_call_on_connect;
00698 }
00699
00700
00701 void Socket::SetReuse(bool x)
00702 {
00703 m_opt_reuse = x;
00704 }
00705
00706
00707 void Socket::SetKeepalive(bool x)
00708 {
00709 m_opt_keepalive = x;
00710 }
00711
00712
00713 void Socket::SetConnectTimeout(int x)
00714 {
00715 m_connect_timeout = x;
00716 }
00717
00718
00719 int Socket::GetConnectTimeout()
00720 {
00721 return m_connect_timeout;
00722 }
00723
00724
00725 void Socket::DisableRead(bool x)
00726 {
00727 m_b_disable_read = x;
00728 }
00729
00730
00731 bool Socket::IsDisableRead()
00732 {
00733 return m_b_disable_read;
00734 }
00735
00736
00737 void Socket::SetRetryClientConnect(bool x)
00738 {
00739 Handler().AddList(m_socket, LIST_RETRY, x);
00740 m_b_retry_connect = x;
00741 }
00742
00743
00744 bool Socket::RetryClientConnect()
00745 {
00746 return m_b_retry_connect;
00747 }
00748
00749
00750 void Socket::SendBuf(const char *,size_t,int)
00751 {
00752 }
00753
00754
00755 void Socket::Send(const std::string&,int)
00756 {
00757 }
00758
00759
00760 void Socket::SetConnected(bool x)
00761 {
00762 m_connected = x;
00763 }
00764
00765
00766 bool Socket::IsConnected()
00767 {
00768 return m_connected;
00769 }
00770
00771
00772 void Socket::SetFlushBeforeClose(bool x)
00773 {
00774 m_flush_before_close = x;
00775 }
00776
00777
00778 bool Socket::GetFlushBeforeClose()
00779 {
00780 return m_flush_before_close;
00781 }
00782
00783
00784 int Socket::GetConnectionRetry()
00785 {
00786 return m_connection_retry;
00787 }
00788
00789
00790 void Socket::SetConnectionRetry(int x)
00791 {
00792 m_connection_retry = x;
00793 }
00794
00795
00796 int Socket::GetConnectionRetries()
00797 {
00798 return m_retries;
00799 }
00800
00801
00802 void Socket::IncreaseConnectionRetries()
00803 {
00804 m_retries++;
00805 }
00806
00807
00808 void Socket::ResetConnectionRetries()
00809 {
00810 m_retries = 0;
00811 }
00812
00813
00814 #ifdef ENABLE_RECONNECT
00815 void Socket::OnDisconnect()
00816 {
00817 }
00818 #endif
00819
00820
00821 void Socket::SetErasedByHandler(bool x)
00822 {
00823 m_b_erased_by_handler = x;
00824 }
00825
00826
00827 bool Socket::ErasedByHandler()
00828 {
00829 return m_b_erased_by_handler;
00830 }
00831
00832
00833 time_t Socket::TimeSinceClose()
00834 {
00835 return time(NULL) - m_tClose;
00836 }
00837
00838
00839 void Socket::SetShutdown(int x)
00840 {
00841 m_shutdown = x;
00842 }
00843
00844
00845 int Socket::GetShutdown()
00846 {
00847 return m_shutdown;
00848 }
00849
00850
00851 void Socket::SetClientRemoteAddress(SocketAddress& ad)
00852 {
00853 if (!ad.IsValid())
00854 {
00855 Handler().LogError(this, "SetClientRemoteAddress", 0, "remote address not valid", LOG_LEVEL_ERROR);
00856 }
00857 m_client_remote_address = ad.GetCopy();
00858 }
00859
00860
00861 std::auto_ptr<SocketAddress> Socket::GetClientRemoteAddress()
00862 {
00863 if (!m_client_remote_address.get())
00864 {
00865 Handler().LogError(this, "GetClientRemoteAddress", 0, "remote address not yet set", LOG_LEVEL_ERROR);
00866 }
00867 return std::auto_ptr<SocketAddress>(m_client_remote_address -> GetCopy());
00868 }
00869
00870
00871 uint64_t Socket::GetBytesSent(bool)
00872 {
00873 return 0;
00874 }
00875
00876
00877 uint64_t Socket::GetBytesReceived(bool)
00878 {
00879 return 0;
00880 }
00881
00882
00883 unsigned long int Socket::Random()
00884 {
00885 return m_prng.next();
00886 }
00887
00888
00889 #ifdef HAVE_OPENSSL
00890 void Socket::OnSSLConnect()
00891 {
00892 }
00893
00894
00895 void Socket::OnSSLAccept()
00896 {
00897 }
00898
00899
00900 bool Socket::SSLNegotiate()
00901 {
00902 return false;
00903 }
00904
00905
00906 bool Socket::IsSSL()
00907 {
00908 return m_b_enable_ssl;
00909 }
00910
00911
00912 void Socket::EnableSSL(bool x)
00913 {
00914 m_b_enable_ssl = x;
00915 }
00916
00917
00918 bool Socket::IsSSLNegotiate()
00919 {
00920 return m_b_ssl;
00921 }
00922
00923
00924 void Socket::SetSSLNegotiate(bool x)
00925 {
00926 m_b_ssl = x;
00927 }
00928
00929
00930 bool Socket::IsSSLServer()
00931 {
00932 return m_b_ssl_server;
00933 }
00934
00935
00936 void Socket::SetSSLServer(bool x)
00937 {
00938 m_b_ssl_server = x;
00939 }
00940
00941
00942 void Socket::OnSSLConnectFailed()
00943 {
00944 }
00945
00946
00947 void Socket::OnSSLAcceptFailed()
00948 {
00949 }
00950 #endif // HAVE_OPENSSL
00951
00952
00953 #ifdef ENABLE_POOL
00954 void Socket::CopyConnection(Socket *sock)
00955 {
00956 Attach( sock -> GetSocket() );
00957 #ifdef ENABLE_IPV6
00958 SetIpv6( sock -> IsIpv6() );
00959 #endif
00960 SetSocketType( sock -> GetSocketType() );
00961 SetSocketProtocol( sock -> GetSocketProtocol() );
00962
00963 SetClientRemoteAddress( *sock -> GetClientRemoteAddress() );
00964 SetRemoteAddress( *sock -> GetRemoteSocketAddress() );
00965 }
00966
00967
00968 void Socket::SetIsClient()
00969 {
00970 m_bClient = true;
00971 }
00972
00973
00974 void Socket::SetSocketType(int x)
00975 {
00976 m_socket_type = x;
00977 }
00978
00979
00980 int Socket::GetSocketType()
00981 {
00982 return m_socket_type;
00983 }
00984
00985
00986 void Socket::SetSocketProtocol(const std::string& x)
00987 {
00988 m_socket_protocol = x;
00989 }
00990
00991
00992 const std::string& Socket::GetSocketProtocol()
00993 {
00994 return m_socket_protocol;
00995 }
00996
00997
00998 void Socket::SetRetain()
00999 {
01000 if (m_bClient) m_bRetain = true;
01001 }
01002
01003
01004 bool Socket::Retain()
01005 {
01006 return m_bRetain;
01007 }
01008
01009
01010 void Socket::SetLost()
01011 {
01012 m_bLost = true;
01013 }
01014
01015
01016 bool Socket::Lost()
01017 {
01018 return m_bLost;
01019 }
01020 #endif // ENABLE_POOL
01021
01022
01023 #ifdef ENABLE_SOCKS4
01024 void Socket::OnSocks4Connect()
01025 {
01026 Handler().LogError(this, "OnSocks4Connect", 0, "Use with TcpSocket only");
01027 }
01028
01029
01030 void Socket::OnSocks4ConnectFailed()
01031 {
01032 Handler().LogError(this, "OnSocks4ConnectFailed", 0, "Use with TcpSocket only");
01033 }
01034
01035
01036 bool Socket::OnSocks4Read()
01037 {
01038 Handler().LogError(this, "OnSocks4Read", 0, "Use with TcpSocket only");
01039 return true;
01040 }
01041
01042
01043 void Socket::SetSocks4Host(const std::string& host)
01044 {
01045 Utility::u2ip(host, m_socks4_host);
01046 }
01047
01048
01049 bool Socket::Socks4()
01050 {
01051 return m_bSocks4;
01052 }
01053
01054
01055 void Socket::SetSocks4(bool x)
01056 {
01057 m_bSocks4 = x;
01058 }
01059
01060
01061 void Socket::SetSocks4Host(ipaddr_t a)
01062 {
01063 m_socks4_host = a;
01064 }
01065
01066
01067 void Socket::SetSocks4Port(port_t p)
01068 {
01069 m_socks4_port = p;
01070 }
01071
01072
01073 void Socket::SetSocks4Userid(const std::string& x)
01074 {
01075 m_socks4_userid = x;
01076 }
01077
01078
01079 ipaddr_t Socket::GetSocks4Host()
01080 {
01081 return m_socks4_host;
01082 }
01083
01084
01085 port_t Socket::GetSocks4Port()
01086 {
01087 return m_socks4_port;
01088 }
01089
01090
01091 const std::string& Socket::GetSocks4Userid()
01092 {
01093 return m_socks4_userid;
01094 }
01095 #endif // ENABLE_SOCKS4
01096
01097
01098 #ifdef ENABLE_DETACH
01099 bool Socket::Detach()
01100 {
01101 if (!DeleteByHandler())
01102 return false;
01103 if (m_pThread)
01104 return false;
01105 if (m_detached)
01106 return false;
01107 SetDetach();
01108 return true;
01109 }
01110
01111
01112 void Socket::DetachSocket()
01113 {
01114 SetDetached();
01115 m_pThread = new SocketThread(this);
01116 m_pThread -> SetRelease(true);
01117 }
01118
01119
01120 void Socket::OnDetached()
01121 {
01122 }
01123
01124
01125 void Socket::SetDetach(bool x)
01126 {
01127 Handler().AddList(m_socket, LIST_DETACH, x);
01128 m_detach = x;
01129 }
01130
01131
01132 bool Socket::IsDetach()
01133 {
01134 return m_detach;
01135 }
01136
01137
01138 void Socket::SetDetached(bool x)
01139 {
01140 m_detached = x;
01141 }
01142
01143
01144 const bool Socket::IsDetached() const
01145 {
01146 return m_detached;
01147 }
01148
01149
01150 void Socket::SetSlaveHandler(ISocketHandler *p)
01151 {
01152 m_slave_handler = p;
01153 }
01154
01155
01156 Socket::SocketThread::SocketThread(Socket *p)
01157 :Thread(false)
01158 ,m_socket(p)
01159 {
01160
01161 }
01162
01163
01164 Socket::SocketThread::~SocketThread()
01165 {
01166 if (IsRunning())
01167 {
01168 SetRelease(true);
01169 SetRunning(false);
01170 #ifdef _WIN32
01171 Sleep(1000);
01172 #else
01173 sleep(1);
01174 #endif
01175 }
01176 }
01177
01178
01179 void Socket::SocketThread::Run()
01180 {
01181 SocketHandler h;
01182 h.SetSlave();
01183 h.Add(m_socket);
01184 m_socket -> SetSlaveHandler(&h);
01185 m_socket -> OnDetached();
01186 while (h.GetCount() && IsRunning())
01187 {
01188 h.Select(0, 500000);
01189 }
01190
01191
01192
01193 SetDeleteOnExit();
01194 }
01195 #endif // ENABLE_DETACH
01196
01197
01198 #ifdef ENABLE_RESOLVER
01199 int Socket::Resolve(const std::string& host,port_t port)
01200 {
01201 return Handler().Resolve(this, host, port);
01202 }
01203
01204
01205 #ifdef ENABLE_IPV6
01206 int Socket::Resolve6(const std::string& host,port_t port)
01207 {
01208 return Handler().Resolve6(this, host, port);
01209 }
01210 #endif
01211
01212
01213 int Socket::Resolve(ipaddr_t a)
01214 {
01215 return Handler().Resolve(this, a);
01216 }
01217
01218
01219 #ifdef ENABLE_IPV6
01220 int Socket::Resolve(in6_addr& a)
01221 {
01222 return Handler().Resolve(this, a);
01223 }
01224 #endif
01225
01226
01227 void Socket::OnResolved(int,ipaddr_t,port_t)
01228 {
01229 }
01230
01231
01232 #ifdef ENABLE_IPV6
01233 void Socket::OnResolved(int,in6_addr&,port_t)
01234 {
01235 }
01236 #endif
01237
01238
01239 void Socket::OnReverseResolved(int,const std::string&)
01240 {
01241 }
01242
01243
01244 void Socket::OnResolveFailed(int)
01245 {
01246 }
01247 #endif // ENABLE_RESOLVER
01248
01249
01250 #ifdef SOCKETS_NAMESPACE
01251 }
01252 #endif
01253