简体   繁体   中英

XDR serializing variable lenght array of string

I am serializing a packet over XDR but i do not understand how to provide vector of string. I have here a small fully working serialization / deserialization for a std::vector of uint64_t . Here my code:

Serializer :

#include <stdio.h>
#include <iostream>
#include <rpc/rpc.h>
#include <vector>
#define MAX_LENGTH_ 100

int main(void)
{
    XDR xdr;
    xdrstdio_create(&xdr, stdout, XDR_ENCODE);

    std::vector<uint64_t> ids; // vector i want to send
    ids.push_back(1);
    ids.push_back(2);
    ids.push_back(3);

    // serializing the vector
    uint64_t *_ids = &ids[0];
    uint32_t size = ids.size();
    xdr_array(&xdr,(char**)(&_ids), &size, MAX_LENGTH_,sizeof(uint64_t),(xdrproc_t)xdr_u_long);

    return 1;
}

Deserializer :

#include <stdio.h>
#include <iostream>
#include <rpc/rpc.h>
#include <vector>
#define MAX_LENGTH_ 100

int main(void)
{
    XDR xdrs;
    xdrstdio_create(&xdrs, stdin, XDR_DECODE);

    uint64_t *ids_ = new uint64_t[MAX_LENGTH_];
    uint32_t size;
    bool status = xdr_array(&xdrs,(char**)(&ids_), &size, MAX_LENGTH_,
                        sizeof(uint64_t), (xdrproc_t)xdr_u_long);

    std::vector<uint64_t> ids(ids_,ids_+size);
    for(std::vector<uint64_t>::iterator it = ids.begin(); it != ids.end(); ++it) 
    {
        std::cout << *it <<std::endl;
    }

    return 1;
}

The following code works... running ./serializer | ./deserializer ./serializer | ./deserializer i obtain 1 2 3. Now I do not know how to handle having to serialize std::vector<std::string> . A single string works well using xdr_string.

http://linux.die.net/man/3/xdr_array

Any help would be very much appreciated!

EDIT:

I have tried the following: Serializer :

#include <stdio.h>
#include <iostream>
#include <rpc/rpc.h>
#include <vector>
#include <algorithm>
#include <cstring>


#define MAX_VECTOR_LENGTH_ 100
#define MAX_STRING_LENGTH_ 50

char *convert(const std::string & s)
{
   char *pc = new char[s.size()+1];
   std::strcpy(pc, s.c_str());
   return pc; 
}

int main(void)
{
    XDR xdr;
    xdrstdio_create(&xdr, stdout, XDR_ENCODE);

    std::vector<std::string> messages; // vector i want to send
    messages.push_back("this is");
    messages.push_back("my string");
    messages.push_back("vector test");

    // transform the vector to c style
    std::vector<char*> messagesCStyle;
    std::transform(messages.begin(), messages.end(), std::back_inserter(messagesCStyle), convert);  

    // serializing the vector
    char **_messages = &messagesCStyle[0];
    uint32_t size = messages.size();
    xdr_array(&xdr,(char**)(&_messages), &size, MAX_VECTOR_LENGTH_ * MAX_STRING_LENGTH_,sizeof(char),(xdrproc_t)xdr_string);

    return 1;
}

Deserializer :

#include <stdio.h>
#include <iostream>
#include <rpc/rpc.h>
#include <vector>

#define MAX_VECTOR_LENGTH_ 100
#define MAX_STRING_LENGTH_ 50

int main(void)
{
    XDR xdrs;
    xdrstdio_create(&xdrs, stdin, XDR_DECODE);

    std::vector<char*> messagesCStyle_;
    uint32_t size;
    bool status = xdr_array(&xdrs,(char**)(&messagesCStyle_), &size, MAX_VECTOR_LENGTH_,
                        MAX_STRING_LENGTH_, (xdrproc_t)xdr_string);

    for(std::vector<char*>::iterator it = messagesCStyle_.begin(); it != messagesCStyle_.end(); ++it) 
    {
        std::cout << *it <<std::endl;
    }

    return 1;
}

I am pretty sure the code for the Serializer is not best but at least it seams to work. However the deserializer does not!! I think the problem is related to the fact that i do not know how much memory to allocate before calling the xdr_array. Any help?

I made it work:

Encoder :

#include <stdio.h>
#include <iostream>
#include <rpc/rpc.h>
#include <vector>
#include <algorithm>
#include <cstring>


#define MAX_VECTOR_LENGTH_ 100
#define MAX_STRING_LENGTH_ 50

char *convert(const std::string & s)
{
   char *pc = new char[s.size()+1];
   std::strcpy(pc, s.c_str());
   return pc; 
}

int main(void)
{
    XDR xdr;
    xdrstdio_create(&xdr, stdout, XDR_ENCODE);

    std::vector<std::string> messages; // vector i want to send
    messages.push_back("this is");
    messages.push_back("my string");
    messages.push_back("vector test");
    messages.push_back("this is a relatively long string!!!");

    // transform the vector to c style
    std::vector<char*> messagesCStyle;
    std::transform(messages.begin(), messages.end(), 
        std::back_inserter(messagesCStyle), 
        [](const std::string & s){ 
           char *pc = new char[s.size()+1];
           std::strcpy(pc, s.c_str());
           return pc; 
        });  

    // serializing the vector
    char **_messages = &messagesCStyle[0];
    uint32_t size = messages.size();
    xdr_array(&xdr,(char**)(&_messages), &size, MAX_VECTOR_LENGTH_ * MAX_STRING_LENGTH_,sizeof(char*),(xdrproc_t)xdr_string);

    return 1;
}

Decoder :

#include <stdio.h>
#include <iostream>
#include <rpc/rpc.h>
#include <vector>

#define MAX_VECTOR_LENGTH_ 100
#define MAX_STRING_LENGTH_ 50

int main(void)
{
    XDR xdrs;
    uint32_t size;
    char** buffer = NULL;

    xdrstdio_create(&xdrs, stdin, XDR_DECODE);

    bool status = xdr_array(&xdrs, (char**) &buffer, &size, MAX_VECTOR_LENGTH_,
                        sizeof(char*), (xdrproc_t)xdr_string);

    std::cout << "status: " << status << std::endl;                    
    std::cout << "size: " << size << std::endl;

    std::vector<std::string> stringMessages_(buffer, buffer + size);

    for(std::vector<std::string>::iterator it = stringMessages_.begin(); it != stringMessages_.end(); ++it) 
    {
        std::cout << *it <<std::endl;
    }

    for (int i = 0; i < size; i++) {
        free(buffer[i]);
    }
    free(buffer);

    return 1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM