use llm to write count up tcp test
This commit is contained in:
parent
7e6125066e
commit
908d563bc4
1 changed files with 39 additions and 27 deletions
66
tcp.c
66
tcp.c
|
|
@ -1,9 +1,3 @@
|
|||
/**
|
||||
* Copyright (c) 2022 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
|
|
@ -50,6 +44,7 @@ typedef struct TCP_CLIENT_T_ {
|
|||
bool complete;
|
||||
int run_count;
|
||||
bool connected;
|
||||
int counter;
|
||||
} TCP_CLIENT_T;
|
||||
|
||||
static err_t tcp_client_close(void *arg) {
|
||||
|
|
@ -89,18 +84,16 @@ static err_t tcp_client_sent(void *arg, struct tcp_pcb *tpcb, u16_t len) {
|
|||
printf("tcp_client_sent %u\n", len);
|
||||
state->sent_len += len;
|
||||
|
||||
if (state->sent_len >= BUF_SIZE) {
|
||||
|
||||
if (state->sent_len >= state->buffer_len) {
|
||||
state->run_count++;
|
||||
if (state->run_count >= TEST_ITERATIONS) {
|
||||
tcp_result(arg, 0);
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
// We should receive a new buffer from the server
|
||||
state->buffer_len = 0;
|
||||
// Wait a bit then send the next number
|
||||
state->sent_len = 0;
|
||||
printf("Waiting for buffer from server\n");
|
||||
printf("Sent number %d, preparing next...\n", state->counter - 1);
|
||||
}
|
||||
|
||||
return ERR_OK;
|
||||
|
|
@ -113,13 +106,46 @@ static err_t tcp_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err) {
|
|||
return tcp_result(arg, err);
|
||||
}
|
||||
state->connected = true;
|
||||
printf("Waiting for buffer from server\n");
|
||||
state->counter = 1;
|
||||
|
||||
// Send the first number immediately
|
||||
char number_str[32];
|
||||
snprintf(number_str, sizeof(number_str), "%d\n", state->counter);
|
||||
state->buffer_len = strlen(number_str);
|
||||
memcpy(state->buffer, number_str, state->buffer_len);
|
||||
|
||||
printf("Sending number %d to server\n", state->counter);
|
||||
err_t write_err = tcp_write(tpcb, state->buffer, state->buffer_len, TCP_WRITE_FLAG_COPY);
|
||||
if (write_err != ERR_OK) {
|
||||
printf("Failed to write data %d\n", write_err);
|
||||
return tcp_result(arg, -1);
|
||||
}
|
||||
state->counter++;
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
static err_t tcp_client_poll(void *arg, struct tcp_pcb *tpcb) {
|
||||
TCP_CLIENT_T *state = (TCP_CLIENT_T*)arg;
|
||||
printf("tcp_client_poll\n");
|
||||
return tcp_result(arg, -1); // no response is an error?
|
||||
|
||||
// Send the next number
|
||||
if (state->connected && state->sent_len == 0) {
|
||||
char number_str[32];
|
||||
snprintf(number_str, sizeof(number_str), "%d\n", state->counter);
|
||||
state->buffer_len = strlen(number_str);
|
||||
memcpy(state->buffer, number_str, state->buffer_len);
|
||||
|
||||
printf("Sending number %d to server\n", state->counter);
|
||||
err_t write_err = tcp_write(tpcb, state->buffer, state->buffer_len, TCP_WRITE_FLAG_COPY);
|
||||
if (write_err != ERR_OK) {
|
||||
printf("Failed to write data %d\n", write_err);
|
||||
return tcp_result(arg, -1);
|
||||
}
|
||||
state->counter++;
|
||||
}
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
static void tcp_client_err(void *arg, err_t err) {
|
||||
|
|
@ -143,23 +169,9 @@ err_t tcp_client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err
|
|||
for (struct pbuf *q = p; q != NULL; q = q->next) {
|
||||
DUMP_BYTES(q->payload, q->len);
|
||||
}
|
||||
// Receive the buffer
|
||||
const uint16_t buffer_left = BUF_SIZE - state->buffer_len;
|
||||
state->buffer_len += pbuf_copy_partial(p, state->buffer + state->buffer_len,
|
||||
p->tot_len > buffer_left ? buffer_left : p->tot_len, 0);
|
||||
tcp_recved(tpcb, p->tot_len);
|
||||
}
|
||||
pbuf_free(p);
|
||||
|
||||
// If we have received the whole buffer, send it back to the server
|
||||
if (state->buffer_len == BUF_SIZE) {
|
||||
printf("Writing %d bytes to server\n", state->buffer_len);
|
||||
err_t err = tcp_write(tpcb, state->buffer, state->buffer_len, TCP_WRITE_FLAG_COPY);
|
||||
if (err != ERR_OK) {
|
||||
printf("Failed to write data %d\n", err);
|
||||
return tcp_result(arg, -1);
|
||||
}
|
||||
}
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue