mirror of
https://git.sr.ht/~thestr4ng3r/chiaki
synced 2025-08-20 13:33:13 -07:00
Wait for session id
This commit is contained in:
parent
ad6295d706
commit
3b961ff0d8
4 changed files with 49 additions and 1 deletions
|
@ -22,6 +22,7 @@
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
|
@ -102,7 +102,11 @@ typedef struct chiaki_session_t
|
||||||
void *event_cb_user;
|
void *event_cb_user;
|
||||||
|
|
||||||
ChiakiThread session_thread;
|
ChiakiThread session_thread;
|
||||||
|
|
||||||
ChiakiCtrl ctrl;
|
ChiakiCtrl ctrl;
|
||||||
|
ChiakiCond ctrl_cond;
|
||||||
|
ChiakiMutex ctrl_cond_mutex;
|
||||||
|
bool ctrl_session_id_received;
|
||||||
|
|
||||||
ChiakiLog log;
|
ChiakiLog log;
|
||||||
} ChiakiSession;
|
} ChiakiSession;
|
||||||
|
|
|
@ -59,6 +59,9 @@ static void *ctrl_thread_func(void *user)
|
||||||
if(err != CHIAKI_ERR_SUCCESS)
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
{
|
{
|
||||||
chiaki_session_set_quit_reason(ctrl->session, CHIAKI_QUIT_REASON_CTRL_UNKNOWN);
|
chiaki_session_set_quit_reason(ctrl->session, CHIAKI_QUIT_REASON_CTRL_UNKNOWN);
|
||||||
|
chiaki_mutex_lock(&ctrl->session->ctrl_cond_mutex);
|
||||||
|
chiaki_cond_signal(&ctrl->session->ctrl_cond);
|
||||||
|
chiaki_mutex_unlock(&ctrl->session->ctrl_cond_mutex);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,6 +143,12 @@ static void ctrl_message_received(ChiakiCtrl *ctrl, uint16_t msg_type, uint8_t *
|
||||||
|
|
||||||
static void ctrl_message_received_session_id(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size)
|
static void ctrl_message_received_session_id(ChiakiCtrl *ctrl, uint8_t *payload, size_t payload_size)
|
||||||
{
|
{
|
||||||
|
if(ctrl->session->ctrl_session_id_received)
|
||||||
|
{
|
||||||
|
CHIAKI_LOGW(&ctrl->session->log, "Received another Session Id Message\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if(payload_size < 2 || (char)payload[0] != 'J')
|
if(payload_size < 2 || (char)payload[0] != 'J')
|
||||||
{
|
{
|
||||||
CHIAKI_LOGE(&ctrl->session->log, "Invalid Session Id received\n");
|
CHIAKI_LOGE(&ctrl->session->log, "Invalid Session Id received\n");
|
||||||
|
@ -171,6 +180,10 @@ static void ctrl_message_received_session_id(ChiakiCtrl *ctrl, uint8_t *payload,
|
||||||
|
|
||||||
memcpy(ctrl->session->session_id, payload, payload_size);
|
memcpy(ctrl->session->session_id, payload, payload_size);
|
||||||
ctrl->session->session_id[payload_size] = '\0';
|
ctrl->session->session_id[payload_size] = '\0';
|
||||||
|
ctrl->session->ctrl_session_id_received = true;
|
||||||
|
chiaki_mutex_lock(&ctrl->session->ctrl_cond_mutex);
|
||||||
|
chiaki_cond_signal(&ctrl->session->ctrl_cond);
|
||||||
|
chiaki_mutex_unlock(&ctrl->session->ctrl_cond_mutex);
|
||||||
|
|
||||||
CHIAKI_LOGI(&ctrl->session->log, "Received valid Session Id: %s\n", ctrl->session->session_id);
|
CHIAKI_LOGI(&ctrl->session->log, "Received valid Session Id: %s\n", ctrl->session->session_id);
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,6 +45,17 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_session_init(ChiakiSession *session, Chiaki
|
||||||
|
|
||||||
session->quit_reason = CHIAKI_QUIT_REASON_NONE;
|
session->quit_reason = CHIAKI_QUIT_REASON_NONE;
|
||||||
|
|
||||||
|
if(chiaki_cond_init(&session->ctrl_cond) != CHIAKI_ERR_SUCCESS)
|
||||||
|
{
|
||||||
|
return CHIAKI_ERR_UNKNOWN;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(chiaki_mutex_init(&session->ctrl_cond_mutex) != CHIAKI_ERR_SUCCESS)
|
||||||
|
{
|
||||||
|
chiaki_cond_fini(&session->ctrl_cond);
|
||||||
|
return CHIAKI_ERR_UNKNOWN;
|
||||||
|
}
|
||||||
|
|
||||||
int r = getaddrinfo(connect_info->host, NULL, NULL, &session->connect_info.host_addrinfos);
|
int r = getaddrinfo(connect_info->host, NULL, NULL, &session->connect_info.host_addrinfos);
|
||||||
if(r != 0)
|
if(r != 0)
|
||||||
{
|
{
|
||||||
|
@ -77,6 +88,8 @@ CHIAKI_EXPORT void chiaki_session_fini(ChiakiSession *session)
|
||||||
{
|
{
|
||||||
if(!session)
|
if(!session)
|
||||||
return;
|
return;
|
||||||
|
chiaki_cond_fini(&session->ctrl_cond);
|
||||||
|
chiaki_mutex_fini(&session->ctrl_cond_mutex);
|
||||||
free(session->connect_info.regist_key);
|
free(session->connect_info.regist_key);
|
||||||
free(session->connect_info.ostype);
|
free(session->connect_info.ostype);
|
||||||
freeaddrinfo(session->connect_info.host_addrinfos);
|
freeaddrinfo(session->connect_info.host_addrinfos);
|
||||||
|
@ -121,12 +134,29 @@ static void *session_thread_func(void *arg)
|
||||||
|
|
||||||
CHIAKI_LOGI(&session->log, "Starting ctrl\n");
|
CHIAKI_LOGI(&session->log, "Starting ctrl\n");
|
||||||
|
|
||||||
|
chiaki_mutex_lock(&session->ctrl_cond_mutex);
|
||||||
|
session->ctrl_session_id_received = false;
|
||||||
|
|
||||||
ChiakiErrorCode err = chiaki_ctrl_start(&session->ctrl, session);
|
ChiakiErrorCode err = chiaki_ctrl_start(&session->ctrl, session);
|
||||||
if(err != CHIAKI_ERR_SUCCESS)
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
goto quit;
|
goto quit;
|
||||||
|
|
||||||
chiaki_ctrl_join(&session->ctrl);
|
chiaki_cond_wait(&session->ctrl_cond, &session->ctrl_cond_mutex);
|
||||||
|
chiaki_mutex_unlock(&session->ctrl_cond_mutex);
|
||||||
|
|
||||||
|
if(!session->ctrl_session_id_received)
|
||||||
|
{
|
||||||
|
CHIAKI_LOGE(&session->log, "Ctrl has failed, shutting down\n");
|
||||||
|
chiaki_ctrl_join(&session->ctrl);
|
||||||
|
goto quit_ctrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
CHIAKI_LOGI(&session->log, "Looking good, we should now start the Senkusha\n");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
quit_ctrl:
|
||||||
|
chiaki_ctrl_join(&session->ctrl);
|
||||||
CHIAKI_LOGI(&session->log, "Ctrl stopped\n");
|
CHIAKI_LOGI(&session->log, "Ctrl stopped\n");
|
||||||
|
|
||||||
ChiakiEvent quit_event;
|
ChiakiEvent quit_event;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue