Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 :
6 : //
7 : // sandesh_session.cc
8 : //
9 : // Sandesh session
10 : //
11 :
12 : #include <boost/bind/bind.hpp>
13 : #include <boost/assign.hpp>
14 : #include <boost/algorithm/string.hpp>
15 :
16 : #include <base/parse_object.h>
17 :
18 : #include <sandesh/common/vns_types.h>
19 : #include <sandesh/common/vns_constants.h>
20 : #include <sandesh/transport/TBufferTransports.h>
21 : #include <sandesh/protocol/TXMLProtocol.h>
22 : #include <sandesh/protocol/TJSONProtocol.h>
23 : #include "sandesh/sandesh_types.h"
24 : #include "sandesh/sandesh.h"
25 :
26 : #include "sandesh_connection.h"
27 : #include "sandesh_session.h"
28 :
29 :
30 : using namespace std;
31 : using namespace contrail::sandesh::protocol;
32 : using namespace contrail::sandesh::transport;
33 : using namespace boost::placeholders;
34 :
35 : using boost::asio::mutable_buffer;
36 : using boost::asio::buffer_cast;
37 :
38 : const std::string SandeshWriter::sandesh_open_ = sXML_SANDESH_OPEN;
39 : const std::string SandeshWriter::sandesh_open_attr_length_ =
40 : sXML_SANDESH_OPEN_ATTR_LENGTH;
41 : const std::string SandeshWriter::sandesh_close_ = sXML_SANDESH_CLOSE;
42 :
43 : //
44 : // SandeshWriter
45 : //
46 71 : SandeshWriter::SandeshWriter(SandeshSession *session)
47 71 : : session_(session),
48 71 : ready_to_send_(true),
49 71 : send_buf_(new uint8_t[kDefaultSendSize]),
50 71 : send_buf_offset_(0) {
51 71 : }
52 :
53 71 : SandeshWriter::~SandeshWriter() {
54 71 : delete [] send_buf_;
55 71 : }
56 :
57 0 : void SandeshWriter::WriteReady(const boost::system::error_code &ec) {
58 0 : if (ec) {
59 0 : SANDESH_LOG(ERROR, "SandeshSession Write error value: " << ec.value()
60 : << " category: " << ec.category().name()
61 : << " message: " << ec.message());
62 0 : session_->increment_write_ready_cb_error();
63 0 : return;
64 : }
65 :
66 : {
67 0 : std::scoped_lock lock(send_mutex_);
68 0 : ready_to_send_ = true;
69 0 : }
70 :
71 : // We may want to start the Runner for the send_queue
72 0 : session_->send_queue()->MayBeStartRunner();
73 : }
74 :
75 500 : void SandeshWriter::SendMsg(Sandesh *sandesh, bool more) {
76 500 : SandeshHeader header;
77 500 : std::stringstream ss;
78 : uint8_t *buffer;
79 500 : int32_t xfer = 0, ret;
80 : uint32_t offset;
81 : boost::shared_ptr<TMemoryBuffer> btrans(
82 500 : new TMemoryBuffer(kEncodeBufferSize));
83 : boost::shared_ptr<TXMLProtocol> prot(
84 500 : new TXMLProtocol(btrans));
85 : // Populate the header
86 500 : header.set_Namespace(sandesh->scope());
87 500 : header.set_Timestamp(sandesh->timestamp());
88 500 : header.set_Module(sandesh->module());
89 500 : header.set_Source(sandesh->source());
90 500 : header.set_Context(sandesh->context());
91 500 : header.set_SequenceNum(sandesh->seqnum());
92 500 : header.set_VersionSig(sandesh->versionsig());
93 500 : header.set_Type(sandesh->type());
94 500 : header.set_Hints(sandesh->hints());
95 500 : header.set_Level(sandesh->level());
96 500 : header.set_Category(sandesh->category());
97 500 : header.set_NodeType(sandesh->node_type());
98 500 : header.set_InstanceId(sandesh->instance_id());
99 :
100 : // Write the sandesh open envelope.
101 500 : buffer = btrans->getWritePtr(sandesh_open_.length());
102 500 : memcpy(buffer, sandesh_open_.c_str(), sandesh_open_.length());
103 500 : btrans->wroteBytes(sandesh_open_.length());
104 : // Write the sandesh header
105 500 : if ((ret = header.write(prot)) < 0) {
106 0 : SANDESH_LOG(ERROR, __func__ << ": Sandesh header write FAILED: " <<
107 : sandesh->Name() << " : " << sandesh->source() << ":" <<
108 : sandesh->module() << ":" << sandesh->instance_id() <<
109 : " Sequence Number:" << sandesh->seqnum());
110 0 : session_->increment_send_msg_fail();
111 0 : Sandesh::UpdateTxMsgFailStats(sandesh->Name(), 0,
112 : SandeshTxDropReason::HeaderWriteFailed);
113 0 : sandesh->Release();
114 0 : return;
115 : }
116 500 : xfer += ret;
117 : // Write the sandesh
118 500 : if ((ret = sandesh->Write(prot)) < 0) {
119 0 : SANDESH_LOG(ERROR, __func__ << ": Sandesh write FAILED: "<<
120 : sandesh->Name() << " : " << sandesh->source() << ":" <<
121 : sandesh->module() << ":" << sandesh->instance_id() <<
122 : " Sequence Number:" << sandesh->seqnum());
123 0 : session_->increment_send_msg_fail();
124 0 : Sandesh::UpdateTxMsgFailStats(sandesh->Name(), 0,
125 : SandeshTxDropReason::WriteFailed);
126 0 : sandesh->Release();
127 0 : return;
128 : }
129 500 : xfer += ret;
130 : // Write the sandesh close envelope
131 500 : buffer = btrans->getWritePtr(sandesh_close_.length());
132 500 : memcpy(buffer, sandesh_close_.c_str(), sandesh_close_.length());
133 500 : btrans->wroteBytes(sandesh_close_.length());
134 : // Get the buffer
135 500 : btrans->getBuffer(&buffer, &offset);
136 : // Sanity
137 500 : assert(sandesh_open_.length() + xfer + sandesh_close_.length() ==
138 : offset);
139 : // Update the sandesh open envelope length;
140 500 : char prev = ss.fill('0');
141 : // Adjust for '">'
142 500 : ss.width(sandesh_open_.length() - sandesh_open_attr_length_.length() - 2);
143 500 : ss << offset;
144 500 : ss.fill(prev);
145 500 : memcpy(buffer + sandesh_open_attr_length_.length(), ss.str().c_str(),
146 1000 : ss.str().length());
147 :
148 : // Update sandesh stats
149 500 : Sandesh::UpdateTxMsgStats(sandesh->Name(), offset);
150 500 : session_->increment_send_msg();
151 :
152 500 : if (send_buf()) {
153 500 : if (more) {
154 : // There are more messages in the send_queue_.
155 : // Try to package as many sandesh messages as possible
156 : // (== kEncodeBufferSize) before transporting to the
157 : // receiver.
158 214 : SendMsgMore(btrans);
159 : } else {
160 : // send_queue_ is empty. Flush sandesh->send_buf_ and this message.
161 286 : SendMsgAll(btrans);
162 : }
163 : } else {
164 : // Send the message
165 0 : SendInternal(btrans);
166 : }
167 500 : sandesh->Release();
168 500 : }
169 :
170 : // Package as many sandesh messages as possible [not more than
171 : // kEncodeBufferSize] before transporting it to the receiver.
172 : //
173 : // send_buf_ => unsent data (partial/complete message).
174 : // buf => new message
175 : // buf_len => buf's len
176 214 : void SandeshWriter::SendMsgMore(boost::shared_ptr<TMemoryBuffer>
177 : send_buffer) {
178 : uint8_t *buf;
179 : uint32_t buf_len;
180 :
181 214 : send_buffer->getBuffer(&buf, &buf_len);
182 :
183 214 : if (send_buf_offset()) {
184 : // We have some unsent data.
185 126 : size_t bulk_msg_len = send_buf_offset() + buf_len;
186 126 : if (bulk_msg_len < kDefaultSendSize) {
187 : // We still have space for more data. Don't send the message yet.
188 : // Add the message to the existing data.
189 76 : append_send_buf(buf, buf_len);
190 : } else {
191 : uint8_t *buffer;
192 : // (send_buf_offset() + buf_len) >= kDefaultSendSize
193 : boost::shared_ptr<TMemoryBuffer> bulk_msg(
194 50 : new TMemoryBuffer(send_buf_offset()));
195 50 : buffer = bulk_msg->getWritePtr(send_buf_offset());
196 : // Copy unsent data
197 50 : memcpy(buffer, send_buf(), send_buf_offset());
198 50 : bulk_msg->wroteBytes(send_buf_offset());
199 : // Send it
200 50 : SendInternal(bulk_msg);
201 : // Cleanup send_buf
202 50 : reset_send_buf();
203 : // Next send the new message
204 50 : SendInternal(send_buffer);
205 50 : }
206 : } else {
207 : // We don't have any unsent data.
208 88 : if (buf_len >= kDefaultSendSize) {
209 : // We don't have room to accommodate anything more.
210 : // Send the message now.
211 0 : SendInternal(send_buffer);
212 : } else {
213 : // We have room to accommodate more data.
214 : // Save this message.
215 : // Note: The memcpy here can be avoided by passing send_buf_
216 : // to TMemoryBuffer so that the message is encoded in
217 : // send_buf_ itself.
218 88 : set_send_buf(buf, buf_len);
219 : }
220 : }
221 214 : }
222 :
223 : // sandesh->send_queue_ is empty.
224 : // Flush unsent data (if any) and this message.
225 286 : void SandeshWriter::SendMsgAll(boost::shared_ptr<TMemoryBuffer> send_buffer) {
226 : uint8_t *buf;
227 : uint32_t buf_len;
228 :
229 286 : send_buffer->getBuffer(&buf, &buf_len);
230 :
231 286 : if (send_buf_offset()) {
232 : // We have some unsent data.
233 38 : size_t bulk_msg_len = send_buf_offset() + buf_len;
234 38 : if (bulk_msg_len <= kDefaultSendSize) {
235 : uint8_t *buffer;
236 : // We have enough room to send all the pending data in one message.
237 : boost::shared_ptr<TMemoryBuffer> bulk_msg(new
238 0 : TMemoryBuffer(bulk_msg_len));
239 0 : buffer = bulk_msg->getWritePtr(bulk_msg_len);
240 0 : memcpy(buffer, send_buf(), send_buf_offset());
241 0 : memcpy(buffer + send_buf_offset(), buf, buf_len);
242 0 : bulk_msg->wroteBytes(bulk_msg_len);
243 : // send the message
244 0 : SendInternal(bulk_msg);
245 : // reset send_buf_
246 0 : reset_send_buf();
247 0 : } else {
248 : uint8_t *buffer;
249 : // We don't have enough space to accommodate all the
250 : // pending data in one message.
251 : boost::shared_ptr<TMemoryBuffer> old_buf(new
252 38 : TMemoryBuffer(send_buf_offset()));
253 38 : buffer = old_buf->getWritePtr(send_buf_offset());
254 38 : memcpy(buffer, send_buf(),
255 : send_buf_offset());
256 38 : old_buf->wroteBytes(send_buf_offset());
257 : // Take care of the unsent data in send_buf_ first.
258 : // Note that we could have accomodated part of buf [last message]
259 : // here. But, not doing so to avoid additional memcpy() :)
260 38 : SendInternal(old_buf);
261 : // Cleanup send_buf_
262 38 : reset_send_buf();
263 : // Well, send the last message now.
264 38 : SendInternal(send_buffer);
265 38 : }
266 : } else {
267 : // No unsent data. Send the message now.
268 248 : SendInternal(send_buffer);
269 : }
270 286 : }
271 :
272 424 : void SandeshWriter::SendInternal(boost::shared_ptr<TMemoryBuffer> buf) {
273 : uint8_t *buffer;
274 : uint32_t len;
275 424 : buf->getBuffer(&buffer, &len);
276 424 : std::scoped_lock lock(send_mutex_);
277 424 : ready_to_send_ = session_->Send((const uint8_t *)buffer, len, NULL);
278 424 : }
279 :
280 : //
281 : // SandeshSession
282 : //
283 71 : SandeshSession::SandeshSession(SslServer *client, SslSocket *socket,
284 71 : int task_instance, int writer_task_id, int reader_task_id) :
285 : SslSession(client, socket),
286 71 : instance_(task_instance),
287 71 : writer_(new SandeshWriter(this)),
288 71 : reader_(new SandeshReader(this)),
289 142 : send_queue_(new Sandesh::SandeshQueue(writer_task_id,
290 : task_instance,
291 : boost::bind(&SandeshSession::SendMsg, this, _1),
292 71 : kQueueSize)),
293 71 : stats_client_(NULL),
294 71 : connection_(NULL),
295 71 : keepalive_idle_time_(kSessionKeepaliveIdleTime),
296 71 : keepalive_interval_(kSessionKeepaliveInterval),
297 71 : keepalive_probes_(kSessionKeepaliveProbes),
298 71 : tcp_user_timeout_(kSessionTcpUserTimeout),
299 71 : reader_task_id_(reader_task_id),
300 142 : sending_level_(SandeshLevel::INVALID) {
301 71 : if (Sandesh::role() == Sandesh::SandeshRole::Collector) {
302 0 : send_buffer_queue_.reset(new Sandesh::SandeshBufferQueue(writer_task_id,
303 : task_instance,
304 0 : boost::bind(&SandeshSession::SendBuffer, this, _1)));
305 0 : send_buffer_queue_->SetStartRunnerFunc(boost::bind(&SandeshSession::SessionSendReady, this));
306 : }
307 71 : send_queue_->SetStartRunnerFunc(boost::bind(&SandeshSession::SessionSendReady, this));
308 71 : }
309 :
310 142 : SandeshSession::~SandeshSession() {
311 142 : }
312 :
313 573 : bool SandeshSession::SessionSendReady() {
314 1145 : return (IsEstablished() && writer_->SendReady() &&
315 1145 : Sandesh::IsSendQueueEnabled());
316 : }
317 :
318 568 : void SandeshSession::SetSendQueueWaterMark(
319 : Sandesh::QueueWaterMarkInfo &swmi) {
320 568 : WaterMarkInfo wm(boost::get<0>(swmi),
321 : boost::bind(&SandeshSession::SetSendingLevel, this, _1,
322 1136 : boost::get<1>(swmi)));
323 568 : if (boost::get<2>(swmi)) {
324 284 : send_queue_->SetHighWaterMark(wm);
325 : } else {
326 284 : send_queue_->SetLowWaterMark(wm);
327 : }
328 568 : }
329 :
330 0 : void SandeshSession::ResetSendQueueWaterMark() {
331 0 : send_queue_->ResetHighWaterMark();
332 0 : send_queue_->ResetLowWaterMark();
333 0 : }
334 :
335 57 : void SandeshSession::SetSendingLevel(size_t count, SandeshLevel::type level) {
336 57 : if (sending_level_ != level) {
337 0 : sending_level_ = level;
338 : }
339 57 : }
340 :
341 788 : SandeshLevel::type SandeshSession::SendingLevel() const {
342 788 : return sending_level_;
343 : }
344 :
345 71 : void SandeshSession::Shutdown() {
346 71 : if (Sandesh::role() == Sandesh::SandeshRole::Collector) {
347 0 : send_buffer_queue_->Shutdown();
348 : }
349 71 : send_queue_->Shutdown();
350 71 : }
351 :
352 267 : std::string SandeshSession::ToString() const {
353 267 : std::stringstream out;
354 267 : out << TcpSession::ToString() << "(" << instance_ << ")";
355 534 : return out.str();
356 267 : }
357 :
358 134 : boost::system::error_code SandeshSession::SetSocketOptions() {
359 134 : boost::system::error_code ec = TcpSession::SetSocketOptions();
360 134 : if (ec) {
361 0 : return ec;
362 : }
363 134 : return SetSocketKeepaliveOptions(keepalive_idle_time_, keepalive_interval_,
364 134 : keepalive_probes_, tcp_user_timeout_);
365 : }
366 :
367 104 : void SandeshSession::OnRead(Buffer buffer) {
368 104 : reader_->OnRead(buffer);
369 104 : }
370 :
371 500 : bool SandeshSession::SendMsg(SandeshElement element) {
372 500 : Sandesh *sandesh = element.snh_;
373 500 : std::scoped_lock lock(send_mutex_);
374 500 : if (!IsEstablished()) {
375 0 : if (Sandesh::IsLoggingDroppedAllowed(sandesh->type())) {
376 0 : SANDESH_LOG(ERROR, __func__ << " Not Connected : Dropping Message: " <<
377 : sandesh->ToString());
378 : }
379 0 : increment_send_msg_fail();
380 0 : Sandesh::UpdateTxMsgFailStats(sandesh->Name(), 0,
381 : SandeshTxDropReason::SessionNotConnected);
382 0 : sandesh->Release();
383 0 : return true;
384 : }
385 500 : if (sandesh->IsLoggingAllowed()) {
386 127 : sandesh->Log();
387 : }
388 500 : bool more = !send_queue_->IsQueueEmpty();
389 500 : if (stats_client_ && sandesh->type() == SandeshType::UVE) {
390 0 : stats_client_->SendMsg(sandesh);
391 : }
392 500 : writer_->SendMsg(sandesh, more);
393 500 : return true;
394 500 : }
395 :
396 0 : bool SandeshSession::SendBuffer(boost::shared_ptr<TMemoryBuffer> sbuffer) {
397 0 : std::scoped_lock lock(send_mutex_);
398 0 : if (!IsEstablished()) {
399 0 : increment_send_buffer_fail();
400 0 : return true;
401 : }
402 : // No buffer packing supported currently
403 0 : writer_->SendBuffer(sbuffer);
404 0 : return true;
405 0 : }
406 :
407 0 : bool SandeshSession::EnqueueBuffer(u_int8_t *buf, u_int32_t buf_len) {
408 0 : boost::shared_ptr<TMemoryBuffer> sbuffer(new TMemoryBuffer(buf_len));
409 0 : u_int8_t *write_buf = sbuffer->getWritePtr(buf_len);
410 0 : memcpy(write_buf, buf, buf_len);
411 0 : sbuffer->wroteBytes(buf_len);
412 0 : return send_buffer_queue()->Enqueue(sbuffer);
413 0 : }
414 :
415 50 : Sandesh * SandeshSession::DecodeCtrlSandesh(const string& msg,
416 : const SandeshHeader& header,
417 : const string& sandesh_name, const uint32_t& header_offset) {
418 : namespace sandesh_prot = contrail::sandesh::protocol;
419 : namespace sandesh_trans = contrail::sandesh::transport;
420 :
421 50 : assert(header.get_Hints() & g_sandesh_constants.SANDESH_CONTROL_HINT);
422 :
423 : // Create and process the sandesh
424 50 : Sandesh *sandesh = SandeshBaseFactory::CreateInstance(sandesh_name);
425 50 : if (sandesh == NULL) {
426 0 : SANDESH_LOG(ERROR, __func__ << ": Unknown sandesh ctrl message: " << sandesh_name);
427 0 : return NULL;
428 : }
429 : boost::shared_ptr<sandesh_trans::TMemoryBuffer> btrans =
430 : boost::shared_ptr<sandesh_trans::TMemoryBuffer>(
431 50 : new sandesh_trans::TMemoryBuffer((uint8_t *)msg.c_str() + header_offset,
432 50 : msg.size() - header_offset));
433 : boost::shared_ptr<sandesh_prot::TXMLProtocol> prot =
434 50 : boost::shared_ptr<sandesh_prot::TXMLProtocol>(new sandesh_prot::TXMLProtocol(btrans));
435 50 : int32_t xfer = sandesh->Read(prot);
436 50 : if (xfer < 0) {
437 0 : SANDESH_LOG(ERROR, __func__ << ": Decoding " << sandesh_name << " for ctrl FAILED");
438 0 : sandesh->Release();
439 0 : return NULL;
440 : } else {
441 50 : return sandesh;
442 : }
443 50 : }
444 :
445 0 : void SandeshSession::EnqueueClose() {
446 0 : if (IsClosed()) {
447 0 : return;
448 : }
449 0 : std::scoped_lock lock(conn_mutex_);
450 0 : if (connection_) {
451 0 : connection_->state_machine()->OnSessionEvent(this,
452 : TcpSession::CLOSE);
453 : } else {
454 0 : TcpSession::EventObserver eobs(observer());
455 0 : if (eobs) {
456 0 : eobs(this, TcpSession::CLOSE);
457 : }
458 0 : }
459 0 : }
460 :
461 : //
462 : // SandeshReader
463 : //
464 71 : SandeshReader::SandeshReader(SandeshSession *session) :
465 71 : buf_(""),
466 71 : offset_(0),
467 71 : msg_length_(-1),
468 142 : session_(session) {
469 71 : buf_.reserve(kDefaultRecvSize);
470 71 : }
471 :
472 142 : SandeshReader::~SandeshReader() {
473 142 : }
474 :
475 50 : int SandeshReader::ExtractMsgHeader(const std::string& msg,
476 : SandeshHeader& header, std::string& msg_type, uint32_t& header_offset) {
477 50 : int32_t xfer = 0, ret;
478 : boost::shared_ptr<TMemoryBuffer> btrans =
479 : boost::shared_ptr<TMemoryBuffer>(
480 50 : new TMemoryBuffer((uint8_t *)msg.c_str(), msg.size()));
481 : boost::shared_ptr<TXMLProtocol> prot =
482 50 : boost::shared_ptr<TXMLProtocol>(new TXMLProtocol(btrans));
483 : // Read the sandesh header and note the offset
484 50 : if ((ret = header.read(prot)) <= 0) {
485 0 : SANDESH_LOG(ERROR, __func__ << ": Sandesh header read FAILED: " << msg);
486 0 : return EINVAL;
487 : }
488 50 : xfer += ret;
489 50 : header_offset = xfer;
490 : // Extract the message name
491 50 : if ((ret = prot->readSandeshBegin(msg_type)) <= 0) {
492 0 : SANDESH_LOG(ERROR, __func__ << ": Sandesh begin read FAILED: " << msg);
493 0 : return EINVAL;
494 : }
495 50 : xfer += ret;
496 50 : return 0;
497 50 : }
498 :
499 104 : void SandeshReader::SetBuf(const std::string &str) {
500 104 : if (buf_.empty()) {
501 103 : ReplaceBuf(str);
502 : } else {
503 1 : buf_ += str;
504 : }
505 : // TODO handle buf_ > kMaxMessageSize
506 104 : }
507 :
508 103 : void SandeshReader::ReplaceBuf(const std::string &str) {
509 103 : buf_ = str;
510 103 : buf_.reserve(SandeshReader::kDefaultRecvSize);
511 103 : offset_ = 0;
512 103 : }
513 :
514 50 : bool SandeshReader::LeftOver() const {
515 50 : if (buf_.empty()) {
516 0 : return false;
517 : }
518 50 : return (buf_.size() != offset_);
519 : }
520 :
521 : // Returns false if not able to extract the message length, true otherwise
522 104 : bool SandeshReader::ExtractMsgLength(size_t &msg_length, int *result) {
523 : // Have we read enough to extract the message length?
524 104 : if (buf_.size() - offset_ < SandeshWriter::sandesh_open_.size()) {
525 54 : return false;
526 : }
527 : // Some sanity check
528 50 : if (!boost::algorithm::starts_with(buf_.c_str() + offset_,
529 : SandeshWriter::sandesh_open_attr_length_)) {
530 0 : *result = -1;
531 0 : return false;
532 : }
533 :
534 50 : std::string::const_iterator end = buf_.begin() + offset_ +
535 100 : SandeshWriter::sandesh_open_.size() - 1;
536 50 : if (*end != '>') {
537 0 : *result = -2;
538 0 : return false;
539 : }
540 :
541 50 : std::string::const_iterator st = buf_.begin() + offset_ +
542 100 : SandeshWriter::sandesh_open_attr_length_.size();
543 : // Adjust for double quote
544 50 : --end;
545 50 : string length = string(st, end);
546 :
547 50 : stringToInteger(length.c_str(), msg_length);
548 50 : if (msg_length == 0) {
549 0 : *result = -3;
550 0 : return false;
551 : }
552 50 : return true;
553 50 : }
554 :
555 : // Returns false if not able to extract the full message, true otherwise
556 104 : bool SandeshReader::ExtractMsg(Buffer buffer, int *result, bool NewBuf) {
557 104 : if (NewBuf) {
558 104 : const uint8_t *cp = TcpSession::BufferData(buffer);
559 : // TODO Avoid this copy
560 104 : std::string str(cp, cp + TcpSession::BufferSize(buffer));
561 104 : SetBuf(str);
562 104 : }
563 : // Extract the message length
564 104 : if (!MsgLengthKnown()) {
565 104 : size_t msg_length = 0;
566 104 : bool done = ExtractMsgLength(msg_length, result);
567 104 : if (done == false) {
568 54 : return false;
569 : }
570 50 : set_msg_length(msg_length);
571 : }
572 : // Check if the entire message is read or not
573 50 : if (buf_.size() < msg_length()) {
574 0 : return false;
575 : }
576 50 : return true;
577 : }
578 :
579 104 : void SandeshReader::OnRead(Buffer buffer) {
580 104 : std::scoped_lock lock(cb_mutex_);
581 : // Check if session is being deleted, then drop the packet
582 104 : if (cb_.empty()) {
583 0 : SANDESH_LOG(ERROR, __func__ <<
584 : " Session being deleted: Dropping Message");
585 0 : session_->increment_recv_fail();
586 0 : session_->ReleaseBuffer(buffer);
587 0 : return;
588 : }
589 104 : int result = 0;
590 104 : bool done = ExtractMsg(buffer, &result, true);
591 : do {
592 104 : if (result < 0) {
593 : // Generate error and close connection
594 0 : SANDESH_LOG(ERROR, __func__ << " Message extract failed: " << result);
595 0 : const uint8_t *cp = TcpSession::BufferData(buffer);
596 0 : size_t cp_size = TcpSession::BufferSize(buffer);
597 0 : SANDESH_LOG(ERROR, __func__ << " OnRead Buffer Size: " << cp_size);
598 0 : SANDESH_LOG(ERROR, __func__ << " OnRead Buffer: ");
599 0 : std::string debug((const char*)cp, cp_size);
600 0 : SANDESH_LOG(ERROR, debug);
601 0 : SANDESH_LOG(ERROR, __func__ << " Reader Size: " << buf_.size());
602 0 : SANDESH_LOG(ERROR, __func__ << " Reader Offset: " << offset_);
603 0 : SANDESH_LOG(ERROR, __func__ << " Reader Buffer: " << buf_);
604 0 : buf_.clear();
605 0 : offset_ = 0;
606 : // Enqueue a close on the state machine
607 0 : session_->increment_recv_fail();
608 0 : session_->EnqueueClose();
609 0 : break;
610 0 : }
611 104 : if (done == true) {
612 : // We got good match. Process the message after extracting out
613 : // the sandesh open and close envelope
614 50 : std::string::const_iterator st = buf_.begin() + offset_ +
615 100 : SandeshWriter::sandesh_open_.size();
616 50 : std::string::const_iterator end = buf_.begin() + offset_ +
617 100 : msg_length() - SandeshWriter::sandesh_close_.size();
618 50 : std::string xml(st, end);
619 50 : offset_ += msg_length();
620 50 : reset_msg_length();
621 50 : if (!cb_(xml, session_)) {
622 : // Enqueue a close on the state machine
623 0 : session_->increment_recv_fail();
624 0 : session_->EnqueueClose();
625 0 : break;
626 : }
627 50 : } else {
628 : // Read more data.
629 54 : break;
630 : }
631 :
632 50 : if (LeftOver()) {
633 0 : ReplaceBuf(string(buf_, offset_, buf_.size() - offset_));
634 0 : done = ExtractMsg(buffer, &result, false);
635 : } else {
636 : // No more data in the Buffer
637 50 : buf_.clear();
638 50 : offset_ = 0;
639 50 : break;
640 : }
641 0 : } while (true);
642 :
643 104 : session_->ReleaseBuffer(buffer);
644 104 : return;
645 104 : }
646 :
647 142 : void SandeshReader::SetReceiveMsgCb(SandeshReceiveMsgCb cb) {
648 142 : std::scoped_lock lock(cb_mutex_);
649 142 : cb_ = cb;
650 142 : }
|