Logo
~Sockets~
~Examples~
~Contact~


TcpSocket.h

Go to the documentation of this file.
00001 
00005 /*
00006 Copyright (C) 2004-2007  Anders Hedstrom
00007 
00008 This library is made available under the terms of the GNU GPL.
00009 
00010 If you would like to use this library in a closed-source application,
00011 a separate license agreement is available. For information about 
00012 the closed-source license agreement for the C++ sockets library,
00013 please visit http://www.alhem.net/Sockets/license.html and/or
00014 email license@alhem.net.
00015 
00016 This program is free software; you can redistribute it and/or
00017 modify it under the terms of the GNU General Public License
00018 as published by the Free Software Foundation; either version 2
00019 of the License, or (at your option) any later version.
00020 
00021 This program is distributed in the hope that it will be useful,
00022 but WITHOUT ANY WARRANTY; without even the implied warranty of
00023 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024 GNU General Public License for more details.
00025 
00026 You should have received a copy of the GNU General Public License
00027 along with this program; if not, write to the Free Software
00028 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00029 */
00030 #ifndef _SOCKETS_TcpSocket_H
00031 #define _SOCKETS_TcpSocket_H
00032 #include "sockets-config.h"
00033 #include "StreamSocket.h"
00034 #ifdef HAVE_OPENSSL
00035 #include <openssl/ssl.h>
00036 #include "SSLInitializer.h"
00037 #endif
00038 
00039 
00040 #define TCP_BUFSIZE_READ 16400
00041 #define TCP_OUTPUT_CAPACITY 1024000
00042 
00043 
00044 #ifdef SOCKETS_NAMESPACE
00045 namespace SOCKETS_NAMESPACE {
00046 #endif
00047 
00048 class SocketAddress;
00049 
00050 
00053 class TcpSocket : public StreamSocket
00054 {
00056 protected:
00059         class CircularBuffer
00060         {
00061         public:
00062                 CircularBuffer(size_t size);
00063                 ~CircularBuffer();
00064 
00066                 bool Write(const char *p,size_t l);
00068                 bool Read(char *dest,size_t l);
00070                 bool Remove(size_t l);
00072                 std::string ReadString(size_t l);
00073 
00075                 size_t GetLength();
00077                 const char *GetStart();
00079                 size_t GetL();
00081                 size_t Space();
00082 
00084                 unsigned long ByteCounter(bool clear = false);
00085 
00086         private:
00087                 CircularBuffer(const CircularBuffer& s) {}
00088                 CircularBuffer& operator=(const CircularBuffer& ) { return *this; }
00089                 char *buf;
00090                 size_t m_max;
00091                 size_t m_q;
00092                 size_t m_b;
00093                 size_t m_t;
00094                 unsigned long m_count;
00095         };
00098         struct OUTPUT {
00099                 OUTPUT() : _b(0), _t(0), _q(0) {}
00100                 OUTPUT(const char *buf, size_t len) : _b(0), _t(len), _q(len) {
00101                         memcpy(_buf, buf, len);
00102                 }
00103                 size_t Space() {
00104                         return TCP_OUTPUT_CAPACITY - _t;
00105                 }
00106                 void Add(const char *buf, size_t len) {
00107                         memcpy(_buf + _t, buf, len);
00108                         _t += len;
00109                         _q += len;
00110                 }
00111                 size_t Remove(size_t len) {
00112                         _b += len;
00113                         _q -= len;
00114                         return _q;
00115                 }
00116                 const char *Buf() {
00117                         return _buf + _b;
00118                 }
00119                 size_t Len() {
00120                         return _q;
00121                 }
00122                 size_t _b;
00123                 size_t _t;
00124                 size_t _q;
00125                 char _buf[TCP_OUTPUT_CAPACITY];
00126         };
00127         typedef std::list<OUTPUT *> output_l;
00128 
00129 public:
00131         TcpSocket(ISocketHandler& );
00136         TcpSocket(ISocketHandler& h,size_t isize,size_t osize);
00137         ~TcpSocket();
00138 
00147         bool Open(ipaddr_t ip,port_t port,bool skip_socks = false);
00148 #ifdef ENABLE_IPV6
00149 #ifdef IPPROTO_IPV6
00150 
00154         bool Open(in6_addr ip,port_t port,bool skip_socks = false);
00155 #endif
00156 #endif
00157         bool Open(SocketAddress&,bool skip_socks = false);
00158         bool Open(SocketAddress&,SocketAddress& bind_address,bool skip_socks = false);
00162         bool Open(const std::string &host,port_t port);
00163 
00165         void OnConnectTimeout();
00166 #ifdef _WIN32
00167 
00168         void OnException();
00169 #endif
00170 
00173         int Close();
00174 
00178         void Send(const std::string &s,int f = 0);
00180         void Sendf(const char *format, ...);
00185         void SendBuf(const char *buf,size_t len,int f = 0);
00189         virtual void OnRawData(const char *buf,size_t len);
00190 
00192         virtual void OnWriteComplete();
00194         size_t GetInputLength();
00196         size_t GetOutputLength();
00197 
00200         void OnLine(const std::string& line);
00202         uint64_t GetBytesReceived(bool clear = false);
00204         uint64_t GetBytesSent(bool clear = false);
00205 
00207         void OnSocks4Connect();
00209         void OnSocks4ConnectFailed();
00212         bool OnSocks4Read();
00213 
00214 #ifdef ENABLE_RESOLVER
00215 
00216         void OnResolved(int id,ipaddr_t a,port_t port);
00217 #ifdef ENABLE_IPV6
00218         void OnResolved(int id,in6_addr& a,port_t port);
00219 #endif
00220 #endif
00221 #ifdef HAVE_OPENSSL
00222 
00223         void OnSSLConnect();
00225         void OnSSLAccept();
00228         virtual void InitSSLClient();
00231         virtual void InitSSLServer();
00232 #endif
00233 
00234 #ifdef ENABLE_RECONNECT
00235 
00236         void SetReconnect(bool = true);
00238         bool Reconnect();
00240         void SetIsReconnect(bool x = true);
00242         bool IsReconnect();
00243 #endif
00244 
00245         void DisableInputBuffer(bool = true);
00246 
00247         void OnOptions(int,int,int,SOCKET);
00248 
00249         void SetLineProtocol(bool = true);
00250 
00251         // TCP options
00252         bool SetTcpNodelay(bool = true);
00253 
00254         virtual int Protocol();
00255 
00257         void SetTransferLimit(size_t sz);
00260         virtual void OnTransferLimit();
00261 
00262 protected:
00263         TcpSocket(const TcpSocket& );
00264         void OnRead();
00265         void OnWrite();
00266 #ifdef HAVE_OPENSSL
00267 
00269         void InitializeContext(const std::string& context, SSL_METHOD *meth_in = NULL);
00274         void InitializeContext(const std::string& context, const std::string& keyfile, const std::string& password, SSL_METHOD *meth_in = NULL);
00276 static  int SSL_password_cb(char *buf,int num,int rwflag,void *userdata);
00278         virtual SSL_CTX *GetSslContext();
00280         virtual SSL *GetSsl();
00282         bool SSLNegotiate();
00284         const std::string& GetPassword();
00285 #endif
00286 
00287         CircularBuffer ibuf; 
00288 
00289 private:
00290         TcpSocket& operator=(const TcpSocket& ) { return *this; }
00291 
00293         int TryWrite(const char *buf, size_t len);
00295         void Buffer(const char *buf, size_t len);
00296 
00297         //
00298         bool m_b_input_buffer_disabled;
00299         uint64_t m_bytes_sent;
00300         uint64_t m_bytes_received;
00301         bool m_skip_c; 
00302         char m_c; 
00303         std::string m_line; 
00304 #ifdef SOCKETS_DYNAMIC_TEMP
00305         char *m_buf; 
00306 #endif
00307         output_l m_obuf; 
00308         OUTPUT *m_obuf_top; 
00309         size_t m_transfer_limit;
00310         size_t m_output_length;
00311 
00312 #ifdef HAVE_OPENSSL
00313 static  SSLInitializer m_ssl_init;
00314         SSL_CTX *m_ssl_ctx; 
00315         SSL *m_ssl; 
00316         BIO *m_sbio; 
00317         std::string m_password; 
00318 #endif
00319 
00320 #ifdef ENABLE_SOCKS4
00321         int m_socks4_state; 
00322         char m_socks4_vn; 
00323         char m_socks4_cd; 
00324         unsigned short m_socks4_dstport; 
00325         unsigned long m_socks4_dstip; 
00326 #endif
00327 
00328 #ifdef ENABLE_RESOLVER
00329         int m_resolver_id; 
00330 #endif
00331 
00332 #ifdef ENABLE_RECONNECT
00333         bool m_b_reconnect; 
00334         bool m_b_is_reconnect; 
00335 #endif
00336 
00337 };
00338 
00339 
00340 #ifdef SOCKETS_NAMESPACE
00341 }
00342 #endif
00343 
00344 #endif // _SOCKETS_TcpSocket_H
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4