forked from tuxnsk/nodejs_libmodbus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmt.patch
More file actions
313 lines (284 loc) · 8.41 KB
/
Copy pathmt.patch
File metadata and controls
313 lines (284 loc) · 8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
diff --git src/modbus-private.h src/modbus-private.h
index 7994459..799eccd 100644
--- src/modbus-private.h
+++ src/modbus-private.h
@@ -109,6 +109,7 @@ typedef struct _modbus_backend {
const uint8_t *rsp, int rsp_length);
int (*connect) (modbus_t *ctx);
void (*close) (modbus_t *ctx);
+ void (*close_mt) (modbus_t *ctx);
int (*flush) (modbus_t *ctx);
int (*select) (modbus_t *ctx, fd_set *rset, struct timeval *tv, int msg_length);
} modbus_backend_t;
@@ -124,6 +125,7 @@ struct _modbus {
struct timeval byte_timeout;
const modbus_backend_t *backend;
void *backend_data;
+ void *mt_data;
};
void _modbus_init_common(modbus_t *ctx);
diff --git src/modbus-rtu-private.h src/modbus-rtu-private.h
index c8fdeab..4f20db4 100644
--- src/modbus-rtu-private.h
+++ src/modbus-rtu-private.h
@@ -94,4 +94,10 @@ typedef struct _modbus_rtu {
int confirmation_to_ignore;
} modbus_rtu_t;
+typedef struct _modbus_rtu_mt {
+ /* Pipes for control in multithread */
+ int mtp_r;
+ int mtp_w;
+} modbus_rtu_mt_t;
+
#endif /* _MODBUS_RTU_PRIVATE_H_ */
diff --git src/modbus-rtu.c src/modbus-rtu.c
index c41110e..60e7456 100644
--- src/modbus-rtu.c
+++ src/modbus-rtu.c
@@ -937,6 +937,20 @@ void _modbus_rtu_close(modbus_t *ctx)
#endif
}
+void _modbus_rtu_close_mt(modbus_t *ctx)
+{
+ ssize_t size;
+ _modbus_rtu_close(ctx);
+
+ modbus_rtu_mt_t *ctx_mt = ctx->mt_data;
+ size = write(ctx_mt->mtp_w, "q", 1);
+ if (size == -1)
+ fprintf(stderr, "ERROR returned by write() (%s)\n",
+ strerror(errno));
+ close(ctx_mt->mtp_w);
+ close(ctx_mt->mtp_r);
+}
+
int _modbus_rtu_flush(modbus_t *ctx)
{
#if defined(_WIN32)
@@ -964,7 +978,13 @@ int _modbus_rtu_select(modbus_t *ctx, fd_set *rset,
return -1;
}
#else
- while ((s_rc = select(ctx->s+1, rset, NULL, NULL, tv)) == -1) {
+ /* Add pipe descriptor to select */
+ modbus_rtu_mt_t *ctx_mt = ctx->mt_data;
+ FD_SET(ctx_mt->mtp_r, rset);
+
+ int max_n = (ctx->s > ctx_mt->mtp_r) ? ctx->s : ctx_mt->mtp_r;
+
+ while ((s_rc = select(max_n+1, rset, NULL, NULL, tv)) == -1) {
if (errno == EINTR) {
if (ctx->debug) {
fprintf(stderr, "A non blocked signal was caught\n");
@@ -972,6 +992,7 @@ int _modbus_rtu_select(modbus_t *ctx, fd_set *rset,
/* Necessary after an error */
FD_ZERO(rset);
FD_SET(ctx->s, rset);
+ FD_SET(ctx_mt->mtp_r, rset);
} else {
return -1;
}
@@ -983,7 +1004,12 @@ int _modbus_rtu_select(modbus_t *ctx, fd_set *rset,
return -1;
}
#endif
-
+ if (FD_ISSET(ctx_mt->mtp_r, rset)) {
+ /* Connection reset. */
+ errno = ECONNRESET;
+ return -1;
+ }
+
return s_rc;
}
@@ -1004,6 +1030,7 @@ const modbus_backend_t _modbus_rtu_backend = {
_modbus_rtu_pre_check_confirmation,
_modbus_rtu_connect,
_modbus_rtu_close,
+ _modbus_rtu_close_mt,
_modbus_rtu_flush,
_modbus_rtu_select
};
@@ -1014,6 +1041,7 @@ modbus_t* modbus_new_rtu(const char *device,
{
modbus_t *ctx;
modbus_rtu_t *ctx_rtu;
+ modbus_rtu_mt_t *ctx_mt;
size_t dest_size;
size_t ret_size;
@@ -1062,6 +1090,20 @@ modbus_t* modbus_new_rtu(const char *device,
#endif
ctx_rtu->confirmation_to_ignore = FALSE;
+
+ /* Create pipe chanel */
+ ctx->mt_data = (modbus_rtu_mt_t *) malloc(sizeof(modbus_rtu_mt_t));
+ ctx_mt = (modbus_rtu_mt_t *)ctx->mt_data;
+
+ int mtp[2];
+ if (pipe(mtp) == -1) {
+ fprintf(stderr, "ERROR Can't create pipe (%s)\n",
+ strerror(errno));
+ modbus_free(ctx);
+ return NULL;
+ }
+ ctx_mt->mtp_r = mtp[0];
+ ctx_mt->mtp_w = mtp[1];
return ctx;
}
diff --git src/modbus-tcp-private.h src/modbus-tcp-private.h
index 408d08a..8a2b43b 100644
--- src/modbus-tcp-private.h
+++ src/modbus-tcp-private.h
@@ -53,4 +53,10 @@ typedef struct _modbus_tcp_pi {
char service[_MODBUS_TCP_PI_SERVICE_LENGTH];
} modbus_tcp_pi_t;
+
+typedef struct _modbus_tcp_mt {
+ /* Main listen socket */
+ int lst_socket;
+} modbus_tcp_mt_t;
+
#endif /* _MODBUS_TCP_PRIVATE_H_ */
diff --git src/modbus-tcp.c src/modbus-tcp.c
index b3eae81..928b850 100644
--- src/modbus-tcp.c
+++ src/modbus-tcp.c
@@ -404,6 +404,17 @@ void _modbus_tcp_close(modbus_t *ctx)
close(ctx->s);
}
+void _modbus_tcp_close_mt(modbus_t *ctx)
+{
+ _modbus_tcp_close(ctx);
+
+ modbus_tcp_mt_t *ctx_mt = ctx->mt_data;
+ if (ctx_mt->lst_socket != -1) {
+ shutdown(ctx_mt->lst_socket, SHUT_RDWR);
+ close(ctx_mt->lst_socket);
+ }
+}
+
int _modbus_tcp_flush(modbus_t *ctx)
{
int rc;
@@ -448,6 +459,7 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
int yes;
struct sockaddr_in addr;
modbus_tcp_t *ctx_tcp = ctx->backend_data;
+ modbus_tcp_mt_t *ctx_mt = ctx->mt_data;
#ifdef OS_WIN32
if (_modbus_tcp_init_win32() == -1) {
@@ -481,6 +493,8 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
close(new_socket);
return -1;
}
+
+ ctx_mt->lst_socket = new_socket;
return new_socket;
}
@@ -495,6 +509,7 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection)
const char *service;
int new_socket;
modbus_tcp_pi_t *ctx_tcp_pi = ctx->backend_data;
+ modbus_tcp_mt_t *ctx_mt = ctx->mt_data;
if (ctx_tcp_pi->node[0] == 0)
node = NULL; /* == any */
@@ -577,6 +592,8 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection)
if (new_socket < 0) {
return -1;
}
+
+ ctx_mt->lst_socket = new_socket;
return new_socket;
}
@@ -671,6 +688,7 @@ const modbus_backend_t _modbus_tcp_backend = {
_modbus_tcp_pre_check_confirmation,
_modbus_tcp_connect,
_modbus_tcp_close,
+ _modbus_tcp_close_mt,
_modbus_tcp_flush,
_modbus_tcp_select
};
@@ -693,6 +711,7 @@ const modbus_backend_t _modbus_tcp_pi_backend = {
_modbus_tcp_pre_check_confirmation,
_modbus_tcp_pi_connect,
_modbus_tcp_close,
+ _modbus_tcp_close_mt,
_modbus_tcp_flush,
_modbus_tcp_select
};
@@ -701,6 +720,7 @@ modbus_t* modbus_new_tcp(const char *ip, int port)
{
modbus_t *ctx;
modbus_tcp_t *ctx_tcp;
+ modbus_tcp_mt_t *ctx_mt;
size_t dest_size;
size_t ret_size;
@@ -727,6 +747,9 @@ modbus_t* modbus_new_tcp(const char *ip, int port)
ctx->backend_data = (modbus_tcp_t *) malloc(sizeof(modbus_tcp_t));
ctx_tcp = (modbus_tcp_t *)ctx->backend_data;
+
+ ctx->mt_data = (modbus_tcp_mt_t *) malloc(sizeof(modbus_tcp_mt_t));
+ ctx_mt = (modbus_tcp_mt_t *)ctx->mt_data;
dest_size = sizeof(char) * 16;
ret_size = strlcpy(ctx_tcp->ip, ip, dest_size);
@@ -746,6 +769,8 @@ modbus_t* modbus_new_tcp(const char *ip, int port)
ctx_tcp->port = port;
ctx_tcp->t_id = 0;
+
+ ctx_mt->lst_socket = -1;
return ctx;
}
@@ -755,6 +780,7 @@ modbus_t* modbus_new_tcp_pi(const char *node, const char *service)
{
modbus_t *ctx;
modbus_tcp_pi_t *ctx_tcp_pi;
+ modbus_tcp_mt_t *ctx_mt;
size_t dest_size;
size_t ret_size;
@@ -768,6 +794,9 @@ modbus_t* modbus_new_tcp_pi(const char *node, const char *service)
ctx->backend_data = (modbus_tcp_pi_t *) malloc(sizeof(modbus_tcp_pi_t));
ctx_tcp_pi = (modbus_tcp_pi_t *)ctx->backend_data;
+
+ ctx->mt_data = (modbus_tcp_mt_t *) malloc(sizeof(modbus_tcp_mt_t));
+ ctx_mt = (modbus_tcp_mt_t *)ctx->mt_data;
dest_size = sizeof(char) * _MODBUS_TCP_PI_NODE_LENGTH;
ret_size = strlcpy(ctx_tcp_pi->node, node, dest_size);
@@ -802,6 +831,8 @@ modbus_t* modbus_new_tcp_pi(const char *node, const char *service)
}
ctx_tcp_pi->t_id = 0;
+
+ ctx_mt->lst_socket = -1;
return ctx;
}
diff --git src/modbus.c src/modbus.c
index b555913..e08f7ae 100644
--- src/modbus.c
+++ src/modbus.c
@@ -1457,12 +1457,21 @@ void modbus_close(modbus_t *ctx)
ctx->backend->close(ctx);
}
+void modbus_close_mt(modbus_t *ctx)
+{
+ if (ctx == NULL)
+ return;
+
+ ctx->backend->close_mt(ctx);
+}
+
void modbus_free(modbus_t *ctx)
{
if (ctx == NULL)
return;
free(ctx->backend_data);
+ free(ctx->mt_data);
free(ctx);
}
diff --git src/modbus.h src/modbus.h
index 399c41b..e31faf4 100644
--- src/modbus.h
+++ src/modbus.h
@@ -169,6 +169,7 @@ EXPORT int modbus_get_header_length(modbus_t *ctx);
EXPORT int modbus_connect(modbus_t *ctx);
EXPORT void modbus_close(modbus_t *ctx);
+EXPORT void modbus_close_mt(modbus_t *ctx);
EXPORT void modbus_free(modbus_t *ctx);