]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/client_auth.c
c0fa81af2c549e7a797d89240091da1692b97d5c
[dragonblocks_alpha.git] / src / client / client_auth.c
1 #include <stddef.h>
2 #include <stdio.h>
3 #include <linenoise/linenoise.h>
4 #include "client.h"
5 #include "client_auth.h"
6 #include "interrupt.h"
7 #include "types.h"
8
9 struct ClientAuth client_auth;
10
11 #include <string.h>
12 static void auth_loop()
13 {
14         while (!interrupt.set) switch (client_auth.state) {
15                 case AUTH_INIT:
16                         if (client_auth.name)
17                                 linenoiseFree(client_auth.name);
18
19                         /*
20                         if (!(client_auth.name = linenoise("Enter name: ")))
21                                 return;
22                         */
23                         client_auth.name = strdup("singleplayer");
24
25                         printf("[access] authenticating as %s...\n", client_auth.name);
26                         client_auth.state = AUTH_WAIT;
27
28                         dragonnet_peer_send_ToServerAuth(client, &(ToServerAuth) {
29                                 .name = client_auth.name,
30                         });
31
32                         __attribute__((fallthrough));
33
34                 case AUTH_WAIT:
35                         pthread_cond_wait(&client_auth.cv, &client_auth.mtx);
36                         break;
37
38                 case AUTH_SUCCESS:
39                         return;
40         }
41 }
42
43 bool client_auth_init()
44 {
45         client_auth.name = NULL;
46         pthread_cond_init(&client_auth.cv, NULL);
47         pthread_mutex_init(&client_auth.mtx, NULL);
48
49         pthread_mutex_lock(&client_auth.mtx);
50         client_auth.state = AUTH_INIT;
51         flag_sub(&interrupt, &client_auth.cv); // make sure Ctrl+C will work during AUTH_WAIT
52
53         auth_loop();
54
55         flag_uns(&interrupt, &client_auth.cv);
56         bool success = client_auth.state == AUTH_SUCCESS;
57         pthread_mutex_unlock(&client_auth.mtx);
58
59         return success;
60 }
61
62 void client_auth_deinit()
63 {
64         pthread_cond_destroy(&client_auth.cv);
65         pthread_mutex_destroy(&client_auth.mtx);
66         linenoiseFree(client_auth.name);
67 }