XDR序列化变量lenght数组的字符串

时间:2016-05-12 09:02:54

标签: c++ c arrays c-strings xdr

我正在通过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

以下代码有效...运行std::vector<std::string>我获得1 2 3.现在我不知道如何处理必须序列化#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; } 。使用xdr_string可以很好地使用单个字符串。

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

非常感谢任何帮助!

修改

我尝试过以下方法: 串行

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

解串器

    var grid = Xrm.Page.getControl("Produktrader").getGrid();
    grid.getRows().forEach(function (row) {
        var rowData = row.getData();
        if (rowData != null) {
              //delete row here
        }
    }
}

我很确定Serializer的代码并不是最好的,但至少它接缝才能工作。然而,解串器不会!!我认为问题与我在调用xdr_array之前不知道要分配多少内存这一事实有关。有什么帮助吗?

1 个答案:

答案 0 :(得分: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;
}