Skip to content

Commit

Permalink
[dv/dpi] tcp_server synchronous socket create
Browse files Browse the repository at this point in the history
The listening socket creation is made synchronous with the
tcp_server_create call, while the processing of socket events is still
handled on a dedicated thread.

Signed-off-by: Kip Walker <[email protected]>
  • Loading branch information
kwalker27 committed Dec 13, 2024
1 parent 555e1d9 commit 6b0a9e3
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions hw/dv/dpi/common/tcp_server/tcp_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,29 +311,16 @@ static void ctx_free(struct tcp_server_ctx *ctx) {
}

/**
* Thread function to create a new server instance
* Thread function to process server events
*
* @param ctx_void context object
* @return Always returns NULL
*/
static void *server_create(void *ctx_void) {
static void *server_process(void *ctx_void) {
// Cast to a server struct
struct tcp_server_ctx *ctx = (struct tcp_server_ctx *)ctx_void;
struct timeval timeout;

// Start the server
int rv = start(ctx);
if (rv != 0) {
fprintf(stderr, "%s: Unable to create TCP server on port %d\n",
ctx->display_name, ctx->listen_port);
// Failing to create the listening socket is treated as a fatal
// error. If the creation of this socket is not important, it
// should not even be attempted.
assert(false);
}

// Initialise fd_set

// Start waiting for connection / data
char xfer_data;
while (ctx->socket_run) {
Expand All @@ -355,7 +342,7 @@ static void *server_create(void *ctx_void) {
timeout.tv_usec = 50;

// Wait for socket activity or timeout
rv = select(mfd + 1, &read_fds, NULL, NULL, &timeout);
int rv = select(mfd + 1, &read_fds, NULL, NULL, &timeout);

if (rv < 0) {
if (errno == EINTR) {
Expand Down Expand Up @@ -419,7 +406,19 @@ struct tcp_server_ctx *tcp_server_create(const char *display_name,
ctx->display_name = strdup(display_name);
assert(ctx->display_name);

if (pthread_create(&ctx->sock_thread, NULL, server_create, (void *)ctx) !=
// Open the listening socket synchronously
int rv = start(ctx);
if (rv != 0) {
fprintf(stderr, "%s: Unable to create TCP server on port %d\n",
ctx->display_name, ctx->listen_port);
// Failing to create the listening socket is treated as a fatal
// error. If the creation of this socket is not important, it
// should not even be attempted.
assert(false);
}

// Start a thread to accept connections and transfer data
if (pthread_create(&ctx->sock_thread, NULL, server_process, (void *)ctx) !=
0) {
fprintf(stderr, "%s: Unable to create TCP socket thread\n",
ctx->display_name);
Expand Down

0 comments on commit 6b0a9e3

Please sign in to comment.