Skip to content

Commit 74e3ff8

Browse files
committed
fix: get correct agent registry
1 parent ec64da9 commit 74e3ff8

File tree

2 files changed

+65
-45
lines changed

2 files changed

+65
-45
lines changed

src/conn.c

Lines changed: 64 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ typedef struct conn_mode_entry {
3939

4040
} conn_mode_entry_t;
4141

42-
typedef struct conn_impl {
43-
conn_registry_t *registry;
44-
timestamp_t next_timestamp;
45-
bool finished;
46-
} conn_impl_t;
47-
4842
#define MODE_ENTRIES_SIZE 3
4943

5044
static conn_mode_entry_t mode_entries[MODE_ENTRIES_SIZE] = {
@@ -77,6 +71,58 @@ static conn_registry_t *get_registry(conn_mode_entry_t *entry, uint16_t port) {
7771
return NULL;
7872
}
7973

74+
static conn_registry_t *get_agent_registry(conn_mode_entry_t *entry, juice_agent_t *agent) {
75+
for (int i = 0; i < entry->registries_size; i++) {
76+
if (!entry->registries[i]) {
77+
continue;
78+
}
79+
80+
conn_registry_t *registry = entry->registries[i];
81+
82+
for (int j = 0; j < registry->agents_size; j++) {
83+
if (registry->agents[j] == agent) {
84+
return registry;
85+
}
86+
}
87+
}
88+
89+
return NULL;
90+
}
91+
92+
static int add_registry(conn_mode_entry_t *entry, conn_registry_t *registry) {
93+
int i = 0;
94+
while (i < entry->registries_size && entry->registries[i])
95+
++i;
96+
97+
if (i == entry->registries_size) {
98+
int new_size = entry->registries_size * 2;
99+
100+
if (new_size == 0) {
101+
new_size = 1;
102+
}
103+
104+
JLOG_DEBUG("Reallocating registries array, new_size=%d", new_size);
105+
assert(new_size > 0);
106+
107+
conn_registry_t **new_registries =
108+
realloc(entry->registries, new_size * sizeof(conn_registry_t *));
109+
if (!new_registries) {
110+
JLOG_FATAL("Memory reallocation failed for registries array");
111+
return -1;
112+
}
113+
114+
entry->registries = new_registries;
115+
entry->registries_size = new_size;
116+
memset(entry->registries + i, 0, (new_size - i) * sizeof(conn_registry_t *));
117+
}
118+
119+
entry->registries[i] = registry;
120+
registry->registry_index = i;
121+
++entry->registries_count;
122+
123+
return 0;
124+
}
125+
80126
static int acquire_registry(conn_mode_entry_t *entry, udp_socket_config_t *config) {
81127
// entry must be locked
82128
conn_registry_t *registry = get_registry(entry, config->port_begin);
@@ -104,11 +150,11 @@ static int acquire_registry(conn_mode_entry_t *entry, udp_socket_config_t *confi
104150
registry->agents_count = 0;
105151
memset(registry->agents, 0, INITIAL_REGISTRY_SIZE * sizeof(juice_agent_t *));
106152

107-
registry->port = config->port_begin;
108-
109153
mutex_init(&registry->mutex, MUTEX_RECURSIVE);
110154
mutex_lock(&registry->mutex);
111155

156+
registry->port = config->port_begin;
157+
112158
if (entry->registry_init_func(registry, config)) {
113159
JLOG_FATAL("Registry initialization failed");
114160
mutex_unlock(&registry->mutex);
@@ -117,37 +163,13 @@ static int acquire_registry(conn_mode_entry_t *entry, udp_socket_config_t *confi
117163
return -1;
118164
}
119165

120-
int i = 0;
121-
while (i < entry->registries_size && entry->registries[i])
122-
++i;
123-
124-
if (i == entry->registries_size) {
125-
int new_size = entry->registries_size * 2;
126-
127-
if (new_size == 0) {
128-
new_size = 1;
129-
}
130-
131-
JLOG_DEBUG("Reallocating cb_mux_incomings array, new_size=%d", new_size);
132-
assert(new_size > 0);
133-
134-
conn_registry_t **new_registries =
135-
realloc(entry->registries, new_size * sizeof(conn_registry_t *));
136-
if (!new_registries) {
137-
JLOG_FATAL("Memory reallocation failed for cb_mux_incomings array");
138-
mutex_unlock(&registry->mutex);
139-
mutex_unlock(&entry->mutex);
140-
return -1;
141-
}
142-
143-
entry->registries = new_registries;
144-
entry->registries_size = new_size;
145-
memset(entry->registries + i, 0, (new_size - i) * sizeof(conn_registry_t *));
166+
if (add_registry(entry, registry)) {
167+
JLOG_FATAL("Adding registry to entry failed");
168+
mutex_unlock(&registry->mutex);
169+
free(registry->agents);
170+
free(registry);
171+
return -1;
146172
}
147-
148-
entry->registries[i] = registry;
149-
registry->index = i;
150-
++entry->registries_count;
151173
} else {
152174
mutex_lock(&registry->mutex);
153175
}
@@ -170,11 +192,11 @@ static void release_registry(conn_mode_entry_t *entry, conn_registry_t *registry
170192
if (entry->registry_cleanup_func)
171193
entry->registry_cleanup_func(registry);
172194

173-
if (registry->index > -1) {
174-
int i = registry->index;
195+
if (registry->registry_index > -1) {
196+
int i = registry->registry_index;
175197
assert(entry->registries[i] == registry);
176198
entry->registries[i] = NULL;
177-
registry->index = -1;
199+
registry->registry_index = -1;
178200
}
179201

180202
assert(entry->registries_count > 0);
@@ -257,9 +279,7 @@ void conn_destroy(juice_agent_t *agent) {
257279
mutex_lock(&entry->mutex);
258280

259281
JLOG_DEBUG("Destroying connection");
260-
conn_impl_t *conn_impl = agent->conn_impl;
261-
262-
conn_registry_t *registry = conn_impl->registry;
282+
conn_registry_t *registry = get_agent_registry(entry, agent);
263283
if (registry) {
264284
mutex_lock(&registry->mutex);
265285

src/conn.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ typedef struct juice_agent juice_agent_t;
2525
// See include/juice/juice.h for implemented concurrency modes
2626

2727
typedef struct conn_registry {
28-
int index;
28+
int registry_index;
2929
uint16_t port;
3030
void *impl;
3131
mutex_t mutex;

0 commit comments

Comments
 (0)