back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/Makefile5
-rw-r--r--server/card_stack.c2
-rw-r--r--server/card_stack.h2
-rw-r--r--server/server.c29
-rw-r--r--server/server.h3
-rw-r--r--server/server_data_processing.c8
-rw-r--r--server/server_data_processing.h1
7 files changed, 40 insertions, 10 deletions
diff --git a/server/Makefile b/server/Makefile
index 6fb2c5c..bdae4ff 100644
--- a/server/Makefile
+++ b/server/Makefile
@@ -11,7 +11,12 @@ all: server
server: $(OBJMODULES)
$(CC) $(LIBS) $^ -o $@
+ifneq (clean, $(MAKECMDGOALS))
-include deps.mk
+endif
deps.mk: $(SRCMODULES)
$(CC) -MM $^ > $@
+
+clean:
+ rm -f *.o server
diff --git a/server/card_stack.c b/server/card_stack.c
index c7c18d9..bce0133 100644
--- a/server/card_stack.c
+++ b/server/card_stack.c
@@ -59,7 +59,7 @@ const char* remove_card_from_stack(player_cards *deck, const char *str)
return 0;
}
-const char* pop_stack(player_cards *deck)
+void pop_stack(player_cards *deck)
{
struct card_stack_item *tmp;
diff --git a/server/card_stack.h b/server/card_stack.h
index 321cbf0..8581eec 100644
--- a/server/card_stack.h
+++ b/server/card_stack.h
@@ -17,6 +17,6 @@ const char* remove_card_from_stack(player_cards *deck, const char *str);
struct card_stack_item* get_next_card_from_stack(player_cards deck,
player_cards prev);
int find_card_in_stack(player_cards involved_cards, const char *str);
-const char* pop_stack(player_cards *deck);
+void pop_stack(player_cards *deck);
#endif
diff --git a/server/server.c b/server/server.c
index 1655448..f842a70 100644
--- a/server/server.c
+++ b/server/server.c
@@ -56,6 +56,18 @@ static int accept_client(struct server *serv)
static void close_connection(struct server *serv)
{
+ int i;
+
+ for(i = 0; i < serv->max_sess_arr_size; ++i) {
+ if(!serv->sess_arr[i])
+ continue;
+ serv->sess_arr[i]->state = disconnect;
+ send_disconnect_status(serv->sess_arr[i]->fd);
+ close(serv->sess_arr[i]->fd);
+ free(serv->sess_arr[i]);
+ serv->sess_arr[i] = NULL;
+ }
+ serv->connected_players_counter = 0;
}
static int check_playable_player_number(struct server *serv)
@@ -76,6 +88,7 @@ static void init_new_game(struct server *serv)
who_attack = -1;
lowest_trump = 0;
+ serv->shuffled_deck_size = max_shuffled_deck_size;
serv->shuffled_deck = get_shuffled_deck();
serv->trump_card =
extract_trump_card(serv->shuffled_deck, &serv->shuffled_deck_size);
@@ -609,11 +622,6 @@ static void set_up_player_tracking(struct server *serv, fd_set *readfds,
switch(serv->state) {
case first_player:
- if(serv->sess_arr[i]->record) {
- FD_SET(i, writefds);
- *maxfd = i > *maxfd ? i : *maxfd;
- }
- break;
case confirmation_waiting:
FD_SET(i, readfds);
*maxfd = i > *maxfd ? i : *maxfd;
@@ -695,6 +703,9 @@ static void make_data_transfer(struct server *serv, fd_set *readfds,
result = print_message_for_first_player(i);
serv->sess_arr[i]->record = 0;
}
+ /* connection was closed? (result == 0) */
+ if(FD_ISSET(i, readfds))
+ result = read(i, serv->sess_arr[i]->buffer, max_buffer_size);
break;
case confirmation_waiting:
if(FD_ISSET(i, writefds) && serv->sess_arr[i]->record) {
@@ -755,13 +766,17 @@ static void make_data_transfer(struct server *serv, fd_set *readfds,
break;
}
}
- if(serv->state == start_game || serv->state == table)
+ if(serv->state == start_game || serv->state == table ||
+ serv->state == end_game)
sleep(2);
if(check_server_state_change_conditions(serv))
serv->change_server_state = 1;
/* connection is closed */
- if(!result)
+ if(!result) {
close_connection(serv);
+ serv->change_server_state = 0;
+ serv->state = no_players;
+ }
}
int main_loop(struct server *serv)
diff --git a/server/server.h b/server/server.h
index 3578606..5f7ffcc 100644
--- a/server/server.h
+++ b/server/server.h
@@ -42,7 +42,8 @@ enum client_game_states {
card_acceptance_status,
tossing_limit_status,
spectator,
- result
+ result,
+ disconnect
};
enum tossing_mode {
diff --git a/server/server_data_processing.c b/server/server_data_processing.c
index fc81996..df95b7f 100644
--- a/server/server_data_processing.c
+++ b/server/server_data_processing.c
@@ -45,6 +45,14 @@ int print_connected_players(int fd, int number)
return write_to_client(fd, data_size);
}
+int send_disconnect_status(int fd)
+{
+ int data_size;
+
+ data_size = sprintf(output_buffer, "%u\n", disconnect);
+ return write_to_client(fd, data_size);
+}
+
/*
* 1 - ready
* 0 - buffer overflow or player closed session
diff --git a/server/server_data_processing.h b/server/server_data_processing.h
index e6c1ab0..1aead51 100644
--- a/server/server_data_processing.h
+++ b/server/server_data_processing.h
@@ -5,6 +5,7 @@
int print_message_for_first_player(int fd);
int print_connected_players(int fd, int number);
+int send_disconnect_status(int fd);
int check_readiness(struct session *client);
/* for define spectator mode */
int print_game_part(const struct session *client, enum server_states ss,