crossroads

Git mirror of https://crossroads.e-tunity.com/
git clone git://git.finwo.net/app/crossroads
Log | Files | Refs | LICENSE

netbuffer (1724B)


      1 #ifndef _NETBUFFER_
      2 #define _NETBUFFER_
      3 
      4 #include "sys/sys"
      5 #include "memory/memory"
      6 
      7 #include "error/error"
      8 #include "config/config"
      9 #include "profiler/profiler"
     10 #include "fdset/fdset"
     11 #include "servertype/servertype"
     12 
     13 /* A few defs when malloc() / realloc() are suspected to be not thread-safe.
     14  * The defines are used in eg. copy() and check_space(). */
     15 #ifdef MISTRUST_MALLOC_THREADSAFE
     16 #define LOCK_MALLOC  	mutex_lock((void*)malloc)
     17 #define UNLOCK_MALLOC  	mutex_unlock((void*)malloc)
     18 #else
     19 #define LOCK_MALLOC
     20 #define UNLOCK_MALLOC
     21 #endif
     22 
     23 class Netbuffer MEM(: public Memory) {
     24 public:
     25     Netbuffer();
     26     Netbuffer (Netbuffer const &other);
     27     Netbuffer (string const &s);
     28     virtual ~Netbuffer();
     29     Netbuffer const &operator= (Netbuffer const &other);
     30 
     31     char charat(unsigned index) const;
     32     char operator[] (unsigned index) 	{ return charat(index); }
     33 
     34     char const *bufdata() const 	{ return buf_data; }
     35     unsigned bufsz() const 		{ return buf_sz; }
     36 
     37     unsigned netread (Socket &s, unsigned timeout = 0);
     38     unsigned netwrite (Socket &s, unsigned timeout) const;
     39 
     40     unsigned strfind (char const *s) const;
     41     unsigned charfind (char ch, unsigned start = 0) const;
     42 
     43     bool setchar(unsigned offset, char ch);
     44     void setstring(string const &s);
     45 
     46     string stringat(unsigned index, unsigned len);
     47 
     48     bool insertat(unsigned index, char const *s, unsigned len = 0);
     49     bool removeat(unsigned index, unsigned len = 1);
     50 
     51     void reset();
     52 
     53 
     54 private:
     55     void copy (Netbuffer const &other);
     56     void destroy();
     57 
     58     void check_space(unsigned extra);
     59     string printable(char c) const;
     60     string printable() const;
     61 
     62     char *buf_data;
     63     unsigned buf_sz;
     64     unsigned buf_alloced;
     65 };
     66 
     67 #endif