Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9d46c67
[Refact] Change recv method non-blocking only to poll
ggstone0523 Mar 12, 2026
5bce4ef
[Refact] Change serialization logic
ggstone0523 Mar 13, 2026
f7de65c
[Fix] Change packet type before
ggstone0523 Mar 13, 2026
e0561b6
[Refact] Change serialization logic to compress packets
ggstone0523 Mar 13, 2026
d17d5ea
[Fix] Change buffer size of linux
ggstone0523 Mar 13, 2026
2a3ebaa
[Fix] fix magic value check logic
ggstone0523 Mar 13, 2026
6d80335
[Fix] fix non init packet structure to init
ggstone0523 Mar 13, 2026
b617c06
[Fix] fix decompress comment in user_data deserialize
ggstone0523 Mar 13, 2026
39c2161
[Fix] fix user_data deserialize
ggstone0523 Mar 13, 2026
b685633
[Refact] Add compress board array logic
ggstone0523 Mar 13, 2026
c8a1201
[Fix] fix board serialize logic
ggstone0523 Mar 13, 2026
b381b4e
[Fix] fix if statement in board serialize
ggstone0523 Mar 13, 2026
82dcaa3
[Refact] Change to avoid board compression if compressed is greater t…
ggstone0523 Mar 13, 2026
4798f22
[Fix] Fix serialize seg fault error
ggstone0523 Mar 13, 2026
8ab02c1
[Feat] Add other user timeout check logic
ggstone0523 Mar 13, 2026
ad84e68
[Fix] Change the player not to receive a timeout check
ggstone0523 Mar 13, 2026
7c3cbe8
[Fix] Change win condition
ggstone0523 Mar 13, 2026
ee2c091
[Fix] Change esc immediately get out of the room
ggstone0523 Mar 13, 2026
97609ea
[Feat] Add room timeout logic
ggstone0523 Mar 13, 2026
3b66ff6
[Fix] Add lobby re-rendering logic
ggstone0523 Mar 13, 2026
37e122e
[Fix] Change not rendering when change zero of room info
ggstone0523 Mar 13, 2026
2b54c12
[Fix] Add verify room packet logic
ggstone0523 Mar 14, 2026
faa6ace
[Feat] Add auto enter room logic
ggstone0523 Mar 14, 2026
9e4e37a
[Fix] fix time update
ggstone0523 Mar 14, 2026
c26e30c
[Fix] Fix not processing my packet
ggstone0523 Mar 14, 2026
c4ca0e3
[Fix] Change network multi and relay sending logic
ggstone0523 Mar 14, 2026
53277e9
[Fix] fix send same packet to same ip
ggstone0523 Mar 14, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/api/i_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class IRenderer

virtual void render_other_game_over(Packet& pkt) = 0;

virtual void render_other_timeout(std::string id) = 0;

virtual void render_win() = 0;

virtual void render_other_win(Packet& pkt) = 0;
Expand Down
1 change: 1 addition & 0 deletions core/include/engine/multi_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class MultiEngine : public Engine {
Timer timer;
std::vector<std::pair<std::string, std::string>> ids_ips;
std::unordered_map<std::string, std::string> active_user;
std::unordered_map<std::string, int> active_user_time_checker; // 7 => timeout
PacketStruct recv_pkt;

int curr_mino;
Expand Down
45 changes: 41 additions & 4 deletions core/include/util/network_packet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,55 @@
#define __PACKET_HPP__

#include <cstdint>
#define PACKET_SIZE (4 + (20 * 10 * 4) + (7 * 4) + 9)
#define PACKET_MAGIC 0x5041434B

enum PacketInfo
{
PACKET_SIZE = ((20 * 10 * 4) + (7 * 4) + 9),
BUFFER_SIZE = 1024,
PACKET_MAGIC = 0x5041434B,
PACKET_MAGIC_SIZE = 4,
PACKET_ID_SIZE = 9
};

/*
serialization packet
magic 4byte
flag bit 4byte : 0 => board / 1 => type / 2 => rotation / 3 => r / 4 => c / 5 => deleted_line / 6 => is_game_over / 7 => is_win / 8 => id
data ? byte
uint8_t is_board_compressed => 1: compress, 0: non-compress
uint8_t board[20][10]; => compress len + compress data
uint8_t type;
uint8_t rotation;
uint8_t r;
uint8_t c;
uint8_t deleted_line;
uint8_t is_game_over;
uint8_t is_win;
uint8_t id_len;
char id[9];
*/

enum FlagBitInfo : uint32_t
{
BOARD_BIT = (1 << 0),
TYPE_BIT = (1 << 1),
ROTATION_BIT = (1 << 2),
R_BIT = (1 << 3),
C_BIT = (1 << 4),
DELETED_LINE_BIT = (1 << 5),
IS_GAME_OVER_BIT = (1 << 6),
IS_WIN_BIT = (1 << 7),
ID_BIT = (1 << 8)
};

typedef struct PacketStruct {
int32_t magic;
int32_t board[20][10];
int32_t type;
int32_t rotation;
int32_t r;
int32_t c;
int32_t deleted_line;
int32_t is_game_over; // 1 => True, 0 => False
int32_t is_game_over;
int32_t is_win;
char id[9];
} Packet;
Expand Down
71 changes: 61 additions & 10 deletions core/source/engine/multi_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ void MultiEngine::init(bool is_server)
ids_ips = lobby->get_client_ids_ips();
active_user = lobby->get_ids(is_server);

for (auto [key, value] : active_user) {
if (key == lobby->get_my_id()) continue;
active_user_time_checker[key] = 0;
}

curr_mino = 0;
attack = 0;
is_line_fill_complete = false;
Expand Down Expand Up @@ -59,6 +64,33 @@ bool MultiEngine::run(bool is_server)

timer.set_curr_time();
if (timer.check_500ms_time()) {
for (auto it = active_user_time_checker.begin(); it != active_user_time_checker.end();) {
it->second++;
if (it->second >= 7) {
active_user.erase(it->first);
renderer->render_other_timeout(it->first);
it = active_user_time_checker.erase(it);
}
else {
++it;
}
}

if (active_user.size() == 1) {
renderer->render_win();
attack = rule->update_score();
is_game_over = 0;
is_win = 1;
if (is_server == true)
network->send_multi_udp(board, board.get_active_mino(), attack, is_game_over,
is_win, lobby->get_my_id(), ids_ips);
else
network->send_udp(board, board.get_active_mino(), attack, is_game_over, is_win,
lobby->get_server_ip_address(), lobby->get_my_id());
active_user.erase(lobby->get_my_id());
goto out;
}

rule->process(Action::DROP);
renderer->render_level(rule->get_level());
renderer->render_timer(timer.get_seconds());
Expand Down Expand Up @@ -93,11 +125,15 @@ bool MultiEngine::run(bool is_server)

if(network->recv_udp(recv_pkt))
{
if (active_user.find(recv_pkt.id) == active_user.end())
return true;
else
active_user_time_checker[recv_pkt.id] = 0;
renderer->render_other_board(recv_pkt);

if (recv_pkt.is_game_over == 1) {
renderer->render_other_game_over(recv_pkt);
active_user.erase(std::string(recv_pkt.id));
active_user.erase(recv_pkt.id);
}

if (active_user.size() == 1)
Expand Down Expand Up @@ -155,20 +191,35 @@ bool MultiEngine::stop(bool is_server)
if (active_user.size() > 0)
{
if (network->recv_udp(recv_pkt)) {
if (is_server) network->send_relay_udp(recv_pkt, ids_ips);
renderer->render_other_board(recv_pkt);
if (recv_pkt.is_game_over == 1) {
renderer->render_other_game_over(recv_pkt);
active_user.erase(std::string(recv_pkt.id));
}
if (recv_pkt.is_win == 1) {
renderer->render_other_win(recv_pkt);
goto out;
if (active_user.find(recv_pkt.id) != active_user.end()) {
active_user_time_checker[recv_pkt.id] = 0;

if (is_server) network->send_relay_udp(recv_pkt, ids_ips);
renderer->render_other_board(recv_pkt);
if (recv_pkt.is_game_over == 1) {
renderer->render_other_game_over(recv_pkt);
active_user.erase(recv_pkt.id);
}
if (recv_pkt.is_win == 1) {
renderer->render_other_win(recv_pkt);
goto out;
}
}
}

timer.set_curr_time();
if (timer.check_500ms_time()) {
for (auto it = active_user_time_checker.begin(); it != active_user_time_checker.end();) {
it->second++;
if (it->second >= 7) {
active_user.erase(it->first);
renderer->render_other_timeout(it->first);
it = active_user_time_checker.erase(it);
}
else {
++it;
}
}
if (is_server)
network->send_multi_udp(board, board.get_active_mino(), 0, is_game_over,
is_win, lobby->get_my_id(), ids_ips);
Expand Down
91 changes: 80 additions & 11 deletions lobby/include/lobby_network_packet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,102 @@
#define __LOBBY_PACKET_HPP__

#include <cstdint>
#define USER_DATA_SIZE (4 + 13 + 4 + 4 + 101)
#define ROOM_DATA_SIZE (4 + 69 + 9 + 4 + 9 + 101)
#define IDSIZE 9
#define MAXUSER 4
#define ROOMNAMESIZE 9
#define COMMENTSIZE 101

#define USER_DATA_MAGIC 0x55534552
#define ROOM_DATA_MAGIC 0x524F4F4D
enum LobbyPacketInfo
{
USER_DATA_SIZE = (4 + 13 + 4 + 4 + 101),
ROOM_DATA_SIZE = (4 + 69 + 9 + 4 + 9 + 101),
LOBBY_BUFFER_SIZE = 1024,
IDSIZE = 9,
MAXUSER = 4,
ROOMNAMESIZE = 9,
COMMENTSIZE = 101,
LOBBY_MAGIC_SIZE = 4,
USER_DATA_MAGIC = 0x55534552,
ROOM_DATA_MAGIC = 0x524F4F4D
};

/*
serialization packet
magic 4byte
flag bit 4byte : 0 => id / 1 => is_enter / 2 => is_out / 3 => is_chat / 4 => comment
data ? byte
uint8_t id_len;
char id[IDSIZE];
uint8_t is_enter;
uint8_t is_out;
uint8_t is_chat;
uint8_t comment_len;
char comment[COMMENTSIZE];
*/

enum LobbyUserFlagBitInfo : uint32_t
{
LOBBY_USER_ID_BIT = (1 << 0),
LOBBY_USER_IS_ENTER_BIT = (1 << 1),
LOBBY_USER_IS_OUT_BIT = (1 << 2),
LOBBY_USER_IS_CHAT_BIT = (1 << 3),
LOBBY_USER_COMMENT_BIT = (1 << 4),
};

typedef struct _user_data
{
int32_t magic;
char id[IDSIZE];
int32_t is_enter;
int32_t is_out;
int32_t is_chat;
char comment[COMMENTSIZE];
} user_data;

/*
serialization packet
magic 4byte
flag bit 4byte : 0 => room_master_id / 1 => id / 2 => room_name / 3 => id_len / 4 => is_enter_not_success / 5 => is_game_start
/ 6 => is_broadcast / 7 => is_update / 8 => is_broadcast_delete / 9 => is_chat / 10 => comment_id / 11 => comment
data ? byte
uint8_t room_master_id_len;
char room_master_id[IDSIZE];
uint8_t id_len;
[for id_len]
uint8_t user_per_id_len;
char id[MAXUSER][IDSIZE]
[end]
uint8_t room_name_len;
char room_name[ROOMNAMESIZE];
uint8_t is_enter_not_success;
uint8_t is_game_start;
uint8_t is_broadcast;
uint8_t is_update;
uint8_t is_broadcast_delete;
uint8_t is_chat;
uint8_t comment_id_len;
char comment_id[IDSIZE];
uint8_t comment_len;
char comment[COMMENTSIZE];
*/

enum LobbyRoomFlagBitInfo : uint32_t
{
LOBBY_ROOM_ROOM_MASTER_ID_BIT = (1 << 0),
LOBBY_ROOM_ID_BIT = (1 << 1),
LOBBY_ROOM_ROOM_NAME_BIT = (1 << 2),
LOBBY_ROOM_ID_LEN_BIT = (1 << 3),
LOBBY_ROOM_IS_ENTER_NOT_SUCCESS_BIT = (1 << 4),
LOBBY_ROOM_IS_GAME_START_BIT = (1 << 5),
LOBBY_ROOM_IS_BROADCAST_BIT = (1 << 6),
LOBBY_ROOM_IS_UPDATE_BIT = (1 << 7),
LOBBY_ROOM_IS_BROADCAST_DELETE_BIT = (1 << 8),
LOBBY_ROOM_IS_CHAT_BIT = (1 << 9),
LOBBY_ROOM_COMMENT_ID_BIT = (1 << 10),
LOBBY_ROOM_COMMENT_BIT = (1 << 11)
};

typedef struct _room_data
{
int32_t magic;
char room_master_id[IDSIZE];
int32_t id_len;
char id[MAXUSER][IDSIZE];
char room_name[ROOMNAMESIZE];
int32_t id_len;
int32_t is_enter_not_success;
int32_t is_game_start;
int32_t is_broadcast;
Expand Down
Loading
Loading