A simple web server mainly based on ideas from Jasper Schuurmans Netduino web server
Dependents: RdBlindsServer SpideyWallWeb RdGasUseMonitor
A fast and reliable web server for MBED! http://robdobson.com/2015/08/a-reliable-mbed-webserver/
It has a very neat way to implement REST commands and can serve files from local storage (on LPC1768 for instance) and from SD cards. It also has a caching facility which is particularly useful for serving files from local storage.
The server can be run in the main() thread (and has a sub-2ms response time if this is done) or in a mbed-rtos thread which increases the response time to (a still respectable) 30ms or so.
The latest project that uses this is here - https://developer.mbed.org/users/Bobty/code/SpideyWallWeb/
int main (void)
{
// Ethernet interface
EthernetInterface::init();
// Connect ethernet
EthernetInterface::connect();
// Init the web server
pc.printf("Starting web server\r\n");
char* baseWebFolder = "/sd/"; // should be /sd/ for SDcard files - not used for local file system
RdWebServer webServer;
// Add commands to handle the home page and favicon
webServer.addCommand("", RdWebServerCmdDef::CMD_LOCALFILE, NULL, "index.htm", true);
webServer.addCommand("favicon.ico", RdWebServerCmdDef::CMD_LOCALFILE, NULL, NULL, true);
// Add the lightwall control commands
webServer.addCommand("name", RdWebServerCmdDef::CMD_CALLBACK, &lightwallGetSystemName);
webServer.addCommand("clear", RdWebServerCmdDef::CMD_CALLBACK, &lightwallClear);
webServer.addCommand("rawfill", RdWebServerCmdDef::CMD_CALLBACK, &lightwallRawFill);
webServer.addCommand("fill", RdWebServerCmdDef::CMD_CALLBACK, &lightwallFill);
webServer.addCommand("showleds", RdWebServerCmdDef::CMD_CALLBACK, &lightwallShowLeds);
// Start the server
webServer.init(WEBPORT, &led4, baseWebFolder);
webServer.run();
}
// Get system name - No arguments required
char* lightwallGetSystemName(int method, char*cmdStr, char* argStr, char* msgBuffer, int msgLen,
int contentLen, unsigned char* pPayload, int payloadLen, int splitPayloadPos)
{
// Perform any required actions here ....
// ...
// Return the system name
return systemName;
}
This server was originally based on a Netduino web server from Jasper Schuurmans but has been optimised for speed.
Revision 16:0248bbfdb6c1, committed 2015-05-05
- Comitter:
- Bobty
- Date:
- Tue May 05 22:14:16 2015 +0000
- Parent:
- 15:0865fa4b046a
- Child:
- 17:080f2bed8b36
- Commit message:
- Working on FRDM K64F with two clients ok - browser tends to reopen socket so curl has to wait for timeout but seems to retry ok
Changed in this revision
| RdWebServer.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/RdWebServer.cpp Tue May 05 20:27:33 2015 +0000
+++ b/RdWebServer.cpp Tue May 05 22:14:16 2015 +0000
@@ -56,12 +56,23 @@
if (!_initOk)
return;
+ // The sample web server on MBED site turns off blocking after the accept has happened
+ // I've tried this but it doesn't seem to yield a reliable server
bool blockingOnAccept = true;
bool blockingOnReceive = true;
- int timeoutOnBlocking = 2000;
+
+ // This is the same as the default in the socket.cpp file
+ int timeoutOnBlocking = 1500;
+
+ // Currently tested using close connection after send with a single file which works fine
+ // If closeConnAfterSend is set false then the socket connection remains open and only
+ // one client can access the server at a time
+ // Need to test with the closeConnAfterSend seetting true when trying to download multiple files
+ // for a website as previous experience would indicate that requests might be missed in this scenario
+ // although all other settings may not have been the same
+ bool closeConnAfterSend = true;
+ bool closeConnOnReceiveFail = true;
const char* closeConnStr = "Connection: Close\r\n";
- bool closeConnAfterSend = false;
- bool closeConnOnReceiveFail = true;
// Start accepting connections
while (true)
@@ -101,7 +112,7 @@
}
if (rxLen == 0)
{
- RD_DBG("clientSocketConn.receive() returned %d - ignoring\r\n", rxLen);
+ RD_DBG("clientSocketConn.receive() returned %d - ignoring - is connected %d\r\n", rxLen, clientSocketConn.is_connected());
continue;
}
if (rxLen > HTTPD_MAX_REQ_LENGTH)
@@ -126,7 +137,8 @@
if (closeConnAfterSend)
{
int closeRet = clientSocketConn.close();
- RD_DBG("After send connection close() ret %d\r\n", closeRet);
+ RD_DBG("After send connection close() ret %d is connected %d\r\n", closeRet, clientSocketConn.is_connected());
+ forcedClosed = true;
}
}
}