Ai
1 Star 0 Fork 0

周乃青/LinuxLearning

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Protocol.cpp 4.10 KB
一键复制 编辑 原始数据 按行查看 历史
周乃青 提交于 2024-02-21 00:00 +08:00 . Httpbegin
#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;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/c-learning--c_0/LinuxLearning.git
git@gitee.com:c-learning--c_0/LinuxLearning.git
c-learning--c_0
LinuxLearning
LinuxLearning
master

搜索帮助