Index: trunk/FACT++/dim/README_v20.txt
===================================================================
--- trunk/FACT++/dim/README_v20.txt	(revision 18085)
+++ trunk/FACT++/dim/README_v20.txt	(revision 18097)
@@ -1,4 +1,4 @@
 
-                    DIM version 20r11 Release Notes
+                    DIM version 20r13 Release Notes
 
 Notes 1 and 2 for Unix Users only
@@ -17,5 +17,20 @@
 		Dns </dev/null >& dns.log &
 
-NOTE 3: The Version Number service provided by servers is now set to 2011.
+NOTE 3: The Version Number service provided by servers is now set to 2013.
+
+22/01/2014
+Changes for version 2013:
+    - Tried to improve in the DNS the handling of many connections at startup 
+      (and many applications "simultaneous" restart) 
+
+
+03/12/2014
+Changes for version 2012:
+    - In Linux the number of connections is now completely dynamic.
+      (replaced select by poll)
+    - Changed the default SEND and RECV buffer sizes per connection from 16Kb to 64Kb
+      (except for the DNS connections: 32 Kb, to limit the memory increase)
+    - Changed the DNS Keepalive timeout from 15 to 20 seconds (to use a little less CPU) 
+
 
 24/10/2014
Index: trunk/FACT++/dim/dim/dim.h
===================================================================
--- trunk/FACT++/dim/dim/dim.h	(revision 18085)
+++ trunk/FACT++/dim/dim/dim.h	(revision 18097)
@@ -14,5 +14,5 @@
 #include "dim_common.h"
 
-#define DIM_VERSION_NUMBER 2011
+#define DIM_VERSION_NUMBER 2013
 
 
@@ -163,6 +163,6 @@
 #define MAX_CONNS		1024
 #define ID_BLOCK		512
-#define TCP_RCV_BUF_SIZE	16384/*32768*//*65536*/
-#define TCP_SND_BUF_SIZE	16384/*32768*//*65536*/
+#define TCP_RCV_BUF_SIZE	/*16384*//*32768*/65536
+#define TCP_SND_BUF_SIZE	/*16384*//*32768*/65536
 #endif
 #define DID_DNS_TMOUT_MIN	5
Index: trunk/FACT++/dim/src/dns.c
===================================================================
--- trunk/FACT++/dim/src/dns.c	(revision 18085)
+++ trunk/FACT++/dim/src/dns.c	(revision 18097)
@@ -395,5 +395,5 @@
 					{
 						dim_print_date_time();
-printf(" Service %s already declared by conn %d - %s@%s:%d (PID %d), Redeclared by conn %d - %s@%s:%d (PID %d)(same server) - Closing old conn %d\n",
+printf(" Service %s already declared by conn %d - %s@%s:%d (PID %d), Redeclared by conn %d - %s@%s:%d (PID %d)(same server) - Closing both conns %d %d\n",
 							servp->serv_name, servp->conn_id, 
 							Dns_conns[servp->conn_id].task_name,
@@ -406,11 +406,13 @@
 							Dns_conns[conn_id].port,
 							Dns_conns[conn_id].pid,
-							servp->conn_id);
+							servp->conn_id, conn_id);
 						fflush(stdout);
 						release_conn(servp->conn_id);
+						release_conn(conn_id);
+/*
 						update_did = 0;
-/*
+*/
 						return(0);
-*/
+
 					}
 					else
@@ -1079,5 +1081,7 @@
 	void service_remove();
 
-	if( Dns_conns[conn_id].src_type == SRC_DIS ) 
+	servp = (DNS_SERVICE *)Dns_conns[conn_id].service_head;
+	nodep = (NODE *)Dns_conns[conn_id].node_head;
+	if(( Dns_conns[conn_id].src_type == SRC_DIS ) || (servp))
 	{
 		if( Debug )
@@ -1143,5 +1147,5 @@
 		dna_close(conn_id);
 	}
-	else if(Dns_conns[conn_id].src_type == SRC_DIC)
+	else if((Dns_conns[conn_id].src_type == SRC_DIC) || (nodep))
 	{
 		if(Debug)
@@ -1435,4 +1439,7 @@
 	dim_set_write_timeout(10);
 	dim_set_listen_backlog(1024);
+	dim_set_keepalive_timeout(90);
+	dim_set_write_buffer_size(32768);
+	dim_set_read_buffer_size(32768);
 	dim_init();
 	conn_arr_create( SRC_DNS );
Index: trunk/FACT++/dim/src/tcpip.c
===================================================================
--- trunk/FACT++/dim/src/tcpip.c	(revision 18085)
+++ trunk/FACT++/dim/src/tcpip.c	(revision 18097)
@@ -14,9 +14,7 @@
 */
 
-/* Modifies the number of open connections to 8192 for Windows and Linux */
-/* Can not be moved from here ! */
-#include <dim_tcpip.h>
-
 #ifdef WIN32
+#define FD_SETSIZE      16384
+#define poll(pfd,nfds,timeout)	WSAPoll(pfd,nfds,timeout)
 #define ioctl ioctlsocket
 
@@ -70,4 +68,17 @@
 #endif
 
+#ifdef __linux__
+#include <poll.h>
+#define MY_FD_ZERO(set)	
+#define MY_FD_SET(fd, set)		poll_add(fd)
+#define MY_FD_CLR(fd, set)
+#define MY_FD_ISSET(fd, set)	poll_test(fd)
+#else
+#define MY_FD_ZERO(set)			FD_ZERO(set)
+#define MY_FD_SET(fd, set)		FD_SET(fd, set)
+#define MY_FD_CLR(fd, set)		FD_CLR(fd, set)
+#define MY_FD_ISSET(fd, set)	FD_ISSET(fd, set)
+#endif
+
 #include <stdio.h>
 #include <time.h>
@@ -123,4 +134,5 @@
 	{
 		ret = get_keepalive_tmout();
+		Keepalive_timeout_set = ret;
 	}
 	return(ret);
@@ -470,4 +482,55 @@
 */
 
+#ifdef __linux__
+static struct pollfd *Pollfds = 0;
+static int Pollfd_size = 0;
+
+static int poll_create()
+{
+	int i;
+	if(Pollfd_size == 0)
+	{
+		Pollfd_size = Curr_N_Conns;
+		Pollfds = malloc(Pollfd_size * sizeof(struct pollfd));
+		Pollfds[0].fd = -1;
+		for(i = 0; i < Pollfd_size; i++)
+		{
+			Pollfds[i].events = POLLIN;
+		}
+	}
+	else if(Pollfd_size < Curr_N_Conns)
+	{
+		free(Pollfds);
+		Pollfd_size = Curr_N_Conns;
+		Pollfds = malloc(Pollfd_size * sizeof(struct pollfd));
+		Pollfds[0].fd = -1;
+		for(i = 0; i < Pollfd_size; i++)
+		{
+			Pollfds[i].events = POLLIN;
+		}
+	}
+	return 1;
+}
+
+static int poll_add(int fd)
+{
+	Pollfds[0].fd = fd;
+	return 1;
+}
+
+static int poll_test(int fd)
+{
+	if(Pollfds[0].fd == fd)
+	{
+		if( (Pollfds[0].revents & POLLIN) || (Pollfds[0].revents & POLLHUP) ) 
+		{
+		    Pollfds[0].revents = 0;
+			return 1;
+		}
+	}
+	return 0;
+}
+#endif
+
 static int list_to_fds( fd_set *fds )
 {
@@ -475,7 +538,16 @@
 	int found = 0;
 
+	DISABLE_AST
+#ifdef __linux__
+	if(fds) {}
+	poll_create();
+#else
 	FD_ZERO( fds ) ;
+#endif
 	for( i = 1; i < Curr_N_Conns; i++ )
     {
+#ifdef __linux__
+		Pollfds[i].fd = -1;
+#endif
 		if( Dna_conns[i].busy )
 		{
@@ -483,9 +555,14 @@
 			{
 				found = 1;
+#ifdef __linux__
+				Pollfds[i].fd = Net_conns[i].channel;
+#else
 				FD_SET( Net_conns[i].channel, fds );
+#endif
 
 			}
 		}
 	}
+	ENABLE_AST
 	return(found);
 }
@@ -495,4 +572,23 @@
 	int	i;
 
+#ifdef __linux__
+	int index = *conn_id;
+	if(fds) {}
+	index++;
+	for( i = index; i < Pollfd_size; i++ )
+	{
+		if( Dna_conns[i].busy && (
+		    (Pollfds[i].revents & POLLIN) || (Pollfds[i].revents & POLLHUP) ) ) 
+		{
+		    Pollfds[i].revents = 0;
+		    if(Net_conns[i].channel)
+		    {
+				*conn_id = i;
+				return 1;
+			}
+		}
+	}
+	return 0;
+#else
 	for( i = 1; i < Curr_N_Conns; i++ )
 	{
@@ -508,4 +604,5 @@
 	}
 	return 0;
+#endif
 }
 
@@ -724,7 +821,12 @@
 		timeout.tv_usec = 0;
 		list_to_fds( &rfds );
+#ifdef __linux__
+		selret = poll(Pollfds, Pollfd_size, 0);
+#else
 		selret = select(FD_SETSIZE, &rfds, NULL, NULL, &timeout);
+#endif
 		if(selret > 0)
 		{
+			conn_id = 0;
 			while( (ret = fds_get_entry( &rfds, &conn_id )) > 0 ) 
 			{
@@ -749,5 +851,5 @@
 					do_accept( conn_id );
 				}
-				FD_CLR( (unsigned)Net_conns[conn_id].channel, &rfds );
+				MY_FD_CLR( (unsigned)Net_conns[conn_id].channel, &rfds );
 	    	}
 		}
@@ -760,5 +862,8 @@
 	 * call the right routine to handle the situation.
 	 */
-	fd_set	rfds, efds, *pfds;
+	fd_set	rfds, *pfds;
+#ifndef __linux__
+	fd_set efds;
+#endif
 	int	conn_id, ret, count;
 #ifndef WIN32
@@ -772,5 +877,5 @@
 
 		list_to_fds( &rfds );
-		FD_ZERO(&efds);
+		MY_FD_ZERO(&efds);
 #ifdef WIN32
 		pfds = &efds;
@@ -778,9 +883,17 @@
 		pfds = &rfds;
 #endif
-		FD_SET( DIM_IO_path[0], pfds );
+		MY_FD_SET( DIM_IO_path[0], pfds );
+#ifdef __linux__
+		ret = poll(Pollfds, Pollfd_size, -1);
+#else
 		ret = select(FD_SETSIZE, &rfds, NULL, &efds, NULL);
+#endif
+		if(ret <= 0)
+		  {
+		    printf("poll returned %d, errno %d\n", ret, errno);
+		  }
 		if(ret > 0)
 		{
-			if(FD_ISSET(DIM_IO_path[0], pfds) )
+			if(MY_FD_ISSET(DIM_IO_path[0], pfds) )
 			{
 #ifndef WIN32
@@ -788,5 +901,5 @@
 				DIM_IO_Done = 0;
 #endif
-				FD_CLR( (unsigned)DIM_IO_path[0], pfds );
+				MY_FD_CLR( (unsigned)DIM_IO_path[0], pfds );
 			}
 /*
@@ -794,4 +907,5 @@
 			DISABLE_AST
 */
+			conn_id = 0;
 			while( (ret = fds_get_entry( &rfds, &conn_id )) > 0 ) 
 			{
@@ -820,5 +934,5 @@
 					ENABLE_AST
 				}
-				FD_CLR( (unsigned)Net_conns[conn_id].channel, &rfds );
+				MY_FD_CLR( (unsigned)Net_conns[conn_id].channel, &rfds );
 			}
 /*
@@ -1284,4 +1398,6 @@
 		Net_conns[conn_id].read_rout( conn_id, -1, 0 );
 */
+		dna_report_error(conn_id, 0,
+			"Writing (blocking) to", DIM_ERROR, DIMTCPWRRTY);
 		return(0);
 	}
@@ -1322,8 +1438,11 @@
 	 */
 	int	wrote, ret, selret;
-
+	int tcpip_would_block();
+#ifdef __linux__
+	struct pollfd pollitem;
+#else
 	struct timeval	timeout;
 	fd_set wfds;
-	int tcpip_would_block();
+#endif
 	
 	set_non_blocking(Net_conns[conn_id].channel);
@@ -1352,4 +1471,10 @@
 		if(tcpip_would_block(ret))
 		{
+#ifdef __linux__
+		  pollitem.fd = Net_conns[conn_id].channel;
+		  pollitem.events = POLLOUT;
+		  pollitem.revents = 0;
+		  selret = poll(&pollitem, 1, Write_timeout*1000);
+#else
 			timeout.tv_sec = Write_timeout;
 			timeout.tv_usec = 0;
@@ -1357,4 +1482,5 @@
 			FD_SET( Net_conns[conn_id].channel, &wfds);
 			selret = select(FD_SETSIZE, NULL, &wfds, NULL, &timeout);
+#endif
 			if(selret > 0)
 			{
@@ -1362,8 +1488,6 @@
 				if( wrote == -1 ) 
 				{
-/*
-		dna_report_error(conn_id, 0,
-			"Writing to", DIM_ERROR, DIMTCPWRRTY);
-*/
+					dna_report_error(conn_id, 0,
+						"Writing to", DIM_ERROR, DIMTCPWRRTY);
 					return(0);
 				}
@@ -1372,8 +1496,6 @@
 		else
 		{
-/*
-dna_report_error(conn_id, 0,
-			"Writing (non-blocking) to", DIM_ERROR, DIMTCPWRRTY);
-*/
+			dna_report_error(conn_id, 0,
+				"Writing (non-blocking) to", DIM_ERROR, DIMTCPWRRTY);
 			return(0);
 		}
