Skip to content

TCP/HTTP h*ll – but some light

March 9, 2015

I’m still poking at the bagels server but i dropped back to the simplest server example to try to wrestle something manageable. In the dear dead days of the w5100 blinkenlights servers, I had a couple of heuristics I was following: If a session was established but there was no data to read I disconnected it. If I got any of a set of statuses that inluded SOCK_FIN_WAIT among others, I closed the session and just waited for a new one. Both of those seem to give me grief with the W5500 and traffic coming in from outside the LAN. Trading notes with Matt Millman, he assures me that it’s valid for a session to be established with 0 data and the data to show up later. I put specific logic in to retry reading the received data size register for up to 2 seconds. I also put in logic to ignore the SOCK_FIN_WAIT status a couple of times.

With a lot of traces in place, i tried the simple server with the client being safari on my phone coming into the lan from outside. The corrections seem effective in this use case. In the trace below the statuses are the bare hex pairs (00,14,16,17,18). 00 is closed, i open the socket. 14 is the standard “listening” 17 is session established. 16 is sort of “session being established” and 18 is sort of “session is going down”.

Transactions are separated by blanks. In the first three, we see a litany of failures: it retries a zero length 10 times then gives up and disconnects, the socket goes to SOCK_FIN_WAIT and never moves.

After that, things go as designed: you see it routinely retrying zero lengths 4-6 times and you usually see SOCK_FIN_WAIT show up once or twice then go to a normal close state.

I’m nowhere near declaring victory but these changes are simple enough that i’ll put them into bagels and see what happens.

safari over cellular -1st trx locked up/failed.
00 14...14 16 16 16 16 16 16 16 17 retry rsizeX10 
10:30:17.315> handling session 0
10:30:17.315> disconnecting
10:30:17.377> 18 SOCK_FIN_WAIT: ignoring
10:30:17.439> 18 SOCK_FIN_WAIT: ignoring
10:30:17.501> 18 SOCK_FIN_WAIT: lost patience, closing

00 14 14 16X30 17 retry rsizeX10
10:30:19.375> handling session 0
10:30:19.375> disconnecting
10:30:19.437> 18 SOCK_FIN_WAIT: ignoring
10:30:19.499> 18 SOCK_FIN_WAIT: ignoring
10:30:19.561> 18 SOCK_FIN_WAIT: lost patience, closing

00 14 14 14 14 16 16 16 16 16 17 retry rsizeX10
10:30:20.249> handling session 0
10:30:20.310> disconnecting
10:30:20.372> 18 SOCK_FIN_WAIT: ignoring
10:30:20.434> 18 SOCK_FIN_WAIT: ignoring
10:30:20.497> 18 SOCK_FIN_WAIT: lost patience, closing

00 14X200 16X19 17 retry rsizeX6 
10:30:31.417> handling session 381
10:30:31.417> disconnecting
10:30:31.479> 18 SOCK_FIN_WAIT: ignoring

00 14...14 16 16 17 retry rsizeX4 
10:30:34.228> handling session 381
10:30:34.228> disconnecting
10:30:34.350> 18 SOCK_FIN_WAIT: ignoring
10:30:34.350> 18 SOCK_FIN_WAIT: ignoring

00 14...14 16 16 16 17 retry rsizeX4 
10:30:36.097> handling session 381
10:30:36.097> disconnecting
10:30:36.159> 18 SOCK_FIN_WAIT: ignoring
00 14..14 16 17 retry rsize retry rsize retry rsize retry rsize retry rsize 10:30:38.220> retry rsize retry rsize 
10:30:38.220> handling session 381
10:30:38.220> disconnecting
10:30:38.280> 18 SOCK_FIN_WAIT: ignoring

00 14...14 16 17 retry rsizeX4 
10:30:39.778> handling session 381
10:30:39.778> disconnecting
10:30:39.842> 18 SOCK_FIN_WAIT: ignoring

00 14...14 16 16 17 retry rsizeX4 
10:30:41.214> handling session 381
10:30:41.275> disconnecting
10:30:41.340> 18 SOCK_FIN_WAIT: ignoring
10:30:42.587> 00 14...14 16 16 17 retry rsizeX4 
10:30:42.650> handling session 381
10:30:42.650> disconnecting
10:30:42.712> 18 SOCK_FIN_WAIT: ignoring

00 14...14 14 16 16 17 retry rsize retry rsize retry 10:30:46.643> rsize retryX4
10:30:46.705> handling session 381
10:30:46.705> disconnecting
10:30:46.769> 18 SOCK_FIN_WAIT: ignoring
00 14...14 16 16 17 retry rsize 
10:30:49.638> handling session 381
10:30:49.638> disconnecting
10:30:49.700> 18 SOCK_FIN_WAIT: ignoring

00 14.....
void main(void){
	unsigned char socket0status;
    unsigned char ip_addr[] = {192,168,1,182};//{10,0,0,182};//{169,254,180,2};
    unsigned int SFWs=0;
	delay(500);
    wiz_Init(ip_addr); //initialize the wiznet chip
	while(1){  // Loop forever
		socket0status=wizGetCtl8(SnSR); //socket 0 status
		printf("%cx ",socket0status);
		switch (socket0status){
			case SOCK_CLOSED: //initial condition
				SFWs=0;
				socket0_init();	//initialize socket 0
				break;
			case SOCK_ESTABLISHED: //someone wants to talk to the server
				SFWs=0;
				handlesession();
				break;
			//following are cases where we have to reset and reopen the socket
			case SOCK_FIN_WAIT:
				printf("SOCK_FIN_WAIT:");
				if (++SFWs>2){
					printf(" lost patience, closing\n");
					wizCmd(CR_CLOSE);
				}else{
					printf(" ignoring\n");
				}
				break;
			case SOCK_CLOSING: case SOCK_TIME_WAIT:
			case SOCK_CLOSE_WAIT: case SOCK_LAST_ACK:
				SFWs=0;
				wizCmd(CR_CLOSE);
				break;
		}
		delay(100);
	}
}

void handlesession(){
	unsigned int rsize;
	unsigned int tries=10;
	rsize=wizGetCtl16(SnRX_RSR); //get the size of the received data
	while(rsize==0 && tries-->0){
		printf("retry rsize ");
		rsize=wizGetCtl16(SnRX_RSR); //retry size of the received data
		delay(20);
	}
	printf("\nhandling session %d\n",rsize);
	if (rsize>0){
		sendresp(); //send a response
		flush(rsize);	//get rid of the received data
	}
	printf("disconnecting\n");
	wizCmd(CR_DISCON);
}

Advertisements

From → web server

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: