Explorar el Código

New API to close slave TCP socket

Stéphane Raimbault hace 15 años
padre
commit
5f65efd83f
Se han modificado 3 ficheros con 13 adiciones y 6 borrados
  1. 8 1
      src/modbus.c
  2. 2 0
      src/modbus.h
  3. 3 5
      tests/bandwidth-slave-many-up.c

+ 8 - 1
src/modbus.c

@@ -1460,7 +1460,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param)
 
 
            Timeouts are ignored in canonical input mode or when the
            Timeouts are ignored in canonical input mode or when the
            NDELAY option is set on the file via open or fcntl */
            NDELAY option is set on the file via open or fcntl */
-        mb_param->fd = open(mb_param->device, O_RDWR | O_NOCTTY | O_NDELAY);
+        mb_param->fd = open(mb_param->device, O_RDWR | O_NOCTTY | O_NDELAY | O_EXCL);
         if (mb_param->fd < 0) {
         if (mb_param->fd < 0) {
                 perror("open");
                 perror("open");
                 printf("ERROR Can't open the device %s (%s)\n",
                 printf("ERROR Can't open the device %s (%s)\n",
@@ -1924,6 +1924,13 @@ int modbus_slave_accept_tcp(modbus_param_t *mb_param, int *socket)
         return mb_param->fd;
         return mb_param->fd;
 }
 }
 
 
+/* Closes a TCP socket */
+void modbus_slave_close_tcp(int socket)
+{
+        shutdown(socket, SHUT_RDWR);
+        close(socket);
+}
+
 /** Utils **/
 /** Utils **/
 
 
 /* Sets many input/coil status from a single byte value (all 8 bits of
 /* Sets many input/coil status from a single byte value (all 8 bits of

+ 2 - 0
src/modbus.h

@@ -326,6 +326,8 @@ int modbus_slave_receive(modbus_param_t *mb_param, int sockfd,
 void modbus_slave_manage(modbus_param_t *mb_param, const uint8_t *query,
 void modbus_slave_manage(modbus_param_t *mb_param, const uint8_t *query,
                          int query_length, modbus_mapping_t *mb_mapping);
                          int query_length, modbus_mapping_t *mb_mapping);
 
 
+/* Closes a TCP socket */
+void modbus_slave_close_tcp(int socket);
 
 
 /**
 /**
  * UTILS FUNCTIONS
  * UTILS FUNCTIONS

+ 3 - 5
tests/bandwidth-slave-many-up.c

@@ -32,11 +32,10 @@ modbus_mapping_t mb_mapping;
 
 
 static void close_sigint(int dummy)
 static void close_sigint(int dummy)
 {
 {
-        shutdown(slave_socket, SHUT_RDWR);
+        modbus_slave_close_tcp(slave_socket);
-        close(slave_socket);
         modbus_mapping_free(&mb_mapping);
         modbus_mapping_free(&mb_mapping);
 
 
-	exit(dummy);
+        exit(dummy);
 }
 }
 
 
 int main(void)
 int main(void)
@@ -115,8 +114,7 @@ int main(void)
                                         } else {
                                         } else {
                                                 /* Connection closed by the client, end of server */
                                                 /* Connection closed by the client, end of server */
                                                 printf("Connection closed on socket %d\n", master_socket);
                                                 printf("Connection closed on socket %d\n", master_socket);
-                                                shutdown(master_socket, SHUT_RDWR);
+                                                modbus_slave_close_tcp(master_socket);
-                                                close(master_socket);
 
 
                                                 /* Remove from reference set */
                                                 /* Remove from reference set */
                                                 FD_CLR(master_socket, &refset);
                                                 FD_CLR(master_socket, &refset);