udpmsgpk.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifdef _WIN32
  14. #pragma warning( disable : 4786)
  15. #pragma warning( disable : 4018)
  16. #endif
  17. #include "platform.h"
  18. #include <string>
  19. #include <map>
  20. #include <queue>
  21. #include "jthread.hpp"
  22. #include "jlog.hpp"
  23. #include "jisem.hpp"
  24. #include "jencrypt.hpp"
  25. #include "udplib.hpp"
  26. #include "udptrr.hpp"
  27. #include "udptrs.hpp"
  28. #include "udpmsgpk.hpp"
  29. #include "roxiemem.hpp"
  30. #include "roxie.hpp"
  31. using roxiemem::DataBuffer;
  32. using roxiemem::IRowManager;
  33. RelaxedAtomic<unsigned> unwantedDiscarded;
  34. // PackageSequencer ====================================================================================
  35. //
  36. typedef DataBuffer * data_buffer_ptr;
  37. int g_sequence_compare(const void *arg1, const void *arg2 )
  38. {
  39. DataBuffer *dataBuff1 = *(DataBuffer **) arg1;
  40. DataBuffer *dataBuff2 = *(DataBuffer **) arg2;
  41. UdpPacketHeader *pktHdr1 = (UdpPacketHeader*) dataBuff1->data;
  42. UdpPacketHeader *pktHdr2 = (UdpPacketHeader*) dataBuff2->data;
  43. if (pktHdr1->pktSeq < pktHdr2->pktSeq) return -1;
  44. if (pktHdr1->pktSeq > pktHdr2->pktSeq) return 1;
  45. return 0;
  46. }
  47. static byte key[32] = {
  48. 0xf7, 0xe8, 0x79, 0x40, 0x44, 0x16, 0x66, 0x18, 0x52, 0xb8, 0x18, 0x6e, 0x76, 0xd1, 0x68, 0xd3,
  49. 0x87, 0x47, 0x01, 0xe6, 0x66, 0x62, 0x2f, 0xbe, 0xc1, 0xd5, 0x9f, 0x4a, 0x53, 0x27, 0xae, 0xa1,
  50. };
  51. class PackageSequencer : public CInterface, implements IInterface
  52. {
  53. DataBuffer *firstPacket;
  54. DataBuffer *lastContiguousPacket;
  55. DataBuffer *tail = nullptr;
  56. unsigned metaSize;
  57. unsigned headerSize;
  58. const void *header;
  59. unsigned maxSeqSeen = 0;
  60. unsigned numPackets = 0;
  61. #ifdef _DEBUG
  62. unsigned scans = 0;
  63. unsigned overscans = 0;
  64. #endif
  65. MemoryBuffer metadata;
  66. InterruptableSemaphore dataAvailable; // MORE - need to work out when to interrupt it!
  67. bool encrypted = false;
  68. public:
  69. IMPLEMENT_IINTERFACE;
  70. PackageSequencer(bool _encrypted) : encrypted(_encrypted)
  71. {
  72. if (checkTraceLevel(TRACE_MSGPACK, 3))
  73. DBGLOG("UdpCollator: PackageSequencer::PackageSequencer this=%p", this);
  74. metaSize = 0;
  75. headerSize = 0;
  76. header = NULL;
  77. firstPacket = NULL;
  78. lastContiguousPacket = NULL;
  79. }
  80. ~PackageSequencer()
  81. {
  82. if (checkTraceLevel(TRACE_MSGPACK, 3))
  83. DBGLOG("UdpCollator: PackageSequencer::~PackageSequencer this=%p", this);
  84. DataBuffer *finger = firstPacket;
  85. while (finger)
  86. {
  87. DataBuffer *goer = finger;
  88. finger = finger->msgNext;
  89. goer->Release();
  90. }
  91. }
  92. DataBuffer *next(DataBuffer *after)
  93. {
  94. dataAvailable.wait(); // MORE - when do I interrupt? Should I time out? Will potentially block indefinitely if sender restarts (leading to an abandoned packet) or stalls.
  95. DataBuffer *ret;
  96. if (after)
  97. ret = after->msgNext;
  98. else
  99. ret = firstPacket;
  100. if (checkTraceLevel(TRACE_MSGPACK, 5))
  101. {
  102. if (ret)
  103. {
  104. StringBuffer s;
  105. UdpPacketHeader *pktHdr = (UdpPacketHeader*) ret->data;
  106. DBGLOG("UdpCollator: PackageSequencer::next returns ruid=" RUIDF " id=0x%.8X mseq=%u pkseq=0x%.8X node=%s dataBuffer=%p this=%p",
  107. pktHdr->ruid, pktHdr->msgId, pktHdr->msgSeq, pktHdr->pktSeq, pktHdr->node.getTraceText(s).str(), ret, this);
  108. }
  109. else
  110. DBGLOG("UdpCollator: PackageSequencer::next returns NULL this=%p", this);
  111. }
  112. return ret;
  113. }
  114. bool insert(DataBuffer *dataBuff, std::atomic<unsigned> &duplicates, std::atomic<unsigned> &resends) // returns true if message is complete.
  115. {
  116. bool res = false;
  117. assert(dataBuff->msgNext == NULL);
  118. UdpPacketHeader *pktHdr = (UdpPacketHeader*) dataBuff->data;
  119. unsigned pktseq = pktHdr->pktSeq;
  120. if ((pktseq & UDP_PACKET_SEQUENCE_MASK) > maxSeqSeen)
  121. maxSeqSeen = pktseq & UDP_PACKET_SEQUENCE_MASK;
  122. if (pktseq & UDP_PACKET_RESENT)
  123. {
  124. pktseq &= ~UDP_PACKET_RESENT;
  125. pktHdr->pktSeq = pktseq;
  126. resends++;
  127. }
  128. if (checkTraceLevel(TRACE_MSGPACK, 5))
  129. {
  130. StringBuffer s;
  131. DBGLOG("UdpCollator: PackageSequencer::insert ruid=" RUIDF " id=0x%.8X mseq=%u pkseq=0x%.8X sendSeq=%" SEQF "u node=%s dataBuffer=%p this=%p",
  132. pktHdr->ruid, pktHdr->msgId, pktHdr->msgSeq, pktHdr->pktSeq, pktHdr->sendSeq, pktHdr->node.getTraceText(s).str(), dataBuff, this);
  133. }
  134. // Optimize the (very) common case where I need to add to the end
  135. DataBuffer *finger;
  136. DataBuffer *prev;
  137. if (tail && (pktseq > ((UdpPacketHeader*) tail->data)->pktSeq))
  138. {
  139. assert(tail->msgNext==nullptr);
  140. finger = nullptr;
  141. prev = tail;
  142. tail = dataBuff;
  143. }
  144. else
  145. {
  146. // This is an insertion sort - YUK!
  147. if (lastContiguousPacket)
  148. {
  149. UdpPacketHeader *oldHdr = (UdpPacketHeader*) lastContiguousPacket->data;
  150. if (pktHdr->pktSeq <= oldHdr->pktSeq)
  151. {
  152. // discard duplicated incoming packet - should be uncommon unless we requested a resend for a packet already in flight
  153. if (checkTraceLevel(TRACE_MSGPACK, 5))
  154. DBGLOG("UdpCollator: Discarding duplicate incoming packet %u (we have all up to %u)", pktHdr->pktSeq, oldHdr->pktSeq);
  155. dataBuff->Release();
  156. duplicates++;
  157. return false;
  158. }
  159. finger = lastContiguousPacket->msgNext;
  160. prev = lastContiguousPacket;
  161. }
  162. else
  163. {
  164. finger = firstPacket;
  165. prev = NULL;
  166. }
  167. while (finger)
  168. {
  169. #ifdef _DEBUG
  170. scans++;
  171. if (scans==1000000)
  172. {
  173. overscans++;
  174. DBGLOG("%u million scans in UdpCollator insert(DataBuffer *dataBuff", overscans);
  175. if (lastContiguousPacket)
  176. {
  177. UdpPacketHeader *oldHdr = (UdpPacketHeader*) lastContiguousPacket->data;
  178. DBGLOG("lastContiguousPacket is at %u , last packet seen is %u", oldHdr->pktSeq & UDP_PACKET_SEQUENCE_MASK, pktHdr->pktSeq & UDP_PACKET_SEQUENCE_MASK);
  179. }
  180. else
  181. DBGLOG("lastContiguousPacket is NULL , last packet seen is %u", pktHdr->pktSeq & UDP_PACKET_SEQUENCE_MASK);
  182. scans = 0;
  183. }
  184. #endif
  185. UdpPacketHeader *oldHdr = (UdpPacketHeader*) finger->data;
  186. if (pktHdr->pktSeq == oldHdr->pktSeq)
  187. {
  188. // discard duplicated incoming packet - should be uncommon unless we requested a resend for a packet already in flight
  189. if (checkTraceLevel(TRACE_MSGPACK, 5))
  190. DBGLOG("UdpCollator: Discarding duplicate incoming packet %u", pktHdr->pktSeq);
  191. dataBuff->Release();
  192. return false;
  193. }
  194. else if (pktHdr->pktSeq < oldHdr->pktSeq)
  195. {
  196. break;
  197. }
  198. else
  199. {
  200. prev = finger;
  201. finger = finger->msgNext;
  202. }
  203. }
  204. }
  205. if (prev)
  206. {
  207. assert(prev->msgNext == finger);
  208. prev->msgNext = dataBuff;
  209. }
  210. else
  211. {
  212. firstPacket = dataBuff;
  213. if (!tail)
  214. tail = dataBuff;
  215. }
  216. dataBuff->msgNext = finger;
  217. #ifdef _DEBUG
  218. numPackets++;
  219. #endif
  220. // Now that we know we are keeping it, decrypt it
  221. // MORE - could argue that we would prefer to wait even longer - until we know consumer wants it - but that might be complex
  222. if (encrypted)
  223. {
  224. // MORE - This is decrypting in-place. Is that ok?? Seems to be with the code we currently use, but if that changed
  225. // might need to rethink this
  226. size_t decryptedSize = aesDecrypt(key, sizeof(key), pktHdr+1, pktHdr->length-sizeof(UdpPacketHeader), pktHdr+1, DATA_PAYLOAD-sizeof(UdpPacketHeader));
  227. if (checkTraceLevel(TRACE_MSGPACK, 5))
  228. DBGLOG("Decrypted %u bytes at %p resulting in %u bytes", (unsigned) (pktHdr->length-sizeof(UdpPacketHeader)), pktHdr+1, (unsigned) decryptedSize);
  229. pktHdr->length = decryptedSize + sizeof(UdpPacketHeader);
  230. }
  231. if (prev == lastContiguousPacket)
  232. {
  233. unsigned prevseq;
  234. if (lastContiguousPacket)
  235. {
  236. UdpPacketHeader *lastHdr = (UdpPacketHeader*) lastContiguousPacket->data;
  237. prevseq = lastHdr->pktSeq & UDP_PACKET_SEQUENCE_MASK;
  238. finger = lastContiguousPacket->msgNext;
  239. }
  240. else
  241. {
  242. prevseq = (unsigned) -1;
  243. finger = firstPacket;
  244. }
  245. while (finger)
  246. {
  247. UdpPacketHeader *fingerHdr = (UdpPacketHeader*) finger->data;
  248. unsigned pktseq = fingerHdr->pktSeq & UDP_PACKET_SEQUENCE_MASK;
  249. if (pktseq == prevseq+1)
  250. {
  251. unsigned packetDataSize = fingerHdr->length - fingerHdr->metalength - sizeof(UdpPacketHeader);
  252. assert(packetDataSize < roxiemem::DATA_ALIGNMENT_SIZE);
  253. if (pktseq == 0)
  254. {
  255. // MORE - Is this safe - header lifetime is somewhat unpredictable without a copy of it...
  256. // Client header is at the start of packet 0
  257. headerSize = *(unsigned short *)(finger->data + sizeof(UdpPacketHeader));
  258. header = finger->data + sizeof(UdpPacketHeader) + sizeof(unsigned short);
  259. packetDataSize -= headerSize + sizeof(unsigned short);
  260. }
  261. if (fingerHdr->metalength)
  262. {
  263. // MORE - may be worth taking the effort to avoid copy of metadata unless it's split up
  264. metaSize += fingerHdr->metalength;
  265. metadata.append(fingerHdr->metalength, finger->data + fingerHdr->length - fingerHdr->metalength);
  266. }
  267. lastContiguousPacket = finger;
  268. dataAvailable.signal();
  269. if (fingerHdr->pktSeq & UDP_PACKET_COMPLETE)
  270. {
  271. res = true;
  272. dataAvailable.signal(); // allowing us to read the NULL that signifies end of message. May prefer to use the flag to stop?
  273. }
  274. }
  275. else
  276. break;
  277. finger = finger->msgNext;
  278. prevseq = pktseq;
  279. }
  280. }
  281. return res;
  282. }
  283. inline const void *getMetaData(unsigned &length)
  284. {
  285. length = metadata.length();
  286. return metadata.toByteArray();
  287. }
  288. inline const void *getMessageHeader()
  289. {
  290. return header;
  291. }
  292. inline unsigned getHeaderSize()
  293. {
  294. return headerSize;
  295. }
  296. void dump()
  297. {
  298. DBGLOG("Contains %u packets, lastSeq = %u", numPackets, maxSeqSeen);
  299. if (lastContiguousPacket)
  300. {
  301. UdpPacketHeader *hdr = (UdpPacketHeader*) lastContiguousPacket->data;
  302. DBGLOG("lastContiguousPacket is %u %" SEQF "u", hdr->pktSeq, hdr->sendSeq);
  303. }
  304. }
  305. };
  306. // MessageResult ====================================================================================
  307. //
  308. class CMessageUnpackCursor: implements IMessageUnpackCursor, public CInterface
  309. {
  310. PackageSequencer *pkSequencer;
  311. DataBuffer *dataBuff;
  312. unsigned current_pos;
  313. Linked<IRowManager> rowMgr;
  314. public:
  315. IMPLEMENT_IINTERFACE;
  316. CMessageUnpackCursor(PackageSequencer *_pkSqncr, IRowManager *_rowMgr) : rowMgr(_rowMgr)
  317. {
  318. if (checkTraceLevel(TRACE_MSGPACK, 3))
  319. DBGLOG("UdpCollator: CMessageUnpackCursor::CMessageUnpackCursor this=%p", this);
  320. pkSequencer = _pkSqncr;
  321. dataBuff = pkSequencer->next(NULL);
  322. UdpPacketHeader *pktHdr = (UdpPacketHeader*) dataBuff->data;
  323. current_pos = sizeof(UdpPacketHeader) + pkSequencer->getHeaderSize() + sizeof(unsigned short); // YUK - code neads cleaning!
  324. unsigned packetDataLimit = pktHdr->length - pktHdr->metalength;
  325. while (current_pos >= packetDataLimit)
  326. {
  327. current_pos = sizeof(UdpPacketHeader);
  328. dataBuff = pkSequencer->next(dataBuff); // NULL, we expect, unless packing was really weird.
  329. if (dataBuff)
  330. {
  331. pktHdr = (UdpPacketHeader*) dataBuff->data;
  332. packetDataLimit = pktHdr->length - pktHdr->metalength;
  333. }
  334. else
  335. break;
  336. }
  337. }
  338. ~CMessageUnpackCursor()
  339. {
  340. if (checkTraceLevel(TRACE_MSGPACK, 3))
  341. DBGLOG("UdpCollator: CMessageUnpackCursor::~CMessageUnpackCursor this=%p", this);
  342. pkSequencer->Release();
  343. }
  344. virtual bool atEOF() const
  345. {
  346. return dataBuff == NULL;
  347. }
  348. virtual bool isSerialized() const
  349. {
  350. return true;
  351. }
  352. virtual const void *getNext(int length)
  353. {
  354. // YUK horrid code! Though packer is even more horrid
  355. void *res = 0;
  356. if (dataBuff)
  357. {
  358. UdpPacketHeader *pktHdr = (UdpPacketHeader*) dataBuff->data;
  359. if (checkTraceLevel(TRACE_MSGPACK, 4))
  360. {
  361. StringBuffer s;
  362. DBGLOG("UdpCollator: CMessageUnpackCursor::getNext(%u) pos=%u pktLength=%u metaLen=%u ruid=" RUIDF " id=0x%.8X mseq=%u pkseq=0x%.8X node=%s dataBuff=%p this=%p",
  363. length, current_pos, pktHdr->length, pktHdr->metalength,
  364. pktHdr->ruid, pktHdr->msgId, pktHdr->msgSeq, pktHdr->pktSeq,
  365. pktHdr->node.getTraceText(s).str(), dataBuff, this);
  366. }
  367. unsigned packetDataLimit = pktHdr->length - pktHdr->metalength;
  368. if ((packetDataLimit - current_pos) >= (unsigned) length)
  369. {
  370. // Simple case - no need to copy
  371. res = &dataBuff->data[current_pos];
  372. current_pos += length;
  373. while (current_pos >= packetDataLimit)
  374. {
  375. dataBuff = pkSequencer->next(dataBuff);
  376. current_pos = sizeof(UdpPacketHeader);
  377. if (dataBuff)
  378. {
  379. pktHdr = (UdpPacketHeader*) dataBuff->data;
  380. packetDataLimit = pktHdr->length - pktHdr->metalength;
  381. }
  382. else
  383. break;
  384. }
  385. LinkRoxieRow(res);
  386. return res;
  387. }
  388. char *currResLoc = (char*)rowMgr->allocate(length, 0);
  389. res = currResLoc;
  390. while (length && dataBuff)
  391. {
  392. // Spans more than one block - allocate and copy
  393. unsigned cpyLen = packetDataLimit - current_pos;
  394. if (cpyLen > (unsigned) length) cpyLen = length;
  395. memcpy(currResLoc, &dataBuff->data[current_pos], cpyLen);
  396. length -= cpyLen;
  397. currResLoc += cpyLen;
  398. current_pos += cpyLen;
  399. while (current_pos >= packetDataLimit)
  400. {
  401. dataBuff = pkSequencer->next(dataBuff);
  402. if (dataBuff)
  403. {
  404. current_pos = sizeof(UdpPacketHeader);
  405. pktHdr = (UdpPacketHeader*) dataBuff->data;
  406. packetDataLimit = pktHdr->length - pktHdr->metalength;
  407. }
  408. else
  409. {
  410. current_pos = 0;
  411. pktHdr = NULL;
  412. packetDataLimit = 0;
  413. break;
  414. }
  415. }
  416. }
  417. assertex(!length); // fail if not enough data available
  418. }
  419. else
  420. res = NULL;
  421. return res;
  422. }
  423. };
  424. class CMessageResult : public IMessageResult, CInterface {
  425. PackageSequencer *pkSequencer;
  426. mutable MemoryBuffer metaInfo;
  427. mutable CriticalSection metaCrit;
  428. public:
  429. IMPLEMENT_IINTERFACE;
  430. CMessageResult(PackageSequencer *_pkSqncr)
  431. {
  432. if (checkTraceLevel(TRACE_MSGPACK, 3))
  433. DBGLOG("UdpCollator: CMessageResult::CMessageResult pkSqncr=%p, this=%p", _pkSqncr, this);
  434. pkSequencer = _pkSqncr;
  435. }
  436. ~CMessageResult()
  437. {
  438. if (checkTraceLevel(TRACE_MSGPACK, 3))
  439. DBGLOG("UdpCollator: CMessageResult::~CMessageResult this=%p", this);
  440. pkSequencer->Release();
  441. }
  442. virtual IMessageUnpackCursor *getCursor(IRowManager *rowMgr) const
  443. {
  444. return new CMessageUnpackCursor(LINK(pkSequencer), rowMgr);
  445. }
  446. virtual const void *getMessageHeader(unsigned &length) const
  447. {
  448. length = pkSequencer->getHeaderSize();
  449. return pkSequencer->getMessageHeader();
  450. }
  451. virtual const void *getMessageMetadata(unsigned &length) const
  452. {
  453. return pkSequencer->getMetaData(length);
  454. }
  455. virtual void discard() const
  456. {
  457. if (checkTraceLevel(TRACE_MSGPACK, 2))
  458. DBGLOG("UdpCollator: CMessageResult - Roxie server discarded a packet");
  459. unwantedDiscarded++;
  460. }
  461. };
  462. // MessageCollator ====================================================================================
  463. //
  464. PUID GETPUID(DataBuffer *dataBuff)
  465. {
  466. UdpPacketHeader *pktHdr = (UdpPacketHeader*) dataBuff->data;
  467. unsigned ip4 = pktHdr->node.getIp4();
  468. return (((PUID) ip4) << 32) | (PUID) pktHdr->msgSeq;
  469. }
  470. CMessageCollator::CMessageCollator(IRowManager *_rowMgr, unsigned _ruid, bool _encrypted) : rowMgr(_rowMgr), ruid(_ruid), encrypted(_encrypted)
  471. {
  472. if (checkTraceLevel(TRACE_MSGPACK, 3))
  473. DBGLOG("UdpCollator: CMessageCollator::CMessageCollator rowMgr=%p this=%p ruid=" RUIDF "", _rowMgr, this, ruid);
  474. memLimitExceeded = false;
  475. activity = false;
  476. totalBytesReceived = 0;
  477. }
  478. CMessageCollator::~CMessageCollator()
  479. {
  480. if (checkTraceLevel(TRACE_MSGPACK, 3))
  481. DBGLOG("UdpCollator: CMessageCollator::~CMessageCollator ruid=" RUIDF ", this=%p", ruid, this);
  482. while (!queue.empty())
  483. {
  484. PackageSequencer *pkSqncr = queue.front();
  485. queue.pop();
  486. pkSqncr->Release();
  487. }
  488. }
  489. void CMessageCollator::noteDuplicate(bool isResend)
  490. {
  491. totalDuplicates++;
  492. if (isResend)
  493. totalResends++;
  494. }
  495. unsigned CMessageCollator::queryBytesReceived() const
  496. {
  497. return totalBytesReceived;
  498. }
  499. unsigned CMessageCollator::queryDuplicates() const
  500. {
  501. return totalDuplicates;
  502. }
  503. unsigned CMessageCollator::queryResends() const
  504. {
  505. return totalResends;
  506. }
  507. bool CMessageCollator::attach_databuffer(DataBuffer *dataBuff)
  508. {
  509. activity = true;
  510. UdpPacketHeader *pktHdr = (UdpPacketHeader*) dataBuff->data;
  511. totalBytesReceived += pktHdr->length;
  512. if (memLimitExceeded || roxiemem::memPoolExhausted())
  513. {
  514. DBGLOG("UdpCollator: mem limit exceeded");
  515. return false;
  516. }
  517. if (!dataBuff->attachToRowMgr(rowMgr))
  518. {
  519. memLimitExceeded = true;
  520. DBGLOG("UdpCollator: mem limit exceeded");
  521. return(false);
  522. }
  523. collate(dataBuff);
  524. return true;
  525. }
  526. bool CMessageCollator::attach_data(const void *data, unsigned len)
  527. {
  528. // Simple code can allocate databuffer, copy data in, then call attach_databuffer
  529. // MORE - we can attach as we create may be more sensible (and simplify roxiemem rather if it was the ONLY way)
  530. activity = true;
  531. totalBytesReceived += len;
  532. if (memLimitExceeded || roxiemem::memPoolExhausted())
  533. {
  534. DBGLOG("UdpCollator: mem limit exceeded");
  535. return false;
  536. }
  537. DataBuffer *dataBuff = bufferManager->allocate();
  538. assertex(len <= DATA_PAYLOAD);
  539. memcpy(dataBuff->data, data, len);
  540. if (!dataBuff->attachToRowMgr(rowMgr))
  541. {
  542. memLimitExceeded = true;
  543. DBGLOG("UdpCollator: mem limit exceeded");
  544. dataBuff->Release();
  545. return(false);
  546. }
  547. collate(dataBuff);
  548. return true;
  549. }
  550. void CMessageCollator::collate(DataBuffer *dataBuff)
  551. {
  552. PUID puid = GETPUID(dataBuff);
  553. // MORE - we leak (at least until query terminates) a PackageSequencer for messages that we only receive parts of - maybe only an issue for "catchall" case
  554. PackageSequencer *pkSqncr = mapping.getValue(puid);
  555. if (!pkSqncr)
  556. {
  557. pkSqncr = new PackageSequencer(encrypted);
  558. mapping.setValue(puid, pkSqncr);
  559. pkSqncr->Release();
  560. }
  561. bool isComplete = pkSqncr->insert(dataBuff, totalDuplicates, totalResends);
  562. if (isComplete)
  563. {
  564. pkSqncr->Link();
  565. mapping.remove(puid);
  566. queueCrit.enter();
  567. queue.push(pkSqncr);
  568. sem.signal();
  569. queueCrit.leave();
  570. }
  571. }
  572. IMessageResult *CMessageCollator::getNextResult(unsigned time_out, bool &anyActivity)
  573. {
  574. if (checkTraceLevel(TRACE_MSGPACK, 3))
  575. DBGLOG("UdpCollator: CMessageCollator::getNextResult() timeout=%.8X ruid=%u rowMgr=%p this=%p", time_out, ruid, (void*) rowMgr, this);
  576. if (memLimitExceeded)
  577. {
  578. DBGLOG("UdpCollator: CMessageCollator::getNextResult() throwing memory limit exceeded exception - rowMgr=%p this=%p", (void*) rowMgr, this);
  579. throw MakeStringException(0, "memory limit exceeded");
  580. }
  581. else if (roxiemem::memPoolExhausted())
  582. {
  583. DBGLOG("UdpCollator: CMessageCollator::getNextResult() throwing memory pool exhausted exception - rowMgr=%p this=%p", (void*)rowMgr, this);
  584. throw MakeStringException(0, "memory pool exhausted");
  585. }
  586. if (sem.wait(time_out))
  587. {
  588. queueCrit.enter();
  589. PackageSequencer *pkSqncr = queue.front();
  590. queue.pop();
  591. queueCrit.leave();
  592. anyActivity = true;
  593. activity = false;
  594. return new CMessageResult(pkSqncr);
  595. }
  596. anyActivity = activity;
  597. activity = false;
  598. if (!anyActivity && ruid>=RUID_FIRST && checkTraceLevel(TRACE_MSGPACK, 1)) // suppress the tracing for pings where we expect the timeout...
  599. {
  600. #ifdef _DEBUG
  601. DBGLOG("GetNextResult timeout: mapping has %d partial results", mapping.ordinality());
  602. HashIterator h(mapping);
  603. ForEach(h)
  604. {
  605. auto *r = mapping.mapToValue(&h.query());
  606. PUID puid = *(PUID *) h.query().getKey();
  607. DBGLOG("puid=%" I64F "x:", puid);
  608. PackageSequencer *pkSqncr = mapping.getValue(puid);
  609. pkSqncr->dump();
  610. }
  611. #endif
  612. DBGLOG("UdpCollator: CMessageCollator::GetNextResult timeout");
  613. }
  614. return 0;
  615. }
  616. void CMessageCollator::interrupt(IException *E)
  617. {
  618. sem.interrupt(E);
  619. }
  620. // ====================================================================================
  621. //