int main(int argc, const char* argv[]) { char buffer[2048]; MessageHeader hdr; Car car; std::size_t encodeHdrLength = encodeHdr(hdr, car, buffer, 0, sizeof(buffer)); std::size_t encodeMsgLength = encodeCar(car, buffer, hdr.encodedLength(), sizeof(buffer)); cout << "Encoded Lengths are " << encodeHdrLength << " + " << encodeMsgLength << endl; std::size_t decodeHdrLength = decodeHdr(hdr, buffer, 0, sizeof(buffer)); std::size_t decodeMsgLength = decodeCar(car, buffer, hdr.encodedLength(), hdr.blockLength(), hdr.version(), sizeof(buffer)); cout << "Decoded Lengths are " << decodeHdrLength << " + " << decodeMsgLength << endl; if (encodeHdrLength != decodeHdrLength) { cerr << "Encode/Decode header lengths do not match\n"; return EXIT_FAILURE; } if (encodeMsgLength != decodeMsgLength) { cerr << "Encode/Decode message lengths do not match\n"; return EXIT_FAILURE; } return EXIT_SUCCESS; }
void POP3ClientSessionTest::testRetrieveHeader() { DialogServer server; server.addResponse("+OK POP3 Ready..."); server.addResponse("+OK USER"); server.addResponse("+OK PASS"); server.addResponse( "+OK Here comes the message\r\n" "From: [email protected]\r\n" "To: [email protected]\r\n" "Subject: test\r\n" "\r\n" "." ); server.addResponse("+OK QUIT"); POP3ClientSession session("localhost", server.port()); session.login("user", "secret"); server.clearCommands(); MessageHeader header; session.retrieveHeader(1, header); std::string cmd = server.popCommand(); assert (cmd == "TOP 1 0"); assert (header.get("From") == "*****@*****.**"); assert (header.get("To") == "*****@*****.**"); assert (header.get("Subject") == "test"); session.close(); }
MessageHeader * NNTPSession::fetchHeader(String *groupName, unsigned int index, ErrorCode * pError) { int r; char * content; size_t content_len; MCLog("fetch header at index %u", index); selectGroup(groupName, pError); if (* pError != ErrorNone) { return NULL; } r = newsnntp_head(mNNTP, index, &content, &content_len); if (r != NEWSNNTP_NO_ERROR) { * pError = ErrorFetchMessageList; return NULL; } Data * data; data = new Data(content, (unsigned int) content_len); MessageHeader * result = new MessageHeader(); result->importHeadersData(data); result->autorelease(); data->release(); newsnntp_head_free(content); * pError = ErrorNone; return result; }
virtual std::uint64_t encodeHdrAndMsg() { MessageHeader hdr; Msg msg; hdr.wrap(m_buffer, 0, 0, sizeof(m_buffer)) .blockLength(Msg::sbeBlockLength()) .templateId(Msg::sbeTemplateId()) .schemaId(Msg::sbeSchemaId()) .version(Msg::sbeSchemaVersion()); msg.wrapForEncode(m_buffer, hdr.encodedLength(), sizeof(m_buffer)); msg.structure() .enumOne(EnumOne::Value10) .zeroth(42) .setOne().clear().bit0(false).bit16(true).bit26(false); msg.structure() .inner() .first(101) .second(202); return hdr.encodedLength() + msg.encodedLength(); }
void MultipartReaderTest::testReadOnePart() { std::string s("\r\n--MIME_boundary_01234567\r\nname1: value1\r\n\r\nthis is part 1\r\n--MIME_boundary_01234567--\r\n"); std::istringstream istr(s); MultipartReader r(istr, "MIME_boundary_01234567"); assert (r.boundary() == "MIME_boundary_01234567"); assert (r.hasNextPart()); MessageHeader h; r.nextPart(h); assert (h.size() == 1); assert (h["name1"] == "value1"); std::istream& i = r.stream(); int ch = i.get(); std::string part; while (ch >= 0) { part += (char) ch; ch = i.get(); } assert (part == "this is part 1"); assert (!r.hasNextPart()); try { r.nextPart(h); fail("no more parts - must throw"); } catch (MultipartException&) { } }
void MessageRequestScheduler::addRequest (DisServiceDataReqMsg::FragmentRequest *pFragReq, bool bSeq, bool bRel) { if (pFragReq == NULL) { return; } MessageHeader *pMH = pFragReq->pMsgHeader; if (pMH != NULL) { // Create the proper FragReqWrapper FragReqWrapper *pReq = NULL; if (pMH->isChunk()) { pReq = new ChunkFragReqWrapper (pFragReq, bSeq, bRel, _bDeleteDuplicateRequest); } else { pReq = new MessageFragReqWrapper (pFragReq, bSeq, bRel, _bDeleteDuplicateRequest); } if (pReq == NULL) { checkAndLogMsg ("MessageRequestScheduler::addRequest", memoryExhausted); delete pFragReq; } else { checkAndLogMsg ("MessageRequestScheduler::addRequest", Logger::L_HighDetailDebug, "added request for %d fragments\n", pFragReq->pRequestedRanges->getCount()); // Insert the FragReqWrapper _requests.insert (pReq); } } }
std::uint64_t encodeHdrAndMsg() { MessageHeader hdr; TestMessage1 msg; hdr.wrap(m_buffer, 0, 0, sizeof(m_buffer)) .blockLength(TestMessage1::sbeBlockLength()) .templateId(TestMessage1::sbeTemplateId()) .schemaId(TestMessage1::sbeSchemaId()) .version(TestMessage1::sbeSchemaVersion()); msg.wrapForEncode(m_buffer, hdr.encodedLength(), sizeof(m_buffer)); TestMessage1::Entries &entries = msg.entriesCount(2); entries.next() .tagGroup1(10) .tagGroup2(20); entries.next() .tagGroup1(30) .tagGroup2(40); return hdr.encodedLength() + msg.encodedLength(); }
bool JsAgent::dispatchAndReturn(JsValue& ret) { ArchiveReader<Channel> ar(*m_channel); while(true) { Debug::println("Waiting for MessageHeader..."); MessageHeader mh; if(!mh.serialize(ar)) return false; switch(mh.getMessageType()) { case MT_DefineFunction: Debug::println("MT_DefineFunction"); MSG_DISPATCH(DefineFunctionMessage, onDefineFunction); break; case MT_InvokeFunction: Debug::println("MT_InvokeFunction"); MSG_DISPATCH(InvokeFunctionMessage, onInvokeFunction); break; case MT_Return: { Debug::println("MT_Return"); ReturnMessage msg; msg.serialize(ar); return onReturn(msg, ret); } case MT_Quit: Debug::println("MT_Quit"); return true; default: Debug::println("Unknown message type!"); return false; } } return false; }
void FifoReadCallback::handleMessageHeader(MessageHeader msgHeader) noexcept { from_ = msgHeader.getLocalAddress(); to_ = msgHeader.getPeerAddress(); if (msgHeader.direction() == MessageDirection::Received) { std::swap(from_, to_); } }
std::uint64_t encodeHdrAndMsg() { MessageHeader hdr; MsgName msg; hdr.wrap(m_buffer, 0, 0, sizeof(m_buffer)) .blockLength(MsgName::sbeBlockLength()) .templateId(MsgName::sbeTemplateId()) .schemaId(MsgName::sbeSchemaId()) .version(MsgName::sbeSchemaVersion()); msg.wrapForEncode(m_buffer, hdr.encodedLength(), sizeof(m_buffer)); msg.field1(187); msg.field2().clear() .choice1(true); MsgName::GrName &grp = msg.grNameCount(2); grp.next() .grField1(10) .grField2(20); grp.next() .grField1(30) .grField2(40); return hdr.encodedLength() + msg.encodedLength(); }
void MultipartReader::parseHeader(MessageHeader& messageHeader) { messageHeader.clear(); messageHeader.read(_istr); int ch = _istr.get(); if (ch == '\r' && _istr.peek() == '\n') ch = _istr.get(); }
void HTMLForm::writeMultipart(std::ostream& ostr) { MultipartWriter writer(ostr, _boundary); for (NameValueCollection::ConstIterator it = begin(); it != end(); ++it) { MessageHeader header; std::string disp("form-data; name=\""); disp.append(it->first); disp.append("\""); header.set("Content-Disposition", disp); writer.nextPart(header); ostr << it->second; } for (PartVec::iterator ita = _parts.begin(); ita != _parts.end(); ++ita) { MessageHeader header; std::string disp("file; name=\""); disp.append(ita->name); disp.append("\""); std::string filename = ita->pSource->filename(); if (!filename.empty()) { disp.append("; filename=\""); disp.append(filename); disp.append("\""); } header.set("Content-Disposition", disp); header.set("Content-Type", ita->pSource->mediaType()); writer.nextPart(header); StreamCopier::copyStream(ita->pSource->stream(), ostr); } writer.close(); _boundary = writer.boundary(); }
void AbstractProtocol::sendMessage(const AbstractMessage &message) { QByteArray totalData; QByteArray messageData; MessageHeader header; static int nextSeq = 0; nextSeq++; message.serialize(&messageData); header.data.size = messageData.size(); header.data.type = message.getType(); header.data.seqNum = nextSeq; header.serialize(&totalData); totalData.append(messageData); qDebug() << "Sending Total size: " << totalData.size(); int written = 0; int totalSize = totalData.size(); while (written < totalSize) { int ret = socket->write(totalData.data() + written, totalSize - written); if (ret == -1) { qDebug() << "write error"; return; } written+=ret; } }
void MailMessage::readPart(std::istream& istr, const MessageHeader& header, PartHandler& handler) { std::string encoding; if (header.has(HEADER_CONTENT_TRANSFER_ENCODING)) { encoding = header.get(HEADER_CONTENT_TRANSFER_ENCODING); // get rid of a parameter if one is set std::string::size_type pos = encoding.find(';'); if (pos != std::string::npos) encoding.resize(pos); } if (icompare(encoding, CTE_QUOTED_PRINTABLE) == 0) { QuotedPrintableDecoder decoder(istr); handlePart(decoder, header, handler); } else if (icompare(encoding, CTE_BASE64) == 0) { Base64Decoder decoder(istr); handlePart(decoder, header, handler); } else { handlePart(istr, header, handler); } }
void MessageHeaderTest::testRead3() { std::string s("name1: value1\r\n"); std::istringstream istr(s); MessageHeader mh; mh.read(istr); assert (mh.size() == 1); assert (mh["name1"] == "value1"); }
static std::uint64_t encodeHdr(MessageHeader& hdr) { hdr.blockLength(Car::sbeBlockLength()) .templateId(Car::sbeTemplateId()) .schemaId(Car::sbeSchemaId()) .version(Car::sbeSchemaVersion()); return hdr.encodedLength(); }
TEST(MessageHeader, testRead3) { std::string s("name1: value1\r\n"); std::istringstream istr(s); MessageHeader mh; mh.read(istr); EXPECT_TRUE (mh.size() == 1); EXPECT_TRUE (mh["name1"] == "value1"); }
const char * MessageRequestScheduler::FragReqWrapper::getSenderNodeId() const { if (pFRequests != NULL) { MessageHeader *pMH = pFRequests->pMsgHeader; if (pMH != NULL) { return pMH->getPublisherNodeId(); } } return NULL; }
void MessageHeaderTest::testReadFolding4() { std::string s("name1: value1\r\nname2: value21\r\n value22\r\n value23"); std::istringstream istr(s); MessageHeader mh; mh.read(istr); assert (mh.size() == 2); assert (mh["name1"] == "value1"); assert (mh["name2"] == "value21 value22 value23"); }
void decodeHdr(MessageHeader &hdr, char *buffer, int offset) { hdr.wrap(buffer, offset, messageHeaderVersion); // decode the header cout << "messageHeader.blockLength=" << hdr.blockLength() << endl; cout << "messageHeader.templateId=" << hdr.templateId() << endl; cout << "messageHeader.version=" << (sbe_uint32_t)hdr.version() << endl; cout << "messageHeader.reserved=" << (sbe_uint32_t)hdr.reserved() << endl; }
TEST(MessageHeader, testReadFolding4) { std::string s("name1: value1\r\nname2: value21\r\n value22\r\n value23"); std::istringstream istr(s); MessageHeader mh; mh.read(istr); EXPECT_TRUE (mh.size() == 2); EXPECT_TRUE (mh["name1"] == "value1"); EXPECT_TRUE (mh["name2"] == "value21 value22 value23"); }
const char * MessageRequestScheduler::FragReqWrapper::getGroupName() const { if (pFRequests != NULL) { MessageHeader *pMH = pFRequests->pMsgHeader; if (pMH != NULL) { return pMH->getGroupName(); } } return NULL; }
TEST(MessageHeader, testRead5) { std::string s("name1:\r\nname2: value2\r\nname3: value3 \r\n"); std::istringstream istr(s); MessageHeader mh; mh.read(istr); EXPECT_TRUE (mh.size() == 3); EXPECT_TRUE (mh["name1"] == ""); EXPECT_TRUE (mh["name2"] == "value2"); EXPECT_TRUE (mh["name3"] == "value3"); }
void MessageHeaderTest::testRead2() { std::string s("name1: value1\nname2: value2\nname3: value3\n"); std::istringstream istr(s); MessageHeader mh; mh.read(istr); assert (mh.size() == 3); assert (mh["name1"] == "value1"); assert (mh["name2"] == "value2"); assert (mh["name3"] == "value3"); }
TEST(MessageHeader, testReadFolding2) { std::string s("name1: value1\nname2: value21\n\tvalue22\nname3: value3\n"); std::istringstream istr(s); MessageHeader mh; mh.read(istr); EXPECT_TRUE (mh.size() == 3); EXPECT_TRUE (mh["name1"] == "value1"); EXPECT_TRUE (mh["name2"] == "value21\tvalue22"); EXPECT_TRUE (mh["name3"] == "value3"); }
std::size_t encodeHdr(MessageHeader &hdr, Car &car, char *buffer, std::uint64_t offset, std::uint64_t bufferLength) { // encode the header hdr.wrap(buffer, offset, messageHeaderVersion, bufferLength) .blockLength(Car::sbeBlockLength()) .templateId(Car::sbeTemplateId()) .schemaId(Car::sbeSchemaId()) .version(Car::sbeSchemaVersion()); return hdr.encodedLength(); }
void MessageHeaderTest::testWrite() { MessageHeader mh; mh.set("name1", "value1"); mh.set("name2", "value2"); mh.set("name3", "value3"); std::ostringstream ostr; mh.write(ostr); std::string s = ostr.str(); assert (s == "name1: value1\r\nname2: value2\r\nname3: value3\r\n"); }
TEST(MessageHeader, testWrite) { MessageHeader mh; mh.set("name1", "value1"); mh.set("name2", "value2"); mh.set("name3", "value3"); std::ostringstream ostr; mh.write(ostr); std::string s = ostr.str(); EXPECT_TRUE (s == "name1: value1\r\nname2: value2\r\nname3: value3\r\n"); }
void MultipartWriterTest::testWriteOnePart() { std::ostringstream ostr; MultipartWriter w(ostr, "MIME_boundary_01234567"); assert (w.boundary() == "MIME_boundary_01234567"); MessageHeader h; h.set("name1", "value1"); w.nextPart(h); ostr << "this is part 1"; w.close(); std::string s = ostr.str(); assert (s == "--MIME_boundary_01234567\r\nname1: value1\r\n\r\nthis is part 1\r\n--MIME_boundary_01234567--\r\n"); }
TEST_F(CompositeElementsTest, shouldEncodeAndDecodeMsgCorrectly) { std::uint64_t sz = encodeHdrAndMsg(); ASSERT_EQ(sz, MessageHeader::encodedLength() + Msg::sbeBlockLength()); MessageHeader hdr; Msg msg; hdr.wrap(m_buffer, 0, Msg::sbeSchemaVersion(), sizeof(m_buffer)); EXPECT_EQ(hdr.blockLength(), Msg::sbeBlockLength()); EXPECT_EQ(hdr.templateId(), Msg::sbeTemplateId()); EXPECT_EQ(hdr.schemaId(), Msg::sbeSchemaId()); EXPECT_EQ(hdr.version(), Msg::sbeSchemaVersion()); msg.wrapForDecode(m_buffer, MessageHeader::encodedLength(), hdr.blockLength(), hdr.version(), sizeof(m_buffer)); EXPECT_EQ(msg.structure().enumOne(), EnumOne::Value::Value10); EXPECT_EQ(msg.structure().zeroth(), 42u); EXPECT_EQ(msg.structure().setOne().bit0(), false); EXPECT_EQ(msg.structure().setOne().bit16(), true); EXPECT_EQ(msg.structure().setOne().bit26(), false); EXPECT_EQ(msg.structure().inner().first(), 101l); EXPECT_EQ(msg.structure().inner().second(), 202l); EXPECT_EQ(msg.encodedLength(), sz - MessageHeader::encodedLength()); }