Commit d9a744aa authored by fallenstardust's avatar fallenstardust

set info to default value before creating duel_mode

add default member initializer to all packet
use uint16_t in NetServer, DuelClient
CGUITTFont: call final virtual function in constructor
parent f47f990f
...@@ -809,11 +809,6 @@ core::vector2di CGUITTFont::getKerning(const uchar32_t thisLetter, const uchar32 ...@@ -809,11 +809,6 @@ core::vector2di CGUITTFont::getKerning(const uchar32_t thisLetter, const uchar32
} }
void CGUITTFont::setInvisibleCharacters(const wchar_t *s) { void CGUITTFont::setInvisibleCharacters(const wchar_t *s) {
core::ustring us(s);
Invisible = us;
}
void CGUITTFont::setInvisibleCharacters(const core::ustring& s) {
Invisible = s; Invisible = s;
} }
......
...@@ -295,8 +295,7 @@ public: ...@@ -295,8 +295,7 @@ public:
s32 getKerningHeight() const override; s32 getKerningHeight() const override;
//! Define which characters should not be drawn by the font. //! Define which characters should not be drawn by the font.
void setInvisibleCharacters(const wchar_t *s) override; void setInvisibleCharacters(const wchar_t *s) final;
void setInvisibleCharacters(const core::ustring& s);
//! Get the last glyph page if there's still available slots. //! Get the last glyph page if there's still available slots.
//! If not, it will return zero. //! If not, it will return zero.
......
...@@ -52,31 +52,28 @@ public: ...@@ -52,31 +52,28 @@ public:
static void SendResponse(); static void SendResponse();
static void SendPacketToServer(unsigned char proto) { static void SendPacketToServer(unsigned char proto) {
auto p = duel_client_write; auto p = duel_client_write;
BufferIO::WriteInt16(p, 1); buffer_write<uint16_t>(p, 1);
BufferIO::WriteInt8(p, proto); buffer_write<uint8_t>(p, proto);
bufferevent_write(client_bev, duel_client_write, 3); bufferevent_write(client_bev, duel_client_write, 3);
} }
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;
if ((int)sizeof(ST) > MAX_DATA_SIZE) if (sizeof(ST) > MAX_DATA_SIZE)
return; return;
BufferIO::WriteInt16(p, (short)(1 + sizeof(ST))); buffer_write<uint16_t>(p, (uint16_t)(1 + sizeof(ST)));
BufferIO::WriteInt8(p, proto); buffer_write<uint8_t>(p, proto);
std::memcpy(p, &st, sizeof(ST)); std::memcpy(p, &st, sizeof(ST));
bufferevent_write(client_bev, duel_client_write, sizeof(ST) + 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;
int blen = len; if (len > MAX_DATA_SIZE)
if (blen < 0) len = MAX_DATA_SIZE;
return; buffer_write<uint16_t>(p, (uint16_t)(1 + len));
if (blen > MAX_DATA_SIZE) buffer_write<uint8_t>(p, proto);
blen = MAX_DATA_SIZE; std::memcpy(p, buffer, len);
BufferIO::WriteInt16(p, (short)(1 + blen)); bufferevent_write(client_bev, duel_client_write, len + 3);
BufferIO::WriteInt8(p, proto);
std::memcpy(p, buffer, blen);
bufferevent_write(client_bev, duel_client_write, blen + 3);
} }
static std::vector<HostPacket> hosts; static std::vector<HostPacket> hosts;
......
...@@ -11,7 +11,7 @@ event* NetServer::broadcast_ev = 0; ...@@ -11,7 +11,7 @@ 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_write[SIZE_NETWORK_BUFFER]; unsigned char NetServer::net_server_write[SIZE_NETWORK_BUFFER];
unsigned short NetServer::last_sent = 0; size_t NetServer::last_sent = 0;
bool NetServer::StartServer(unsigned short port) { bool NetServer::StartServer(unsigned short port) {
if(net_evbase) if(net_evbase)
...@@ -261,29 +261,37 @@ void NetServer::HandleCTOSPacket(DuelPlayer* dp, unsigned char* data, int len) { ...@@ -261,29 +261,37 @@ void NetServer::HandleCTOSPacket(DuelPlayer* dp, unsigned char* data, int len) {
CTOS_CreateGame packet; CTOS_CreateGame packet;
std::memcpy(&packet, pdata, sizeof packet); std::memcpy(&packet, pdata, sizeof packet);
auto pkt = &packet; auto pkt = &packet;
if(pkt->info.mode == MODE_SINGLE) {
duel_mode = new SingleDuel(false);
duel_mode->etimer = event_new(net_evbase, 0, EV_TIMEOUT | EV_PERSIST, SingleDuel::SingleTimer, duel_mode);
} else if(pkt->info.mode == MODE_MATCH) {
duel_mode = new SingleDuel(true);
duel_mode->etimer = event_new(net_evbase, 0, EV_TIMEOUT | EV_PERSIST, SingleDuel::SingleTimer, duel_mode);
} else if(pkt->info.mode == MODE_TAG) {
duel_mode = new TagDuel();
duel_mode->etimer = event_new(net_evbase, 0, EV_TIMEOUT | EV_PERSIST, TagDuel::TagTimer, duel_mode);
}
if(pkt->info.rule > CURRENT_RULE) if(pkt->info.rule > CURRENT_RULE)
pkt->info.rule = CURRENT_RULE; pkt->info.rule = CURRENT_RULE;
if(pkt->info.mode > MODE_TAG) if(pkt->info.mode > MODE_TAG)
pkt->info.mode = MODE_SINGLE; pkt->info.mode = MODE_SINGLE;
unsigned int hash = 1; bool found = false;
for(auto lfit = deckManager._lfList.begin(); lfit != deckManager._lfList.end(); ++lfit) { for (const auto& lflist : deckManager._lfList) {
if(pkt->info.lflist == lfit->hash) { if(pkt->info.lflist == lflist.hash) {
hash = pkt->info.lflist; found = true;
break; break;
} }
} }
if(hash == 1) if (!found) {
pkt->info.lflist = deckManager._lfList[0].hash; if (deckManager._lfList.size())
pkt->info.lflist = deckManager._lfList[0].hash;
else
pkt->info.lflist = 0;
}
if (pkt->info.mode == MODE_SINGLE) {
duel_mode = new SingleDuel(false);
duel_mode->etimer = event_new(net_evbase, 0, EV_TIMEOUT | EV_PERSIST, SingleDuel::SingleTimer, duel_mode);
}
else if (pkt->info.mode == MODE_MATCH) {
duel_mode = new SingleDuel(true);
duel_mode->etimer = event_new(net_evbase, 0, EV_TIMEOUT | EV_PERSIST, SingleDuel::SingleTimer, duel_mode);
}
else if (pkt->info.mode == MODE_TAG) {
duel_mode = new TagDuel();
duel_mode->etimer = event_new(net_evbase, 0, EV_TIMEOUT | EV_PERSIST, TagDuel::TagTimer, duel_mode);
}
else
return;
#ifdef _IRR_ANDROID_PLATFORM_ #ifdef _IRR_ANDROID_PLATFORM_
HostInfo tmp; HostInfo tmp;
memcpy(&tmp, &pkt->info, sizeof(struct HostInfo)); memcpy(&tmp, &pkt->info, sizeof(struct HostInfo));
......
...@@ -19,7 +19,7 @@ private: ...@@ -19,7 +19,7 @@ private:
static evconnlistener* listener; static evconnlistener* listener;
static DuelMode* duel_mode; static DuelMode* duel_mode;
static unsigned char net_server_write[SIZE_NETWORK_BUFFER]; static unsigned char net_server_write[SIZE_NETWORK_BUFFER];
static unsigned short last_sent; static size_t last_sent;
public: public:
static bool StartServer(unsigned short port); static bool StartServer(unsigned short port);
...@@ -38,20 +38,19 @@ public: ...@@ -38,20 +38,19 @@ public:
static size_t CreateChatPacket(unsigned char* src, int src_size, unsigned char* dst, uint16_t dst_player_type); static size_t CreateChatPacket(unsigned char* src, int src_size, unsigned char* dst, uint16_t dst_player_type);
static void SendPacketToPlayer(DuelPlayer* dp, unsigned char proto) { static void SendPacketToPlayer(DuelPlayer* dp, unsigned char proto) {
auto p = net_server_write; auto p = net_server_write;
BufferIO::WriteInt16(p, 1); buffer_write<uint16_t>(p, 1);
BufferIO::WriteInt8(p, proto); buffer_write<uint8_t>(p, proto);
last_sent = 3; last_sent = 3;
if(!dp) if (dp)
return; bufferevent_write(dp->bev, net_server_write, 3);
bufferevent_write(dp->bev, net_server_write, 3);
} }
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;
if ((int)sizeof(ST) > MAX_DATA_SIZE) if (sizeof(ST) > MAX_DATA_SIZE)
return; return;
BufferIO::WriteInt16(p, (short)(1 + sizeof(ST))); buffer_write<uint16_t>(p, (uint16_t)(1 + sizeof(ST)));
BufferIO::WriteInt8(p, proto); buffer_write<uint8_t>(p, proto);
std::memcpy(p, &st, sizeof(ST)); std::memcpy(p, &st, sizeof(ST));
last_sent = sizeof(ST) + 3; last_sent = sizeof(ST) + 3;
if (dp) if (dp)
...@@ -59,17 +58,14 @@ public: ...@@ -59,17 +58,14 @@ public:
} }
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;
int blen = len; if (len > MAX_DATA_SIZE)
if (blen < 0) len = MAX_DATA_SIZE;
return; buffer_write<uint16_t>(p, (uint16_t)(1 + len));
if (blen > MAX_DATA_SIZE) buffer_write<uint8_t>(p, proto);
blen = MAX_DATA_SIZE; std::memcpy(p, buffer, len);
BufferIO::WriteInt16(p, (short)(1 + blen)); last_sent = len + 3;
BufferIO::WriteInt8(p, proto);
std::memcpy(p, buffer, blen);
last_sent = blen + 3;
if (dp) if (dp)
bufferevent_write(dp->bev, net_server_write, blen + 3); bufferevent_write(dp->bev, net_server_write, len + 3);
} }
static void ReSendToPlayer(DuelPlayer* dp) { static void ReSendToPlayer(DuelPlayer* dp) {
if(dp) if(dp)
......
...@@ -36,86 +36,86 @@ check_trivially_copyable(HostInfo); ...@@ -36,86 +36,86 @@ check_trivially_copyable(HostInfo);
static_assert(sizeof(HostInfo) == 20, "size mismatch: HostInfo"); static_assert(sizeof(HostInfo) == 20, "size mismatch: HostInfo");
struct HostPacket { struct HostPacket {
uint16_t identifier; uint16_t identifier{};
uint16_t version; uint16_t version{};
uint16_t port; uint16_t port{};
// byte padding[2] // byte padding[2]
uint32_t ipaddr; uint32_t ipaddr{};
uint16_t name[20]; uint16_t name[20]{};
HostInfo host; HostInfo host;
}; };
check_trivially_copyable(HostPacket); check_trivially_copyable(HostPacket);
static_assert(sizeof(HostPacket) == 72, "size mismatch: HostPacket"); static_assert(sizeof(HostPacket) == 72, "size mismatch: HostPacket");
struct HostRequest { struct HostRequest {
uint16_t identifier; uint16_t identifier{};
}; };
check_trivially_copyable(HostRequest); check_trivially_copyable(HostRequest);
static_assert(sizeof(HostRequest) == 2, "size mismatch: HostRequest"); static_assert(sizeof(HostRequest) == 2, "size mismatch: HostRequest");
struct CTOS_HandResult { struct CTOS_HandResult {
unsigned char res; unsigned char res{};
}; };
check_trivially_copyable(CTOS_HandResult); check_trivially_copyable(CTOS_HandResult);
static_assert(sizeof(CTOS_HandResult) == 1, "size mismatch: CTOS_HandResult"); static_assert(sizeof(CTOS_HandResult) == 1, "size mismatch: CTOS_HandResult");
struct CTOS_TPResult { struct CTOS_TPResult {
unsigned char res; unsigned char res{};
}; };
check_trivially_copyable(CTOS_TPResult); check_trivially_copyable(CTOS_TPResult);
static_assert(sizeof(CTOS_TPResult) == 1, "size mismatch: CTOS_TPResult"); static_assert(sizeof(CTOS_TPResult) == 1, "size mismatch: CTOS_TPResult");
struct CTOS_PlayerInfo { struct CTOS_PlayerInfo {
uint16_t name[20]; uint16_t name[20]{};
}; };
check_trivially_copyable(CTOS_PlayerInfo); check_trivially_copyable(CTOS_PlayerInfo);
static_assert(sizeof(CTOS_PlayerInfo) == 40, "size mismatch: CTOS_PlayerInfo"); static_assert(sizeof(CTOS_PlayerInfo) == 40, "size mismatch: CTOS_PlayerInfo");
struct CTOS_CreateGame { struct CTOS_CreateGame {
HostInfo info; HostInfo info;
uint16_t name[20]; uint16_t name[20]{};
uint16_t pass[20]; uint16_t pass[20]{};
}; };
check_trivially_copyable(CTOS_CreateGame); check_trivially_copyable(CTOS_CreateGame);
static_assert(sizeof(CTOS_CreateGame) == 100, "size mismatch: CTOS_CreateGame"); static_assert(sizeof(CTOS_CreateGame) == 100, "size mismatch: CTOS_CreateGame");
struct CTOS_JoinGame { struct CTOS_JoinGame {
uint16_t version; uint16_t version{};
// byte padding[2] // byte padding[2]
uint32_t gameid; uint32_t gameid{};
uint16_t pass[20]; uint16_t pass[20]{};
}; };
check_trivially_copyable(CTOS_JoinGame); check_trivially_copyable(CTOS_JoinGame);
static_assert(sizeof(CTOS_JoinGame) == 48, "size mismatch: CTOS_JoinGame"); static_assert(sizeof(CTOS_JoinGame) == 48, "size mismatch: CTOS_JoinGame");
struct CTOS_Kick { struct CTOS_Kick {
unsigned char pos; unsigned char pos{};
}; };
check_trivially_copyable(CTOS_Kick); check_trivially_copyable(CTOS_Kick);
static_assert(sizeof(CTOS_Kick) == 1, "size mismatch: CTOS_Kick"); static_assert(sizeof(CTOS_Kick) == 1, "size mismatch: CTOS_Kick");
// STOC // STOC
struct STOC_ErrorMsg { struct STOC_ErrorMsg {
unsigned char msg; unsigned char msg{};
// byte padding[3] // byte padding[3]
uint32_t code; uint32_t code{};
}; };
check_trivially_copyable(STOC_ErrorMsg); check_trivially_copyable(STOC_ErrorMsg);
static_assert(sizeof(STOC_ErrorMsg) == 8, "size mismatch: STOC_ErrorMsg"); static_assert(sizeof(STOC_ErrorMsg) == 8, "size mismatch: STOC_ErrorMsg");
struct STOC_HandResult { struct STOC_HandResult {
unsigned char res1; unsigned char res1{};
unsigned char res2; unsigned char res2{};
}; };
check_trivially_copyable(STOC_HandResult); check_trivially_copyable(STOC_HandResult);
static_assert(sizeof(STOC_HandResult) == 2, "size mismatch: STOC_HandResult"); static_assert(sizeof(STOC_HandResult) == 2, "size mismatch: STOC_HandResult");
// reserved for STOC_CREATE_GAME // reserved for STOC_CREATE_GAME
struct STOC_CreateGame { struct STOC_CreateGame {
uint32_t gameid; uint32_t gameid{};
}; };
check_trivially_copyable(STOC_CreateGame); check_trivially_copyable(STOC_CreateGame);
static_assert(sizeof(STOC_CreateGame) == 4, "size mismatch: STOC_CreateGame"); static_assert(sizeof(STOC_CreateGame) == 4, "size mismatch: STOC_CreateGame");
...@@ -127,23 +127,23 @@ check_trivially_copyable(STOC_JoinGame); ...@@ -127,23 +127,23 @@ check_trivially_copyable(STOC_JoinGame);
static_assert(sizeof(STOC_JoinGame) == 20, "size mismatch: STOC_JoinGame"); static_assert(sizeof(STOC_JoinGame) == 20, "size mismatch: STOC_JoinGame");
struct STOC_TypeChange { struct STOC_TypeChange {
unsigned char type; unsigned char type{};
}; };
check_trivially_copyable(STOC_TypeChange); check_trivially_copyable(STOC_TypeChange);
static_assert(sizeof(STOC_TypeChange) == 1, "size mismatch: STOC_TypeChange"); static_assert(sizeof(STOC_TypeChange) == 1, "size mismatch: STOC_TypeChange");
// reserved for STOC_LEAVE_GAME // reserved for STOC_LEAVE_GAME
struct STOC_ExitGame { struct STOC_ExitGame {
unsigned char pos; unsigned char pos{};
}; };
check_trivially_copyable(STOC_ExitGame); check_trivially_copyable(STOC_ExitGame);
static_assert(sizeof(STOC_ExitGame) == 1, "size mismatch: STOC_ExitGame"); static_assert(sizeof(STOC_ExitGame) == 1, "size mismatch: STOC_ExitGame");
struct STOC_TimeLimit { struct STOC_TimeLimit {
unsigned char player; unsigned char player{};
// byte padding[1] // byte padding[1]
uint16_t left_time; uint16_t left_time{};
}; };
check_trivially_copyable(STOC_TimeLimit); check_trivially_copyable(STOC_TimeLimit);
static_assert(sizeof(STOC_TimeLimit) == 4, "size mismatch: STOC_TimeLimit"); static_assert(sizeof(STOC_TimeLimit) == 4, "size mismatch: STOC_TimeLimit");
...@@ -158,8 +158,8 @@ constexpr int LEN_CHAT_MSG = 256; ...@@ -158,8 +158,8 @@ constexpr int LEN_CHAT_MSG = 256;
constexpr int SIZE_STOC_CHAT = (LEN_CHAT_PLAYER + LEN_CHAT_MSG) * sizeof(uint16_t); constexpr int SIZE_STOC_CHAT = (LEN_CHAT_PLAYER + LEN_CHAT_MSG) * sizeof(uint16_t);
struct STOC_HS_PlayerEnter { struct STOC_HS_PlayerEnter {
uint16_t name[20]; uint16_t name[20]{};
unsigned char pos; unsigned char pos{};
// byte padding[1] // byte padding[1]
}; };
check_trivially_copyable(STOC_HS_PlayerEnter); check_trivially_copyable(STOC_HS_PlayerEnter);
...@@ -168,13 +168,13 @@ constexpr int STOC_HS_PlayerEnter_size = 41; //workwround ...@@ -168,13 +168,13 @@ constexpr int STOC_HS_PlayerEnter_size = 41; //workwround
struct STOC_HS_PlayerChange { struct STOC_HS_PlayerChange {
//pos<<4 | state //pos<<4 | state
unsigned char status; unsigned char status{};
}; };
check_trivially_copyable(STOC_HS_PlayerChange); check_trivially_copyable(STOC_HS_PlayerChange);
static_assert(sizeof(STOC_HS_PlayerChange) == 1, "size mismatch: STOC_HS_PlayerChange"); static_assert(sizeof(STOC_HS_PlayerChange) == 1, "size mismatch: STOC_HS_PlayerChange");
struct STOC_HS_WatchChange { struct STOC_HS_WatchChange {
uint16_t watch_count; uint16_t watch_count{};
}; };
check_trivially_copyable(STOC_HS_WatchChange); check_trivially_copyable(STOC_HS_WatchChange);
static_assert(sizeof(STOC_HS_WatchChange) == 2, "size mismatch: STOC_HS_WatchChange"); static_assert(sizeof(STOC_HS_WatchChange) == 2, "size mismatch: STOC_HS_WatchChange");
...@@ -183,10 +183,10 @@ class DuelMode; ...@@ -183,10 +183,10 @@ class DuelMode;
struct DuelPlayer { struct DuelPlayer {
unsigned short name[20]{}; unsigned short name[20]{};
DuelMode* game{ nullptr }; DuelMode* game{};
unsigned char type{ 0 }; unsigned char type{};
unsigned char state{ 0 }; unsigned char state{};
bufferevent* bev{ 0 }; bufferevent* bev{};
}; };
inline bool check_msg_size(int size) { inline bool check_msg_size(int size) {
......
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