3 #include <linenoise/linenoise.h>
5 #include "client_auth.h"
9 struct ClientAuth client_auth;
12 static void auth_loop()
14 while (!interrupt.set) switch (client_auth.state) {
17 linenoiseFree(client_auth.name);
20 if (!(client_auth.name = linenoise("Enter name: ")))
23 client_auth.name = strdup("singleplayer");
25 printf("[access] authenticating as %s...\n", client_auth.name);
26 client_auth.state = AUTH_WAIT;
28 dragonnet_peer_send_ToServerAuth(client, &(ToServerAuth) {
29 .name = client_auth.name,
32 __attribute__((fallthrough));
35 pthread_cond_wait(&client_auth.cv, &client_auth.mtx);
43 bool client_auth_init()
45 client_auth.name = NULL;
46 pthread_cond_init(&client_auth.cv, NULL);
47 pthread_mutex_init(&client_auth.mtx, NULL);
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
55 flag_uns(&interrupt, &client_auth.cv);
56 bool success = client_auth.state == AUTH_SUCCESS;
57 pthread_mutex_unlock(&client_auth.mtx);
62 void client_auth_deinit()
64 pthread_cond_destroy(&client_auth.cv);
65 pthread_mutex_destroy(&client_auth.mtx);
66 linenoiseFree(client_auth.name);