mirror of
https://git.sr.ht/~thestr4ng3r/chiaki
synced 2025-08-19 21:13:12 -07:00
Add Mirai, Start Nagare
This commit is contained in:
parent
8f42cff06e
commit
62ae3613c1
8 changed files with 212 additions and 31 deletions
|
@ -9,7 +9,9 @@ set(HEADER_FILES
|
||||||
include/chiaki/ctrl.h
|
include/chiaki/ctrl.h
|
||||||
include/chiaki/rpcrypt.h
|
include/chiaki/rpcrypt.h
|
||||||
include/chiaki/takion.h
|
include/chiaki/takion.h
|
||||||
include/chiaki/senkusha.h)
|
include/chiaki/senkusha.h
|
||||||
|
include/chiaki/nagare.h
|
||||||
|
include/chiaki/mirai.h)
|
||||||
|
|
||||||
set(SOURCE_FILES
|
set(SOURCE_FILES
|
||||||
src/common.c
|
src/common.c
|
||||||
|
@ -23,7 +25,9 @@ set(SOURCE_FILES
|
||||||
src/takion.c
|
src/takion.c
|
||||||
src/senkusha.c
|
src/senkusha.c
|
||||||
src/utils.h
|
src/utils.h
|
||||||
src/pb_utils.h)
|
src/pb_utils.h
|
||||||
|
src/nagare.c
|
||||||
|
src/mirai.c)
|
||||||
|
|
||||||
add_subdirectory(protobuf)
|
add_subdirectory(protobuf)
|
||||||
include_directories("${NANOPB_SOURCE_DIR}")
|
include_directories("${NANOPB_SOURCE_DIR}")
|
||||||
|
|
47
lib/include/chiaki/mirai.h
Normal file
47
lib/include/chiaki/mirai.h
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
/*
|
||||||
|
* This file is part of Chiaki.
|
||||||
|
*
|
||||||
|
* Chiaki is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Chiaki is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef CHIAKI_MIRAI_H
|
||||||
|
#define CHIAKI_MIRAI_H
|
||||||
|
|
||||||
|
#include "thread.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct chiaki_mirai_t
|
||||||
|
{
|
||||||
|
bool expected;
|
||||||
|
bool success;
|
||||||
|
ChiakiMutex mutex;
|
||||||
|
ChiakiCond cond;
|
||||||
|
} ChiakiMirai;
|
||||||
|
|
||||||
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_mirai_init(ChiakiMirai *mirai);
|
||||||
|
CHIAKI_EXPORT void chiaki_mirai_fini(ChiakiMirai *mirai);
|
||||||
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_mirai_signal(ChiakiMirai *mirai, bool success);
|
||||||
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_mirai_expect_begin(ChiakiMirai *mirai);
|
||||||
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_mirai_expect_wait(ChiakiMirai *mirai, uint64_t timeout_ms);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // CHIAKI_MIRAI_H
|
41
lib/include/chiaki/nagare.h
Normal file
41
lib/include/chiaki/nagare.h
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
/*
|
||||||
|
* This file is part of Chiaki.
|
||||||
|
*
|
||||||
|
* Chiaki is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Chiaki is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef CHIAKI_NAGARE_H
|
||||||
|
#define CHIAKI_NAGARE_H
|
||||||
|
|
||||||
|
#include "takion.h"
|
||||||
|
#include "log.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct chiaki_nagare_t
|
||||||
|
{
|
||||||
|
struct chiaki_session_t *session;
|
||||||
|
ChiakiLog *log;
|
||||||
|
ChiakiTakion takion;
|
||||||
|
} ChiakiNagare;
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // CHIAKI_NAGARE_H
|
|
@ -18,6 +18,7 @@
|
||||||
#ifndef CHIAKI_SESSION_H
|
#ifndef CHIAKI_SESSION_H
|
||||||
#define CHIAKI_SESSION_H
|
#define CHIAKI_SESSION_H
|
||||||
|
|
||||||
|
#include "nagare.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
@ -110,6 +111,8 @@ typedef struct chiaki_session_t
|
||||||
bool ctrl_session_id_received;
|
bool ctrl_session_id_received;
|
||||||
|
|
||||||
ChiakiLog log;
|
ChiakiLog log;
|
||||||
|
|
||||||
|
ChiakiNagare nagare;
|
||||||
} ChiakiSession;
|
} ChiakiSession;
|
||||||
|
|
||||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_session_init(ChiakiSession *session, ChiakiConnectInfo *connect_info);
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_session_init(ChiakiSession *session, ChiakiConnectInfo *connect_info);
|
||||||
|
|
71
lib/src/mirai.c
Normal file
71
lib/src/mirai.c
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
/*
|
||||||
|
* This file is part of Chiaki.
|
||||||
|
*
|
||||||
|
* Chiaki is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Chiaki is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <chiaki/mirai.h>
|
||||||
|
|
||||||
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_mirai_init(ChiakiMirai *mirai)
|
||||||
|
{
|
||||||
|
mirai->expected = false;
|
||||||
|
mirai->success = false;
|
||||||
|
ChiakiErrorCode err = chiaki_mutex_init(&mirai->mutex);
|
||||||
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
|
return err;
|
||||||
|
err = chiaki_cond_init(&mirai->cond);
|
||||||
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
|
{
|
||||||
|
chiaki_mutex_fini(&mirai->mutex);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
return CHIAKI_ERR_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
CHIAKI_EXPORT void chiaki_mirai_fini(ChiakiMirai *mirai)
|
||||||
|
{
|
||||||
|
chiaki_mutex_fini(&mirai->mutex);
|
||||||
|
chiaki_cond_fini(&mirai->cond);
|
||||||
|
}
|
||||||
|
|
||||||
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_mirai_signal(ChiakiMirai *mirai, bool success)
|
||||||
|
{
|
||||||
|
ChiakiErrorCode err = chiaki_mutex_lock(&mirai->mutex);
|
||||||
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
|
return err;
|
||||||
|
mirai->success = success;
|
||||||
|
err = chiaki_cond_signal(&mirai->cond);
|
||||||
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
|
return err;
|
||||||
|
return chiaki_mutex_unlock(&mirai->mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_mirai_expect_begin(ChiakiMirai *mirai)
|
||||||
|
{
|
||||||
|
ChiakiErrorCode err = chiaki_mutex_lock(&mirai->mutex);
|
||||||
|
mirai->expected = true;
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_mirai_expect_wait(ChiakiMirai *mirai, uint64_t timeout_ms)
|
||||||
|
{
|
||||||
|
ChiakiErrorCode err = chiaki_cond_timedwait(&mirai->cond, &mirai->mutex, timeout_ms);
|
||||||
|
if(err != CHIAKI_ERR_SUCCESS && err != CHIAKI_ERR_TIMEOUT)
|
||||||
|
return err;
|
||||||
|
mirai->expected = false;
|
||||||
|
ChiakiErrorCode err2 = chiaki_mutex_unlock(&mirai->mutex);
|
||||||
|
if(err2 != CHIAKI_ERR_SUCCESS)
|
||||||
|
return err2;
|
||||||
|
return err;
|
||||||
|
}
|
30
lib/src/nagare.c
Normal file
30
lib/src/nagare.c
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
/*
|
||||||
|
* This file is part of Chiaki.
|
||||||
|
*
|
||||||
|
* Chiaki is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Chiaki is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <chiaki/nagare.h>
|
||||||
|
#include <chiaki/session.h>
|
||||||
|
|
||||||
|
|
||||||
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_nagare_run(ChiakiSession *session)
|
||||||
|
{
|
||||||
|
ChiakiNagare *nagare = &session->nagare;
|
||||||
|
nagare->log = &session->log;
|
||||||
|
nagare->log = &session->log;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
#include <chiaki/senkusha.h>
|
#include <chiaki/senkusha.h>
|
||||||
#include <chiaki/session.h>
|
#include <chiaki/session.h>
|
||||||
|
#include <chiaki/mirai.h>
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
@ -43,10 +44,7 @@ typedef struct senkusha_t
|
||||||
ChiakiLog *log;
|
ChiakiLog *log;
|
||||||
ChiakiTakion takion;
|
ChiakiTakion takion;
|
||||||
|
|
||||||
bool bang_expected;
|
ChiakiMirai bang_mirai;
|
||||||
bool bang_received;
|
|
||||||
ChiakiMutex bang_mutex;
|
|
||||||
ChiakiCond bang_cond;
|
|
||||||
} Senkusha;
|
} Senkusha;
|
||||||
|
|
||||||
|
|
||||||
|
@ -58,14 +56,9 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_senkusha_run(ChiakiSession *session)
|
||||||
{
|
{
|
||||||
Senkusha senkusha;
|
Senkusha senkusha;
|
||||||
senkusha.log = &session->log;
|
senkusha.log = &session->log;
|
||||||
senkusha.bang_expected = false;
|
ChiakiErrorCode err = chiaki_mirai_init(&senkusha.bang_mirai);
|
||||||
senkusha.bang_received = false;
|
|
||||||
ChiakiErrorCode err = chiaki_mutex_init(&senkusha.bang_mutex);
|
|
||||||
if(err != CHIAKI_ERR_SUCCESS)
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
return err;
|
goto error_bang_mirai;
|
||||||
err = chiaki_cond_init(&senkusha.bang_cond);
|
|
||||||
if(err != CHIAKI_ERR_SUCCESS)
|
|
||||||
goto error_bang_mutex;
|
|
||||||
|
|
||||||
ChiakiTakionConnectInfo takion_info;
|
ChiakiTakionConnectInfo takion_info;
|
||||||
takion_info.log = senkusha.log;
|
takion_info.log = senkusha.log;
|
||||||
|
@ -85,16 +78,14 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_senkusha_run(ChiakiSession *session)
|
||||||
if(err != CHIAKI_ERR_SUCCESS)
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
{
|
{
|
||||||
CHIAKI_LOGE(&session->log, "Senkusha connect failed\n");
|
CHIAKI_LOGE(&session->log, "Senkusha connect failed\n");
|
||||||
goto error_bang_cond;
|
goto error_bang_mirai;
|
||||||
}
|
}
|
||||||
|
|
||||||
CHIAKI_LOGI(&session->log, "Senkusha sending big\n");
|
CHIAKI_LOGI(&session->log, "Senkusha sending big\n");
|
||||||
|
|
||||||
err = chiaki_mutex_lock(&senkusha.bang_mutex);
|
err = chiaki_mirai_expect_begin(&senkusha.bang_mirai);
|
||||||
assert(err == CHIAKI_ERR_SUCCESS);
|
assert(err == CHIAKI_ERR_SUCCESS);
|
||||||
|
|
||||||
senkusha.bang_expected = true;
|
|
||||||
|
|
||||||
err = senkusha_send_big(&senkusha);
|
err = senkusha_send_big(&senkusha);
|
||||||
if(err != CHIAKI_ERR_SUCCESS)
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
{
|
{
|
||||||
|
@ -102,11 +93,10 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_senkusha_run(ChiakiSession *session)
|
||||||
goto error_takion;
|
goto error_takion;
|
||||||
}
|
}
|
||||||
|
|
||||||
err = chiaki_cond_timedwait(&senkusha.bang_cond, &senkusha.bang_mutex, BIG_TIMEOUT_MS);
|
err = chiaki_mirai_expect_wait(&senkusha.bang_mirai, BIG_TIMEOUT_MS);
|
||||||
senkusha.bang_expected = false;
|
assert(err == CHIAKI_ERR_SUCCESS || err == CHIAKI_ERR_TIMEOUT);
|
||||||
chiaki_mutex_unlock(&senkusha.bang_mutex);
|
|
||||||
|
|
||||||
if(!senkusha.bang_received)
|
if(!senkusha.bang_mirai.success)
|
||||||
{
|
{
|
||||||
if(err == CHIAKI_ERR_TIMEOUT)
|
if(err == CHIAKI_ERR_TIMEOUT)
|
||||||
CHIAKI_LOGE(&session->log, "Senkusha bang receive timeout\n");
|
CHIAKI_LOGE(&session->log, "Senkusha bang receive timeout\n");
|
||||||
|
@ -126,10 +116,8 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_senkusha_run(ChiakiSession *session)
|
||||||
error_takion:
|
error_takion:
|
||||||
chiaki_takion_close(&senkusha.takion);
|
chiaki_takion_close(&senkusha.takion);
|
||||||
CHIAKI_LOGI(&session->log, "Senkusha closed takion\n");
|
CHIAKI_LOGI(&session->log, "Senkusha closed takion\n");
|
||||||
error_bang_cond:
|
error_bang_mirai:
|
||||||
chiaki_cond_init(&senkusha.bang_cond);
|
chiaki_mirai_fini(&senkusha.bang_mirai);
|
||||||
error_bang_mutex:
|
|
||||||
chiaki_mutex_fini(&senkusha.bang_mutex);
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,17 +136,14 @@ static void senkusha_takion_data(uint8_t *buf, size_t buf_size, void *user)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(senkusha->bang_expected)
|
if(senkusha->bang_mirai.expected)
|
||||||
{
|
{
|
||||||
if(msg.type != tkproto_TakionMessage_PayloadType_BANG || !msg.has_bang_payload)
|
if(msg.type != tkproto_TakionMessage_PayloadType_BANG || !msg.has_bang_payload)
|
||||||
{
|
{
|
||||||
CHIAKI_LOGE(senkusha->log, "Senkusha expected bang payload but received something else\n");
|
CHIAKI_LOGE(senkusha->log, "Senkusha expected bang payload but received something else\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
chiaki_mutex_lock(&senkusha->bang_mutex);
|
chiaki_mirai_signal(&senkusha->bang_mirai, true);
|
||||||
senkusha->bang_received = true;
|
|
||||||
chiaki_cond_signal(&senkusha->bang_cond);
|
|
||||||
chiaki_mutex_unlock(&senkusha->bang_mutex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -151,7 +151,6 @@ static void *session_thread_func(void *arg)
|
||||||
if(!session->ctrl_session_id_received)
|
if(!session->ctrl_session_id_received)
|
||||||
{
|
{
|
||||||
CHIAKI_LOGE(&session->log, "Ctrl has failed, shutting down\n");
|
CHIAKI_LOGE(&session->log, "Ctrl has failed, shutting down\n");
|
||||||
chiaki_ctrl_join(&session->ctrl);
|
|
||||||
goto quit_ctrl;
|
goto quit_ctrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,6 +160,7 @@ static void *session_thread_func(void *arg)
|
||||||
if(err != CHIAKI_ERR_SUCCESS)
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
{
|
{
|
||||||
CHIAKI_LOGE(&session->log, "Senkusha failed\n");
|
CHIAKI_LOGE(&session->log, "Senkusha failed\n");
|
||||||
|
goto quit_ctrl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue