Skip to content

Commit

Permalink
Merge pull request #2345 from hxy7yx/v2.7-1
Browse files Browse the repository at this point in the history
[v2.7]fix(modbus):clear recv buf before sending
  • Loading branch information
hxy7yx authored Dec 23, 2024
2 parents f0dd9f7 + 3d755cd commit bb16e51
Show file tree
Hide file tree
Showing 3 changed files with 28 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 @@ -104,6 +104,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
24 changes: 24 additions & 0 deletions src/connection/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,30 @@ 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 && conn->param.type == NEU_CONN_TCP_CLIENT) {
uint8_t temp_buf[256];
ssize_t ret;
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);
}
}

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 bb16e51

Please sign in to comment.