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) {
67 massert(16141,
str::stream() <<
"cannot translate opcode " << op, !op);
72 inline bool opIsWrite(
int op) {
98 int32_t messageLength;
110 ConstView(
const char* data) : _data(data) {}
112 const char* view2ptr()
const {
113 return data().view();
116 int32_t getMessageLength()
const {
117 return data().readLE<int32_t>(offsetof(
Layout, messageLength));
120 int32_t getRequestID()
const {
121 return data().readLE<int32_t>(offsetof(
Layout, requestID));
124 int32_t getResponseTo()
const {
125 return data().readLE<int32_t>(offsetof(
Layout, responseTo));
128 int32_t getOpCode()
const {
129 return data().readLE<int32_t>(offsetof(
Layout, opCode));
133 const view_type& data()
const {
147 using ConstView::view2ptr;
149 return data().view();
152 void setMessageLength(int32_t value) {
153 data().writeLE(value, offsetof(
Layout, messageLength));
156 void setRequestID(int32_t value) {
157 data().writeLE(value, offsetof(
Layout, requestID));
160 void setResponseTo(int32_t value) {
161 data().writeLE(value, offsetof(
Layout, responseTo));
164 void setOpCode(int32_t value) {
165 data().writeLE(value, offsetof(
Layout, opCode));
169 view_type data()
const {
170 return const_cast<char*
>(ConstView::view2ptr());
177 BOOST_STATIC_ASSERT(
sizeof(
Value) ==
sizeof(
Layout));
195 ConstView(
const char* storage) : _storage(storage) {}
197 const char* view2ptr()
const {
198 return storage().view();
201 int32_t getLen()
const {
202 return header().getMessageLength();
205 MSGID getId()
const {
206 return header().getRequestID();
209 MSGID getResponseTo()
const {
210 return header().getResponseTo();
213 int32_t getOperation()
const {
214 return header().getOpCode();
217 const char* data()
const {
218 return storage().view(offsetof(
Layout, data));
222 if (getLen() <= 0 || getLen() > (4 * BSONObjMaxInternalSize))
224 if (getOperation() < 0 || getOperation() > 30000)
229 int64_t getCursor()
const {
230 verify(getResponseTo() > 0);
231 verify(getOperation() == opReply);
232 return ConstDataView(data() +
sizeof(int32_t)).readLE<int64_t>();
243 return storage().view(offsetof(
Layout, header));
254 using ConstView::view2ptr;
256 return storage().view();
259 void setLen(
int value) {
260 return header().setMessageLength(value);
263 void setId(MSGID value) {
264 return header().setRequestID(value);
267 void setResponseTo(MSGID value) {
268 return header().setResponseTo(value);
271 void setOperation(
int value) {
272 return header().setOpCode(value);
275 using ConstView::data;
277 return storage().view(offsetof(
Layout, data));
282 return const_cast<char*
>(ConstView::view2ptr());
286 return storage().view(offsetof(
Layout, header));
293 BOOST_STATIC_ASSERT(
sizeof(
Value) ==
sizeof(
Layout));
299 const int MsgDataHeaderSize =
sizeof(
Value) - 4;
300 inline int ConstView::dataLen()
const {
301 return getLen() - MsgDataHeaderSize;
309 Message() : _buf(0), _data(0), _freeIt(
false) {}
310 Message(
void* data,
bool freeIt) : _buf(0), _data(0), _freeIt(
false) {
311 _setData(reinterpret_cast<char*>(data), freeIt);
324 return _buf ? _buf : _data[0].first;
327 int operation()
const {
328 return header().getOperation();
332 massert(13273,
"single data buffer expected", _buf);
337 return !_buf && _data.empty();
345 for (MsgVec::const_iterator it = _data.begin(); it != _data.end(); ++it) {
352 int dataSize()
const {
359 if (_buf || empty()) {
365 for (std::vector<std::pair<char*, int> >::const_iterator i = _data.begin();
368 totalSize += i->second;
370 char* buf = (
char*)malloc(totalSize);
372 for (std::vector<std::pair<char*, int> >::const_iterator i = _data.begin();
375 memcpy(p, i->first, i->second);
388 if (r._data.size() > 0) {
401 for (std::vector<std::pair<char*, int> >::const_iterator i = _data.begin();
414 void appendData(
char* d,
int size) {
421 _setData(md.view2ptr(),
true);
429 _data.push_back(std::make_pair(d, size));
430 header().setLen(header().getLen() + size);
434 void setData(
char* d,
bool freeIt) {
438 void setData(
int operation,
const char* msgtxt) {
439 setData(operation, msgtxt, strlen(msgtxt) + 1);
441 void setData(
int operation,
const char* msgdata,
size_t len) {
445 memcpy(d.data(), msgdata, len);
447 d.setOperation(operation);
448 _setData(d.view2ptr(),
true);
457 std::string toString()
const;
460 void _setData(
char* d,
bool freeIt) {
468 typedef std::vector<std::pair<char*, int> > MsgVec;
474 MSGID nextMessageId();
Definition: message.h:305
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
the idea here is to make one liners easy.
Definition: str.h:44
Definition: message.h:193
Definition: data_view.h:30
Definition: message.h:290
wrapped around os representation of network address
Definition: sock.h:96
Definition: encoded_value_storage.h:24
Definition: message.h:250
const size_t MaxMessageSizeBytes
Maximum accepted message size on the wire protocol.
Definition: message.h:36
Definition: message.h:187
Definition: encoded_value_storage.h:31
Definition: data_view.h:71
Definition: message_port.h:74