Some quick code to use UDP-only (no TCP) with mBed. Echos received packets and sends packets when a button is pressed

Dependencies:   mbed

Committer:
pehrhovey
Date:
Sun Mar 14 00:54:12 2010 +0000
Revision:
0:a548a085de55

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pehrhovey 0:a548a085de55 1 #ifndef HTTPSTATICPAGE_H
pehrhovey 0:a548a085de55 2 #define HTTPSTATICPAGE_H
pehrhovey 0:a548a085de55 3
pehrhovey 0:a548a085de55 4 #include "HTTPServer.h"
pehrhovey 0:a548a085de55 5
pehrhovey 0:a548a085de55 6 /**
pehrhovey 0:a548a085de55 7 * A datastorage helper for the HTTPStaticPage class.
pehrhovey 0:a548a085de55 8 * Stores only the left upload size.
pehrhovey 0:a548a085de55 9 */
pehrhovey 0:a548a085de55 10 class HTTPStaticPageData : public HTTPData {
pehrhovey 0:a548a085de55 11 public: int left;
pehrhovey 0:a548a085de55 12 };
pehrhovey 0:a548a085de55 13
pehrhovey 0:a548a085de55 14 /**
pehrhovey 0:a548a085de55 15 * This class Provide a Handler to send Static HTTP Pages from the bin file.
pehrhovey 0:a548a085de55 16 */
pehrhovey 0:a548a085de55 17 class HTTPStaticPage : public HTTPHandler {
pehrhovey 0:a548a085de55 18 public:
pehrhovey 0:a548a085de55 19 /**
pehrhovey 0:a548a085de55 20 * Constructer takes the pagename and the pagedata.
pehrhovey 0:a548a085de55 21 * As long as the pagedata is NULL terminated you have not to tell the data length.
pehrhovey 0:a548a085de55 22 * But if you want to send binary data you should tell us the size.
pehrhovey 0:a548a085de55 23 */
pehrhovey 0:a548a085de55 24 HTTPStaticPage(const char *path, const char *page, int length = 0)
pehrhovey 0:a548a085de55 25 : HTTPHandler(path), _page(page) {
pehrhovey 0:a548a085de55 26 _size = (length)?length:strlen(_page);
pehrhovey 0:a548a085de55 27 }
pehrhovey 0:a548a085de55 28
pehrhovey 0:a548a085de55 29 HTTPStaticPage(HTTPServer *server, const char *path, const char *page, int length = 0)
pehrhovey 0:a548a085de55 30 : HTTPHandler(path), _page(page) {
pehrhovey 0:a548a085de55 31 _size = (length)?length:strlen(_page);
pehrhovey 0:a548a085de55 32 server->addHandler(this);
pehrhovey 0:a548a085de55 33 }
pehrhovey 0:a548a085de55 34
pehrhovey 0:a548a085de55 35 private:
pehrhovey 0:a548a085de55 36 /**
pehrhovey 0:a548a085de55 37 * A this Static Page is requested!
pehrhovey 0:a548a085de55 38 * Prepare a datastorage helper "HTTPStaticPageHelper" to store the left data size.
pehrhovey 0:a548a085de55 39 * And return HTTP_OK
pehrhovey 0:a548a085de55 40 */
pehrhovey 0:a548a085de55 41 virtual HTTPStatus init(HTTPConnection *con) const {
pehrhovey 0:a548a085de55 42 HTTPStaticPageData *data = new HTTPStaticPageData();
pehrhovey 0:a548a085de55 43 con->data = data;
pehrhovey 0:a548a085de55 44 data->left = _size;
pehrhovey 0:a548a085de55 45 con->setLength(_size);
pehrhovey 0:a548a085de55 46 return HTTP_OK;
pehrhovey 0:a548a085de55 47 }
pehrhovey 0:a548a085de55 48
pehrhovey 0:a548a085de55 49 /**
pehrhovey 0:a548a085de55 50 * Send the maximum data out to the client.
pehrhovey 0:a548a085de55 51 * If the file is complete transmitted close connection by returning HTTP_SuccessEnded
pehrhovey 0:a548a085de55 52 */
pehrhovey 0:a548a085de55 53 virtual HTTPHandle send(HTTPConnection *con, int maximum) const {
pehrhovey 0:a548a085de55 54 HTTPStaticPageData *data = static_cast<HTTPStaticPageData *>(con->data);
pehrhovey 0:a548a085de55 55 int len = min(maximum, data->left);
pehrhovey 0:a548a085de55 56 err_t err;
pehrhovey 0:a548a085de55 57
pehrhovey 0:a548a085de55 58 do {
pehrhovey 0:a548a085de55 59 err = con->write((void*)&_page[_size - data->left], len, 1);
pehrhovey 0:a548a085de55 60 if(err == ERR_MEM) {
pehrhovey 0:a548a085de55 61 len >>= 1;
pehrhovey 0:a548a085de55 62 }
pehrhovey 0:a548a085de55 63 } while(err == ERR_MEM && len > 1);
pehrhovey 0:a548a085de55 64 if(err == ERR_OK) {
pehrhovey 0:a548a085de55 65 data->left -= len;
pehrhovey 0:a548a085de55 66 }
pehrhovey 0:a548a085de55 67
pehrhovey 0:a548a085de55 68 return (data->left)? HTTP_Success : HTTP_SuccessEnded;
pehrhovey 0:a548a085de55 69 }
pehrhovey 0:a548a085de55 70
pehrhovey 0:a548a085de55 71 /** Pointer to the page data */
pehrhovey 0:a548a085de55 72 const char *_page;
pehrhovey 0:a548a085de55 73
pehrhovey 0:a548a085de55 74 /** page data size*/
pehrhovey 0:a548a085de55 75 int _size;
pehrhovey 0:a548a085de55 76 };
pehrhovey 0:a548a085de55 77
pehrhovey 0:a548a085de55 78 #endif