The library with which to configure a Web Socket Server on a Mbed. This lib was coded by a day at least one year before when this description is written. It will be updated adopting mbed os 5.
WS_SERVER.h@2:ccaae77f91b8, 2018-03-03 (annotated)
- Committer:
- aktk
- Date:
- Sat Mar 03 19:01:29 2018 +0000
- Revision:
- 2:ccaae77f91b8
- Parent:
- 1:73f2f67d1732
Put out the HTTP_SERVER lib and copy&pasted just the handler directory from the lib to this.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aktk | 0:86a479dd1814 | 1 | #ifndef WS_SERVER_H |
aktk | 0:86a479dd1814 | 2 | #define WS_SERVER_H |
aktk | 0:86a479dd1814 | 3 | |
aktk | 0:86a479dd1814 | 4 | #include "mbed.h" |
aktk | 0:86a479dd1814 | 5 | #include "EthernetInterface.h" |
aktk | 0:86a479dd1814 | 6 | #include "base64.h" |
aktk | 0:86a479dd1814 | 7 | #include "sha1.h" |
aktk | 0:86a479dd1814 | 8 | #include "ResponseMessenger.h" |
aktk | 0:86a479dd1814 | 9 | #include "FileHandler.h" |
aktk | 0:86a479dd1814 | 10 | #include "string.h" |
aktk | 0:86a479dd1814 | 11 | #include <stdlib.h> |
aktk | 0:86a479dd1814 | 12 | using namespace std; |
aktk | 0:86a479dd1814 | 13 | |
aktk | 0:86a479dd1814 | 14 | /** Websocket Server class |
aktk | 0:86a479dd1814 | 15 | * |
aktk | 0:86a479dd1814 | 16 | * This is the class to make a mbed a simple HTTP Server. |
aktk | 0:86a479dd1814 | 17 | */ |
aktk | 0:86a479dd1814 | 18 | class WSS |
aktk | 0:86a479dd1814 | 19 | { |
aktk | 0:86a479dd1814 | 20 | public: |
aktk | 0:86a479dd1814 | 21 | /** Constractor |
aktk | 0:86a479dd1814 | 22 | * @bref Default constractor |
aktk | 0:86a479dd1814 | 23 | * RxBufferSize = 1024 (Default) |
aktk | 0:86a479dd1814 | 24 | * PortNumber = 8085 (Default) |
aktk | 0:86a479dd1814 | 25 | */ |
aktk | 0:86a479dd1814 | 26 | WSS(); |
aktk | 0:86a479dd1814 | 27 | /** Constractor |
aktk | 0:86a479dd1814 | 28 | * @param (uint32_t)RxBufferSize is buffer size for recieving data from clients. |
aktk | 0:86a479dd1814 | 29 | * @param (uint32_t)PortNumber is port number of tcp port this server uses.(defulat 8085) |
aktk | 0:86a479dd1814 | 30 | */ |
aktk | 0:86a479dd1814 | 31 | WSS(uint32_t RxBufferSize, uint32_t PortNumber = 8085); |
aktk | 0:86a479dd1814 | 32 | |
aktk | 0:86a479dd1814 | 33 | ~WSS(); |
aktk | 0:86a479dd1814 | 34 | |
aktk | 0:86a479dd1814 | 35 | /** Websocket SERVER Initialization. |
aktk | 0:86a479dd1814 | 36 | * @return result of init() as boolean. |
aktk | 0:86a479dd1814 | 37 | * @retval TRUE SACCESS |
aktk | 0:86a479dd1814 | 38 | * @retval FALSE |
aktk | 0:86a479dd1814 | 39 | */ |
aktk | 0:86a479dd1814 | 40 | bool init(); |
aktk | 0:86a479dd1814 | 41 | |
aktk | 0:86a479dd1814 | 42 | /** Make sure whether the server (actually the tcp server) is listening |
aktk | 0:86a479dd1814 | 43 | * @retyrn whther the server is listening |
aktk | 0:86a479dd1814 | 44 | * @retval TRUE the server is listening. |
aktk | 0:86a479dd1814 | 45 | * @retval FALSE the server is no longer listening. |
aktk | 0:86a479dd1814 | 46 | */ |
aktk | 0:86a479dd1814 | 47 | bool isListening(); |
aktk | 0:86a479dd1814 | 48 | |
aktk | 0:86a479dd1814 | 49 | |
aktk | 0:86a479dd1814 | 50 | /** Run the surver service while listening flag is true. |
aktk | 0:86a479dd1814 | 51 | */ |
aktk | 0:86a479dd1814 | 52 | void run(); |
aktk | 0:86a479dd1814 | 53 | |
aktk | 0:86a479dd1814 | 54 | /** Make sure whether there are any data. |
aktk | 0:86a479dd1814 | 55 | * @return whtere data existing |
aktk | 0:86a479dd1814 | 56 | * @retval TRUE recived data exist |
aktk | 0:86a479dd1814 | 57 | * @retval FALSE no data recived |
aktk | 0:86a479dd1814 | 58 | */ |
aktk | 0:86a479dd1814 | 59 | bool isReadable(); |
aktk | 0:86a479dd1814 | 60 | |
aktk | 0:86a479dd1814 | 61 | /** Read recieved data which are unmasked. |
aktk | 0:86a479dd1814 | 62 | * @return Unmasked octed data |
aktk | 0:86a479dd1814 | 63 | */ |
aktk | 0:86a479dd1814 | 64 | int8_t getRxData(); |
aktk | 0:86a479dd1814 | 65 | |
aktk | 0:86a479dd1814 | 66 | /** Clear Rx Buffer |
aktk | 0:86a479dd1814 | 67 | */ |
aktk | 0:86a479dd1814 | 68 | void discardRxBuffer(); |
aktk | 0:86a479dd1814 | 69 | |
aktk | 0:86a479dd1814 | 70 | |
aktk | 0:86a479dd1814 | 71 | template<typename T> void txMessage(T arg_msg); |
aktk | 0:86a479dd1814 | 72 | void txClosing(int arg_status_code, const char* arg_msg); |
aktk | 0:86a479dd1814 | 73 | void txPing(); |
aktk | 0:86a479dd1814 | 74 | void txPong(); |
aktk | 0:86a479dd1814 | 75 | |
aktk | 0:86a479dd1814 | 76 | private: |
aktk | 0:86a479dd1814 | 77 | // |
aktk | 0:86a479dd1814 | 78 | // Handlers |
aktk | 0:86a479dd1814 | 79 | // |
aktk | 0:86a479dd1814 | 80 | EthernetInterface m_eth; // Eternet |
aktk | 0:86a479dd1814 | 81 | TCPSocketServer m_tcpsvr; // TCP server |
aktk | 0:86a479dd1814 | 82 | TCPSocketConnection m_tcpcon; // TCP server connection clerk |
aktk | 0:86a479dd1814 | 83 | ResponseMessenger m_msger; |
aktk | 0:86a479dd1814 | 84 | // |
aktk | 0:86a479dd1814 | 85 | // Param |
aktk | 0:86a479dd1814 | 86 | // |
aktk | 0:86a479dd1814 | 87 | bool m_ListeningFlag; |
aktk | 0:86a479dd1814 | 88 | bool m_DiscontiuanceFlag; |
aktk | 0:86a479dd1814 | 89 | bool m_EODFlag; |
aktk | 0:86a479dd1814 | 90 | const uint32_t m_RxBufferSize; |
aktk | 1:73f2f67d1732 | 91 | uint8_t* m_RxBuffer; // = new uint8_t(RxBufferSize = 1024); |
aktk | 0:86a479dd1814 | 92 | const uint32_t m_PortNumber; |
aktk | 0:86a479dd1814 | 93 | // bits (not bytes) is commented to right |
aktk | 1:73f2f67d1732 | 94 | uint8_t m_FIN_RSV; //[1bit x4(FIN, RSV1, RSV2, RSV3)] |
aktk | 1:73f2f67d1732 | 95 | uint8_t m_Opcode; //[4bits] |
aktk | 1:73f2f67d1732 | 96 | uint8_t m_Mask; //[1bit] |
aktk | 1:73f2f67d1732 | 97 | uint64_t m_PayloadLen; //=x+y bytes[7 xor 7\16 xor 7\64 bits] |
aktk | 1:73f2f67d1732 | 98 | uint8_t m_Masking_key[4]; //[0-4 bytes] |
aktk | 1:73f2f67d1732 | 99 | uint8_t* m_PayloadData; //[x+y bytes] |
aktk | 1:73f2f67d1732 | 100 | uint32_t m_ExtensionLen; //=x bytes |
aktk | 1:73f2f67d1732 | 101 | uint64_t m_ApplicationLen; //=y bytes |
aktk | 1:73f2f67d1732 | 102 | uint64_t m_restAppDataLen; //=m_ApplicationLen - already read data len |
aktk | 0:86a479dd1814 | 103 | enum OpCode{ |
aktk | 0:86a479dd1814 | 104 | cont = 0, txt = 1, bin = 2, close = 8, ping = 9, pong = 10 |
aktk | 0:86a479dd1814 | 105 | }; |
aktk | 0:86a479dd1814 | 106 | OpCode m_FrameOpcode; //data mode of payload |
aktk | 0:86a479dd1814 | 107 | |
aktk | 0:86a479dd1814 | 108 | // |
aktk | 0:86a479dd1814 | 109 | // Functions |
aktk | 0:86a479dd1814 | 110 | // |
aktk | 0:86a479dd1814 | 111 | bool handshake(); |
aktk | 0:86a479dd1814 | 112 | //bool startCom(); |
aktk | 0:86a479dd1814 | 113 | int8_t rxMessage(); |
aktk | 0:86a479dd1814 | 114 | }; |
aktk | 0:86a479dd1814 | 115 | |
aktk | 0:86a479dd1814 | 116 | #endif |