Hayato Hiratori / Mbed 2 deprecated WebSocketServerTest

Dependencies:   SNICInterface_mod WebSocketServer mbed-rtos mbed PowerControl C12832

Committer:
flatbird
Date:
Mon Mar 16 18:48:50 2015 +0900
Revision:
10:578778037efb
Parent:
9:774f408b9740
added WebSocketConnection.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
flatbird 0:f7596ed7ab5c 1 #include "WebSocketServer.h"
flatbird 10:578778037efb 2 #include "WebSocketConnection.h"
flatbird 0:f7596ed7ab5c 3
flatbird 10:578778037efb 4 WebSocketServer::WebSocketServer()
flatbird 10:578778037efb 5 {
flatbird 0:f7596ed7ab5c 6 }
flatbird 0:f7596ed7ab5c 7
flatbird 10:578778037efb 8 WebSocketServer::~WebSocketServer()
flatbird 10:578778037efb 9 {
flatbird 0:f7596ed7ab5c 10 }
flatbird 0:f7596ed7ab5c 11
flatbird 10:578778037efb 12 bool WebSocketServer::init(int port)
flatbird 10:578778037efb 13 {
flatbird 1:84af7a219830 14 mTCPSocketServer.set_blocking(true);
flatbird 1:84af7a219830 15
flatbird 1:84af7a219830 16 int ret = mTCPSocketServer.bind(port);
flatbird 1:84af7a219830 17 if (ret != 0) {
flatbird 1:84af7a219830 18 printf("ERROR: Failed to bind %d\r\n", ret);
flatbird 1:84af7a219830 19 return false;
flatbird 1:84af7a219830 20 }
flatbird 1:84af7a219830 21 ret = mTCPSocketServer.listen();
flatbird 1:84af7a219830 22 if (ret != 0) {
flatbird 1:84af7a219830 23 printf("ERROR: Failed to listen %d\r\n", ret);
flatbird 1:84af7a219830 24 return false;
flatbird 1:84af7a219830 25 }
flatbird 1:84af7a219830 26
flatbird 1:84af7a219830 27 return true;
flatbird 1:84af7a219830 28 }
flatbird 1:84af7a219830 29
flatbird 10:578778037efb 30 void WebSocketServer::run()
flatbird 10:578778037efb 31 {
flatbird 10:578778037efb 32 WebSocketConnection connection(this);
flatbird 1:84af7a219830 33
flatbird 1:84af7a219830 34 while (true) {
flatbird 10:578778037efb 35 // printf("accepting\r\n");
flatbird 10:578778037efb 36 int ret = mTCPSocketServer.accept(connection.getTCPSocketConnection());
flatbird 1:84af7a219830 37 if (ret != 0) {
flatbird 1:84af7a219830 38 continue;
flatbird 1:84af7a219830 39 }
flatbird 10:578778037efb 40 connection.run();
flatbird 1:84af7a219830 41 }
flatbird 1:84af7a219830 42 }
flatbird 1:84af7a219830 43
flatbird 10:578778037efb 44 void WebSocketServer::setHandler(const char* path, WebSocketHandler* handler)
flatbird 10:578778037efb 45 {
flatbird 10:578778037efb 46 mHandlers[path] = handler;
flatbird 5:ce3f1dd90068 47 }
flatbird 5:ce3f1dd90068 48
flatbird 10:578778037efb 49 WebSocketHandler* WebSocketServer::getHandler(const char* path)
flatbird 10:578778037efb 50 {
flatbird 10:578778037efb 51 WebSocketHandlerContainer::iterator it;
flatbird 8:6635ca3b5a5c 52
flatbird 10:578778037efb 53 it = mHandlers.find(path);
flatbird 10:578778037efb 54 if (it != mHandlers.end()) {
flatbird 10:578778037efb 55 return it->second;
flatbird 7:6cfe0638b957 56 }
flatbird 10:578778037efb 57 return NULL;
flatbird 1:84af7a219830 58 }
flatbird 1:84af7a219830 59