Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of webserverBlinky by
Diff: main.cpp
- Revision:
- 36:2a9b457f8276
- Parent:
- 35:e7068df4d971
- Child:
- 37:2e6689f8b181
--- a/main.cpp Thu Jan 05 08:16:50 2017 +0000 +++ b/main.cpp Thu Jan 05 10:32:12 2017 +0000 @@ -53,6 +53,7 @@ unsigned int seq; int crc; int ledState; + int oldFlags; struct { char * buf; volatile int head; @@ -83,6 +84,7 @@ ppp.sync=0; ppp.seq=1000; ppp.ledState=0; + ppp.oldFlags=0; } void crcReset() @@ -513,20 +515,21 @@ // A sparse TCP flag interpreter that implements basic TCP connections from a single source int dataLen = 0; // most of our responses will have zero TCP data, only a header - int teardown = 0; // see if we must tear down the link + int flagsOut = TCP_FLAG_ACK; // the default case is an ack + int fastResponse = 0; // normally we wait 200ms before sending but this can make it faster if ( ((flagsTCP & ~TCP_FLAG_ACK) == 0) && ((flagsTCP & TCP_FLAG_ACK) != 0) ) { - if (incomingLen == 0) return; // no new data + if (incomingLen == 0) return; // no new data } else if ( (flagsTCP & TCP_FLAG_FIN) != 0 ) { // got FIN - flagbitstcp[1] = TCP_FLAG_ACK; // do an ack - teardown=1; + if ((ppp.oldFlags & TCP_FLAG_FIN) !=0) return; + seq++; // increase sequence by 1 for FIN and SYNC } else if ( (flagsTCP & TCP_FLAG_SYN) != 0 ) { // got SYN - flagbitstcp[1] |= TCP_FLAG_ACK; // do a syn-ack - seq++; + flagsOut = TCP_FLAG_SYN | TCP_FLAG_ACK; // do a syn-ack + seq++; // increase sequency by for FIN and SYN } else if ( (flagsTCP & TCP_FLAG_PSH) != 0 ) { // they are pushing data + fastResponse = 1; // we can response fast to a push // It's a push - let's check the data for an HTTP home page GET request if ( strncmp(dataStart, "GET / HTTP/1.1", 14) == 0) { - flagbitstcp[1] = TCP_FLAG_ACK; // | TCP_FLAG_FIN; // close connection after delivering page dataLen = 15*32; // this block has to hold the web page below, but keep it under 1k memset(dataStart,'x', dataLen ); // initialize the block int n=1; // number of bytes we have printed so far @@ -545,7 +548,6 @@ debug(("HTTP GET dataLen %d*32=%d Header %d Content-Length %d Total %d Margin %d\n",dataLen/32,dataLen,nHeader,contentLength,n,dataLen-n)); } } else if ( strncmp(dataStart, "GET /", 4) == 0) { // all other files get 404 not found - flagbitstcp[1] = TCP_FLAG_ACK;// | TCP_FLAG_FIN; // close connection after delivering page dataLen = 5*32; // this block has to hold the web page below, but keep it under 1k memset(dataStart,'x', dataLen ); // initialize the block int n=0; @@ -561,36 +563,20 @@ debug(("HTTP GET dataLen %d*32=%d Header %d Content-Length %d Total %d Margin %d\n",dataLen/32,dataLen,nHeader,contentLength,n,dataLen-n)); } } else { - flagbitstcp[1] = TCP_FLAG_ACK; // we have nothing to say, just ack dataLen=0; } + } else { + dataLen=0; } // All the flag checking is now donw + ppp.oldFlags = flagsTCP; // remember the flags for the next round + // Now we have to recalculate all the header sizes ack = seq + incomingLen; // acknowledge the number of bytes they sent by adding it to seq seq = ppp.seq; - -teardownEntry: - - int newPacketSize = headerSizeIP + headerSizeTCP + dataLen; - pktLen[0] = (newPacketSize>>8); - pktLen[1]=newPacketSize; // ip total packet size - ppp.pkt.len = newPacketSize+6; // ppp packet length - tcpSize = headerSizeTCP + dataLen; // tcp packet size - - // redo all the header stuff - - acktcp[0]=ack>>24; - acktcp[1]=ack>>16; - acktcp[2]=ack>>8; - acktcp[3]=ack>>0; // save ack - seqtcp[0]=seq>>24; - seqtcp[1]=seq>>16; - seqtcp[2]=seq>>8; - seqtcp[3]=seq>>0; // save seq char src[4]; char dst[4]; @@ -605,6 +591,27 @@ memcpy(srctcp, pdst,2); memcpy(dsttcp, psrc,2); // swap ip port source/dest + + int newPacketSize = headerSizeIP + headerSizeTCP + dataLen; + pktLen[0] = (newPacketSize>>8); + pktLen[1]=newPacketSize; // ip total packet size + ppp.pkt.len = newPacketSize+6; // ppp packet length + tcpSize = headerSizeTCP + dataLen; // tcp packet size + + flagbitstcp[1] = flagsOut; // set up the new flags + + acktcp[0]=ack>>24; + acktcp[1]=ack>>16; + acktcp[2]=ack>>8; + acktcp[3]=ack>>0; // save ack + seqtcp[0]=seq>>24; + seqtcp[1]=seq>>16; + seqtcp[2]=seq>>8; + seqtcp[3]=seq>>0; // save seq + + + // redo all the header stuff + headercheck[0]=0; headercheck[1]=0; headerCheckSum(); // redo the ip header checksum @@ -624,14 +631,13 @@ checksumtcp[0]=sum>>8; checksumtcp[1]=sum; memcpy( s-12, temp, 12); // overwrite the pseudoheader + if (fastResponse==1) { + fastResponse=0; // reset and skip 200 ms wait + } else { + wait(0.2); // wait 200 ms before sending packet + } sendFrame(); // return the TCP packet ppp.seq = ppp.seq + dataLen; // increase OUR sequence by outgoing data length - for the next round - if (teardown) { - teardown=0; // clear the teardown flag - flagbitstcp[1] = TCP_FLAG_FIN; // we want to end the link - dataLen=0; // no data needed - goto teardownEntry; - } } void dumpDataTCP()