test init request包 序列化的字符串 会多出字节 浮点型server解析不出来。

This commit is contained in:
2023-05-11 18:26:47 +08:00
parent ba80c3954d
commit c11a292f51
34 changed files with 555 additions and 351 deletions

View File

@@ -2,6 +2,24 @@
// Created by dongl on 23-4-15.
//
//#include "core/Client.h"
//#include "user/UserOperation.h"
//
//int main() {
// auto client = Client("127.0.0.1", 9999);
// client.run();
//
// auto user = new UserOperation(&client);
// user->login("783556037", "Aa316216");
//
//
//}
//
// Created by dongl on 23-4-15.
//
#include <cstdio>
#include <cstring>
#include "Request.h"
@@ -11,6 +29,7 @@
void client_read_cb(struct bufferevent *bev, void *ctx);
void client_write_cb(struct bufferevent *bev, void *ctx);
void event_cb(struct bufferevent *bev, short what, void *ctx);
int main(int argc, char **argv) {
event_base *base = event_base_new();
@@ -25,7 +44,7 @@ int main(int argc, char **argv) {
evutil_inet_pton(AF_INET, "127.0.0.1", &c_sin.sin_addr.s_addr);
bufferevent_setcb(bev, client_read_cb, client_write_cb, nullptr, nullptr);
bufferevent_setcb(bev, client_read_cb, client_write_cb, event_cb, nullptr);
bufferevent_enable(bev, EV_READ | EV_WRITE);
int ret = bufferevent_socket_connect(bev, (sockaddr *) &c_sin, sizeof(c_sin));
if (ret == 0) {
@@ -34,20 +53,31 @@ int main(int argc, char **argv) {
}
auto request = new Request(mp::MP_REQUEST_LOGIN, mp::MP_REQUEST_LOGIN_ACCOUNT, "783556037", "Aa316216");
auto packet = request->operator()();
auto packet = request->packet();
bufferevent_write(bev, packet.c_str(), packet.size());
delete request;
uint8_t l;
memcpy(&l, packet.c_str(), 1);
memcpy((void *) packet.c_str(), packet.c_str() + 1, packet.size() - 1);
std::string h;
memcpy((void *) h.c_str(), packet.c_str(), l);
char h[256];
memcpy(h, packet.c_str(), l);
char b[257];
strncpy(b, packet.c_str() + strlen(h), packet.size() - strlen(h));
auto mph = new mp::mph();
mph->ParseFromString(h);
printf("packet size: %u\n", mph->mpb_size());
printf("packet size: %zu\n", packet.size());
printf("h size: %hhu\n", l);
printf("b size: %hhu\n", mph->mpb_size());
printf("total size: %hhu\n", 1 + l + mph->mpb_size());
printf("str size: %zu\n", packet.size());
std::string temp;
temp.assign(packet.c_str(), packet.length());
printf("temp str: %s\n", packet.c_str());
fflush(stdout);
@@ -57,26 +87,31 @@ int main(int argc, char **argv) {
void client_read_cb(struct bufferevent *bev, void *ctx) {
printf("[read]: ");
// read L 读包长度
uint8_t packetLen;
bufferevent_read(bev, &packetLen, 1);
// read V 读包
char data_h[256] = {0};
bufferevent_read(bev, data_h, packetLen);
int i = 0;
while (i < 3) {
i++;
// read L 读包长度
uint8_t packetLen;
size_t len = bufferevent_read(bev, &packetLen, 1);
auto mph = std::make_shared<mp::mph>(mp::mph());
mph->ParseFromString(data_h);
printf("mph->mpb_size: %d\n", mph->mpb_size());
// read V 读包头
char data_h[256] = {0};
bufferevent_read(bev, data_h, packetLen);
auto mph = std::make_shared<mp::mph>(mp::mph());
mph->ParseFromString(data_h);
printf("mph->mpb_size: %d\n", mph->mpb_size());
// read V 读包体 包头内含有包体长度
char data_b[256] = {0};
bufferevent_read(bev, data_b, mph->mpb_size());
printf("data_b: %s\n", data_b);
// read V 读包体 包头内含有包体长度
char data_b[256] = {0};
bufferevent_read(bev, data_b, mph->mpb_size());
printf("data_b: %s\n", data_b);
mp::response* resp = new mp::response();
resp->ParseFromString(data_b);
printf("%s\n", resp->sri().sri_msg().c_str());
mp::response* resp = new mp::response();
resp->ParseFromString(data_b);
printf("%s\n", resp->sri().sri_msg().c_str());
}
fflush(stdout);
}
@@ -86,4 +121,18 @@ void client_write_cb(struct bufferevent *bev, void *ctx) {
// uint32_t len = login(2725096176, "Aa316216", data, sizeof(data));
// bufferevent_write(bev, data, len);
// sleep(3);
}
void event_cb(struct bufferevent *bev, short what, void *ctx) {
printf("[event]\n");
if (what == BEV_EVENT_ERROR) {
printf("[BEV_EVENT_ERROR]: %p\n", ctx);
}
else if (what == BEV_EVENT_EOF) {
printf("[BEV_EVENT_ERROR]: %s\n",/* strerror(EVUTIL_SOCKET_ERROR())*/ "xx");
}
else if (what == BEV_EVENT_CONNECTED) {
printf("[BEV_EVENT_ERROR]\n");
}
}