Browse Source

New setter/getter for context socket (fixes bandwidth-server-many-up)

These new functions seem clearer than the recent modbus_receive_from
function IHMO. This change fixes the bandwidth-server-many-up program.
Stéphane Raimbault 14 years ago
parent
commit
1faf5c3aad

+ 2 - 1
doc/Makefile.am

@@ -4,6 +4,7 @@ MAN3 = \
         modbus_flush.3 \
         modbus_free.3 \
         modbus_get_header_length.3 \
+        modbus_get_socket.3 \
         modbus_get_timeout_begin.3 \
         modbus_get_timeout_end.3 \
         modbus_new_rtu.3 \
@@ -14,12 +15,12 @@ MAN3 = \
         modbus_read_input_registers.3 \
         modbus_read_registers.3 \
         modbus_receive.3 \
-        modbus_receive_from.3 \
         modbus_receive_confirmation.3 \
         modbus_send_raw_request.3 \
         modbus_set_debug.3 \
         modbus_set_error_recovery.3 \
         modbus_set_slave.3 \
+        modbus_set_socket.3 \
         modbus_set_timeout_begin.3 \
         modbus_set_timeout_end.3 \
         modbus_strerror.3 \

+ 2 - 1
doc/libmodbus.txt

@@ -102,6 +102,8 @@ Context setters and getters::
     linkmb:modbus_set_debug[3]
     linkmb:modbus_set_error_recovery[3]
     linkmb:modbus_set_slave[3]
+    linkmb:modbus_set_socket[3]
+    linkmb:modbus_get_socket[3]
     linkmb:modbus_get_timeout_begin[3]
     linkmb:modbus_set_timeout_begin[3]
     linkmb:modbus_get_timeout_end[3]
@@ -159,7 +161,6 @@ receive and reply:
 
 Receive::
      linkmb:modbus_receive[3]
-     linkmb:modbus_receive_from[3]
 
 Reply::
      linkmb:modbus_reply[3]

+ 34 - 0
doc/modbus_get_socket.txt

@@ -0,0 +1,34 @@
+modbus_get_socket(3)
+====================
+
+
+NAME
+----
+modbus_get_socket - get the current socket of the context
+
+
+SYNOPSIS
+--------
+*int modbus_get_socket(modbus_t *'ctx')*
+
+
+DESCRIPTION
+-----------
+The _modbus_get_socket()_ function shall return the current socket or file
+descriptor of the libmodbus context.
+
+
+RETURN VALUE
+------------
+The current socket or file descriptor of the context.
+
+
+SEE ALSO
+--------
+linkmb:modbus_set_socket[3]
+
+
+AUTHORS
+-------
+The libmodbus documentation was written by Stéphane Raimbault
+<stephane.raimbault@gmail.com>

+ 4 - 3
doc/modbus_receive.txt

@@ -18,8 +18,8 @@ The _modbus_receive()_ function shall receive an indication request from the
 socket of the context 'ctx'. This function is used by Modbus slave/server to
 receive and analyze indication request sent by the masters/clients.
 
-If you need to use another socket than the one defined in the context 'ctx', see
-the function linkmb:modbus_receive_from[3].
+If you need to use another socket or file descriptor than the one defined in the
+context 'ctx', see the function linkmb:modbus_set_socket[3].
 
 
 RETURN VALUE
@@ -31,7 +31,8 @@ errno.
 
 SEE ALSO
 --------
-linkmb:modbus_receive_from[3]
+linkmb:modbus_set_socket[3]
+linkmb:modbus_reply[3]
 
 
 AUTHORS

+ 56 - 0
doc/modbus_set_socket.txt

@@ -0,0 +1,56 @@
+modbus_set_socket(3)
+====================
+
+
+NAME
+----
+modbus_set_socket - set socket of the context
+
+
+SYNOPSIS
+--------
+*void modbus_set_socket(modbus_t *'ctx', int 'socket')*
+
+
+DESCRIPTION
+-----------
+The _modbus_set_socket()_ function shall set the socket or file descriptor in
+the libmodbus context. This function is useful for managing multiple client
+connections to the same server.
+
+
+RETURN VALUE
+------------
+There is no return values.
+
+
+EXAMPLE
+-------
+[source,c]
+-------------------
+ctx = modbus_new_tcp("127.0.0.1", 1502);
+server_socket = modbus_tcp_listen(ctx, NB_CONNECTION);
+
+FD_ZERO(&rdset);
+FD_SET(server_socket, &rdset);
+
+/* .... */
+
+if (FD_ISSET(master_socket, &rdset)) {
+    modbus_set_socket(ctx, master_socket);
+    rc = modbus_receive(ctx, query);
+    if (rc != -1) {
+        modbus_reply(ctx, query, rc, mb_mapping);
+    }
+}
+-------------------
+
+SEE ALSO
+--------
+linkmb:modbus_get_socket[3]
+
+
+AUTHORS
+-------
+The libmodbus documentation was written by Stéphane Raimbault
+<stephane.raimbault@gmail.com>

+ 2 - 2
doc/modbus_write_registers.txt

@@ -23,8 +23,8 @@ The function uses the Modbus function code 0x10 (preset multiple registers).
 
 RETURN VALUE
 ------------
-The _modbus_write_registers()_ function shall return the number of written registers
-if successful. Otherwise it shall return -1 and set errno.
+The _modbus_write_registers()_ function shall return the number of written
+registers if successful. Otherwise it shall return -1 and set errno.
 
 
 SEE ALSO

+ 10 - 17
src/modbus.c

@@ -417,23 +417,6 @@ int modbus_receive(modbus_t *ctx, uint8_t *req)
     return receive_msg(ctx, req, MSG_INDICATION);
 }
 
-/* Requires a socket file descriptor with a connection etablished in
-   argument */
-int modbus_receive_from(modbus_t *ctx, int sockfd, uint8_t *req)
-{
-    int rc;
-    const int s_old = ctx->s;
-
-    ctx->s = sockfd;
-
-    rc = receive_msg(ctx, req, MSG_INDICATION);
-
-    /* Restore orignal socket */
-    ctx->s = s_old;
-
-    return rc;
-}
-
 /* Receives the confirmation.
 
    The function shall store the read response in rsp and return the number of
@@ -1353,6 +1336,16 @@ int modbus_set_error_recovery(modbus_t *ctx, int enabled)
     return 0;
 }
 
+void modbus_set_socket(modbus_t *ctx, int socket)
+{
+    ctx->s = socket;
+}
+
+int modbus_get_socket(modbus_t *ctx)
+{
+    return ctx->s;
+}
+
 /* Get the timeout of begin of message */
 void modbus_get_timeout_begin(modbus_t *ctx, struct timeval *timeout)
 {

+ 2 - 1
src/modbus.h

@@ -135,8 +135,9 @@ typedef struct {
 } modbus_mapping_t;
 
 int modbus_set_slave(modbus_t* ctx, int slave);
-
 int modbus_set_error_recovery(modbus_t *ctx, int enabled);
+void modbus_set_socket(modbus_t *ctx, int socket);
+int modbus_get_socket(modbus_t *ctx);
 
 void modbus_get_timeout_begin(modbus_t *ctx, struct timeval *timeout);
 void modbus_set_timeout_begin(modbus_t *ctx, const struct timeval *timeout);

+ 1 - 1
tests/bandwidth-client.c

@@ -167,7 +167,7 @@ int main(int argc, char *argv[])
     rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
     printf("* %.3f ms for %d bytes\n", elapsed, bytes);
     printf("* %d KiB/s\n", rate);
-    printf("\n");
+    printf("\n\n");
 
     printf("READ AND WRITE REGISTERS\n\n");
 

+ 2 - 1
tests/bandwidth-server-many-up.c

@@ -118,7 +118,8 @@ int main(void)
                     /* An already connected master has sent a new query */
                     uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
 
-                    rc = modbus_receive_from(ctx, master_socket, query);
+                    modbus_set_socket(ctx, master_socket);
+                    rc = modbus_receive(ctx, query);
                     if (rc != -1) {
                         modbus_reply(ctx, query, rc, mb_mapping);
                     } else {