22#include "mongo/platform/atomic_word.h"
23#include "mongo/platform/cstdint.h"
24#include "mongo/base/data_view.h"
25#include "mongo/base/encoded_value_storage.h"
26#include "mongo/util/mongoutils/str.h"
27#include "mongo/util/net/hostandport.h"
28#include "mongo/util/net/operation.h"
29#include "mongo/util/net/sock.h"
42 typedef uint32_t MSGID;
44 bool doesOpGetAResponse(
int op );
46 inline const char * opToString(
int op ) {
48 case 0:
return "none";
49 case opReply:
return "reply";
50 case dbMsg:
return "msg";
51 case dbUpdate:
return "update";
52 case dbInsert:
return "insert";
53 case dbQuery:
return "query";
54 case dbGetMore:
return "getmore";
55 case dbDelete:
return "remove";
56 case dbKillCursors:
return "killcursors";
58 massert( 16141,
str::stream() <<
"cannot translate opcode " << op, !op );
63 inline bool opIsWrite(
int op ) {
91 int32_t messageLength;
101 typedef ConstDataView view_type;
103 ConstView(
const char* data) : _data(data) { }
105 const char* view2ptr()
const {
106 return data().view();
109 int32_t getMessageLength()
const {
110 return data().readLE<int32_t>(offsetof(
Layout, messageLength));
113 int32_t getRequestID()
const {
114 return data().readLE<int32_t>(offsetof(
Layout, requestID));
117 int32_t getResponseTo()
const {
118 return data().readLE<int32_t>(offsetof(
Layout, responseTo));
121 int32_t getOpCode()
const {
122 return data().readLE<int32_t>(offsetof(
Layout, opCode));
126 const view_type& data()
const {
136 typedef DataView view_type;
140 using ConstView::view2ptr;
142 return data().view();
145 void setMessageLength(int32_t value) {
146 data().writeLE(value, offsetof(
Layout, messageLength));
149 void setRequestID(int32_t value) {
150 data().writeLE(value, offsetof(
Layout, requestID));
153 void setResponseTo(int32_t value) {
154 data().writeLE(value, offsetof(
Layout, responseTo));
157 void setOpCode(int32_t value) {
158 data().writeLE(value, offsetof(
Layout, opCode));
162 view_type data()
const {
163 return const_cast<char *
>(ConstView::view2ptr());
167 class Value :
public EncodedValueStorage<Layout, ConstView, View> {
170 BOOST_STATIC_ASSERT(
sizeof(
Value) ==
sizeof(
Layout));
173 Value(ZeroInitTag_t zit) : EncodedValueStorage<Layout, ConstView, View>(zit) {}
188 ConstView(
const char* storage) : _storage(storage) { }
190 const char* view2ptr()
const {
191 return storage().view();
194 int32_t getLen()
const {
195 return header().getMessageLength();
198 MSGID getId()
const {
199 return header().getRequestID();
202 MSGID getResponseTo()
const {
203 return header().getResponseTo();
206 int32_t getOperation()
const {
207 return header().getOpCode();
210 const char* data()
const {
211 return storage().view(offsetof(
Layout, data));
215 if ( getLen() <= 0 || getLen() > ( 4 * BSONObjMaxInternalSize ) )
217 if ( getOperation() < 0 || getOperation() > 30000 )
222 int64_t getCursor()
const {
223 verify( getResponseTo() > 0 );
224 verify( getOperation() == opReply );
225 return ConstDataView(data() +
sizeof(int32_t)).readLE<int64_t>();
231 const ConstDataView& storage()
const {
236 return storage().view(offsetof(
Layout, header));
240 ConstDataView _storage;
247 using ConstView::view2ptr;
249 return storage().view();
252 void setLen(
int value) {
253 return header().setMessageLength(value);
256 void setId(MSGID value) {
257 return header().setRequestID(value);
260 void setResponseTo(MSGID value) {
261 return header().setResponseTo(value);
264 void setOperation(
int value) {
265 return header().setOpCode(value);
268 using ConstView::data;
270 return storage().view(offsetof(
Layout, data));
274 DataView storage()
const {
275 return const_cast<char *
>(ConstView::view2ptr());
279 return storage().view(offsetof(
Layout, header));
283 class Value :
public EncodedValueStorage<Layout, ConstView, View> {
286 BOOST_STATIC_ASSERT(
sizeof(
Value) ==
sizeof(
Layout));
289 Value(ZeroInitTag_t zit) : EncodedValueStorage<Layout, ConstView, View>(zit) {}
292 const int MsgDataHeaderSize =
sizeof(
Value) - 4;
293 inline int ConstView::dataLen()
const {
294 return getLen() - MsgDataHeaderSize;
301 Message() : _buf( 0 ), _data( 0 ), _freeIt(
false ) {}
302 Message(
void * data ,
bool freeIt ) :
303 _buf( 0 ), _data( 0 ), _freeIt(
false ) {
304 _setData(
reinterpret_cast< char*
>( data ), freeIt );
317 return _buf ? _buf : _data[ 0 ].first;
320 int operation()
const {
return header().getOperation(); }
323 massert( 13273,
"single data buffer expected", _buf );
327 bool empty()
const {
return !_buf && _data.empty(); }
335 for (MsgVec::const_iterator it = _data.begin(); it != _data.end(); ++it) {
347 if ( _buf || empty() ) {
353 for (std::vector< std::pair< char *, int > >::const_iterator i = _data.begin();
354 i != _data.end(); ++i) {
355 totalSize += i->second;
357 char *buf = (
char*)malloc( totalSize );
359 for (std::vector< std::pair< char *, int > >::const_iterator i = _data.begin();
360 i != _data.end(); ++i) {
361 memcpy( p, i->first, i->second );
365 _setData( buf,
true );
374 if ( r._data.size() > 0 ) {
375 _data.swap( r._data );
387 for (std::vector< std::pair< char *, int > >::const_iterator i = _data.begin();
388 i != _data.end(); ++i) {
399 void appendData(
char *d,
int size) {
406 _setData( md.view2ptr(),
true );
414 _data.push_back(std::make_pair(d, size));
415 header().setLen(header().getLen() + size);
419 void setData(
char* d,
bool freeIt) {
421 _setData( d, freeIt );
423 void setData(
int operation,
const char *msgtxt) {
424 setData(operation, msgtxt, strlen(msgtxt)+1);
426 void setData(
int operation,
const char *msgdata,
size_t len) {
430 memcpy(d.data(), msgdata, len);
432 d.setOperation(operation);
433 _setData( d.view2ptr(),
true );
442 std::string toString()
const;
445 void _setData(
char* d,
bool freeIt ) {
452 typedef std::vector< std::pair< char*, int > > MsgVec;
458 MSGID nextMessageId();
Definition message_port.h:68
the idea here is to make one liners easy.
Definition str.h:44
the main MongoDB namespace
Definition bulk_operation_builder.h:24
const size_t MaxMessageSizeBytes
Maximum accepted message size on the wire protocol.
Definition message.h:36
wrapped around os representation of network address
Definition sock.h:94