Browse Source

Fix test_etcp_api: use single callback with connection check

etcp_bind uses global table - multiple binds with same ID overwrite.
Fixed by using single recv_callback that checks conn pointer:
- conn == server_conn: forward direction (client->server)
- conn == client_conn: backward direction (server->client)

Test now passes: All 100 packets transmitted in each direction.
nodeinfo-routing-update
Evgeny 8 months ago
parent
commit
7a1f160094
  1. 75
      tests/test_etcp_api.c

75
tests/test_etcp_api.c

@ -154,12 +154,11 @@ static int verify_packet_data(uint8_t* buffer, int size, int expected_seq) {
return 1;
}
// Callback для получения пакетов на сервере (forward direction)
static void server_recv_callback(struct ETCP_CONN* conn, struct ll_entry* entry) {
(void)conn;
// Универсальный callback для получения пакетов
// Определяет направление по соединению (client_conn vs server_conn)
static void recv_callback(struct ETCP_CONN* conn, struct ll_entry* entry) {
if (!entry || !entry->dgram || entry->len < PACKET_HEADER_SIZE + 1) {
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Server received invalid packet");
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Received invalid packet");
if (entry) {
queue_entry_free(entry);
queue_dgram_free(entry);
@ -167,40 +166,27 @@ static void server_recv_callback(struct ETCP_CONN* conn, struct ll_entry* entry)
return;
}
// Verify packet with strict sequence checking
if (verify_packet_data(entry->dgram, entry->len, packets_received_fwd)) {
packets_received_fwd++;
} else {
int seq = entry->dgram[1] | (entry->dgram[2] << 8);
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Server packet verification failed, seq=%d, expected=%d",
seq, packets_received_fwd);
}
// Освобождаем entry и dgram
queue_entry_free(entry);
queue_dgram_free(entry);
}
// Callback для получения пакетов на клиенте (backward direction)
static void client_recv_callback(struct ETCP_CONN* conn, struct ll_entry* entry) {
(void)conn;
if (!entry || !entry->dgram || entry->len < PACKET_HEADER_SIZE + 1) {
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Client received invalid packet");
if (entry) {
queue_entry_free(entry);
queue_dgram_free(entry);
// Определяем направление по соединению
if (conn == server_conn) {
// Пакет пришел на сервер (forward direction: client->server)
if (verify_packet_data(entry->dgram, entry->len, packets_received_fwd)) {
packets_received_fwd++;
} else {
int seq = entry->dgram[1] | (entry->dgram[2] << 8);
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Server packet verification failed, seq=%d, expected=%d",
seq, packets_received_fwd);
}
} else if (conn == client_conn) {
// Пакет пришел на клиент (backward direction: server->client)
if (verify_packet_data(entry->dgram, entry->len, packets_received_back)) {
packets_received_back++;
} else {
int seq = entry->dgram[1] | (entry->dgram[2] << 8);
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Client packet verification failed, seq=%d, expected=%d",
seq, packets_received_back);
}
return;
}
// Verify packet with strict sequence checking
if (verify_packet_data(entry->dgram, entry->len, packets_received_back)) {
packets_received_back++;
} else {
int seq = entry->dgram[1] | (entry->dgram[2] << 8);
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Client packet verification failed, seq=%d, expected=%d",
seq, packets_received_back);
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Unknown connection %p", (void*)conn);
}
// Освобождаем entry и dgram
@ -486,21 +472,14 @@ int main() {
// Инициализация ETCP API
etcp_api_init();
// Регистрируем callbacks для получения пакетов
if (etcp_bind(DATA_PACKET_ID, server_recv_callback) != 0) {
printf("Failed to bind server callback\n");
etcp_api_deinit();
return 1;
}
if (etcp_bind(DATA_PACKET_ID, client_recv_callback) != 0) {
printf("Failed to bind client callback\n");
etcp_unbind(DATA_PACKET_ID);
// Регистрируем callback для получения пакетов (один на все соединения)
if (etcp_bind(DATA_PACKET_ID, recv_callback) != 0) {
printf("Failed to bind callback\n");
etcp_api_deinit();
return 1;
}
printf("Callbacks registered for ID=%d\n", DATA_PACKET_ID);
printf("Callback registered for ID=%d\n", DATA_PACKET_ID);
printf("Creating server...\n");
ua = uasync_create();

Loading…
Cancel
Save