back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/client/client.c
diff options
context:
space:
mode:
authorscratko <m@scratko.xyz>2024-08-14 02:35:56 +0300
committerscratko <m@scratko.xyz>2024-08-14 02:35:56 +0300
commit880b8be2c28d761505b2eecc1386919d5add6f2f (patch)
tree63e965c3f82bcd09bb07baf9b669a736293dddc7 /client/client.c
parenta2d696dea797faaa3157046c8ae89cd70e965bff (diff)
downloaddurak-880b8be2c28d761505b2eecc1386919d5add6f2f.tar.gz
durak-880b8be2c28d761505b2eecc1386919d5add6f2f.tar.bz2
durak-880b8be2c28d761505b2eecc1386919d5add6f2f.zip
Global fixes v1.0
Defense hint now only takes into account unbeaten cards. The client accounts for sticky data in one packet via TCP. Changed delimiter when sending data related to cards on the table and the queue (from '0' to '='). Accepting cards from a client (verification_client_input.c) is heavily patched. Cards are taken from the stack at the hint's prompt. Added pop_stack(). The trump card is retrieved from the end of the array. Changed the check of the defender's ability to beat all cards.
Diffstat (limited to 'client/client.c')
-rw-r--r--client/client.c34
1 files changed, 27 insertions, 7 deletions
diff --git a/client/client.c b/client/client.c
index d932a51..4abcf7d 100644
--- a/client/client.c
+++ b/client/client.c
@@ -4,6 +4,7 @@
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
+#include <stdio.h> /* test */
#include "client.h"
#include "data_decryption.h"
@@ -15,15 +16,23 @@ static const int port = 8082;
static void get_data_from_server(struct client *cl, fd_set *readfds)
{
+ int i;
int update_info = 0;
/* pointer to delimeter (:) that is behind extract data */
char *end_p;
if(FD_ISSET(cl->fd, readfds)) {
- read(cl->fd, cl->buffer, max_buffer_size);
+ if(!cl->data_left_in_buffer) {
+ cl->data_left_in_buffer = read(cl->fd, cl->buffer, max_buffer_size);
+ printf("%d\n", cl->data_left_in_buffer);
+ for(i = 0; i < cl->data_left_in_buffer; ++i)
+ putchar(cl->buffer[i]);
+ putchar('\n');
+ }
decrypt_set_state(cl, &end_p);
update_info = 1;
cl->display_new_frame = 1;
+ cl->pending_server_response = 0;
}
if(update_info) {
switch(cl->state) {
@@ -50,13 +59,20 @@ static void get_data_from_server(struct client *cl, fd_set *readfds)
decrypt_set_card_queue(cl, end_p+1);
break;
case card_acceptance_status:
- decrypt_set_card_acceptance_status(cl, cl->buffer);
+ decrypt_set_card_acceptance_status(cl, end_p+1);
break;
/* no data to extract */
case tossing_limit_status:
default:
{}
}
+ for(i = 0; cl->buffer[i] != '\n'; ++i)
+ {}
+ if((cl->buffer+i - cl->buffer + 1) != cl->data_left_in_buffer) {
+ cl->data_left_in_buffer -= i + 1;
+ memmove(cl->buffer, cl->buffer+i+1, cl->data_left_in_buffer);
+ } else
+ cl->data_left_in_buffer = 0;
}
}
@@ -66,7 +82,7 @@ static void prepare_tips_for_client(struct client *cl)
if(!is_empty_stack(cl->deck))
add_hint_letters_stack(cl->deck);
-
+/* TODO: clear marked cards ??? */
switch(cl->state) {
case attack:
mark_card_for_attackers_stack(cl->deck);
@@ -126,7 +142,7 @@ static void change_client_frame(struct client *cl)
static void send_data_to_server(struct client *cl, fd_set *readfds)
{
- int input_result;
+ int input_result = 0;
if(FD_ISSET(0, readfds)) { /* 0 - stdin */
read(0, cl->buffer, max_buffer_size);
@@ -181,9 +197,11 @@ int main_loop(struct client *cl)
if(check_tracking_client_input(cl->state, cl->pending_server_response))
FD_SET(0, &readfds);
- select_result = select(cl->fd + 1, &readfds, NULL, NULL, NULL);
- if(select_result == -1)
- return 2;
+ if(!cl->data_left_in_buffer) {
+ select_result = select(cl->fd + 1, &readfds, NULL, NULL, NULL);
+ if(select_result == -1)
+ return 2;
+ }
get_data_from_server(cl, &readfds);
prepare_tips_for_client(cl);
if(cl->display_new_frame)
@@ -210,6 +228,7 @@ static void init_client(struct client *cl)
cl->position_whose_turn = 0;
cl->display_new_frame = 0;
cl->all_input_cards_accepted = 0;
+ cl->data_left_in_buffer = 0;
}
/*
@@ -222,6 +241,7 @@ static int connect_to_server(struct client *cl)
struct sockaddr_in addr;
cl->fd = socket(AF_INET, SOCK_STREAM, 0);
+
if(cl->fd == -1)
return 0;
addr.sin_family = AF_INET;