back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/client/client.c
diff options
context:
space:
mode:
Diffstat (limited to 'client/client.c')
-rw-r--r--client/client.c42
1 files changed, 31 insertions, 11 deletions
diff --git a/client/client.c b/client/client.c
index 5d49e1a..66bfb01 100644
--- a/client/client.c
+++ b/client/client.c
@@ -4,7 +4,7 @@
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
-#include <stdio.h> /* test */
+#include <stdio.h> /* for debug */
#include "client.h"
#include "data_decryption.h"
@@ -12,7 +12,13 @@
#include "verification_client_input.h"
static const char *ip = "127.0.0.1";
-static const int port = 8082;
+static const int port = 1025;
+
+static void clean_up_resources(struct client *cl)
+{
+ free(cl->cc.number_arr);
+ clear_stack(&cl->deck);
+}
static void get_data_from_server(struct client *cl, fd_set *readfds)
{
@@ -24,9 +30,12 @@ static void get_data_from_server(struct client *cl, fd_set *readfds)
if(FD_ISSET(cl->fd, readfds)) {
if(!cl->data_left_in_buffer) {
cl->data_left_in_buffer = read(cl->fd, cl->buffer, max_buffer_size);
- /* end of file -- closed connection */
- if(!cl->data_left_in_buffer)
+ /* end of file -- closed connection (from server) */
+ if(!cl->data_left_in_buffer) {
+ clean_up_resources(cl);
+ pgf_disconnect();
exit(0);
+ }
#ifdef DEBUG
printf("%d\n", cl->data_left_in_buffer);
for(i = 0; i < cl->data_left_in_buffer; ++i)
@@ -47,13 +56,13 @@ static void get_data_from_server(struct client *cl, fd_set *readfds)
cl->total_players = decrypt_get_number(end_p+1, &end_p);
break;
case display_only_table:
- case attack:
- case defense:
case tossing_expectation:
case tossing:
/* last arg will contain pointer to '\n' */
decrypt_set_base_info(cl, end_p+1, &end_p);
break;
+ case attack:
+ case defense:
case attack_expectation:
decrypt_set_base_info(cl, end_p+1, &end_p);
decrypt_set_position_whose_turn(cl, end_p+1, &end_p);
@@ -163,14 +172,26 @@ static void change_client_frame(struct client *cl)
}
}
+static int check_users_exti(enum client_states state, const char *buffer,
+ int size)
+{
+ if((state == first_player || state == confirmation_waiting) &&
+ size == 2 && (buffer[0] == 'q' && buffer[1] == '\n'))
+ return 1;
+
+ return size == 3 && !strncmp(buffer, "qq", 2) && buffer[2] == '\n';
+}
+
static void send_data_to_server(struct client *cl, fd_set *readfds)
{
- int input_result = 0;
+ int input_result = 0, data_size;
if(FD_ISSET(0, readfds)) { /* 0 - stdin */
- read(0, cl->buffer, max_buffer_size);
- if(cl->buffer[0] == 'q' && cl->buffer[1] == 'q') {
+ data_size = read(0, cl->buffer, max_buffer_size);
+ if(check_users_exti(cl->state, cl->buffer, data_size)) {
close(cl->fd);
+ clean_up_resources(cl);
+ pgf_disconnect();
exit(0);
}
@@ -202,6 +223,7 @@ static int check_tracking_client_input(enum client_states state,
return 0;
switch(state) {
+ case first_player:
case confirmation_waiting:
case attack:
case defense:
@@ -246,9 +268,7 @@ static void init_client(struct client *cl)
cl->total_cards_left = 0;
cl->player_position = 0;
cl->cc.number_arr = NULL;
-#if 0
cl->cc.number_arr_idx = -1;
-#endif
cl->cot.card_arr_idx = -1;
cl->deck = NULL;
cl->pending_server_response = 0;