代码拉取完成,页面将自动刷新
#pragma once
#include <iostream>
#include <string>
// 自定义序列化反序列化协议
const std::string blank_space_sep = " ";
const std::string protocol_sep = "\n";
//封装报文
std::string Encode(std::string &content){
//报文正文字节数
std::string package = std::to_string(content.size());
package += protocol_sep;
package += content; //用分隔符封装正文
package += protocol_sep;
return package;
}
//解析报文package-->"正文长度"\n"正文"\n
bool Decode(std::string &package, std::string& content){
size_t pos = package.find(protocol_sep);
if(pos == std::string::npos) return false;
//解析报文正文长度
size_t Len = std::atoi(package.substr(0,pos).c_str());
//确定报文是否完整
size_t total_Len = pos + Len + 2;
if(package.size() != total_Len) return false;
//获取正文内容
content = package.substr(pos+1,Len);
package.erase(0,total_Len);
return true;
}
//用户层协议请求结构体
class Request{
public:
int x;
int y;
char op;
public:
Request(int data1 , int data2 , char op)
: x(data1),y(data2),op(op){}
Request(){}
public:
//请求结构体 序列化 成报文正文字符串 "x op y"
bool Serialize(std::string& out){
std::string content = std::to_string(x);
content += blank_space_sep;
content += op;
content += blank_space_sep;
content += std::to_string(y);
out = content;
return true;
// 等价的jason代码
// Json::Value root;
// root["x"] = x;
// root["y"] = y;
// root["op"] = op;
// // Json::FastWriter w;
// Json::StyledWriter w;
// out = w.write(root);
// return true;
}
//报文正文字符串 反序列化 成请求结构体
// "x op y"
bool Deserialize(const std::string &in) {
size_t left = in.find(blank_space_sep);
if(left == std::string::npos)return false;
x = std::stoi(in.substr(0,left).c_str());
std::size_t right = in.rfind(blank_space_sep);
if (right == std::string::npos)return false;
y = std::atoi(in.substr(right + 1).c_str());
if(left + 2 != right) return false;
op = in[left+1];
return true;
// 等价的jason代码
// Json::Value root;
// Json::Reader r;
// r.parse(in, root);
// x = root["x"].asInt();
// y = root["y"].asInt();
// op = root["op"].asInt();
// return true;
}
void DebugPrint()
{
std::cout << "新请求构建完成: " << x << op << y << "=?" << std::endl;
}
};
//用户层协议请求回应结构体
class Response{
public:
int result;
int code;
public:
Response(int res , int c)
: result(res),code(c){}
Response(){}
public:
//请求回应结构体 序列化 成报文正文字符串 "result code"
bool Serialize(std::string& out){
std::string s = std::to_string(result);
s += blank_space_sep;
s += std::to_string(code);
out = s;
return true;
// 等价的jason代码
// Json::Value root;
// root["result"] = result;
// root["code"] = code;
// // Json::FastWriter w;
// Json::StyledWriter w;
// out = w.write(root);
// return true;
}
//"result code"
//报文正文字符串 反序列化 成请求回应结构体
bool Deserialize(const std::string &in)
{
std::size_t pos = in.find(blank_space_sep);
if (pos == std::string::npos)return false;
if(pos == 0 || pos == in.size() - 1) return false;
result = std::stoi(in.substr(0, pos).c_str());
code = std::stoi(in.substr(pos+1).c_str());
return true;
// 等价的jason代码
// Json::Value root;
// Json::Reader r;
// r.parse(in, root);
// result = root["result"].asInt();
// code = root["code"].asInt();
// return true;
}
void DebugPrint()
{
std::cout << "结果响应完成, result: " << result << ", code: "<< code << std::endl;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。