|
26 | 26 | #include <apm/csh_api.h> |
27 | 27 | #include <dtp/dtp.h> |
28 | 28 | #include <dtp/dtp_file_payload.h> |
29 | | -#include <dtp/dtp_protocol.h> |
30 | 29 | #include "deploy.pb-c.h" |
31 | 30 | #include "config.h" |
32 | 31 | #include "history.h" |
@@ -289,79 +288,16 @@ bool get_payload_meta(dtp_payload_meta_t *meta, uint8_t payload_id) { |
289 | 288 | return result; |
290 | 289 | } |
291 | 290 |
|
292 | | -/* DTP server thread — custom loop that holds the RDP connection open |
293 | | - * long enough for the client to read the metadata response. |
294 | | - * The stock dtp_server_run() closes immediately after sending data, |
295 | | - * which races with the client on small files. */ |
| 291 | +/* DTP server thread context */ |
296 | 292 | typedef struct { |
297 | 293 | volatile bool exit_flag; |
298 | | - volatile bool ready; /* set once port 7 is bound */ |
299 | | - int bind_error; |
| 294 | + volatile bool ready; |
300 | 295 | } dtp_server_ctx_t; |
301 | 296 |
|
302 | | -/* Static socket — must outlive the thread since the CSP port table |
303 | | - * holds a pointer to it. Stack-local would leave a dangling ref. */ |
304 | | -static csp_socket_t dtp_server_sock; |
305 | | - |
306 | 297 | static void *dtp_server_thread(void *arg) { |
307 | 298 | dtp_server_ctx_t *ctx = (dtp_server_ctx_t *)arg; |
308 | | - |
309 | | - memset(&dtp_server_sock, 0, sizeof(dtp_server_sock)); |
310 | | - dtp_server_sock.opts = CSP_O_RDP; |
311 | | - |
312 | | - int rc = csp_bind(&dtp_server_sock, 7); |
313 | | - if (rc != CSP_ERR_NONE) { |
314 | | - printf("[dtp-server] Failed to bind port 7 (error %d)\n", rc); |
315 | | - fflush(stdout); |
316 | | - ctx->bind_error = rc; |
317 | | - ctx->ready = true; /* unblock main thread */ |
318 | | - return NULL; |
319 | | - } |
320 | | - csp_listen(&dtp_server_sock, 1); |
321 | | - printf("[dtp-server] Listening on port 7\n"); |
322 | | - fflush(stdout); |
323 | | - ctx->ready = true; /* signal main thread: ready to accept */ |
324 | | - |
325 | | - while (!ctx->exit_flag) { |
326 | | - csp_conn_t *conn = csp_accept(&dtp_server_sock, 1000); |
327 | | - if (!conn) continue; |
328 | | - |
329 | | - csp_packet_t *packet = csp_read(conn, 10000); |
330 | | - if (!packet) { |
331 | | - csp_close(conn); |
332 | | - continue; |
333 | | - } |
334 | | - |
335 | | - printf("[dtp-server] Got metadata request from node %d, len=%u\n", |
336 | | - csp_conn_src(conn), packet->length); |
337 | | - printf("[dtp-server] raw packet data (first 16 bytes):"); |
338 | | - for (int i = 0; i < 16 && i < packet->length; i++) |
339 | | - printf(" %02x", packet->data[i]); |
340 | | - printf("\n"); |
341 | | - fflush(stdout); |
342 | | - |
343 | | - packet = setup_server_transfer(&server_transfer_ctx, csp_conn_src(conn), packet); |
344 | | - server_transfer_ctx.keep_running = true; |
345 | | - if (packet) { |
346 | | - csp_send(conn, packet); |
347 | | - /* Give the client time to process the metadata response |
348 | | - * before we start blasting data packets */ |
349 | | - usleep(200000); /* 200ms */ |
350 | | - start_sending_data(&server_transfer_ctx); |
351 | | - printf("[dtp-server] Data sent, holding connection...\n"); |
352 | | - fflush(stdout); |
353 | | - } |
354 | | - server_transfer_ctx.keep_running = false; |
355 | | - |
356 | | - /* Hold the RDP connection open so the RST doesn't race |
357 | | - * with the metadata response on small transfers */ |
358 | | - usleep(500000); /* 500ms */ |
359 | | - csp_close(conn); |
360 | | - printf("[dtp-server] Transfer complete\n"); |
361 | | - fflush(stdout); |
362 | | - } |
363 | | - |
364 | | - csp_socket_close(&dtp_server_sock); |
| 299 | + ctx->ready = true; |
| 300 | + dtp_server_main(&ctx->exit_flag); |
365 | 301 | return NULL; |
366 | 302 | } |
367 | 303 |
|
@@ -480,25 +416,20 @@ static int deploy_single_app(unsigned int node, char *app_name, |
480 | 416 | fflush(stdout); |
481 | 417 |
|
482 | 418 | /* Step 2: Start DTP server in background thread */ |
483 | | - dtp_server_ctx_t dtp_ctx = { .exit_flag = false, .ready = false, .bind_error = 0 }; |
| 419 | + dtp_server_ctx_t dtp_ctx = { .exit_flag = false, .ready = false }; |
484 | 420 | pthread_t dtp_thread; |
485 | 421 | if (pthread_create(&dtp_thread, NULL, dtp_server_thread, &dtp_ctx) != 0) { |
486 | 422 | printf("Error: Failed to start DTP server thread\n"); |
487 | 423 | dtp_file_payload_del(payload_id); |
488 | 424 | return SLASH_EIO; |
489 | 425 | } |
490 | 426 |
|
491 | | - /* Wait for server to bind port 7 before sending deploy request */ |
| 427 | + /* Wait for server thread to start */ |
492 | 428 | for (int i = 0; i < 50 && !dtp_ctx.ready; i++) { |
493 | | - usleep(20000); /* 20ms, max 1s total */ |
494 | | - } |
495 | | - if (dtp_ctx.bind_error || !dtp_ctx.ready) { |
496 | | - printf("Error: DTP server failed to start (bind_error=%d)\n", dtp_ctx.bind_error); |
497 | | - dtp_ctx.exit_flag = true; |
498 | | - pthread_join(dtp_thread, NULL); |
499 | | - dtp_file_payload_del(payload_id); |
500 | | - return SLASH_EIO; |
| 429 | + usleep(20000); |
501 | 430 | } |
| 431 | + /* Give dtp_server_main time to bind port 7 */ |
| 432 | + usleep(200000); |
502 | 433 |
|
503 | 434 | /* Step 3: Send CMD_DEPLOY — agent will pull the file via DTP */ |
504 | 435 | csp_iface_t *default_iface = csp_iflist_get_by_isdfl(NULL); |
|
0 commit comments