php - 如何将 PHP 对象转换为 C++ 对象
问题描述
我有一个用 C++ 编写的 TCP 服务器。它旨在发送一个固定大小的对象,并且可以在除一个之外的所有方式上完美运行。当我在网络浏览器中通过 php 连接到 tcp 服务器时,我不确定如何像在 c++ 中那样转换我的对象进行传输。
在 c++ 中,我使用以下内容:
#define MAX_MESSAGE 256
enum MESSAGE_TYPE {NO_TYPE, COMMAND, REQUEST, RESPONSE, POST, SETUP, QUIT};
enum PAYLOAD_TYPE {NO_LOAD, INT, UINT, FLOAT, DOUBLE, CHAR};
struct Message{
MESSAGE_TYPE type;
int client;
PAYLOAD_TYPE p_type;
char payload[MAX_MESSAGE];
Message (MESSAGE_TYPE _type, int _client){
type = _type;
client = _client;
}
/* UNSIGNED INT PAYLOAD */
Message (MESSAGE_TYPE _type, int _client, unsigned int value){
type = _type;
client = _client;
p_type = UINT;
memcpy(payload, &value, sizeof(unsigned int));
}
/* FLOAT PAYLOAD */
Message (MESSAGE_TYPE _type, int _client, float value){
type = _type;
client = _client;
p_type = FLOAT;
memcpy(payload, &value, sizeof(value));
}
}
然后像这样在传输时将其转换为 char* ..
Message m(SETUP,id);
channel->cwrite((char *)&m, sizeof(m)+1 );
我现在正在尝试创建一个 Web 客户端,用于将命令发送到服务器以及单个设备(自定义 IOT yada yada)。我可以使用 PHP 连接到服务器,但不确定我需要做什么才能在 PHP 中转换我的 Message 对象,以便服务器可以理解它,然后从服务器接收一个 Message 对象。
让我感到困惑的事情:当我检查类似 PHP 对象的大小时......
class Message{
public $type;
public $client;
public $p_type;
public $payload;
function __construct (){
$this->type = 1; // Fixed Test Value
$this->client = 22; // Fixed Test Value
$this->p_type = 1; // Fixed Test Value
$this->payload = new SplFixedArray(256);
}
}
它甚至不接近预期的 c++ 字节大小。我需要正确地转换我的 PHP 消息,以便 C++ 将重建一个正确的消息对象,但我不知道如何。
解决方案
推荐阅读
- javascript - 如何在作为数组对象保存的表中呈现数据行?
- c# - 在 Asp.Net C# 中搜索 XML 字符串的最快和最有效的方法
- reinforcement-learning - TypeError: QAgent() 没有参数
- azure-data-factory - Azure 数据工厂数据流 - 删除空列
- wordpress-theming - 所有博客页面左侧都显示空白影响视图,需要修复
- android - 证书固定 ssl 不适用于网络安全配置
- css - 为什么一个伪 ::before 三角形消失,溢出-y 设置为滚动?
- performance - 如何并行扩展集合?
- c++ - 会为纯虚函数分配默认函数吗?
- c - 尝试使用 SDL 播放 WAV 但收到嗡嗡声