Skip to content
This repository was archived by the owner on Mar 19, 2021. It is now read-only.

Commit 736b483

Browse files
committed
Add Key State Tracker
1 parent 68aadd4 commit 736b483

5 files changed

Lines changed: 89 additions & 0 deletions

File tree

lib/include/chiaki/gkcrypt.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ static inline void chiaki_gkcrypt_free(ChiakiGKCrypt *gkcrypt)
9696
free(gkcrypt);
9797
}
9898

99+
typedef struct chiaki_key_state_t {
100+
uint64_t prev;
101+
} ChiakiKeyState;
102+
103+
CHIAKI_EXPORT void chiaki_key_state_init(ChiakiKeyState *state);
104+
CHIAKI_EXPORT uint64_t chiaki_key_state_request_pos(ChiakiKeyState *state, uint32_t low);
105+
99106
#ifdef __cplusplus
100107
}
101108
#endif

lib/src/gkcrypt.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,3 +542,19 @@ static void *gkcrypt_thread_func(void *user)
542542
chiaki_mutex_unlock(&gkcrypt->key_buf_mutex);
543543
return NULL;
544544
}
545+
546+
CHIAKI_EXPORT void chiaki_key_state_init(ChiakiKeyState *state)
547+
{
548+
state->prev = 0;
549+
}
550+
551+
CHIAKI_EXPORT uint64_t chiaki_key_state_request_pos(ChiakiKeyState *state, uint32_t low)
552+
{
553+
uint32_t prev_low = (uint32_t)state->prev;
554+
uint32_t high = (uint32_t)(state->prev >> 32);
555+
if(chiaki_seq_num_32_gt(low, prev_low) && low < prev_low)
556+
high++;
557+
else if(chiaki_seq_num_32_lt(low, prev_low) && low > prev_low && high)
558+
high--;
559+
return state->prev = (((uint64_t)high) << 32) | ((uint64_t)low);
560+
}

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ add_executable(chiaki-unit
99
gkcrypt.c
1010
takion.c
1111
seqnum.c
12+
keystate.c
1213
reorderqueue.c
1314
fec.c
1415
test_log.c

test/keystate.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* This file is part of Chiaki.
3+
*
4+
* Chiaki is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Chiaki is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include <munit.h>
19+
20+
#include <chiaki/gkcrypt.h>
21+
22+
static MunitResult test_key_state(const MunitParameter params[], void *user)
23+
{
24+
ChiakiKeyState state;
25+
chiaki_key_state_init(&state);
26+
27+
uint64_t pos = chiaki_key_state_request_pos(&state, 0);
28+
munit_assert_uint64(pos, ==, 0);
29+
pos = chiaki_key_state_request_pos(&state, 0x1337);
30+
munit_assert_uint64(pos, ==, 0x1337);
31+
pos = chiaki_key_state_request_pos(&state, 0xffff0000);
32+
munit_assert_uint64(pos, ==, 0xffff0000);
33+
pos = chiaki_key_state_request_pos(&state, 0x1337);
34+
munit_assert_uint64(pos, ==, 0x100001337);
35+
pos = chiaki_key_state_request_pos(&state, 0xffff1337);
36+
munit_assert_uint64(pos, ==, 0xffff1337);
37+
pos = chiaki_key_state_request_pos(&state, 0x50000000);
38+
munit_assert_uint64(pos, ==, 0x150000000);
39+
pos = chiaki_key_state_request_pos(&state, 0xb0000000);
40+
munit_assert_uint64(pos, ==, 0x1b0000000);
41+
pos = chiaki_key_state_request_pos(&state, 0x00000000);
42+
munit_assert_uint64(pos, ==, 0x200000000);
43+
44+
return MUNIT_OK;
45+
}
46+
47+
MunitTest tests_key_state[] = {
48+
{
49+
"/key_state",
50+
test_key_state,
51+
NULL,
52+
NULL,
53+
MUNIT_TEST_OPTION_NONE,
54+
NULL
55+
},
56+
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
57+
};

test/main.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <munit.h>
1919

2020
extern MunitTest tests_seq_num[];
21+
extern MunitTest tests_key_state[];
2122
extern MunitTest tests_reorder_queue[];
2223
extern MunitTest tests_http[];
2324
extern MunitTest tests_rpcrypt[];
@@ -34,6 +35,13 @@ static MunitSuite suites[] = {
3435
1,
3536
MUNIT_SUITE_OPTION_NONE
3637
},
38+
{
39+
"/key_state",
40+
tests_key_state,
41+
NULL,
42+
1,
43+
MUNIT_SUITE_OPTION_NONE
44+
},
3745
{
3846
"/reorder_queue",
3947
tests_reorder_queue,

0 commit comments

Comments
 (0)