~ubuntu-branches/debian/experimental/libtorrent/experimental

« back to all changes in this revision

Viewing changes to src/net/protocol_buffer.h

  • Committer: Bazaar Package Importer
  • Author(s): Jose Luis Rivas
  • Date: 2007-03-31 10:31:05 UTC
  • mto: (4.1.4 gutsy) (6.2.1 squeeze) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 6.
  • Revision ID: james.westby@ubuntu.com-20070331103105-jzpp1rml6ud0ff75
Tags: upstream-0.11.4
ImportĀ upstreamĀ versionĀ 0.11.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
#ifndef LIBTORRENT_NET_PROTOCOL_BUFFER_H
38
38
#define LIBTORRENT_NET_PROTOCOL_BUFFER_H
39
39
 
 
40
#include <memory>
40
41
#include <inttypes.h>
41
42
#include <netinet/in.h>
42
43
 
54
55
 
55
56
  void                reset()                       { m_position = m_end = begin(); }
56
57
  void                reset_position()              { m_position = m_buffer; }
57
 
  void                move_position(difference_type v) { m_position += v; }
 
58
  bool                consume(difference_type v);
58
59
 
59
60
  void                set_position_itr(iterator itr) { m_position = itr; }
60
61
 
61
 
  void                prepare_end()                 { m_end = m_position; reset_position(); }
62
62
  void                set_end(size_type v)          { m_end = m_buffer + v; }
63
 
  void                move_end(difference_type v)   { m_end += v; }
 
63
  void                set_end_itr(iterator itr)     { m_end = itr; }
 
64
  difference_type     move_end(difference_type v)   { m_end += v; return v; }
64
65
 
65
66
  uint8_t             read_8()                      { return *m_position++; }
66
67
  uint8_t             peek_8()                      { return *m_position; }
75
76
  template <typename Out>
76
77
  void                read_range(Out first, Out last);
77
78
 
 
79
  template <typename Out>
 
80
  void                read_len(Out start, unsigned int len);
 
81
 
78
82
  template <typename T>
79
83
  inline T            read_int();
80
84
 
81
85
  template <typename T>
82
86
  inline T            peek_int();
83
87
 
84
 
  void                write_8(uint8_t v)            { *m_position++ = v; validate_position(); }
 
88
  void                write_8(uint8_t v)            { *m_end++ = v; validate_end(); }
85
89
  void                write_16(uint16_t v);
86
90
  void                write_32(uint32_t v);
87
91
  void                write_32_n(uint32_t v);
93
97
  template <typename In>
94
98
  void                write_range(In first, In last);
95
99
 
 
100
  template <typename In>
 
101
  void                write_len(In start, unsigned int len);
 
102
 
96
103
  iterator            begin()                       { return m_buffer; }
97
104
  iterator            position()                    { return m_position; }
98
105
  iterator            end()                         { return m_end; }
101
108
  size_type           size_end() const              { return m_end - m_buffer; }
102
109
  size_type           remaining() const             { return m_end - m_position; }
103
110
  size_type           reserved() const              { return tmpl_size; }
104
 
  size_type           reserved_left() const         { return reserved() - size_position(); }
 
111
  size_type           reserved_left() const         { return reserved() - size_end(); }
 
112
 
 
113
  void                move_unused();
105
114
 
106
115
  void                validate_position() const {
107
116
#ifdef USE_EXTRA_DEBUG
108
117
    if (m_position > m_buffer + tmpl_size)
 
118
      throw internal_error("ProtocolBuffer tried to read beyond scope of the buffer.");
 
119
    if (m_position > m_end)
 
120
      throw internal_error("ProtocolBuffer tried to read beyond end of the buffer.");
 
121
#endif
 
122
  }
 
123
 
 
124
  void                validate_end() const {
 
125
#ifdef USE_EXTRA_DEBUG
 
126
    if (m_end > m_buffer + tmpl_size)
109
127
      throw internal_error("ProtocolBuffer tried to write beyond scope of the buffer.");
110
128
#endif
111
129
  }
117
135
};
118
136
 
119
137
template <uint16_t tmpl_size>
 
138
inline bool
 
139
ProtocolBuffer<tmpl_size>::consume(difference_type v) {
 
140
  m_position += v;
 
141
 
 
142
  if (remaining())
 
143
    return false;
 
144
 
 
145
  return true; 
 
146
}
 
147
 
 
148
template <uint16_t tmpl_size>
120
149
inline uint16_t
121
150
ProtocolBuffer<tmpl_size>::read_16() {
122
151
#ifndef USE_ALIGNED
180
209
inline void
181
210
ProtocolBuffer<tmpl_size>::write_16(uint16_t v) {
182
211
#ifndef USE_ALIGNED
183
 
  *reinterpret_cast<uint16_t*>(m_position) = htons(v);
184
 
  m_position += sizeof(uint16_t);
 
212
  *reinterpret_cast<uint16_t*>(m_end) = htons(v);
 
213
  m_end += sizeof(uint16_t);
185
214
 
186
 
  validate_position();
 
215
  validate_end();
187
216
#else
188
217
  write_int<uint16_t>(v);
189
218
#endif
193
222
inline void
194
223
ProtocolBuffer<tmpl_size>::write_32(uint32_t v) {
195
224
#ifndef USE_ALIGNED
196
 
  *reinterpret_cast<uint32_t*>(m_position) = htonl(v);
197
 
  m_position += sizeof(uint32_t);
 
225
  *reinterpret_cast<uint32_t*>(m_end) = htonl(v);
 
226
  m_end += sizeof(uint32_t);
198
227
 
199
 
  validate_position();
 
228
  validate_end();
200
229
#else
201
230
  write_int<uint32_t>(v);
202
231
#endif
205
234
template <uint16_t tmpl_size>
206
235
inline void
207
236
ProtocolBuffer<tmpl_size>::write_32_n(uint32_t v) {
208
 
  *reinterpret_cast<uint32_t*>(m_position) = v;
209
 
  m_position += sizeof(uint32_t);
 
237
  *reinterpret_cast<uint32_t*>(m_end) = v;
 
238
  m_end += sizeof(uint32_t);
210
239
 
211
 
  validate_position();
 
240
  validate_end();
212
241
}
213
242
 
214
243
template <uint16_t tmpl_size>
222
251
}
223
252
 
224
253
template <uint16_t tmpl_size>
 
254
template <typename Out>
 
255
void
 
256
ProtocolBuffer<tmpl_size>::read_len(Out start, unsigned int len) {
 
257
  for ( ; len > 0; ++m_position, ++start, --len)
 
258
    *start = *m_position;
 
259
 
 
260
  validate_position();
 
261
}
 
262
 
 
263
template <uint16_t tmpl_size>
225
264
template <typename T>
226
265
inline void
227
266
ProtocolBuffer<tmpl_size>::write_int(T v) {
228
 
  for (iterator itr = m_position + sizeof(T); itr != m_position; v >>= 8)
 
267
  for (iterator itr = m_end + sizeof(T); itr != m_end; v >>= 8)
229
268
    *(--itr) = v;
230
269
 
231
 
  m_position += sizeof(T);
232
 
  validate_position();
 
270
  m_end += sizeof(T);
 
271
  validate_end();
233
272
}
234
273
 
235
274
template <uint16_t tmpl_size>
236
275
template <typename In>
237
276
void
238
277
ProtocolBuffer<tmpl_size>::write_range(In first, In last) {
239
 
  for ( ; first != last; ++m_position, ++first)
240
 
    *m_position = *first;
241
 
 
242
 
  validate_position();
 
278
  for ( ; first != last; ++m_end, ++first)
 
279
    *m_end = *first;
 
280
 
 
281
  validate_end();
 
282
}
 
283
 
 
284
template <uint16_t tmpl_size>
 
285
template <typename In>
 
286
void
 
287
ProtocolBuffer<tmpl_size>::write_len(In start, unsigned int len) {
 
288
  for ( ; len > 0; ++m_end, ++start, --len)
 
289
    *m_end = *start;
 
290
 
 
291
  validate_end();
 
292
}
 
293
 
 
294
template <uint16_t tmpl_size>
 
295
void
 
296
ProtocolBuffer<tmpl_size>::move_unused() {
 
297
  std::memmove(begin(), position(), remaining());
 
298
 
 
299
  set_end(remaining());
 
300
  reset_position();
243
301
}
244
302
 
245
303
}