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