Skip to content

Commit

Permalink
Merge pull request #2344 from hxy7yx/main-1
Browse files Browse the repository at this point in the history
fix(modbus):clear recv buf before sending
  • Loading branch information
fengzeroz authored Dec 24, 2024
2 parents 15a10f5 + d33fff5 commit f91571f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/neuron/connection/neu_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ ssize_t neu_conn_recv(neu_conn_t *conn, uint8_t *buf, ssize_t len);
ssize_t neu_conn_udp_recvfrom(neu_conn_t *conn, uint8_t *buf, ssize_t len,
void *src);

void neu_conn_clear_recv_buffer(neu_conn_t *conn);

/**
* @brief Specify the client to send data.
*
Expand Down
2 changes: 2 additions & 0 deletions plugins/modbus/modbus_req.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ int modbus_send_msg(void *ctx, uint16_t n_byte, uint8_t *bytes)
neu_plugin_t *plugin = (neu_plugin_t *) ctx;
int ret = 0;

neu_conn_clear_recv_buffer(plugin->conn);

plog_send_protocol(plugin, bytes, n_byte);

if (plugin->is_server) {
Expand Down
34 changes: 34 additions & 0 deletions src/connection/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,40 @@ ssize_t neu_conn_send(neu_conn_t *conn, uint8_t *buf, ssize_t len)
return ret;
}

void neu_conn_clear_recv_buffer(neu_conn_t *conn)
{
if (!conn->is_connected) {
return;
}

uint8_t temp_buf[256];
ssize_t ret;

switch (conn->param.type) {
case NEU_CONN_TCP_CLIENT:
do {
ret = recv(conn->fd, temp_buf, sizeof(temp_buf), MSG_DONTWAIT);

if (ret > 0) {
continue;
} else if (ret == 0) {
zlog_info(conn->param.log,
"Connection closed while clearing buffer.");
break;
} else if (errno == EAGAIN || errno == EWOULDBLOCK) {
break;
} else {
zlog_error(conn->param.log, "Error clearing buffer: %s",
strerror(errno));
break;
}
} while (ret > 0);
break;
default:
break;
}
}

ssize_t neu_conn_recv(neu_conn_t *conn, uint8_t *buf, ssize_t len)
{
ssize_t ret = 0;
Expand Down

0 comments on commit f91571f

Please sign in to comment.