Go to the documentation of this file.00001
00002
00003
00004
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
00031
00032
00033
00034
00035 #include "message-buffer.h"
00036
00037
00038 namespace netlib {
00039
00040 typedef std::vector<char> buffer_t;
00041
00042
00043
00044 MessageBuffer::~MessageBuffer(void) throw() { }
00045
00046
00047
00048
00049
00050
00051
00052
00053 class Buffer : public MessageBuffer {
00054 public:
00055 Buffer(void) throw() : m_closed(false) { }
00056 ~Buffer(void) throw() { }
00057
00058
00059 void initialize(void) throw() { }
00060
00061
00062 long getBytes(void) const throw();
00063 const char * getData(void) const throw();
00064 bool isClosed(void) const throw() { return m_closed; }
00065 void reserve(IN long bytes);
00066 void appendData(IN const char * data, IN long bytes);
00067 void appendToken(IN const char * token);
00068 void close(void);
00069
00070 private:
00071
00072 buffer_t m_buffer;
00073 bool m_closed;
00074 };
00075
00076
00077
00078 long
00079 Buffer::getBytes
00080 (
00081 void
00082 )
00083 const
00084 throw()
00085 {
00086 ASSERT(m_closed, "read operation not allowed: message buffer not closed!");
00087 long bytes = m_buffer.size();
00088 ASSERT(bytes > 0, "should be at least 1, even for empty message!");
00089 return bytes - 1;
00090 }
00091
00092
00093
00094 const char *
00095 Buffer::getData
00096 (
00097 void
00098 )
00099 const
00100 throw()
00101 {
00102 ASSERT(m_closed, "read operation not allowed: message buffer not closed!");
00103 return &m_buffer[0];
00104 }
00105
00106
00107
00108 void
00109 Buffer::reserve
00110 (
00111 IN long bytes
00112 )
00113 {
00114 ASSERT(!m_closed, "write operation not allowed: message buffer is closed!");
00115 m_buffer.reserve(bytes + 1);
00116 }
00117
00118
00119
00120 void
00121 Buffer::appendData
00122 (
00123 IN const char * data,
00124 IN long bytes
00125 )
00126 {
00127
00128
00129 ASSERT(!m_closed, "write operation not allowed: message buffer is closed!");
00130 ASSERT(data, "null");
00131 ASSERT(bytes > 0, "bad byte count: %ld", bytes);
00132
00133 long curr = m_buffer.size();
00134 long required = curr + bytes;
00135 m_buffer.resize(required);
00136 char * dst = &m_buffer[0] + curr;
00137 memcpy(dst, data, bytes);
00138 }
00139
00140
00141
00142 void
00143 Buffer::close
00144 (
00145 void
00146 )
00147 {
00148 ASSERT(!m_closed, "message buffer is already closed!");
00149
00150 m_closed = true;
00151 m_buffer.push_back(0);
00152 }
00153
00154
00155
00156 void
00157 Buffer::appendToken
00158 (
00159 IN const char * token
00160 )
00161 {
00162 ASSERT(token, "null");
00163 int bytes = strlen(token);
00164 this->appendData(token, bytes);
00165 }
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 smart_ptr<MessageBuffer>
00176 MessageBuffer::create
00177 (
00178 void
00179 )
00180 {
00181 smart_ptr<Buffer> local = new Buffer;
00182 ASSERT(local, "out of memory");
00183
00184 local->initialize();
00185
00186 return local;
00187 }
00188
00189
00190
00191 };
00192