[srm-cvs] CVS Update: - Made connection handling threaded.

From: <d.rethans[@]jdimedia.nl>
Date: Sun Feb 23 2003 - 21:22:33 CET

Date: Sun Feb 23 21:22:32 CET 2003
User: Derick Rethans
Directory: srm/main/src

Log Message:
 [1.50]
 - Made connection handling threaded.
 - Cleaned up the standard protocol (including a memory leak in
   srm_process_command()).
 - Added handling of SIGHUP as alias for SIGTERM and SIGINT.
 
Modified files:
           srm/TODO (version: 1.11)
           srm/main/main.c (version: 1.75)
           srm/main/src/srm_protocol_standard.c (version: 1.72)
           srm/main/src/srm_protocols.h (version: 1.7)

[FILE: /srm/TODO]

--- srm/TODO:1.10 Tue Feb 18 20:14:26 2003 GMT
+++ srm/TODO Sun Feb 23 19:22:30 2003 GMT
@@ -1,9 +1,6 @@
-/* $Id: cvstemp,v 1.10 2003/02/18 21:14:26 derick Exp $ */
+/* $Id: cvstemp,v 1.11 2003/02/23 20:22:30 derick Exp $ */
 
 TODO:
-
-Daemon:
-- Make connection handling Multi-threaded [Sterling]
 
 Locking:
 - Add mutexes around access to the locking hashes (to make sure Bananas don't

[FILE: /srm/main/main.c]

--- srm/main/main.c:1.74 Fri Feb 21 18:33:09 2003 GMT
+++ srm/main/main.c Sun Feb 23 19:22:31 2003 GMT
@@ -1,4 +1,4 @@
-/* $Id: cvstemp,v 1.74 2003/02/21 19:33:09 derick Exp $ */
+/* $Id: cvstemp,v 1.75 2003/02/23 20:22:31 derick Exp $ */
 
 /* The contents of this file are subject to the Vulcan Logic Public
  * License Version 1.1 (the "License"); you may not use this file
@@ -161,6 +161,29 @@
         return (void *)0;
 }
 
+void* srm_connection_handler(con_info *info)
+{
+ while (info->protocol->process(info->fd_no) == SRM_SUCCESS);
+
+ info->status = SRM_STATUS_NOT_USED;
+ close(info->fd_no);
+ srm_fmt_message(LL_CONNECTION, "Notice", "ended!");
+
+ return NULL;
+}
+
+pthread_t srm_new_connection(con_pool *pool, int new_fd, int ren_fd)
+{
+ pthread_t thread_id;
+
+ pool->counter++;
+ pool->info[new_fd].status = SRM_STATUS_NORMAL;
+ pool->info[new_fd].fd_no = new_fd;
+ pool->info[new_fd].protocol = pool->info[ren_fd].protocol;
+
+ pthread_create(&thread_id, NULL, (void*) srm_connection_handler, &(pool->info[new_fd]));
+ return thread_id;
+}
 
 inline static void srm_init_random (void)
 {
@@ -298,17 +321,7 @@
                                 } else {
                                         abort();
                                 }
- CP_NEW_CONNECTION(new_fd, fd);
- }
-
-
- if ((CP.info[fd].status == SRM_STATUS_NORMAL) && CP_CONNECTION_AVAILABLE(fd)) {
- /* srm_process_data processes all incoming packets */
- if (CP.info[fd].protocol->process(fd) != SRM_SUCCESS) {
- CP_CLOSE_CONNECTION(fd);
- } else {
- /* Do stuff too */
- }
+ srm_new_connection(&connection_pool, new_fd, fd);
                         }
                 }
 
@@ -325,14 +338,9 @@
                                 exit (ERROR_SIGSEGV);
                                 break;
                         case SIGTERM:
- srm_fmt_message (LL_SIGNAL, "Signal", "SIGTERM caught, shutting down SRM");
- pthread_mutex_lock (&mtx_shutdown);
- srm_shutdown = TRUE;
- pthread_mutex_unlock (&mtx_shutdown);
- srm_caught_signal = -1;
- break;
+ case SIGHUP:
                         case SIGINT:
- srm_fmt_message (LL_SIGNAL, "Signal", "SIGINT caught, shutting down SRM");
+ srm_fmt_message (LL_SIGNAL, "Signal", "SIGHUP/SIGINT/SIGTERM caught, shutting down SRM");
                                 pthread_mutex_lock (&mtx_shutdown);
                                 srm_shutdown = TRUE;
                                 pthread_mutex_unlock (&mtx_shutdown);

[FILE: /srm/main/src/srm_protocol_standard.c]

--- srm/main/src/srm_protocol_standard.c:1.71 Fri Feb 21 18:33:10 2003 GMT
+++ srm/main/src/srm_protocol_standard.c Sun Feb 23 19:22:32 2003 GMT
@@ -1,4 +1,4 @@
-/* $Id: cvstemp,v 1.71 2003/02/21 19:33:10 derick Exp $ */
+/* $Id: cvstemp,v 1.72 2003/02/23 20:22:32 derick Exp $ */
 
 /* The contents of this file are subject to the Vulcan Logic Public
  * License Version 1.1 (the "License"); you may not use this file
@@ -164,8 +164,10 @@
 #ifdef SRM_DEBUG
         srm_value_dump(ret_val, 1);
 #endif
+ /* Freeing the deserialized structure */
+ srm_value_free(command);
 
-/* Sending the result */
+ /* Sending the result */
         serialize(ret_val, (srm_ui8**) &ret_data, &length);
 
 #ifdef SRM_DEBUG
@@ -179,25 +181,6 @@
 } /* 1}}} */
 
 
-static void connection_abort (int fd) /* {{{1 */
-{
- srm_fmt_message(LL_CONNECTION_ERROR, "Notice", "Connection aborted");
- srm_close_connection(fd);
-} /* 1}}} */
-
-static void connection_close (int fd) /* {{{1 */
-{
- srm_fmt_message(LL_CONNECTION, "Notice", "Connection closed");
- srm_close_connection(fd);
-} /* 1}}} */
-
-static void connection_abort_with_error (int fd, char* string) /* {{{1 */
-{
- srm_fmt_message(LL_CONNECTION_ERROR, "Notice", "Connection aborted with error: %s", string);
- server_send_packet(fd, PKT_ABORT, NULL, 0, NULL);
- srm_close_connection(fd);
-} /* 1}}} */
-
 /******************************************************************************
 ** Functions that handles reading the receiving of packets
 **/
@@ -206,12 +189,12 @@
 {
         struct pkt_header header;
         srm_ui8 * data = NULL;
- srm_ui32 length = 0, n = 0, read_bytes = 0;
+ srm_ui32 length = 0, n = 0, read_bytes = 0, fail = 0;
 
         length = read(fd, &header, sizeof(header));
         if (length < sizeof(header) || length == -1) {
                 /* Packet did not contain a full header */
- connection_abort(fd);
+ srm_fmt_message(LL_CONNECTION_ERROR, "Notice", "Connection aborted");
                 return SRM_FAILED;
         } else {
 #ifdef SRM_DEBUG
@@ -230,33 +213,39 @@
                                 read_bytes += n;
                         }
                         if (read_bytes < header.data_length) {
- connection_abort_with_error(fd, "Packet is invalid");
+ srm_fmt_message(LL_CONNECTION_ERROR, "Notice", "Connection aborted: Invalid packet");
+ server_send_packet(fd, PKT_ABORT, NULL, 0, NULL);
+ fail = 1;
+ goto cleanup;
                         }
                 }
- if (TRUE) {
- switch (header.type) {
- case PKT_HANDSHAKE1:
- srm_do_handshake(fd, (srm_ui8*) &header);
- break;
- case PKT_COMMAND:
- srm_process_command(fd, (srm_ui8*) &header, length, (srm_ui8*) data, header.data_length);
- break;
- case PKT_DISCONNECT:
- connection_close(fd);
- break;
- default:
- connection_abort_with_error(fd, "Unknown packet type");
- }
- } else {
- connection_abort_with_error(fd, "Packet is invalid");
+
+ switch (header.type) {
+ case PKT_HANDSHAKE1:
+ srm_do_handshake(fd, (srm_ui8*) &header);
+ break;
+ case PKT_COMMAND:
+ srm_process_command(fd, (srm_ui8*) &header, length, (srm_ui8*) data, header.data_length);
+ break;
+ case PKT_DISCONNECT:
+ srm_fmt_message(LL_CONNECTION, "Notice", "Connection closed");
+ fail = 1;
+ goto cleanup;
+ break;
+ default:
+ srm_fmt_message(LL_CONNECTION_ERROR, "Notice", "Connection aborted: Invalid packet");
+ server_send_packet(fd, PKT_ABORT, NULL, 0, NULL);
+ fail = 1;
+ goto cleanup;
                 }
         }
+cleanup:
         /* Free data block if additional data was received */
         if (data) {
                 free(data);
         }
 
- if (length == 0) {
+ if (fail || length == 0) {
                 return SRM_FAILED;
         }
         return SRM_SUCCESS;

[FILE: /srm/main/src/srm_protocols.h]

--- srm/main/src/srm_protocols.h:1.6 Thu Feb 20 18:06:10 2003 GMT
+++ srm/main/src/srm_protocols.h Sun Feb 23 19:22:32 2003 GMT
@@ -1,4 +1,4 @@
-/* $Id: cvstemp,v 1.6 2003/02/20 19:06:10 derick Exp $ */
+/* $Id: cvstemp,v 1.7 2003/02/23 20:22:32 derick Exp $ */
 
 /* The contents of this file are subject to the Vulcan Logic Public
  * License Version 1.1 (the "License"); you may not use this file
@@ -37,8 +37,6 @@
 
 #define CP_NEW_CONNECTION(fd,pfd) FD_SET ((fd), &connection_pool.test_set); \
         connection_pool.counter++; \
- if ((fd) > (connection_pool.max_fd)) \
- connection_pool.max_fd = fd; \
         connection_pool.info[fd].status = SRM_STATUS_NORMAL; \
         connection_pool.info[fd].fd_no = fd; \
         connection_pool.info[fd].protocol = connection_pool.info[pfd].protocol;
Received on Sun Feb 23 21:22:33 2003

This archive was generated by hypermail 2.1.8 : Tue Jan 06 2009 - 02:00:04 CET