Commit bb82764e authored by Chen Bill's avatar Chen Bill Committed by GitHub

Use local variable in ServerEchoRead, ClientRead (#2562)

* use local var in ServerEchoRead, ClientRead

* zero init in GetResponse
parent b6486f86
...@@ -19,8 +19,6 @@ unsigned char DuelClient::selftype = 0; ...@@ -19,8 +19,6 @@ unsigned char DuelClient::selftype = 0;
bool DuelClient::is_host = false; bool DuelClient::is_host = false;
event_base* DuelClient::client_base = 0; event_base* DuelClient::client_base = 0;
bufferevent* DuelClient::client_bev = 0; bufferevent* DuelClient::client_bev = 0;
unsigned char DuelClient::duel_client_read[SIZE_NETWORK_BUFFER];
int DuelClient::read_len = 0;
unsigned char DuelClient::duel_client_write[SIZE_NETWORK_BUFFER]; unsigned char DuelClient::duel_client_write[SIZE_NETWORK_BUFFER];
bool DuelClient::is_closing = false; bool DuelClient::is_closing = false;
bool DuelClient::is_swapping = false; bool DuelClient::is_swapping = false;
...@@ -50,6 +48,7 @@ bool DuelClient::StartClient(unsigned int ip, unsigned short port, bool create_g ...@@ -50,6 +48,7 @@ bool DuelClient::StartClient(unsigned int ip, unsigned short port, bool create_g
sin.sin_addr.s_addr = htonl(ip); sin.sin_addr.s_addr = htonl(ip);
sin.sin_port = htons(port); sin.sin_port = htons(port);
client_bev = bufferevent_socket_new(client_base, -1, BEV_OPT_CLOSE_ON_FREE); client_bev = bufferevent_socket_new(client_base, -1, BEV_OPT_CLOSE_ON_FREE);
bufferevent_setwatermark(client_bev, EV_READ, 3, 0);
bufferevent_setcb(client_bev, ClientRead, NULL, ClientEvent, (void*)create_game); bufferevent_setcb(client_bev, ClientRead, NULL, ClientEvent, (void*)create_game);
if (bufferevent_socket_connect(client_bev, (sockaddr*)&sin, sizeof(sin)) < 0) { if (bufferevent_socket_connect(client_bev, (sockaddr*)&sin, sizeof(sin)) < 0) {
bufferevent_free(client_bev); bufferevent_free(client_bev);
...@@ -100,24 +99,23 @@ void DuelClient::StopClient(bool is_exiting) { ...@@ -100,24 +99,23 @@ void DuelClient::StopClient(bool is_exiting) {
void DuelClient::ClientRead(bufferevent* bev, void* ctx) { void DuelClient::ClientRead(bufferevent* bev, void* ctx) {
evbuffer* input = bufferevent_get_input(bev); evbuffer* input = bufferevent_get_input(bev);
int len = evbuffer_get_length(input); int len = evbuffer_get_length(input);
unsigned short packet_len = 0; unsigned char* duel_client_read = new unsigned char[std::min(len, SIZE_NETWORK_BUFFER)];
while(true) { unsigned short packet_len;
if(len < 2) while (len >= 2) {
return; evbuffer_copyout(input, &packet_len, sizeof packet_len);
evbuffer_copyout(input, &packet_len, 2);
if (packet_len + 2 > SIZE_NETWORK_BUFFER) { if (packet_len + 2 > SIZE_NETWORK_BUFFER) {
delete[] duel_client_read;
ClientEvent(bev, BEV_EVENT_ERROR, 0); ClientEvent(bev, BEV_EVENT_ERROR, 0);
return; return;
} }
if(len < packet_len + 2) if (len < packet_len + 2)
return; break;
if (packet_len < 1) int read_len = evbuffer_remove(input, duel_client_read, packet_len + 2);
return;
read_len = evbuffer_remove(input, duel_client_read, packet_len + 2);
if (read_len >= 3) if (read_len >= 3)
HandleSTOCPacketLan(&duel_client_read[2], read_len - 2); HandleSTOCPacketLan(&duel_client_read[2], read_len - 2);
len -= packet_len + 2; len -= packet_len + 2;
} }
delete[] duel_client_read;
} }
void DuelClient::ClientEvent(bufferevent *bev, short events, void *ctx) { void DuelClient::ClientEvent(bufferevent *bev, short events, void *ctx) {
if (events & BEV_EVENT_CONNECTED) { if (events & BEV_EVENT_CONNECTED) {
......
...@@ -21,8 +21,6 @@ private: ...@@ -21,8 +21,6 @@ private:
static bool is_host; static bool is_host;
static event_base* client_base; static event_base* client_base;
static bufferevent* client_bev; static bufferevent* client_bev;
static unsigned char duel_client_read[SIZE_NETWORK_BUFFER];
static int read_len;
static unsigned char duel_client_write[SIZE_NETWORK_BUFFER]; static unsigned char duel_client_write[SIZE_NETWORK_BUFFER];
static bool is_closing; static bool is_closing;
static bool is_swapping; static bool is_swapping;
...@@ -56,13 +54,12 @@ public: ...@@ -56,13 +54,12 @@ public:
template<typename ST> template<typename ST>
static void SendPacketToServer(unsigned char proto, ST& st) { static void SendPacketToServer(unsigned char proto, ST& st) {
auto p = duel_client_write; auto p = duel_client_write;
int blen = sizeof(ST); if ((int)sizeof(ST) > MAX_DATA_SIZE)
if (blen > MAX_DATA_SIZE)
return; return;
BufferIO::WriteInt16(p, (short)(1 + blen)); BufferIO::WriteInt16(p, (short)(1 + sizeof(ST)));
BufferIO::WriteInt8(p, proto); BufferIO::WriteInt8(p, proto);
std::memcpy(p, &st, blen); std::memcpy(p, &st, sizeof(ST));
bufferevent_write(client_bev, duel_client_write, blen + 3); bufferevent_write(client_bev, duel_client_write, sizeof(ST) + 3);
} }
static void SendBufferToServer(unsigned char proto, void* buffer, size_t len) { static void SendBufferToServer(unsigned char proto, void* buffer, size_t len) {
auto p = duel_client_write; auto p = duel_client_write;
......
...@@ -9,8 +9,6 @@ event_base* NetServer::net_evbase = 0; ...@@ -9,8 +9,6 @@ event_base* NetServer::net_evbase = 0;
event* NetServer::broadcast_ev = 0; event* NetServer::broadcast_ev = 0;
evconnlistener* NetServer::listener = 0; evconnlistener* NetServer::listener = 0;
DuelMode* NetServer::duel_mode = 0; DuelMode* NetServer::duel_mode = 0;
unsigned char NetServer::net_server_read[SIZE_NETWORK_BUFFER];
int NetServer::read_len = 0;
unsigned char NetServer::net_server_write[SIZE_NETWORK_BUFFER]; unsigned char NetServer::net_server_write[SIZE_NETWORK_BUFFER];
unsigned short NetServer::last_sent = 0; unsigned short NetServer::last_sent = 0;
...@@ -109,6 +107,7 @@ void NetServer::ServerAccept(evconnlistener* listener, evutil_socket_t fd, socka ...@@ -109,6 +107,7 @@ void NetServer::ServerAccept(evconnlistener* listener, evutil_socket_t fd, socka
dp.type = 0xff; dp.type = 0xff;
dp.bev = bev; dp.bev = bev;
users[bev] = dp; users[bev] = dp;
bufferevent_setwatermark(bev, EV_READ, 3, 0);
bufferevent_setcb(bev, ServerEchoRead, NULL, ServerEchoEvent, NULL); bufferevent_setcb(bev, ServerEchoRead, NULL, ServerEchoEvent, NULL);
bufferevent_enable(bev, EV_READ); bufferevent_enable(bev, EV_READ);
} }
...@@ -123,24 +122,23 @@ void NetServer::ServerAcceptError(evconnlistener* listener, void* ctx) { ...@@ -123,24 +122,23 @@ void NetServer::ServerAcceptError(evconnlistener* listener, void* ctx) {
void NetServer::ServerEchoRead(bufferevent *bev, void *ctx) { void NetServer::ServerEchoRead(bufferevent *bev, void *ctx) {
evbuffer* input = bufferevent_get_input(bev); evbuffer* input = bufferevent_get_input(bev);
int len = evbuffer_get_length(input); int len = evbuffer_get_length(input);
unsigned short packet_len = 0; unsigned char* net_server_read = new unsigned char[std::min(len, SIZE_NETWORK_BUFFER)];
while(true) { unsigned short packet_len;
if(len < 2) while (len >= 2) {
return; evbuffer_copyout(input, &packet_len, sizeof packet_len);
evbuffer_copyout(input, &packet_len, 2);
if (packet_len + 2 > SIZE_NETWORK_BUFFER) { if (packet_len + 2 > SIZE_NETWORK_BUFFER) {
delete[] net_server_read;
ServerEchoEvent(bev, BEV_EVENT_ERROR, 0); ServerEchoEvent(bev, BEV_EVENT_ERROR, 0);
return; return;
} }
if (len < packet_len + 2) if (len < packet_len + 2)
return; break;
if (packet_len < 1) int read_len = evbuffer_remove(input, net_server_read, packet_len + 2);
return;
read_len = evbuffer_remove(input, net_server_read, packet_len + 2);
if (read_len >= 3) if (read_len >= 3)
HandleCTOSPacket(&users[bev], &net_server_read[2], read_len - 2); HandleCTOSPacket(&users[bev], &net_server_read[2], read_len - 2);
len -= packet_len + 2; len -= packet_len + 2;
} }
delete[] net_server_read;
} }
void NetServer::ServerEchoEvent(bufferevent* bev, short events, void* ctx) { void NetServer::ServerEchoEvent(bufferevent* bev, short events, void* ctx) {
if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) { if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
......
...@@ -18,8 +18,6 @@ private: ...@@ -18,8 +18,6 @@ private:
static event* broadcast_ev; static event* broadcast_ev;
static evconnlistener* listener; static evconnlistener* listener;
static DuelMode* duel_mode; static DuelMode* duel_mode;
static unsigned char net_server_read[SIZE_NETWORK_BUFFER];
static int read_len;
static unsigned char net_server_write[SIZE_NETWORK_BUFFER]; static unsigned char net_server_write[SIZE_NETWORK_BUFFER];
static unsigned short last_sent; static unsigned short last_sent;
...@@ -50,15 +48,14 @@ public: ...@@ -50,15 +48,14 @@ public:
template<typename ST> template<typename ST>
static void SendPacketToPlayer(DuelPlayer* dp, unsigned char proto, ST& st) { static void SendPacketToPlayer(DuelPlayer* dp, unsigned char proto, ST& st) {
auto p = net_server_write; auto p = net_server_write;
int blen = sizeof(ST); if ((int)sizeof(ST) > MAX_DATA_SIZE)
if (blen > MAX_DATA_SIZE)
return; return;
BufferIO::WriteInt16(p, (short)(1 + blen)); BufferIO::WriteInt16(p, (short)(1 + sizeof(ST)));
BufferIO::WriteInt8(p, proto); BufferIO::WriteInt8(p, proto);
std::memcpy(p, &st, blen); std::memcpy(p, &st, sizeof(ST));
last_sent = blen + 3; last_sent = sizeof(ST) + 3;
if (dp) if (dp)
bufferevent_write(dp->bev, net_server_write, blen + 3); bufferevent_write(dp->bev, net_server_write, sizeof(ST) + 3);
} }
static void SendBufferToPlayer(DuelPlayer* dp, unsigned char proto, void* buffer, size_t len) { static void SendBufferToPlayer(DuelPlayer* dp, unsigned char proto, void* buffer, size_t len) {
auto p = net_server_write; auto p = net_server_write;
......
...@@ -1418,7 +1418,7 @@ int SingleDuel::Analyze(unsigned char* msgbuffer, unsigned int len) { ...@@ -1418,7 +1418,7 @@ int SingleDuel::Analyze(unsigned char* msgbuffer, unsigned int len) {
return 0; return 0;
} }
void SingleDuel::GetResponse(DuelPlayer* dp, unsigned char* pdata, unsigned int len) { void SingleDuel::GetResponse(DuelPlayer* dp, unsigned char* pdata, unsigned int len) {
byte resb[SIZE_RETURN_VALUE]; byte resb[SIZE_RETURN_VALUE]{};
if (len > SIZE_RETURN_VALUE) if (len > SIZE_RETURN_VALUE)
len = SIZE_RETURN_VALUE; len = SIZE_RETURN_VALUE;
std::memcpy(resb, pdata, len); std::memcpy(resb, pdata, len);
......
...@@ -1533,7 +1533,7 @@ int TagDuel::Analyze(unsigned char* msgbuffer, unsigned int len) { ...@@ -1533,7 +1533,7 @@ int TagDuel::Analyze(unsigned char* msgbuffer, unsigned int len) {
return 0; return 0;
} }
void TagDuel::GetResponse(DuelPlayer* dp, unsigned char* pdata, unsigned int len) { void TagDuel::GetResponse(DuelPlayer* dp, unsigned char* pdata, unsigned int len) {
byte resb[SIZE_RETURN_VALUE]; byte resb[SIZE_RETURN_VALUE]{};
if (len > SIZE_RETURN_VALUE) if (len > SIZE_RETURN_VALUE)
len = SIZE_RETURN_VALUE; len = SIZE_RETURN_VALUE;
std::memcpy(resb, pdata, len); std::memcpy(resb, pdata, len);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment