Merge pull request #1715 from hxy7yx/main-2

fix(simulator):memory leak
This commit is contained in:
fengzeroz
2023-12-07 11:16:00 +08:00
committed by GitHub
4 changed files with 35 additions and 19 deletions
+2 -1
View File
@@ -14,8 +14,9 @@ case $1 in
(modbus)
lcov -c -d build/plugins/modbus/CMakeFiles/plugin-modbus-rtu.dir -o cov_report/cov-modbus-rtu.info
lcov -c -d build/plugins/modbus/CMakeFiles/plugin-modbus-tcp.dir -o cov_report/cov-modbus-tcp.info
lcov -c -d build/simulator/CMakeFiles/modbus_simulator.dir -o cov_report/cov-simulator-modbus.info
cd cov_report
gen_trace -a cov-modbus-rtu.info -a cov-modbus-tcp.info -o cov-modbus.info;;
gen_trace -a cov-simulator-modbus.info -a cov-modbus-rtu.info -a cov-modbus-tcp.info -o cov-modbus.info;;
(mqtt)
lcov -c -d build/plugins/mqtt/CMakeFiles/plugin-mqtt.dir -o cov_report/cov-mqtt.info
cd cov_report
+24 -9
View File
@@ -32,6 +32,7 @@ neu_events_t * events = NULL;
neu_event_io_t * tcp_server_event = NULL;
neu_conn_t * conn = NULL;
int64_t global_timestamp = 0;
bool exiting = false;
static void start_listen(void *data, int fd);
static void stop_listen(void *data, int fd);
@@ -44,6 +45,7 @@ static bool mode_tcp = true;
struct client_event {
neu_event_io_t *client;
int fd;
void * user_data;
};
struct client_event c_events[10] = { 0 };
@@ -53,14 +55,14 @@ struct cycle_buf {
uint8_t buf[4096];
};
static int add_client(int fd, neu_event_io_t *client)
static int add_client(int fd, neu_event_io_t *client, void *user_data)
{
for (uint32_t i = 0; i < sizeof(c_events) / sizeof(struct client_event);
i++) {
if (c_events[i].fd == 0) {
c_events[i].client = client;
c_events[i].fd = fd;
c_events[i].client = client;
c_events[i].fd = fd;
c_events[i].user_data = user_data;
return 0;
}
}
@@ -83,6 +85,7 @@ static neu_event_io_t *del_client(int fd)
static void sig_handler(int sig)
{
exiting = true;
(void) sig;
for (uint32_t i = 0; i < sizeof(c_events) / sizeof(struct client_event);
i++) {
@@ -208,7 +211,7 @@ static int new_client(enum neu_event_io_type type, int fd, void *usr_data)
neu_event_io_t *client = neu_event_add_io(events, io);
add_client(fd, client);
add_client(fd, client, buf);
nlog_info("accept new client: fd: %d\n", client_fd);
}
@@ -229,12 +232,16 @@ static int new_client(enum neu_event_io_type type, int fd, void *usr_data)
static int recv_msg(enum neu_event_io_type type, int fd, void *usr_data)
{
struct cycle_buf *buf = (struct cycle_buf *) usr_data;
if (exiting) {
return 0;
}
switch (type) {
case NEU_EVENT_IO_READ: {
ssize_t len = 0;
uint8_t res[20000] = { 0 };
int res_len = 0;
struct cycle_buf *buf = (struct cycle_buf *) usr_data;
ssize_t len = 0;
uint8_t res[20000] = { 0 };
int res_len = 0;
len = neu_conn_tcp_server_recv(conn, fd, buf->buf + buf->len,
sizeof(buf->buf) - buf->len);
@@ -249,6 +256,8 @@ static int recv_msg(enum neu_event_io_type type, int fd, void *usr_data)
&res_len);
}
neu_msleep(2);
if (len == -1) {
neu_event_io_t *io = del_client(fd);
@@ -260,9 +269,15 @@ static int recv_msg(enum neu_event_io_type type, int fd, void *usr_data)
neu_conn_tcp_server_close_client(conn, fd);
printf("recv msg parse fail, close client: %d\n", fd);
} else if (len == -2) {
if (exiting) {
return 0;
}
neu_conn_tcp_server_send(conn, fd, NULL, 0);
printf("recv modbus adrress for retry_test, continue wait msg\n");
} else if (len > 0) {
if (exiting) {
return 0;
}
memmove(buf->buf, buf->buf + len, buf->len - len);
buf->len -= len;
+8 -8
View File
@@ -496,7 +496,7 @@ class TestModbus:
api.add_tags_check(node=param[0], group='group1', tags=hold_int16)
api.write_tag_check(
node=param[0], group='group', tag=hold_int16[0]['name'], value=222)
time.sleep(0.5)
time.sleep(0.3)
assert 222 == api.read_tag(
node=param[0], group='group', tag=hold_int16[0]['name'])
assert 222 == api.read_tag(
@@ -512,7 +512,7 @@ class TestModbus:
node=param[0], group='group', tag=hold_int16[0]['name'], value=333)
api.write_tag_check(
node=param[0], group='group1', tag=t_tag[0]['name'], value=444)
time.sleep(0.5)
time.sleep(0.3)
assert 333 == api.read_tag(
node=param[0], group='group', tag=hold_int16[0]['name'])
assert 444 == api.read_tag(
@@ -631,7 +631,7 @@ class TestModbus:
api.write_tag_check(
node=param[0], group='group', tag=hold_double_decimal[0]['name'], value=513.11)
time.sleep(0.5)
time.sleep(0.3)
assert 11 == api.read_tag(
node=param[0], group='group', tag=hold_int16_decimal[0]['name'])
assert 23 == api.read_tag(
@@ -679,7 +679,7 @@ class TestModbus:
{"tag": hold_int16_m8[0]['name'], "value": 8},
{"tag": hold_int16_m9[0]['name'], "value": 9},
{"tag": hold_int16_m10[0]['name'], "value": 10}])
time.sleep(0.5)
time.sleep(0.3)
assert 1 == api.read_tag(
node=param[0], group='group', tag=hold_int16_m1[0]['name'])
assert 2 == api.read_tag(
@@ -846,7 +846,7 @@ class TestModbus:
api.write_tag_check(
node=param[0], group='group', tag=hold_float_BB[0]['name'], value=123.4)
time.sleep(1.0)
time.sleep(0.3)
assert 1234== api.read_tag(
node=param[0], group='group', tag=hold_int16_B[0]['name'])
assert 1234 == api.read_tag(
@@ -891,7 +891,7 @@ class TestModbus:
node=param[0], group='group', tag=hold_string_l1[0]['name'], value='a')
api.write_tag_check(
node=param[0], group='group', tag=hold_string_l2[0]['name'], value='b')
time.sleep(1.0)
time.sleep(0.3)
assert 'a' == api.read_tag(
node=param[0], group='group', tag=hold_string_l1[0]['name'])
assert 'b' == api.read_tag(
@@ -905,7 +905,7 @@ class TestModbus:
response = api.write_gtags(json=modbus_write_gtags)
assert 200 == response.status_code
assert error.NEU_ERR_SUCCESS == response.json()['error']
time.sleep(1.0)
time.sleep(0.3)
assert 1 == api.read_tag(
node=param[0], group='group', tag=hold_uint16[0]['name'])
assert 1 == api.read_tag(
@@ -927,7 +927,7 @@ class TestModbus:
node=param[0], group='group', tag=hold_int16_retry_1[0]['name'], value=111)
api.write_tag_check(
node=param[0], group='group', tag=hold_int16_retry_2[0]['name'], value=222)
time.sleep(1.0)
time.sleep(0.3)
assert 111 == api.read_tag(
node=param[0], group='group', tag=hold_int16_retry_1[0]['name'])
assert 222 == api.read_tag(
+1 -1
View File
@@ -345,7 +345,7 @@ def modbus_tcp_node_setting(node, port, connection_mode=0, transport_mode=0, int
"host": host, "port": port, "timeout": timeout, "max_retries": max_retries})
def modbus_rtu_node_setting(node, port, connection_mode=0, transport_mode=0, interval=10, host='127.0.0.1', timeout=3000, max_retries=2, link=1, device="", stop=0, parity=0, baud=4, data=3):
def modbus_rtu_node_setting(node, port, connection_mode=0, transport_mode=0, interval=0, host='127.0.0.1', timeout=3000, max_retries=2, link=1, device="", stop=0, parity=0, baud=4, data=3):
return node_setting(node, json={"connection_mode": connection_mode, "transport_mode": transport_mode,
"interval": interval, "host": host, "port": port, "timeout": timeout,
"max_retries": max_retries, "link": link, "device": device, "stop": stop,