Skip to content

Commit 70d57d4

Browse files
committed
2026-03-01
1 parent 823072b commit 70d57d4

4 files changed

Lines changed: 484 additions & 541 deletions

File tree

Source/DKTP/connections.c

Lines changed: 134 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
#include "async.h"
33
#include "memutils.h"
44

5-
static qsc_mutex m_pool_mutex;
6-
75
/** \cond */
86
typedef struct dktp_connection_set
97
{
@@ -13,6 +11,8 @@ typedef struct dktp_connection_set
1311
} dktp_connection_set;
1412

1513
static dktp_connection_set m_connection_set;
14+
static qsc_mutex m_pool_mutex;
15+
static bool m_state_initialized;
1616
/** \endcond */
1717

1818
bool dktp_connections_active(size_t index)
@@ -21,14 +21,17 @@ bool dktp_connections_active(size_t index)
2121

2222
res = false;
2323

24-
qsc_async_mutex_lock(m_pool_mutex);
25-
26-
if (index < m_connection_set.count)
24+
if (m_state_initialized == true)
2725
{
28-
res = m_connection_set.active[index];
29-
}
26+
qsc_async_mutex_lock(m_pool_mutex);
3027

31-
qsc_async_mutex_unlock(m_pool_mutex);
28+
if (index < m_connection_set.count)
29+
{
30+
res = m_connection_set.active[index];
31+
}
32+
33+
qsc_async_mutex_unlock(m_pool_mutex);
34+
}
3235

3336
return res;
3437
}
@@ -39,131 +42,150 @@ size_t dktp_connections_available(void)
3942

4043
count = 0U;
4144

42-
qsc_async_mutex_lock(m_pool_mutex);
43-
44-
for (size_t i = 0U; i < m_connection_set.count; ++i)
45+
if (m_state_initialized == true)
4546
{
46-
if (m_connection_set.active[i] == false)
47+
qsc_async_mutex_lock(m_pool_mutex);
48+
49+
for (size_t i = 0U; i < m_connection_set.count; ++i)
4750
{
48-
++count;
51+
if (m_connection_set.active[i] == false)
52+
{
53+
++count;
54+
}
4955
}
50-
}
5156

52-
qsc_async_mutex_unlock(m_pool_mutex);
57+
qsc_async_mutex_unlock(m_pool_mutex);
58+
}
5359

5460
return count;
5561
}
5662

5763
void dktp_connections_clear(void)
5864
{
59-
qsc_async_mutex_lock(m_pool_mutex);
65+
if (m_state_initialized == true)
66+
{
67+
qsc_async_mutex_lock(m_pool_mutex);
6068

61-
qsc_memutils_clear(m_connection_set.conset, sizeof(dktp_connection_state) * m_connection_set.count);
69+
qsc_memutils_clear(m_connection_set.conset, sizeof(dktp_connection_state) * m_connection_set.count);
6270

63-
for (size_t i = 0U; i < m_connection_set.count; ++i)
64-
{
65-
m_connection_set.active[i] = false;
66-
m_connection_set.conset[i].cid = (uint32_t)i;
67-
}
71+
for (size_t i = 0U; i < m_connection_set.count; ++i)
72+
{
73+
m_connection_set.active[i] = false;
74+
m_connection_set.conset[i].cid = (uint32_t)i;
75+
}
6876

69-
qsc_async_mutex_unlock(m_pool_mutex);
77+
qsc_async_mutex_unlock(m_pool_mutex);
78+
}
7079
}
7180

7281
void dktp_connections_dispose(void)
7382
{
74-
75-
if (m_connection_set.conset != NULL)
83+
if (m_state_initialized == true)
7684
{
77-
#if defined(DKTP_ASYMMETRIC_RATCHET)
78-
for (size_t i = 0U; i < m_connection_set.count; ++i)
85+
if (m_connection_set.conset != NULL)
7986
{
80-
if (m_connection_set.conset[i].txlock)
87+
#if defined(DKTP_ASYMMETRIC_RATCHET)
88+
for (size_t i = 0U; i < m_connection_set.count; ++i)
8189
{
82-
qsc_async_mutex_destroy(m_connection_set.conset[i].txlock);
90+
if (m_connection_set.conset[i].txlock)
91+
{
92+
qsc_async_mutex_destroy(m_connection_set.conset[i].txlock);
93+
}
8394
}
84-
}
8595
#endif
8696

87-
dktp_connections_clear();
97+
dktp_connections_clear();
8898

89-
if (m_connection_set.conset != NULL)
99+
if (m_connection_set.conset != NULL)
100+
{
101+
qsc_memutils_alloc_free(m_connection_set.conset);
102+
m_connection_set.conset = NULL;
103+
}
104+
}
105+
106+
if (m_connection_set.active != NULL)
90107
{
91-
qsc_memutils_alloc_free(m_connection_set.conset);
92-
m_connection_set.conset = NULL;
108+
qsc_memutils_alloc_free(m_connection_set.active);
109+
m_connection_set.active = NULL;
93110
}
94-
}
95111

96-
if (m_connection_set.active != NULL)
97-
{
98-
qsc_memutils_alloc_free(m_connection_set.active);
99-
m_connection_set.active = NULL;
100-
}
112+
m_connection_set.count = 0U;
101113

102-
m_connection_set.count = 0U;
114+
if (m_pool_mutex)
115+
{
116+
(void)qsc_async_mutex_destroy(m_pool_mutex);
117+
}
103118

104-
if (m_pool_mutex)
105-
{
106-
(void)qsc_async_mutex_destroy(m_pool_mutex);
119+
m_state_initialized = false;
107120
}
108121
}
109122

110-
dktp_connection_state* dktp_connections_index(size_t index)
123+
bool dktp_connections_full(void)
111124
{
112-
dktp_connection_state* res;
113-
114-
res = NULL;
125+
bool res;
115126

116-
qsc_async_mutex_lock(m_pool_mutex);
127+
res = true;
117128

118-
if (index < m_connection_set.count)
129+
if (m_state_initialized == true)
119130
{
120-
res = &m_connection_set.conset[index];
121-
}
131+
qsc_async_mutex_lock(m_pool_mutex);
122132

123-
qsc_async_mutex_unlock(m_pool_mutex);
133+
for (size_t i = 0U; i < m_connection_set.count; ++i)
134+
{
135+
if (m_connection_set.active[i] == false)
136+
{
137+
res = false;
138+
break;
139+
}
140+
}
141+
142+
qsc_async_mutex_unlock(m_pool_mutex);
143+
}
124144

125145
return res;
126146
}
127147

128-
bool dktp_connections_full(void)
148+
dktp_connection_state* dktp_connections_get(uint32_t cid)
129149
{
130-
bool res;
131-
132-
res = true;
150+
dktp_connection_state* res;
133151

134-
qsc_async_mutex_lock(m_pool_mutex);
152+
res = NULL;
135153

136-
for (size_t i = 0U; i < m_connection_set.count; ++i)
154+
if (m_state_initialized == true)
137155
{
138-
if (m_connection_set.active[i] == false)
156+
qsc_async_mutex_lock(m_pool_mutex);
157+
158+
for (size_t i = 0U; i < m_connection_set.count; ++i)
139159
{
140-
res = false;
141-
break;
160+
if (m_connection_set.conset[i].cid == cid)
161+
{
162+
res = &m_connection_set.conset[i];
163+
}
142164
}
143-
}
144165

145-
qsc_async_mutex_unlock(m_pool_mutex);
166+
qsc_async_mutex_unlock(m_pool_mutex);
167+
}
146168

147169
return res;
148170
}
149171

150-
dktp_connection_state* dktp_connections_get(uint32_t cid)
172+
dktp_connection_state* dktp_connections_index(size_t index)
151173
{
152174
dktp_connection_state* res;
153175

154176
res = NULL;
155177

156-
qsc_async_mutex_lock(m_pool_mutex);
157-
158-
for (size_t i = 0; i < m_connection_set.count; ++i)
178+
if (m_state_initialized == true)
159179
{
160-
if (m_connection_set.conset[i].cid == cid)
180+
qsc_async_mutex_lock(m_pool_mutex);
181+
182+
if (index < m_connection_set.count)
161183
{
162-
res = &m_connection_set.conset[i];
184+
res = &m_connection_set.conset[index];
163185
}
164-
}
165186

166-
qsc_async_mutex_unlock(m_pool_mutex);
187+
qsc_async_mutex_unlock(m_pool_mutex);
188+
}
167189

168190
return res;
169191
}
@@ -196,6 +218,7 @@ bool dktp_connections_initialize(size_t count)
196218
m_connection_set.active[i] = false;
197219
}
198220

221+
m_state_initialized = true;
199222
res = true;
200223
}
201224
}
@@ -210,60 +233,71 @@ dktp_connection_state* dktp_connections_next(void)
210233

211234
res = NULL;
212235

213-
qsc_async_mutex_lock(m_pool_mutex);
214-
215-
for (size_t i = 0U; i < m_connection_set.count; ++i)
236+
if (m_state_initialized == true)
216237
{
217-
if (m_connection_set.active[i] == false)
238+
qsc_async_mutex_lock(m_pool_mutex);
239+
240+
for (size_t i = 0U; i < m_connection_set.count; ++i)
218241
{
219-
res = &m_connection_set.conset[i];
220-
m_connection_set.active[i] = true;
242+
if (m_connection_set.active[i] == false)
243+
{
244+
res = &m_connection_set.conset[i];
245+
m_connection_set.active[i] = true;
221246
#if defined(DKTP_ASYMMETRIC_RATCHET)
222-
res->txlock = qsc_async_mutex_create();
247+
res->txlock = qsc_async_mutex_create();
223248
#endif
224-
break;
249+
break;
250+
}
225251
}
252+
253+
qsc_async_mutex_unlock(m_pool_mutex);
226254
}
227-
228-
qsc_async_mutex_unlock(m_pool_mutex);
229255

230256
return res;
231257
}
232258

233259
void dktp_connections_reset(uint32_t cid)
234260
{
235-
qsc_async_mutex_lock(m_pool_mutex);
236-
237-
for (size_t i = 0U; i < m_connection_set.count; ++i)
261+
if (m_state_initialized == true)
238262
{
239-
if (m_connection_set.conset[i].cid == cid)
263+
qsc_async_mutex_lock(m_pool_mutex);
264+
265+
for (size_t i = 0U; i < m_connection_set.count; ++i)
240266
{
267+
if (m_connection_set.conset[i].cid == cid)
268+
{
241269
#if defined(DKTP_ASYMMETRIC_RATCHET)
242-
qsc_mutex mtmp = m_connection_set.conset[i].txlock;
270+
qsc_mutex mtmp = m_connection_set.conset[i].txlock;
243271
#endif
244-
qsc_memutils_clear(&m_connection_set.conset[i], sizeof(dktp_connection_state));
245-
m_connection_set.conset[i].cid = (uint32_t)i;
246-
m_connection_set.active[i] = false;
272+
qsc_memutils_clear(&m_connection_set.conset[i], sizeof(dktp_connection_state));
273+
m_connection_set.conset[i].cid = (uint32_t)i;
274+
m_connection_set.active[i] = false;
247275
#if defined(DKTP_ASYMMETRIC_RATCHET)
248-
if (mtmp)
249-
{
250-
qsc_async_mutex_destroy(mtmp);
251-
}
276+
if (mtmp)
277+
{
278+
qsc_async_mutex_destroy(mtmp);
279+
}
252280
#endif
253-
break;
281+
break;
282+
}
254283
}
255-
}
256284

257-
qsc_async_mutex_unlock(m_pool_mutex);
285+
qsc_async_mutex_unlock(m_pool_mutex);
286+
}
258287
}
259288

260289
size_t dktp_connections_size(void)
261290
{
262291
size_t res;
263292

264-
qsc_async_mutex_lock(m_pool_mutex);
265-
res = m_connection_set.count;
266-
qsc_async_mutex_unlock(m_pool_mutex);
293+
res = 0U;
294+
295+
if (m_state_initialized == true)
296+
{
297+
qsc_async_mutex_lock(m_pool_mutex);
298+
res = m_connection_set.count;
299+
qsc_async_mutex_unlock(m_pool_mutex);
300+
}
267301

268302
return res;
269303
}

0 commit comments

Comments
 (0)