|
|
|
|
@ -31,7 +31,7 @@ static void monitor_connections(void* arg) {
|
|
|
|
|
|
|
|
|
|
if (test_completed) { |
|
|
|
|
// НЕ перезапускать таймер если тест завершен
|
|
|
|
|
printf("[MONITOR] Test completed, stopping monitor timer\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[MONITOR] Test completed, stopping monitor timer\n"); |
|
|
|
|
monitor_timeout_id = NULL; |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
@ -48,7 +48,7 @@ static void monitor_connections(void* arg) {
|
|
|
|
|
struct ETCP_LINK* link = conn->links; |
|
|
|
|
while (link) { |
|
|
|
|
server_links++; |
|
|
|
|
printf("[SERVER] Link: peer=%llx initialized=%d type=%s\n",
|
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[SERVER] Link: peer=%llx initialized=%d type=%s\n",
|
|
|
|
|
(unsigned long long)conn->peer_node_id, |
|
|
|
|
link->initialized, |
|
|
|
|
link->is_server ? "server" : "client"); |
|
|
|
|
@ -66,7 +66,7 @@ static void monitor_connections(void* arg) {
|
|
|
|
|
while (link) { |
|
|
|
|
client_links++; |
|
|
|
|
if (link->is_server == 0) { // client link
|
|
|
|
|
printf("[CLIENT] Link: peer=%llx initialized=%d timer=%s retry=%d\n", |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLIENT] Link: peer=%llx initialized=%d timer=%s retry=%d\n", |
|
|
|
|
(unsigned long long)conn->peer_node_id, |
|
|
|
|
link->initialized, |
|
|
|
|
link->init_timer ? "active" : "null", |
|
|
|
|
@ -83,18 +83,18 @@ static void monitor_connections(void* arg) {
|
|
|
|
|
|
|
|
|
|
// Check if connection established
|
|
|
|
|
if (client_initialized) { |
|
|
|
|
printf("\n=== SUCCESS: Client connection established! ===\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "\n=== SUCCESS: Client connection established! ===\n"); |
|
|
|
|
test_completed = 1; // Success
|
|
|
|
|
// Отменить таймаут теста для быстрого завершения
|
|
|
|
|
if (test_timeout_id) { |
|
|
|
|
printf("[TEST] Cancelling test timeout for immediate exit\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[TEST] Cancelling test timeout for immediate exit\n"); |
|
|
|
|
uasync_cancel_timeout(server_instance->ua, test_timeout_id); |
|
|
|
|
test_timeout_id = NULL; |
|
|
|
|
} |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
printf("[MONITOR] Server links: %d, Client links: %d, Status: %s\n", |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[MONITOR] Server links: %d, Client links: %d, Status: %s\n", |
|
|
|
|
server_links, client_links,
|
|
|
|
|
client_initialized ? "CONNECTED" : "connecting..."); |
|
|
|
|
|
|
|
|
|
@ -107,7 +107,7 @@ static void monitor_connections(void* arg) {
|
|
|
|
|
static void test_timeout(void* arg) { |
|
|
|
|
(void)arg; |
|
|
|
|
if (!test_completed) { |
|
|
|
|
printf("\n=== TIMEOUT: Connection not established in %d seconds ===\n", TEST_TIMEOUT_MS/1000); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "\n=== TIMEOUT: Connection not established in %d seconds ===\n", TEST_TIMEOUT_MS/1000); |
|
|
|
|
test_completed = 2; // Timeout/failure
|
|
|
|
|
// Cancel the monitoring timeout since we're done
|
|
|
|
|
if (monitor_timeout_id) { |
|
|
|
|
@ -169,43 +169,43 @@ int main() {
|
|
|
|
|
|
|
|
|
|
// Check if TUN is disabled and handle accordingly
|
|
|
|
|
if (client_instance->tun.fd < 0) { |
|
|
|
|
printf("ℹ️ Client TUN disabled - skipping full initialization\n"); |
|
|
|
|
printf("✅ Client instance created successfully (node_id=%llx)\n", (unsigned long long)client_instance->node_id); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "ℹ️ Client TUN disabled - skipping full initialization\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "✅ Client instance created successfully (node_id=%llx)\n", (unsigned long long)client_instance->node_id); |
|
|
|
|
// Skip to test execution
|
|
|
|
|
} else { |
|
|
|
|
// Normal initialization for TUN-enabled mode
|
|
|
|
|
if (utun_instance_init(client_instance) < 0) { |
|
|
|
|
printf("Failed to initialize client instance\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Failed to initialize client instance\n"); |
|
|
|
|
utun_instance_destroy(server_instance); |
|
|
|
|
utun_instance_destroy(client_instance); |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (utun_instance_register_sockets(client_instance) < 0) { |
|
|
|
|
printf("Failed to register client sockets\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Failed to register client sockets\n"); |
|
|
|
|
utun_instance_destroy(server_instance); |
|
|
|
|
utun_instance_destroy(client_instance); |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
printf("Client instance ready (node_id=%llx)\n\n", (unsigned long long)client_instance->node_id); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Client instance ready (node_id=%llx)\n\n", (unsigned long long)client_instance->node_id); |
|
|
|
|
|
|
|
|
|
// Start monitoring
|
|
|
|
|
printf("Starting connection monitoring...\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Starting connection monitoring...\n"); |
|
|
|
|
monitor_timeout_id = uasync_set_timeout(server_ua, 1000, NULL, monitor_connections); |
|
|
|
|
test_timeout_id = uasync_set_timeout(server_ua, TEST_TIMEOUT_MS, NULL, test_timeout); |
|
|
|
|
|
|
|
|
|
// Main event loop
|
|
|
|
|
printf("Running event loop...\n\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Running event loop...\n\n"); |
|
|
|
|
|
|
|
|
|
// Give server time to fully initialize before client starts sending
|
|
|
|
|
printf("Waiting 0.5 seconds for server initialization...\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Waiting 0.5 seconds for server initialization...\n"); |
|
|
|
|
for (int i = 0; i < 50; i++) { // Reduced from 200 to 50 iterations (0.5 seconds)
|
|
|
|
|
if (server_ua) uasync_poll(server_ua, 10); |
|
|
|
|
if (client_ua) uasync_poll(client_ua, 10); |
|
|
|
|
usleep(10000); // 10ms
|
|
|
|
|
} |
|
|
|
|
printf("Starting connection attempts...\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Starting connection attempts...\n"); |
|
|
|
|
|
|
|
|
|
int elapsed = 0; |
|
|
|
|
int poll_interval = 5; // Reduced from 10ms to 5ms for faster response
|
|
|
|
|
@ -218,49 +218,49 @@ int main() {
|
|
|
|
|
|
|
|
|
|
// Быстрый выход если подключение установлено
|
|
|
|
|
if (test_completed == 1) { |
|
|
|
|
printf("[TEST] Connection established, exiting early after %d ms\n", elapsed); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[TEST] Connection established, exiting early after %d ms\n", elapsed); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
|
printf("\nCleaning up...\n"); |
|
|
|
|
printf("[CLEANUP] server_ua=%p, client_ua=%p\n", server_ua, client_ua); |
|
|
|
|
printf("[CLEANUP] server_instance=%p, client_instance=%p\n", server_instance, client_instance); |
|
|
|
|
printf("[CLEANUP] monitor_timeout_id=%p, test_timeout_id=%p\n", monitor_timeout_id, test_timeout_id); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "\nCleaning up...\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLEANUP] server_ua=%p, client_ua=%p\n", server_ua, client_ua); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLEANUP] server_instance=%p, client_instance=%p\n", server_instance, client_instance); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLEANUP] monitor_timeout_id=%p, test_timeout_id=%p\n", monitor_timeout_id, test_timeout_id); |
|
|
|
|
|
|
|
|
|
// СНАЧАЛА отменить таймеры пока uasync ещё жив
|
|
|
|
|
if (monitor_timeout_id) { |
|
|
|
|
printf("[CLEANUP] Canceling monitor timeout on valid uasync\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLEANUP] Canceling monitor timeout on valid uasync\n"); |
|
|
|
|
uasync_cancel_timeout(server_ua, monitor_timeout_id); |
|
|
|
|
monitor_timeout_id = NULL; |
|
|
|
|
} |
|
|
|
|
if (test_timeout_id) { |
|
|
|
|
printf("[CLEANUP] Canceling test timeout on valid uasync\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLEANUP] Canceling test timeout on valid uasync\n"); |
|
|
|
|
uasync_cancel_timeout(server_ua, test_timeout_id); |
|
|
|
|
test_timeout_id = NULL; |
|
|
|
|
} |
|
|
|
|
printf("[CLEANUP] Timeouts canceled\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLEANUP] Timeouts canceled\n"); |
|
|
|
|
|
|
|
|
|
// Диагностика ресурсов перед cleanup
|
|
|
|
|
if (server_ua) { |
|
|
|
|
printf("[CLEANUP] Server resources before destroy:\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLEANUP] Server resources before destroy:\n"); |
|
|
|
|
uasync_print_resources(server_ua, "SERVER"); |
|
|
|
|
} |
|
|
|
|
if (client_ua) { |
|
|
|
|
printf("[CLEANUP] Client resources before destroy:\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLEANUP] Client resources before destroy:\n"); |
|
|
|
|
uasync_print_resources(client_ua, "CLIENT"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ПОТОМ уничтожить instances
|
|
|
|
|
if (server_instance) { |
|
|
|
|
server_instance->running = 0; |
|
|
|
|
printf("[CLEANUP] Destroying server instance %p\n", server_instance); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLEANUP] Destroying server instance %p\n", server_instance); |
|
|
|
|
utun_instance_destroy(server_instance); |
|
|
|
|
} |
|
|
|
|
if (client_instance) { |
|
|
|
|
client_instance->running = 0; |
|
|
|
|
printf("[CLEANUP] Destroying client instance %p\n", client_instance); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[CLEANUP] Destroying client instance %p\n", client_instance); |
|
|
|
|
utun_instance_destroy(client_instance); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -269,27 +269,27 @@ int main() {
|
|
|
|
|
// if (client_ua) uasync_destroy(client_ua);
|
|
|
|
|
|
|
|
|
|
if (test_completed == 1) { |
|
|
|
|
printf("\n=== TEST PASSED ===\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "\n=== TEST PASSED ===\n"); |
|
|
|
|
// Check if TUN was disabled and add note
|
|
|
|
|
if (server_instance->tun.fd < 0 || client_instance->tun.fd < 0) { |
|
|
|
|
printf("✅ ETCP connection test successful with TUN disabled\n"); |
|
|
|
|
printf("✅ Core ETCP protocol functionality verified\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "✅ ETCP connection test successful with TUN disabled\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "✅ Core ETCP protocol functionality verified\n"); |
|
|
|
|
} |
|
|
|
|
return 0; |
|
|
|
|
} else if (test_completed == 2) { |
|
|
|
|
// Check if TUN was disabled - if so, timeout is expected and acceptable
|
|
|
|
|
if (server_instance->tun.fd < 0 || client_instance->tun.fd < 0) { |
|
|
|
|
printf("\n=== TEST PASSED ===\n"); |
|
|
|
|
printf("ℹ️ Connection timeout expected with TUN disabled\n"); |
|
|
|
|
printf("✅ ETCP core infrastructure verified successfully\n"); |
|
|
|
|
printf("✅ Instance creation and configuration works with TUN disabled\n"); |
|
|
|
|
printf("✅ All core components initialized correctly\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "\n=== TEST PASSED ===\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "ℹ️ Connection timeout expected with TUN disabled\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "✅ ETCP core infrastructure verified successfully\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "✅ Instance creation and configuration works with TUN disabled\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "✅ All core components initialized correctly\n"); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
printf("\n=== TEST FAILED: Connection timeout ===\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "\n=== TEST FAILED: Connection timeout ===\n"); |
|
|
|
|
return 1; |
|
|
|
|
} else { |
|
|
|
|
printf("\n=== TEST FAILED: Unknown error ===\n"); |
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "\n=== TEST FAILED: Unknown error ===\n"); |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
} |