mirror of
https://git.sr.ht/~thestr4ng3r/chiaki
synced 2025-08-14 02:36:51 -07:00
Add Key State Tracker
This commit is contained in:
parent
68aadd4343
commit
736b4835df
5 changed files with 89 additions and 0 deletions
|
@ -96,6 +96,13 @@ static inline void chiaki_gkcrypt_free(ChiakiGKCrypt *gkcrypt)
|
||||||
free(gkcrypt);
|
free(gkcrypt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef struct chiaki_key_state_t {
|
||||||
|
uint64_t prev;
|
||||||
|
} ChiakiKeyState;
|
||||||
|
|
||||||
|
CHIAKI_EXPORT void chiaki_key_state_init(ChiakiKeyState *state);
|
||||||
|
CHIAKI_EXPORT uint64_t chiaki_key_state_request_pos(ChiakiKeyState *state, uint32_t low);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -542,3 +542,19 @@ static void *gkcrypt_thread_func(void *user)
|
||||||
chiaki_mutex_unlock(&gkcrypt->key_buf_mutex);
|
chiaki_mutex_unlock(&gkcrypt->key_buf_mutex);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CHIAKI_EXPORT void chiaki_key_state_init(ChiakiKeyState *state)
|
||||||
|
{
|
||||||
|
state->prev = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
CHIAKI_EXPORT uint64_t chiaki_key_state_request_pos(ChiakiKeyState *state, uint32_t low)
|
||||||
|
{
|
||||||
|
uint32_t prev_low = (uint32_t)state->prev;
|
||||||
|
uint32_t high = (uint32_t)(state->prev >> 32);
|
||||||
|
if(chiaki_seq_num_32_gt(low, prev_low) && low < prev_low)
|
||||||
|
high++;
|
||||||
|
else if(chiaki_seq_num_32_lt(low, prev_low) && low > prev_low && high)
|
||||||
|
high--;
|
||||||
|
return state->prev = (((uint64_t)high) << 32) | ((uint64_t)low);
|
||||||
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ add_executable(chiaki-unit
|
||||||
gkcrypt.c
|
gkcrypt.c
|
||||||
takion.c
|
takion.c
|
||||||
seqnum.c
|
seqnum.c
|
||||||
|
keystate.c
|
||||||
reorderqueue.c
|
reorderqueue.c
|
||||||
fec.c
|
fec.c
|
||||||
test_log.c
|
test_log.c
|
||||||
|
|
57
test/keystate.c
Normal file
57
test/keystate.c
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
/*
|
||||||
|
* 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 <munit.h>
|
||||||
|
|
||||||
|
#include <chiaki/gkcrypt.h>
|
||||||
|
|
||||||
|
static MunitResult test_key_state(const MunitParameter params[], void *user)
|
||||||
|
{
|
||||||
|
ChiakiKeyState state;
|
||||||
|
chiaki_key_state_init(&state);
|
||||||
|
|
||||||
|
uint64_t pos = chiaki_key_state_request_pos(&state, 0);
|
||||||
|
munit_assert_uint64(pos, ==, 0);
|
||||||
|
pos = chiaki_key_state_request_pos(&state, 0x1337);
|
||||||
|
munit_assert_uint64(pos, ==, 0x1337);
|
||||||
|
pos = chiaki_key_state_request_pos(&state, 0xffff0000);
|
||||||
|
munit_assert_uint64(pos, ==, 0xffff0000);
|
||||||
|
pos = chiaki_key_state_request_pos(&state, 0x1337);
|
||||||
|
munit_assert_uint64(pos, ==, 0x100001337);
|
||||||
|
pos = chiaki_key_state_request_pos(&state, 0xffff1337);
|
||||||
|
munit_assert_uint64(pos, ==, 0xffff1337);
|
||||||
|
pos = chiaki_key_state_request_pos(&state, 0x50000000);
|
||||||
|
munit_assert_uint64(pos, ==, 0x150000000);
|
||||||
|
pos = chiaki_key_state_request_pos(&state, 0xb0000000);
|
||||||
|
munit_assert_uint64(pos, ==, 0x1b0000000);
|
||||||
|
pos = chiaki_key_state_request_pos(&state, 0x00000000);
|
||||||
|
munit_assert_uint64(pos, ==, 0x200000000);
|
||||||
|
|
||||||
|
return MUNIT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
MunitTest tests_key_state[] = {
|
||||||
|
{
|
||||||
|
"/key_state",
|
||||||
|
test_key_state,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
MUNIT_TEST_OPTION_NONE,
|
||||||
|
NULL
|
||||||
|
},
|
||||||
|
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
|
||||||
|
};
|
|
@ -18,6 +18,7 @@
|
||||||
#include <munit.h>
|
#include <munit.h>
|
||||||
|
|
||||||
extern MunitTest tests_seq_num[];
|
extern MunitTest tests_seq_num[];
|
||||||
|
extern MunitTest tests_key_state[];
|
||||||
extern MunitTest tests_reorder_queue[];
|
extern MunitTest tests_reorder_queue[];
|
||||||
extern MunitTest tests_http[];
|
extern MunitTest tests_http[];
|
||||||
extern MunitTest tests_rpcrypt[];
|
extern MunitTest tests_rpcrypt[];
|
||||||
|
@ -34,6 +35,13 @@ static MunitSuite suites[] = {
|
||||||
1,
|
1,
|
||||||
MUNIT_SUITE_OPTION_NONE
|
MUNIT_SUITE_OPTION_NONE
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"/key_state",
|
||||||
|
tests_key_state,
|
||||||
|
NULL,
|
||||||
|
1,
|
||||||
|
MUNIT_SUITE_OPTION_NONE
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"/reorder_queue",
|
"/reorder_queue",
|
||||||
tests_reorder_queue,
|
tests_reorder_queue,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue