0

I got problem when I tried sended some User-defined Class from C++ server to Nodejs Server By MessagePack .

If I defined a variable to zero in my class then the decode will be broken.
But If I defined a variable not equal zero then it will works fine.

I'm confusing about the result, and Here is what I do:

//The class I tried to send
class MPTest
{
public:
    MPTest(void);
    ~MPTest(void);
    int ObjA;
    MSGPACK_DEFINE(ObjA);
}; 
//sending by curl lib
int httpConnecter::post(MPTest *pNode)
{
    CURL *curl;
    CURLcode res;
    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();
    std::vector<MPTest> vec;
     vec.push_back(*pNode);
    msgpack::sbuffer sbuf;
    msgpack::pack(sbuf, vec);

    msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size());
    msgpack::object deserialized = oh.get();
    std::vector<MPTest> unpackVec;
    deserialized.convert(unpackVec);

    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:3000/");
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, sbuf.data());
        fprintf(stderr, "check sbuf data: %s\n", sbuf.data());

        /* Perform the request, res will get the return code */ 
        res = curl_easy_perform(curl);
        /* Check for errors */ 
        if(res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));

        /* always cleanup */ 
        curl_easy_cleanup(curl);
    }
    return 0;
}
//My Nodejs server

const Hapi = require('hapi');
const MessagePack = require('what-the-pack');
const { encode, decode } = MessagePack.initialize(2**22);

const init = async () => {

    const server = Hapi.server({
        port: 3000,
        host: 'localhost'
    });

    server.route([{
        method: ['PUT', 'POST'],
        path: '/',
        handler: (request, h) => {
            try{
                console.log('1',request.payload);
                console.log('2',Buffer.from(request.payload));
                console.log('3',decode( request.payload));
            }catch(e){
                console.log('test!!!',e)
            }

            return 'Hello World from post!';
        },
        config: {
            payload: {
                output: 'data',
                parse: false
            }
        }
    }]);

    await server.start();
    console.log('Server running on %s', server.info.uri);
};

process.on('unhandledRejection', (err) => {

    console.log(err);
    process.exit(1);
});

init();

And when I put a not zero value to class. It ran well:

MPTest *a = new MPTest();
a->ObjA = 1;
   
httpConnecter *hConnecter = new httpConnecter();
   
hConnecter->post(a);

//Nodejs Result:
<Buffer 91 91 01 cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd ... >

[ [ 1 ] ]

But If I put a zero value to class. And It's broken...

MPTest *a = new MPTest();
a->ObjA = 0;
   
httpConnecter *hConnecter = new httpConnecter();
   
hConnecter->post(a);

//Nodejs Result:
<Buffer 91 91>
Error: @internalDecode : Error decoding value.
at internalDecode (D:\Pusher\PusherTestServer\node_modules\what-the-pack\index.js:550:13)
    at internalDecode (D:\Pusher\PusherTestServer\node_modules\what-the-pack\index.js:373:22)
    at internalDecode (D:\Pusher\PusherTestServer\node_modules\what-the-pack\index.js:373:22)
    at decode (D:\Pusher\PusherTestServer\node_modules\what-the-pack\index.js:556:20)
    at handler (D:\Pusher\PusherTestServer\dist\hapiHttpServer.js:21:33)
    at module.exports.internals.Manager.execute (D:\Pusher\PusherTestServer\node_modules\hapi\lib\toolkit.js:31:106)
    at Object.internals.handler (D:\Pusher\PusherTestServer\node_modules\hapi\lib\handler.js:46:48)
    at exports.execute (D:\Pusher\PusherTestServer\node_modules\hapi\lib\handler.js:31:36)
    at Request._lifecycle (D:\Pusher\PusherTestServer\node_modules\hapi\lib\request.js:312:68)
    at <anonymous>

Do someone know what's the problem?

1 Answers1

0

Okay, I solved the problem ,and I want to write some record .

You Just need to give sbuf size for libcurl, like this:
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, sbuf.size());

And then you will get a normal packet

<Buffer 91 92 00 >
[ [ 0 ] ]