From 1a5654bd49b53693b7fa7e697ae4304fe97c0cfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Thu, 1 Aug 2019 20:07:44 +0200 Subject: [PATCH] Add ChiakiException --- gui/include/streamsession.h | 12 ++++++++++++ gui/src/streamsession.cpp | 29 +++++++++++++++++++---------- lib/include/chiaki/session.h | 1 + lib/src/session.c | 10 ++++++++-- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/gui/include/streamsession.h b/gui/include/streamsession.h index b1f9dbd..8c39c3e 100644 --- a/gui/include/streamsession.h +++ b/gui/include/streamsession.h @@ -33,6 +33,16 @@ class QAudioOutput; class QIODevice; class QKeyEvent; +class ChiakiException : public std::exception +{ + private: + QString msg; + + public: + explicit ChiakiException(const QString &msg) : msg(msg) {} + const char *what() const noexcept override { return msg.toLocal8Bit().constData(); } +}; + class StreamSession : public QObject { friend class StreamSessionPrivate; @@ -60,6 +70,8 @@ class StreamSession : public QObject explicit StreamSession(const QString &host, const QString ®istkey, const QString &ostype, const QString &auth, const QString &morning, const QString &did, QObject *parent = nullptr); ~StreamSession(); + void Stop(); + #if CHIAKI_GUI_ENABLE_QT_GAMEPAD QGamepad *GetGamepad() { return gamepad; } #endif diff --git a/gui/src/streamsession.cpp b/gui/src/streamsession.cpp index 4d6b83c..2dae0df 100644 --- a/gui/src/streamsession.cpp +++ b/gui/src/streamsession.cpp @@ -60,20 +60,15 @@ StreamSession::StreamSession(const QString &host, const QString ®istkey, cons QByteArray morning_str = morning.toUtf8(); ChiakiErrorCode err = chiaki_base64_decode(morning_str.constData(), morning_str.length(), connect_info.morning, &morning_size); if(err != CHIAKI_ERR_SUCCESS || morning_size != sizeof(connect_info.morning)) - { - printf("morning invalid.\n"); - throw std::exception(); // TODO: proper exception - } + throw ChiakiException("Morning invalid"); size_t did_size = sizeof(connect_info.did); QByteArray did_str = did.toUtf8(); err = chiaki_base64_decode(did_str.constData(), did_str.length(), connect_info.did, &did_size); if(err != CHIAKI_ERR_SUCCESS || did_size != sizeof(connect_info.did)) - { - printf("did invalid.\n"); - throw std::exception(); // TODO: proper exception - } + throw ChiakiException("Did invalid"); + // TODO: move audio init out of here and use values from audio header QAudioFormat audio_format; audio_format.setSampleRate(48000); audio_format.setChannelCount(2); @@ -92,10 +87,20 @@ StreamSession::StreamSession(const QString &host, const QString ®istkey, cons memset(&keyboard_state, 0, sizeof(keyboard_state)); - chiaki_session_init(&session, &connect_info); + err = chiaki_session_init(&session, &connect_info); + if(err != CHIAKI_ERR_SUCCESS) + throw ChiakiException("Chiaki Session Init failed"); + chiaki_session_set_audio_frame_cb(&session, AudioFrameCb, this); chiaki_session_set_video_sample_cb(&session, VideoSampleCb, this); - chiaki_session_start(&session); + + err = chiaki_session_start(&session); + if(err != CHIAKI_ERR_SUCCESS) + { + chiaki_session_fini(&session); + throw ChiakiException("Chiaki Session Start failed"); + } + #if CHIAKI_GUI_ENABLE_QT_GAMEPAD connect(QGamepadManager::instance(), &QGamepadManager::connectedGamepadsChanged, this, &StreamSession::UpdateGamepads); @@ -112,6 +117,10 @@ StreamSession::~StreamSession() chiaki_session_fini(&session); } +void StreamSession::Stop() +{ +} + void StreamSession::HandleKeyboardEvent(QKeyEvent *event) { uint64_t button_mask; diff --git a/lib/include/chiaki/session.h b/lib/include/chiaki/session.h index ece2683..e37bfc4 100644 --- a/lib/include/chiaki/session.h +++ b/lib/include/chiaki/session.h @@ -146,6 +146,7 @@ typedef struct chiaki_session_t CHIAKI_EXPORT ChiakiErrorCode chiaki_session_init(ChiakiSession *session, ChiakiConnectInfo *connect_info); CHIAKI_EXPORT void chiaki_session_fini(ChiakiSession *session); CHIAKI_EXPORT ChiakiErrorCode chiaki_session_start(ChiakiSession *session); +CHIAKI_EXPORT ChiakiErrorCode chiaki_session_stop(ChiakiSession *session); CHIAKI_EXPORT ChiakiErrorCode chiaki_session_join(ChiakiSession *session); CHIAKI_EXPORT ChiakiErrorCode chiaki_session_set_controller_state(ChiakiSession *session, ChiakiControllerState *state); diff --git a/lib/src/session.c b/lib/src/session.c index 996ab94..8e60a9f 100644 --- a/lib/src/session.c +++ b/lib/src/session.c @@ -121,6 +121,12 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_session_start(ChiakiSession *session) return chiaki_thread_create(&session->session_thread, session_thread_func, session); } +CHIAKI_EXPORT ChiakiErrorCode chiaki_session_stop(ChiakiSession *session) +{ + // TODO + return CHIAKI_ERR_SUCCESS; +} + CHIAKI_EXPORT ChiakiErrorCode chiaki_session_join(ChiakiSession *session) { return chiaki_thread_join(&session->session_thread, NULL); @@ -183,7 +189,7 @@ static void *session_thread_func(void *arg) goto quit_ctrl; } - CHIAKI_LOGI(&session->log, "Starting Senkusha"); + //CHIAKI_LOGI(&session->log, "Starting Senkusha"); /* TODO err = chiaki_senkusha_run(session); if(err != CHIAKI_ERR_SUCCESS) @@ -196,7 +202,7 @@ static void *session_thread_func(void *arg) session->mtu = 1454; session->rtt = 12; - CHIAKI_LOGI(&session->log, "Senkusha completed successfully"); + //CHIAKI_LOGI(&session->log, "Senkusha completed successfully"); err = chiaki_random_bytes(session->handshake_key, sizeof(session->handshake_key)); if(err != CHIAKI_ERR_SUCCESS)