back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorscratko <m@scratko.xyz>2024-08-30 12:46:56 +0300
committerscratko <m@scratko.xyz>2024-08-30 14:59:44 +0300
commit831f9f01fbe4088eb6bd378c0e417d9996b676fd (patch)
tree53297459d35ad795618c351a79b1829776e5e1f4 /server
parent4b6c15f780d59895f067383a5041edcfe86f504e (diff)
downloaddurak-831f9f01fbe4088eb6bd378c0e417d9996b676fd.tar.gz
durak-831f9f01fbe4088eb6bd378c0e417d9996b676fd.tar.bz2
durak-831f9f01fbe4088eb6bd378c0e417d9996b676fd.zip
Final version v2.0
Added windows client. SIGPIPE signal was being sent to the server when the client was disconnected. Now there is handling of this signal. Added a delay when displaying some informational messages.
Diffstat (limited to 'server')
-rw-r--r--server/server.c8
-rw-r--r--server/server_data_processing.c24
2 files changed, 30 insertions, 2 deletions
diff --git a/server/server.c b/server/server.c
index 6a9075c..0017888 100644
--- a/server/server.c
+++ b/server/server.c
@@ -7,6 +7,7 @@
#include <fcntl.h>
#include <syslog.h>
#include <time.h>
+#include <signal.h>
#include "server.h"
#include "server_data_processing.h"
@@ -14,6 +15,12 @@
#include "card_stack.h"
#include "card_queue.h"
+void handler(int s)
+{
+ if(s == SIGPIPE)
+ signal(SIGPIPE, handler);
+}
+
static void init_session(struct session *new_session, struct sockaddr_in *from,
int fd, int player_position)
{
@@ -958,6 +965,7 @@ int main(int argc, char **argv)
srand(time(NULL));
openlog("durak server", 0, LOG_USER);
syslog(LOG_INFO, "daemon started");
+ signal(SIGPIPE, handler);
port = strtol(argv[1], NULL, 10);
if(!init_server(&serv, port)) {
diff --git a/server/server_data_processing.c b/server/server_data_processing.c
index 41e4b08..40ce5e3 100644
--- a/server/server_data_processing.c
+++ b/server/server_data_processing.c
@@ -235,10 +235,14 @@ int get_cards_from_attacker(struct session *client,
const char *stack_card = NULL;
read_result = read_from_client(client->fd, client->buffer, max_buffer_size);
+
+#if DEBUG
printf("%d\n", read_result);
for(i = 0; i < read_result; ++i)
putchar(client->buffer[i]);
putchar('\n');
+#endif
+
if(!read_result)
return read_result;
for(i = 0, j = 0; i < read_result; ++i) {
@@ -261,6 +265,7 @@ int get_cards_from_attacker(struct session *client,
data_length = sprintf(output_buffer, "%d:%s\n", card_acceptance_status,
all_cards_received_status ? "all" : "not all");
write_to_client(client->fd, data_length);
+ sleep(2);
return read_result;
}
@@ -280,14 +285,21 @@ int get_card_from_defender(struct session *client, struct cards_on_table *cot,
const char *stack_card = NULL;
read_result = read_from_client(client->fd, client->buffer, max_buffer_size);
+
+#if DEBUG
printf("%d\n", read_result);
for(i = 0; i < read_result; ++i)
putchar(client->buffer[i]);
putchar('\n');
+#endif
+
if(!read_result)
return read_result;
- if(client->buffer[0] == '\n')
+ if(client->buffer[0] == '\n') {
+ /* the client sees “accepting cards” at the same time */
+ sleep(2);
return 1;
+ }
for(i = 0; i < read_result && client->buffer[i] != '\n'; ++i)
given_card[i] = client->buffer[i];
given_card[i] = '\0';
@@ -321,16 +333,23 @@ int get_cards_from_tossing_player(struct session *client,
const char *stack_card = NULL;
read_result = read_from_client(client->fd, client->buffer, max_buffer_size);
+
+#if DEBUG
printf("%d\n", read_result);
for(i = 0; i < read_result; ++i)
putchar(client->buffer[i]);
putchar('\n');
+#endif
+
if(!read_result)
return read_result;
/* cancellation */
- if(client->buffer[0] == '\n')
+ if(client->buffer[0] == '\n') {
+ /* the client sees “skipping the card toss” at the same time */
+ sleep(2);
return cancel;
+ }
for(i = 0, j = 0; i < read_result; ++i) {
if(client->buffer[i] == '\\') {
@@ -363,6 +382,7 @@ int get_cards_from_tossing_player(struct session *client,
data_length = sprintf(output_buffer, "%d:%s\n", card_acceptance_status,
all_cards_received_status ? "all" : "not all");
write_to_client(client->fd, data_length);
+ sleep(2);
if(!card_return_status)
return answer_got;
else