当前位置: 首页 > wzjs >正文

玉石电商网站建设方案推广网站制作

玉石电商网站建设方案,推广网站制作,什么网站可以做数据调查问卷,成人高考报名时间2021目录 1.序列化和反序列化的协议实现 (1)基类实现 (2)子类实现 2.计算功能实现 3.TCPServer调用 4.对会话和表示层的理解 5.网络计算器相关代码 (1)myProtocol.hpp (2)TcpSe…

目录

1.序列化和反序列化的协议实现

(1)基类实现

(2)子类实现

2.计算功能实现

3.TCPServer调用

4.对会话和表示层的理解

5.网络计算器相关代码

(1)myProtocol.hpp

(2)TcpServerMain.cc

(3)myProtocol.hpp


本文在前面TCPServer、线程池、日志已经实现的情况下,以网络计算器的实现为主线,重点理解OSI定义的七层模型,以及理解为什么后面精简到了五层。

1.序列化和反序列化的协议实现

客户端发消息前要对即将发出的字符串进行序列化,服务端收到字符串后要对字符串进行反序列化。之后执行完结果后同样需要序列化和反序列化的过程。除此之外,和之前的TCP服务没有区别了。

(1)基类实现

我们将公共部分提取出来,就是手动对json后的内容再次进行包装和解包的过程。

(2)子类实现

注意接收和回应的参数和返回值分别代表的含义即可,其余的都是字符串操作。

2.计算功能实现

接收结构化数据,返回结构化数据

3.TCPServer调用

利用前面实现好的类进行调用即可

4.对会话和表示层的理解

我们发现在代码中表示层和会话层都被嵌入进去了,而非冗余部分。表示层和会话层属于协议栈上层了,越往上,需求不一样,越难统一,因此表示层和会话层无法写到内核里,需要根据需求手动实现,并且表示层和会话层大多都有了相应的现成方案,大多数人只需要关注应用层即可。

因此,TCP/IP协议将上三层压成了一层,总体归为应用层,但事实上这三层永不可缺。

5.网络计算器相关代码

(1)myProtocol.hpp

#pragma once#include <iostream>
#include <string>
#include <sstream>
#include <fstream>#include <jsoncpp/json/json.h> //json方案需要包含的头文件using namespace std;// 结构化数据
class DataInfo
{
public:DataInfo(int _x = 0, char _oper = 0, int _y = 0, int _result = 0, int _error = 0): _x(_x), _oper(_oper), _y(_y), _result(_result), _error(_error){}int _x;char _oper;int _y;int _result;int _error;
};class myProtocol
{
protected:// myProtocol协议的一部分,专门用于信息的编码和解码string Encode(const string &message) // 专门负责encode{string encodedMessage = to_string(message.length()) + "\r\n" + message + "\r\n"; // 有效载荷长度+换行符+有效载荷+换行符return encodedMessage;}string Decode(const string &encodedMessage) // 专门负责decode{size_t first_pos = encodedMessage.find("\r\n"); // 查找第一个换行符if (first_pos == string::npos)return ""; // 如果没有找到第一个换行符,则返回空字符串int len = stoi(encodedMessage.substr(0, first_pos)); // 有效载荷长度if (encodedMessage.length() < first_pos + len + 4) // 如果有效载荷长度大于编码消息长度,则返回空字符串return "";return encodedMessage.substr(first_pos + 2, len); // 有效载荷}public:virtual string Serialize(DataInfo &info) = 0;				// 序列化,同时进行编码virtual DataInfo Deserialize(const string &encodedMessage) = 0; // 反序列化,同时进行解码virtual ~myProtocol() = default;
};class Request : public myProtocol
{
public:string Serialize(DataInfo &info) // 序列化,同时进行编码{Json::Value root;root["x"] = info._x;root["oper"] = info._oper;root["y"] = info._y;root["result"] = info._result; // 结果和错误码root["error"] = info._error;Json::StreamWriterBuilder writer;std::string json_string = Json::writeString(writer, root);return Encode(json_string); // 对json后的数据编码}DataInfo Deserialize(const string &encodedMessage) // 反序列化,同时进行解码{string str = Decode(encodedMessage); // 对json后的数据编码if (str.empty())return DataInfo(); // 反序列化失败,传入的数据不完整Json::Value root;Json::Reader reader;bool parsingSuccessful = reader.parse(str, root);if (!parsingSuccessful)return DataInfo();DataInfo info;info._x = root["x"].asInt();info._y = root["y"].asInt();info._oper = root["oper"].asInt();info._result = root["result"].asInt();info._error = root["error"].asInt();return info;}
};class Response : public myProtocol
{
public:string Serialize(DataInfo &info) // 序列化,同时进行编码{Json::Value root;root["x"] = info._x;root["oper"] = info._oper;root["y"] = info._y;root["result"] = info._result; // 结果和错误码root["error"] = info._error;Json::StreamWriterBuilder writer;std::string json_string = Json::writeString(writer, root);return Encode(json_string); // 对json后的数据编码}DataInfo Deserialize(const string &encodedMessage) // 反序列化,同时进行解码{string str = Decode(encodedMessage); // 对json后的数据编码if (str.empty())return DataInfo(); // 反序列化失败,传入的数据不完整Json::Value root;Json::Reader reader;bool parsingSuccessful = reader.parse(str, root);if (!parsingSuccessful)return DataInfo();DataInfo info;info._x = root["x"].asInt();info._y = root["y"].asInt();info._oper = root["oper"].asInt();info._result = root["result"].asInt();info._error = root["error"].asInt();return info;}
};

(2)TcpServerMain.cc


#include "TcpServer.hpp"int main()
{FILE_LOG(); // 打开日志文件,日志输出而非打印到屏幕,守护进程模式myDaemon(false, false);myCalculator calculator; // 创建一个计算器对象unique_ptr<TcpServer> server(make_unique<TcpServer>()); // 创建一个服务端对象server->start(); // 启动服务端return 0;
}

(3)myProtocol.hpp

#pragma once#include <iostream>
#include <string>
#include <sstream>
#include <fstream>#include <jsoncpp/json/json.h> //json方案需要包含的头文件using namespace std;// 结构化数据
class DataInfo
{
public:DataInfo(int _x = 0, char _oper = 0, int _y = 0, int _result = 0, int _error = 0): _x(_x), _oper(_oper), _y(_y), _result(_result), _error(_error){}int _x;char _oper;int _y;int _result;int _error;
};class myProtocol
{
protected:// myProtocol协议的一部分,专门用于信息的编码和解码string Encode(const string &message) // 专门负责encode{string encodedMessage = to_string(message.length()) + "\r\n" + message + "\r\n"; // 有效载荷长度+换行符+有效载荷+换行符return encodedMessage;}string Decode(const string &encodedMessage) // 专门负责decode{size_t first_pos = encodedMessage.find("\r\n"); // 查找第一个换行符if (first_pos == string::npos)return ""; // 如果没有找到第一个换行符,则返回空字符串int len = stoi(encodedMessage.substr(0, first_pos)); // 有效载荷长度if (encodedMessage.length() < first_pos + len + 4) // 如果有效载荷长度大于编码消息长度,则返回空字符串return "";return encodedMessage.substr(first_pos + 2, len); // 有效载荷}public:virtual string Serialize(DataInfo &info) = 0;				// 序列化,同时进行编码virtual DataInfo Deserialize(const string &encodedMessage) = 0; // 反序列化,同时进行解码virtual ~myProtocol() = default;
};class Request : public myProtocol
{
public:string Serialize(DataInfo &info) // 序列化,同时进行编码{Json::Value root;root["x"] = info._x;root["oper"] = info._oper;root["y"] = info._y;root["result"] = info._result; // 结果和错误码root["error"] = info._error;Json::StreamWriterBuilder writer;std::string json_string = Json::writeString(writer, root);return Encode(json_string); // 对json后的数据编码}DataInfo Deserialize(const string &encodedMessage) // 反序列化,同时进行解码{string str = Decode(encodedMessage); // 对json后的数据编码if (str.empty())return DataInfo(); // 反序列化失败,传入的数据不完整Json::Value root;Json::Reader reader;bool parsingSuccessful = reader.parse(str, root);if (!parsingSuccessful)return DataInfo();DataInfo info;info._x = root["x"].asInt();info._y = root["y"].asInt();info._oper = root["oper"].asInt();info._result = root["result"].asInt();info._error = root["error"].asInt();return info;}
};class Response : public myProtocol
{
public:string Serialize(DataInfo &info) // 序列化,同时进行编码{Json::Value root;root["x"] = info._x;root["oper"] = info._oper;root["y"] = info._y;root["result"] = info._result; // 结果和错误码root["error"] = info._error;Json::StreamWriterBuilder writer;std::string json_string = Json::writeString(writer, root);return Encode(json_string); // 对json后的数据编码}DataInfo Deserialize(const string &encodedMessage) // 反序列化,同时进行解码{string str = Decode(encodedMessage); // 对json后的数据编码if (str.empty())return DataInfo(); // 反序列化失败,传入的数据不完整Json::Value root;Json::Reader reader;bool parsingSuccessful = reader.parse(str, root);if (!parsingSuccessful)return DataInfo();DataInfo info;info._x = root["x"].asInt();info._y = root["y"].asInt();info._oper = root["oper"].asInt();info._result = root["result"].asInt();info._error = root["error"].asInt();return info;}
};
http://www.dtcms.com/wzjs/297741.html

相关文章:

  • 网站维护和推广足球世界排名国家最新
  • 网站建设的三要素百度关键词优化企业
  • 建设项目一次公示网站信阳网站推广公司
  • 网站如何建设二级域名代理免费网页制作成品
  • 苏州新区网站制作建设推建站系统哪个好
  • 网站建设价格需要多少钱制作网站教程
  • 如何做微信网站防封优化软件
  • 网站后台组成手机网页制作app
  • 亚马逊雨林十大恐怖生物seo服务 文库
  • 怎么提交网站google浏览器官网入口
  • 网站建设要经历哪些步骤?数据分析软件工具有哪些
  • 苏州网站推广排名网站站点
  • 有没有做相册的网站磁力王
  • 做娱乐性手机网站seo网站优化平台
  • 帝国cms模板网2023网站seo
  • java 做的网站长春网站制作推广
  • 长葛做网站娄底地seo
  • 局域网中做网站如何网络推广新产品
  • 网站建设策划书是由谁编写的怎么提升关键词的质量度
  • 华为手机网络推广方案朝阳区seo
  • 网站搭建响应式搜狗搜索引擎入口
  • 三网合一网站模板南宁优化推广服务
  • 腾讯云可以做网站电商培训内容有哪些
  • 山东省和住房建设厅网站百度平台我的订单
  • 织梦网站优化教程威海百度seo
  • 红酒企业网站模板免费下载宁波seo教程网
  • 金华大奇网站建设南宁百度关键词排名公司
  • 苏州艺术家网站建设网络优化
  • bing站长平台鞋子软文推广300字
  • 怎么自己做网站怎么赚钱微信运营