Browse code

added seeking maximum receive buffer size

Jiri Kuthan authored on 12/11/2001 19:51:59
Showing 2 changed files
... ...
@@ -26,7 +26,13 @@ int udp_sock;
26 26
 int udp_init(unsigned long ip, unsigned short port)
27 27
 {
28 28
 	struct sockaddr_in* addr;
29
-	int optval;
29
+	int optval, optvallen;
30
+	int ioptval, ioptvallen;
31
+	int foptval, foptvallen;
32
+	int voptval, voptvallen;
33
+	int i;
34
+	int phase=0;
35
+
30 36
 
31 37
 	addr=(struct sockaddr_in*)malloc(sizeof(struct sockaddr));
32 38
 	if (addr==0){
... ...
@@ -51,6 +57,68 @@ int udp_init(unsigned long ip, unsigned short port)
51 51
 		goto error;
52 52
 	}
53 53
 
54
+	/* jku: try to increase buffer size as much as we can */
55
+	ioptvallen=sizeof(ioptval);
56
+	if (getsockopt( udp_sock, SOL_SOCKET, SO_RCVBUF, (void*) &ioptval,
57
+		    &ioptvallen) == -1 )
58
+	{
59
+		LOG(L_ERR, "ERROR: udp_init: getsockopt: %s\n", strerror(errno));
60
+		goto error;
61
+	}
62
+	if ( ioptval==0 ) 
63
+	{
64
+		LOG(L_DBG, "DEBUG: udp_init: SO_RCVBUF initialy set to 0; resetting to %d\n",
65
+			BUFFER_INCREMENT );
66
+		ioptval=BUFFER_INCREMENT;
67
+	} else LOG(L_INFO, "INFO: udp_init: SO_RCVBUF is initially %d\n", ioptval );
68
+	for (optval=ioptval; optval < MAX_RECV_BUFFER_SIZE ;  ) {
69
+		/* increase size; double in initial phase, add linearly later */
70
+		if (phase==0) optval <<= 1; else optval+=BUFFER_INCREMENT;
71
+		LOG(L_DBG, "DEBUG: udp_init: trying SO_RCVBUF: %d\n", optval );
72
+        	if (setsockopt( udp_sock, SOL_SOCKET, SO_RCVBUF,
73
+                             (void*)&optval, sizeof(optval)) ==-1)
74
+        	{
75
+			LOG(L_DBG, "DEBUG: udp_init: SOL_SOCKET failed for %d, phase %d: %s\n",
76
+			    optval,  phase, strerror(errno) );
77
+			/* if setting buffer size failed and still in the aggressive
78
+			   phase, try less agressively; otherwise give up 
79
+			*/
80
+			if (phase==0) { phase=1; optval >>=1 ; continue; } 
81
+			else break;
82
+        	} 
83
+		/* verify if change has taken effect */
84
+		voptvallen=sizeof(voptval);
85
+		if (getsockopt( udp_sock, SOL_SOCKET, SO_RCVBUF, (void*) &voptval,
86
+		    &voptvallen) == -1 )
87
+		{
88
+			LOG(L_ERR, "ERROR: udp_init: getsockopt: %s\n", strerror(errno));
89
+			goto error;
90
+		} else {
91
+			LOG(L_DBG, "DEBUG: setting SO_RCVBUF; set=%d,verify=%d\n", 
92
+				optval, voptval);
93
+			if (voptval<optval) {
94
+				LOG(L_DBG, "DEBUG: setting SO_RCVBUF has no effect\n");
95
+				/* if setting buffer size failed and still in the aggressive
96
+			   	phase, try less agressively; otherwise give up 
97
+				*/
98
+                        	if (phase==0) { phase=1; optval >>=1 ; continue; } 
99
+                        	else break;
100
+			}
101
+		}
102
+
103
+	} /* for ... */
104
+	foptvallen=sizeof(foptval);
105
+	if (getsockopt( udp_sock, SOL_SOCKET, SO_RCVBUF, (void*) &foptval,
106
+		    &foptvallen) == -1 )
107
+	{
108
+		LOG(L_ERR, "ERROR: udp_init: getsockopt: %s\n", strerror(errno));
109
+		goto error;
110
+	}
111
+ 	LOG(L_INFO, "INFO: udp_init: SO_RCVBUF is finally %d\n", foptval );
112
+
113
+
114
+	/* EoJKU */
115
+
54 116
 	if (bind(udp_sock, (struct sockaddr*) addr, sizeof(struct sockaddr))==-1){
55 117
 		LOG(L_ERR, "ERROR: udp_init: bind: %s\n", strerror(errno));
56 118
 		goto error;
... ...
@@ -8,6 +8,9 @@
8 8
 #include <sys/types.h>
9 9
 #include <sys/socket.h>
10 10
 
11
+#define MAX_RECV_BUFFER_SIZE	256*1024
12
+#define BUFFER_INCREMENT	2048
13
+
11 14
 extern int udp_sock;
12 15
 
13 16
 int udp_init(unsigned long ip, unsigned short port);