File indexing completed on 2025-01-05 04:37:12

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "rpcmsg.h"
0007 #include <bcodec/bnode.h>
0008 #include <util/error.h>
0009 
0010 using namespace bt;
0011 
0012 namespace dht
0013 {
0014 RPCMsg::RPCMsg()
0015     : mtid(nullptr)
0016     , method(NONE)
0017     , type(INVALID)
0018 {
0019 }
0020 
0021 RPCMsg::RPCMsg(const QByteArray &mtid, Method m, Type type, const Key &id)
0022     : mtid(mtid)
0023     , method(m)
0024     , type(type)
0025     , id(id)
0026 {
0027 }
0028 
0029 RPCMsg::~RPCMsg()
0030 {
0031 }
0032 
0033 void RPCMsg::parse(bt::BDictNode *dict)
0034 {
0035     mtid = dict->getByteArray(TID);
0036     if (mtid.isEmpty())
0037         throw bt::Error("Invalid DHT transaction ID");
0038 
0039     QString t = dict->getString(TYP, nullptr);
0040     if (t == REQ) {
0041         type = REQ_MSG;
0042         BDictNode *args = dict->getDict(ARG);
0043         if (!args)
0044             return;
0045 
0046         id = Key(args->getByteArray("id"));
0047     } else if (t == RSP) {
0048         type = RSP_MSG;
0049         BDictNode *args = dict->getDict(RSP);
0050         if (!args)
0051             return;
0052 
0053         id = Key(args->getByteArray("id"));
0054     } else if (t == ERR_DHT)
0055         type = ERR_MSG;
0056     else
0057         throw bt::Error(QString("Unknown message type %1").arg(t));
0058 }
0059 }