Skip to content

Commit

Permalink
Made example more comprehensive
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Tjaden committed May 30, 2024
1 parent f40fb37 commit 6fa303f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
19 changes: 11 additions & 8 deletions posix/tcp/wh_client_tcp/wh_client_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ enum {

#define WH_SERVER_TCP_IPSTRING "127.0.0.1"
#define WH_SERVER_TCP_PORT 23456
#define WH_CLIENT_ID 1234
#define WH_CLIENT_ID 12

static void* wh_ClientTask(void* cf)
{
Expand All @@ -35,7 +35,6 @@ static void* wh_ClientTask(void* cf)
whClientContext client[1];
int counter = 1;


uint8_t tx_req[REQ_SIZE] = {0};
uint16_t tx_req_len = 0;

Expand All @@ -47,7 +46,7 @@ static void* wh_ClientTask(void* cf)
}

ret = wh_Client_Init(client, config);
printf("wh_Client_Init:%d\n", ret);
printf("Client connecting to server...\n");

if (ret != 0) {
perror("Init error:");
Expand All @@ -61,8 +60,8 @@ static void* wh_ClientTask(void* cf)
ret = wh_Client_EchoRequest(client,
tx_req_len, tx_req);
if( ret != WH_ERROR_NOTREADY) {
printf("Client EchoRequest:%d, len:%d, %s\n",
ret, tx_req_len, tx_req);
//printf("Client EchoRequest:%d, len:%d, %s\n",
// ret, tx_req_len, tx_req);
}
} while ((ret == WH_ERROR_NOTREADY) && (usleep(ONE_MS)==0));

Expand All @@ -77,8 +76,8 @@ static void* wh_ClientTask(void* cf)
do {
ret = wh_Client_EchoResponse(client,
&rx_resp_len, rx_resp);
printf("Client EchoResponse:%d, len:%d, %s\n",
ret, rx_resp_len, rx_resp);
/*printf("Client EchoResponse:%d, len:%d, %s\n",
ret, rx_resp_len, rx_resp);*/
} while ((ret == WH_ERROR_NOTREADY) && (usleep(ONE_MS)==0));

if (ret != 0) {
Expand All @@ -88,7 +87,11 @@ static void* wh_ClientTask(void* cf)
}
wh_Client_CommClose(client);
ret = wh_Client_Cleanup(client);
printf("wh_Client_Cleanup:%d\n", ret);
if (ret == 0) {
printf("Successfull connection!\n");
}
// printf("wh_Client_Cleanup:%d\n", ret);
printf("Client disconnected\n");
return NULL;
}

Expand Down
14 changes: 9 additions & 5 deletions posix/tcp/wh_server_tcp/wh_server_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,43 @@ enum {

#define WH_SERVER_TCP_IPSTRING "127.0.0.1"
#define WH_SERVER_TCP_PORT 23456
#define WH_SERVER_ID 5678
#define WH_SERVER_ID 56

static void* wh_ServerTask(void* cf)
{
whServerContext server[1];
whServerConfig* config = (whServerConfig*)cf;
int ret = 0;
int connectionMessage = 0;
whCommConnected am_connected = WH_COMM_CONNECTED;

if (config == NULL) {
return NULL;
}

ret = wh_Server_Init(server, config);
printf("wh_Server_Init:%d\n", ret);
printf("Waiting for connection...\n");

if (ret == 0) {
wh_Server_SetConnected(server, am_connected);

while(am_connected == WH_COMM_CONNECTED) {
while (am_connected == WH_COMM_CONNECTED) {
ret = wh_Server_HandleRequestMessage(server);
if (ret == WH_ERROR_NOTREADY) {
usleep(ONE_MS);
} else if (ret == WH_ERROR_OK) {
printf("Server HandleRequestMessage:%d\n",ret);
if (!connectionMessage) {
printf("Successful connection!\n");
connectionMessage = 1;
}
} else {
printf("Failed to wh_Server_HandleRequestMessage: %d\n", ret);
break;
}
wh_Server_GetConnected(server, &am_connected);
}
ret = wh_Server_Cleanup(server);
printf("ServerCleanup:%d\n", ret);
printf("Server disconnected\n");
}
return NULL;
}
Expand Down

0 comments on commit 6fa303f

Please sign in to comment.