inlib  1.2.0
Public Member Functions | Protected Member Functions | Protected Attributes
inlib::net::base_socket Class Reference
Inheritance diagram for inlib::net::base_socket:
Inheritance graph
[legend]

List of all members.

Public Member Functions

virtual void disconnect ()=0
virtual bool fetch_cbk (inlib::uint64)
 base_socket (std::ostream &a_out, bool a_verbose)
virtual ~base_socket ()
bool verbose () const
bool is_connected () const
int socket () const
std::ostream & out () const
bool send_buffer (const char *a_buffer, inlib::uint64 a_length, inlib::uint64 a_BLOCK=65536)
bool fetch_buffer (char *a_buffer, inlib::uint64 a_length, inlib::uint64 a_BLOCK=65536)
bool fetch_upto_char (char a_char, char *&a_buffer, inlib::uint64 &a_length, unsigned int a_BLOCK=65536)
bool fetch_upto_end (char *&a_buffer, inlib::uint64 &a_length, unsigned int a_BLOCK=65536)
bool fetch_upto_end (FILE *a_FILE, inlib::uint64 &a_length, unsigned int a_BLOCK=65536)
template<class T >
bool send (const T &a_value)
template<class T >
bool fetch (T &a_value)
bool send_uchar (unsigned char a_value)
bool fetch_uchar (unsigned char &a_value)
bool send_bool (bool a_value)
bool fetch_bool (bool &a_value)
bool send_string (const std::string &a_string)
bool fetch_string (std::string &a_string)
bool send_text (const std::vector< std::string > &a_text)
bool fetch_text (std::vector< std::string > &a_text)
bool send_file (const std::string &a_file)
bool fetch_file (const std::string &a_file)
bool is_there_input (bool &a_is)

Protected Member Functions

 base_socket (const base_socket &aFrom)
base_socketoperator= (const base_socket &aFrom)
void close ()

Protected Attributes

std::ostream & m_out
bool m_verbose
int m_socket
bool m_is_LE

Detailed Description

Definition at line 138 of file base_socket.


Constructor & Destructor Documentation

inlib::net::base_socket::base_socket ( std::ostream &  a_out,
bool  a_verbose 
) [inline]

Definition at line 143 of file base_socket.

  :m_out(a_out)
  ,m_verbose(a_verbose)
  ,m_socket(-1)           
  ,m_is_LE(true)
  { 
    m_is_LE = inlib::is_little_endian();
  }
virtual inlib::net::base_socket::~base_socket ( ) [inline, virtual]

Definition at line 151 of file base_socket.

{close();}
inlib::net::base_socket::base_socket ( const base_socket aFrom) [inline, protected]

Definition at line 153 of file base_socket.

  :m_out(aFrom.m_out)
  ,m_verbose(aFrom.m_verbose)
  ,m_socket(aFrom.m_socket)           
  ,m_is_LE(aFrom.m_is_LE)
  {}

Member Function Documentation

void inlib::net::base_socket::close ( ) [inline, protected]

Definition at line 171 of file base_socket.

              {
    if(m_socket!=(-1)) {
#ifdef WIN32
      if(::shutdown(m_socket,SD_BOTH)<0) { 
        if(WSAGetLastError()==WSAENOTCONN) {
#else
      if(::shutdown(m_socket,SHUT_RDWR)<0) { 
        if(errno==ENOTCONN) { 
#endif
          // ok, the "other side" may have already done a shutdown.
        } else {
          m_out << "inlib::base_socket::close :"
                << " for socket : " << m_socket
                << ", shutdown : " << serror()
                << std::endl;
        }
      } else {
        if(m_verbose) {
          m_out << "inlib::base_socket::close :"
                << " close ok."
                << " Socket was " << m_socket << "."
                << std::endl;
        }
      }
      ::close(m_socket);
      m_socket = -1;
    }
  }
virtual void inlib::net::base_socket::disconnect ( ) [pure virtual]

Implemented in inlib::net::inet_socket.

template<class T >
bool inlib::net::base_socket::fetch ( T &  a_value) [inline]

Definition at line 496 of file base_socket.

                         {
    char buffer[sizeof(T)];
    if(!fetch_buffer(buffer,sizeof(T))) return false;
    inlib::read_buffer<T>(buffer,a_value);
    return true;
  }
bool inlib::net::base_socket::fetch_bool ( bool &  a_value) [inline]

Definition at line 521 of file base_socket.

                                 {
    unsigned char c;
    if(!fetch_uchar(c)) {a_value = false;return false;}
    a_value = (c==1?true:false);
    return true;
  }
bool inlib::net::base_socket::fetch_buffer ( char *  a_buffer,
inlib::uint64  a_length,
inlib::uint64  a_BLOCK = 65536 
) [inline]

Definition at line 234 of file base_socket.

                                                                                    {
    if(!a_length) return true;
    if(m_socket==(-1)) return false;
    char* buf = a_buffer;
    inlib::uint64 got = 0;
    while(true) {
      inlib::uint64 to_get = mn(a_BLOCK,a_length-got);
      if(!to_get) break;
#ifdef WIN32
      int
#else
      ssize_t
#endif
      num_char = ::recv(m_socket,buf,(size_t)to_get,0);
      if(num_char<0) {
        //ECONNRESET
        m_out << "inlib::base_socket::fetch_buffer :"
              << " recv : " << serror()
              << std::endl;
        disconnect();
        return false;
      }
      if(num_char==0) {
        m_out << "inlib::base_socket::fetch_buffer :"
              << " recv : returned 0."
              << std::endl;
        return false;      
      }

      //NOTE : num_char not necessary equal to to_get.
      buf += num_char;
      got += num_char;
    }
    return true;
  }
virtual bool inlib::net::base_socket::fetch_cbk ( inlib::uint64  ) [inline, virtual]

Reimplemented in inlib::net::ftp::xxx.

Definition at line 141 of file base_socket.

{return true;}
bool inlib::net::base_socket::fetch_file ( const std::string &  a_file) [inline]

Definition at line 588 of file base_socket.

                                           {
    inlib::uint64 length;
    if(!fetch<inlib::uint64>(length)) return false;
    char* buffer = 0;
    if(length) {
      buffer = new char[(size_t)length];
      if(!buffer) return false;
    }
    if(!fetch_buffer(buffer,length)) {
      delete [] buffer;
      return false;
    }
    bool stat = inlib::file::write_bytes(a_file,buffer,(size_t)length);
    if(!stat) {
      m_out << "inlib::base_socket::send_file : "
            << " can't write file \"" << a_file << "\"."
            << std::endl;
    }
    delete [] buffer;
    return stat;
  }
bool inlib::net::base_socket::fetch_string ( std::string &  a_string) [inline]

Definition at line 533 of file base_socket.

                                         {
    inlib::uint64 length;
    if(!fetch<inlib::uint64>(length)) {
      a_string.clear();
      return false;
    }
    a_string.resize((std::string::size_type)length);
    if(!fetch_buffer((char*)a_string.c_str(),length)) {
      a_string.clear();
      return false;
    }
    return true;
  }
bool inlib::net::base_socket::fetch_text ( std::vector< std::string > &  a_text) [inline]

Definition at line 556 of file base_socket.

                                               { 
    inlib::uint64 number;
    if(!fetch<inlib::uint64>(number)) {
      a_text.clear();
      return false;
    }
    for(unsigned int index=0;index<number;index++) {
      std::string s;
      if(!fetch_string(s)) {
        a_text.clear();
        return false;
      }
      a_text.push_back(s);
    }
    return true;
  }
bool inlib::net::base_socket::fetch_uchar ( unsigned char &  a_value) [inline]

Definition at line 509 of file base_socket.

                                           {
    char buffer[1];
    if(!fetch_buffer(buffer,1)) return false;
    ::memcpy(&a_value,buffer,1);
    return true;
  }
bool inlib::net::base_socket::fetch_upto_char ( char  a_char,
char *&  a_buffer,
inlib::uint64 a_length,
unsigned int  a_BLOCK = 65536 
) [inline]

Definition at line 270 of file base_socket.

                                {
    a_buffer = 0;
    a_length = 0;    
    if(m_socket==(-1)) return false;
    char* buf = new char[a_BLOCK];
    if(!buf) return false; 
    while(true) {
#ifdef WIN32
      int
#else
      ssize_t
#endif
      num_char = ::recv(m_socket,buf,(size_t)a_BLOCK,0);
      if(num_char<0) {
        m_out << "inlib::base_socket::fetch_upto_char :"
              << " recv : " << serror()
              << std::endl;
        disconnect();
        delete [] buf;    
        delete [] a_buffer;
        a_buffer = 0;
        a_length = 0;    
        return false;
      }
      if(num_char==0) {
        m_out << "inlib::base_socket::fetch_upto_char :"
              << " recv : returned 0."
              << std::endl;
        delete [] buf;    
        delete [] a_buffer;
        a_buffer = 0;
        a_length = 0;    
        return false;      
      }

      if(m_verbose) {
        m_out << "inlib::base_socket::fetch_upto_char :"
              << " recv : " << (int)num_char
             << std::endl;
      }

      if(!a_buffer) {
        a_buffer = new char[num_char];
        if(!a_buffer) {
          delete [] buf;    
          a_length = 0;    
          return false;
        }
        ::memcpy(a_buffer,buf,num_char);
        a_length = num_char;
      } else {
        char* b = new char[(size_t)(a_length+num_char)];
        if(!b) {
          delete [] buf;    
          delete [] a_buffer;
          a_buffer = 0;
          a_length = 0;    
          return false;
        }
        ::memcpy(b,a_buffer,(size_t)a_length);
        ::memcpy(b+a_length,buf,num_char);
        delete [] a_buffer;
        a_buffer = b;
        a_length += num_char;
      }

     {char* pos = buf;    
      for(unsigned int index=0;index<(unsigned int)num_char;index++,pos++) {
        //printf("debug : %d : %d\n",index,*pos);
        if(*pos==a_char) { //found
          // 01234
          //  ^
          a_length -= (num_char-(index+1));
          delete [] buf;
          return true;
        }
      }}
  
      //a_char not in buf, continue.
    }
  }
bool inlib::net::base_socket::fetch_upto_end ( char *&  a_buffer,
inlib::uint64 a_length,
unsigned int  a_BLOCK = 65536 
) [inline]

Definition at line 356 of file base_socket.

                                                                                         {
    a_buffer = 0;
    a_length = 0;    
    if(m_socket==(-1)) return false;

    char* buf = new char[a_BLOCK];
    if(!buf) return false; 
    while(true) {
      //m_out << "inlib::base_socket::fetch_upto_end :"
      //      << " recv... "
      //      << std::endl;
#ifdef WIN32
      int
#else
      ssize_t
#endif
      num_char = ::recv(m_socket,buf,(size_t)a_BLOCK,0);
      if(num_char<0) {
        //if(errno==EAGAIN) continue;
        m_out << "inlib::base_socket::fetch_upto_end :"
              << " recv : " << serror()
              << std::endl;
        disconnect();
        delete [] buf;
        delete [] a_buffer;
        a_buffer = 0;
        a_length = 0;    
        return false;
      }
      if(num_char==0) {
        if(m_verbose) {
          m_out << "inlib::base_socket::fetch_upto_end :"
                << " end."
                << std::endl;
        }
        delete [] buf;
        return true;
      }

      if(m_verbose) {
        m_out << "inlib::base_socket::fetch_upto_end :"
              << " recv : " << (int)num_char
              << std::endl;
      }
  
      if(!a_buffer) {
        a_buffer = new char[num_char];
        if(!a_buffer) {
          delete [] buf;    
          a_length = 0;    
          return false;
        }
        ::memcpy(a_buffer,buf,num_char);
        a_length = num_char;
      } else {
        char* b = new char[(size_t)(a_length+num_char)];
        if(!b) {
          delete [] buf;    
          delete [] a_buffer;
          a_buffer = 0;
          a_length = 0;    
          return false;
        }
        ::memcpy(b,a_buffer,(size_t)a_length);
        ::memcpy(b+a_length,buf,num_char);
        delete [] a_buffer;
        a_buffer = b;
        a_length += num_char;
      }
    }
  }
bool inlib::net::base_socket::fetch_upto_end ( FILE *  a_FILE,
inlib::uint64 a_length,
unsigned int  a_BLOCK = 65536 
) [inline]

Definition at line 428 of file base_socket.

                                                                                      {
    a_length = 0;    
    if(m_socket==(-1)) return false;

    char* buf = new char[a_BLOCK];
    if(!buf) return false; 
    while(true) {
      //m_out << "inlib::base_socket::fetch_upto_end(FILE) :"
      //      << " recv... "
      //      << std::endl;
#ifdef WIN32
      int
#else
      ssize_t
#endif
      num_char = ::recv(m_socket,buf,(size_t)a_BLOCK,0);
      if(num_char<0) {
        //if(errno==EAGAIN) continue;
        m_out << "inlib::base_socket::fetch_upto_end(FILE) :"
              << " recv : " << serror()
              << std::endl;
        disconnect();
        delete [] buf;
        a_length = 0;    
        return false;
      }
      if(num_char==0) {
        if(m_verbose) {
          m_out << "inlib::base_socket::fetch_upto_end(FILE) :"
                << " end."
                << std::endl;
        }
        delete [] buf;
        return true;
      }

      if(m_verbose) {
        m_out << "inlib::base_socket::fetch_upto_end(FILE) :"
              << " recv : " << (int)num_char
              << std::endl;
      }
  
      if(::fwrite((char*)buf,num_char,(size_t)1,a_FILE)!=1) {
        delete [] buf;    
        a_length = 0;    
        return false;
      }
      a_length += num_char;
      if(!fetch_cbk(a_length)) {
        m_out << "inlib::base_socket::fetch_upto_end(FILE) :"
              << " fetch_cbk returns false, stop recv data."
              << std::endl;
        return false;
      }
    }
  }
bool inlib::net::base_socket::is_connected ( ) const [inline]

Definition at line 167 of file base_socket.

{return (m_socket==(-1)?false:true);}
bool inlib::net::base_socket::is_there_input ( bool &  a_is) [inline]

Definition at line 610 of file base_socket.

                                  {
    a_is = false;
    if(m_socket==(-1)) return false;
  
    fd_set mask;
    FD_ZERO(&mask);
    FD_SET(m_socket,&mask);
  
    struct timeval timeout;
    timeout.tv_sec = 0;
    timeout.tv_usec = 10; //microsec
  
    int nfds = 0;
    nfds = mx(nfds,m_socket);
    nfds++;
    if(::select(nfds,&mask,0,0,&timeout)==(-1)) {
      a_is = false;
      return false;
    }
  
    a_is = FD_ISSET(m_socket,&mask)?true:false;
    return true;
  }
base_socket& inlib::net::base_socket::operator= ( const base_socket aFrom) [inline, protected]

Definition at line 159 of file base_socket.

                                                  { 
    m_verbose = aFrom.m_verbose;
    m_socket = aFrom.m_socket;
    m_is_LE = aFrom.m_is_LE;
    return *this;
  }
std::ostream& inlib::net::base_socket::out ( ) const [inline]

Definition at line 169 of file base_socket.

{return m_out;}
template<class T >
bool inlib::net::base_socket::send ( const T &  a_value) [inline]

Definition at line 489 of file base_socket.

                              {
    char buffer[sizeof(T)];
    inlib::write_buffer<T>(a_value,buffer);
    return send_buffer(buffer,sizeof(T));
  }
bool inlib::net::base_socket::send_bool ( bool  a_value) [inline]

Definition at line 516 of file base_socket.

                               {
    unsigned char c = a_value?1:0;
    return send_uchar(c);
  }
bool inlib::net::base_socket::send_buffer ( const char *  a_buffer,
inlib::uint64  a_length,
inlib::uint64  a_BLOCK = 65536 
) [inline]

Definition at line 200 of file base_socket.

                                                                                          {
    if(!a_length) return true;
    if(m_socket==(-1)) return false;
    char* buf = (char*)a_buffer;
    inlib::uint64 sent = 0;
    while(true) {
      inlib::uint64 to_send = mn(a_BLOCK,a_length-sent);
      if(!to_send) break;    
#ifdef WIN32
      int
#else
      ssize_t
#endif
      num_char = ::send(m_socket,(char*)buf,(size_t)to_send,0);
      if(num_char<0) {
        m_out << "inlib::base_socket::send_buffer :"
              << " send : " << serror()
              << std::endl;
        disconnect();
        return false;
      }
      if(num_char==0) {
        m_out << "inlib::base_socket::send_buffer :"
              << " send : returned 0."
              << std::endl;
      }
  
      //NOTE : num_char not necessary equal to to_send.
      buf += num_char;
      sent += num_char;
    }  
    return true;
  }
bool inlib::net::base_socket::send_file ( const std::string &  a_file) [inline]

Definition at line 573 of file base_socket.

                                          {
    char* buffer;
    long length;
    if(!inlib::file::read_bytes(a_file,buffer,length)) {
      m_out << "inlib::base_socket::send_file : "
            << " can't read file \"" << a_file << "\"."
            << std::endl;
      return false;
    }
    if(!send<inlib::uint64>((inlib::uint64)length)) return false;
    bool stat = send_buffer(buffer,length);
    delete [] buffer;
    return stat;
  }
bool inlib::net::base_socket::send_string ( const std::string &  a_string) [inline]

Definition at line 528 of file base_socket.

                                              {
    if(!send<inlib::uint64>(a_string.size())) return false;
    return send_buffer(a_string.c_str(),a_string.size());
  }
bool inlib::net::base_socket::send_text ( const std::vector< std::string > &  a_text) [inline]

Definition at line 547 of file base_socket.

                                                     { 
    unsigned int number = a_text.size();
    if(!send<inlib::uint64>(number)) return false;
    for(unsigned int index=0;index<number;index++) {
      if(!send_string(a_text[index])) return false;
    }
    return true;
  }
bool inlib::net::base_socket::send_uchar ( unsigned char  a_value) [inline]

Definition at line 503 of file base_socket.

                                         {
    char buffer[1];
    ::memcpy(buffer,&a_value,1);
    return send_buffer(buffer,1);
  }
int inlib::net::base_socket::socket ( ) const [inline]

Definition at line 168 of file base_socket.

{return m_socket;}
bool inlib::net::base_socket::verbose ( ) const [inline]

Definition at line 166 of file base_socket.

{return m_verbose;}

Member Data Documentation

Definition at line 638 of file base_socket.

std::ostream& inlib::net::base_socket::m_out [protected]

Definition at line 635 of file base_socket.

Definition at line 637 of file base_socket.

Definition at line 636 of file base_socket.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines