working noVNC

parent 72788b3f
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <string.h> // for strerror #include <string.h> // for strerror
#include <fcntl.h>
#include <openssl/sha.h> #include <openssl/sha.h>
#include <openssl/ssl.h> #include <openssl/ssl.h>
#include "websocket_protocol.h" #include "websocket_protocol.h"
...@@ -588,10 +589,19 @@ bool ws_send_binary_frame(ws_connection_t *conn, const void *data, size_t len, b ...@@ -588,10 +589,19 @@ bool ws_send_binary_frame(ws_connection_t *conn, const void *data, size_t len, b
memcpy(frame + header_len, data, len); memcpy(frame + header_len, data, len);
} }
// Send frame with partial write handling and retry logic // Send frame - for WebSocket binary frames, we need to send atomically
// Temporarily set socket to blocking mode to ensure complete frame transmission
int original_flags = -1;
if (!conn->ssl) {
original_flags = fcntl(conn->sock_fd, F_GETFL, 0);
if (original_flags != -1) {
fcntl(conn->sock_fd, F_SETFL, original_flags & ~O_NONBLOCK);
}
}
int total_written = 0; int total_written = 0;
int retry_count = 0; int retry_count = 0;
const int max_retries = 5; const int max_retries = 10;
if (debug) printf("[WS-BINARY] Sending frame of total length %zu\n", frame_len); if (debug) printf("[WS-BINARY] Sending frame of total length %zu\n", frame_len);
...@@ -619,8 +629,8 @@ bool ws_send_binary_frame(ws_connection_t *conn, const void *data, size_t len, b ...@@ -619,8 +629,8 @@ bool ws_send_binary_frame(ws_connection_t *conn, const void *data, size_t len, b
written = write(conn->sock_fd, frame + total_written, to_write); written = write(conn->sock_fd, frame + total_written, to_write);
if (written < 0) { if (written < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
// Non-blocking socket, try again // Should not happen in blocking mode, but handle anyway
if (debug) printf("[WS-BINARY] Socket would block, retrying\n"); if (debug) printf("[WS-BINARY] Socket would block in blocking mode, retrying\n");
retry_count++; retry_count++;
usleep(1000); // Short delay before retry usleep(1000); // Short delay before retry
continue; continue;
...@@ -643,6 +653,11 @@ bool ws_send_binary_frame(ws_connection_t *conn, const void *data, size_t len, b ...@@ -643,6 +653,11 @@ bool ws_send_binary_frame(ws_connection_t *conn, const void *data, size_t len, b
if (debug) printf("[WS-BINARY] Wrote %d bytes, total written: %d/%zu\n", written, total_written, frame_len); if (debug) printf("[WS-BINARY] Wrote %d bytes, total written: %d/%zu\n", written, total_written, frame_len);
} }
// Restore original socket flags
if (!conn->ssl && original_flags != -1) {
fcntl(conn->sock_fd, F_SETFL, original_flags);
}
if (total_written != (int)frame_len) { if (total_written != (int)frame_len) {
if (debug) printf("[WS-BINARY] Failed to write complete frame: %d/%zu\n", total_written, frame_len); if (debug) printf("[WS-BINARY] Failed to write complete frame: %d/%zu\n", total_written, frame_len);
free(frame); free(frame);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment