2

我正在通过 XDR 序列化一个数据包,但我不明白如何提供字符串向量。我在这里有一个小型的完全工作的序列化/反序列化,std::vector用于uint64_t. 这是我的代码:

序列化器

#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;
}

解串器

#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;
}

以下代码有效...运行./serializer | ./deserializer我得到 1 2 3。现在我不知道如何处理必须序列化std::vector<std::string>。单个字符串使用 xdr_string 效果很好。

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

任何帮助将不胜感激!

编辑:

我尝试了以下方法: 序列化程序

#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;
}

解串器

#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;
}

我很确定序列化程序的代码不是最好的,但至少它可以工作。但是反序列化器没有!我认为问题与我不知道在调用 xdr_array 之前要分配多少内存有关。有什么帮助吗?

4

1 回答 1

0

我让它工作:

编码器

#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;
}

解码器

#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;
}
于 2016-05-13T12:44:17.413 回答