Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-是否有方法反序列化在C+中序列化的结构+;推进ASIO?_C#_C++_Boost Asio - Fatal编程技术网

C#-是否有方法反序列化在C+中序列化的结构+;推进ASIO?

C#-是否有方法反序列化在C+中序列化的结构+;推进ASIO?,c#,c++,boost-asio,C#,C++,Boost Asio,我使用BoostASIO作为TCP服务器网络解决方案 下面是我如何在C++中序列化结构: struct AccountRole { int id; std::string name; template <typename Archive> void serialize(Archive& ar, const unsigned int version) { ar & id; ar & nam

我使用BoostASIO作为TCP服务器网络解决方案

下面是我如何在C++中序列化结构:

struct AccountRole {
    int id;
    std::string name;

    template <typename Archive>
    void serialize(Archive& ar, const unsigned int version)
    {
        ar & id;
        ar & name;
    }
};

boost::asio::connect(s, resolver.resolve( defaultIP,config.GetConfigValue("AuthServerPort", defaultPort)));

size_t const header_length = 8;
size_t const body_size_b = 8;
std::string data;


ServerOpcode opc;
opc = ServerOpcode::SMSG_LOGIN_REQUEST_RESPONSE_TEST;

Vibranium::AccountRole ac;
ac.id = 5;
ac.name = "John Snow";

std::string message;

std::ostringstream struct_stream;
boost::archive::text_oarchive archive(struct_stream);
archive << ac;
message = struct_stream.str();
message.resize(message.length());
struct AccountRole{
int-id;
std::字符串名;
模板
无效序列化(存档和ar,常量未签名整数版本)
{
ar&id;
ar&name;
}
};
boost::asio::connect(s,resolver.resolve(defaultIP,config.GetConfigValue(“AuthServerPort”,defaultPort));
大小常数头长度=8;
大小常数体大小b=8;
std::字符串数据;
服务器操作码;
opc=服务器操作码::SMSG\u登录\u请求\u响应\u测试;
弧菌::AccountRole ac;
ac.id=5;
ac.name=“约翰·斯诺”;
std::字符串消息;
std::ostringstream struct_stream;
boost::archive::text\u oarchive归档(struct\u stream);

来看看C#中的BinaryReader类。我能够序列化和反序列化C和C++之间的二进制数据。另一种替代方法是使用GRP,那么像
std::string
这样的结构中的类型在
C#
中不存在呢?它是否处理得很好?在C++中字符串是痛苦的。您必须知道它是如何序列化的。很可能是长度后跟字符串。您必须知道长度使用什么类型(byte、short、int或可变宽度数字),以及使用什么字符串编码。最有可能的编码是ASCII,每个字节有1个字符,但它也可以是宽字符。找出C++格式,使用BialRead在C中读取它。Boost序列化仅用于Boost系列化的读取,使用跨平台的序列化,如无法更改序列化的原BuffFi。您可以考虑创建C++ /CLI项目并在其中进行反序列化。
std::ostringstream header_stream;
header_stream << std::setw(header_length) << std::hex << opc;

std::ostringstream body_size_stream;
body_size_stream << std::setw(body_size_b) << std::hex << message.length();

data += header_stream.str();
data += body_size_stream.str();
data +=  message;
size_t request_length = data.length();


boost::asio::write(s, boost::asio::buffer(data,request_length));
   Vibranium::AccountRole ac;

   try
   {
       std::string archive_data(&_packet.body_[0], _packet.body_in_bytes);
       std::istringstream archive_stream(archive_data);
       boost::archive::text_iarchive archive(archive_stream);
       archive >> ac;
   }
   catch (std::exception& e)
   {
       std::cout << "Error bace!" << std::endl;
       // Unable to decode data.
       boost::system::error_code error(boost::asio::error::invalid_argument);
       return;
   }