From 2e1956b49117587e4444944164c61aab2f690bd5 Mon Sep 17 00:00:00 2001 From: Brett Renfer Date: Sun, 22 Apr 2012 20:05:43 -0400 Subject: [PATCH] structure cleanup + addition of ofxLibwebsockets namespace --- .../project.pbxproj | 108 ++++++---- example - client/Project.xcconfig | 3 +- example - client/src/testApp.cpp | 14 +- example - client/src/testApp.h | 14 +- .../project.pbxproj | 108 ++++++---- example - server/Project.xcconfig | 3 +- example - server/src/testApp.cpp | 12 +- example - server/src/testApp.h | 14 +- .../include/ofxLibwebsockets/Client.h | 59 ++++++ .../include/ofxLibwebsockets/Connection.h | 53 +++++ .../include/ofxLibwebsockets/Events.h | 38 ++++ .../include/ofxLibwebsockets/Protocol.h | 76 +++++++ .../include/ofxLibwebsockets/Reactor.h | 68 ++++++ .../include/ofxLibwebsockets/Server.h | 49 +++++ .../include/ofxLibwebsockets/Util.h | 195 ++++++++++++++++++ libs/ofxLibwebsockets/src/Client.cpp | 186 +++++++++++++++++ libs/ofxLibwebsockets/src/Connection.cpp | 106 ++++++++++ libs/ofxLibwebsockets/src/Events.cpp | 12 ++ libs/ofxLibwebsockets/src/Protocol.cpp | 106 ++++++++++ libs/ofxLibwebsockets/src/Reactor.cpp | 145 +++++++++++++ libs/ofxLibwebsockets/src/Server.cpp | 111 ++++++++++ src/ofxLibwebsockets.h | 10 +- src/ofxWebSocketClient.cpp | 184 ----------------- src/ofxWebSocketClient.h | 57 ----- src/ofxWebSocketConnection.cpp | 106 ---------- src/ofxWebSocketConnection.h | 43 ---- src/ofxWebSocketEvents.cpp | 10 - src/ofxWebSocketEvents.h | 27 --- src/ofxWebSocketProtocol.cpp | 113 ---------- src/ofxWebSocketProtocol.h | 65 ------ src/ofxWebSocketReactor.cpp | 151 -------------- src/ofxWebSocketReactor.h | 65 ------ src/ofxWebSocketServer.cpp | 108 ---------- src/ofxWebSocketServer.h | 40 ---- src/ofxWebSocketUtil.h | 192 ----------------- 35 files changed, 1379 insertions(+), 1272 deletions(-) create mode 100644 libs/ofxLibwebsockets/include/ofxLibwebsockets/Client.h create mode 100644 libs/ofxLibwebsockets/include/ofxLibwebsockets/Connection.h create mode 100644 libs/ofxLibwebsockets/include/ofxLibwebsockets/Events.h create mode 100644 libs/ofxLibwebsockets/include/ofxLibwebsockets/Protocol.h create mode 100644 libs/ofxLibwebsockets/include/ofxLibwebsockets/Reactor.h create mode 100644 libs/ofxLibwebsockets/include/ofxLibwebsockets/Server.h create mode 100644 libs/ofxLibwebsockets/include/ofxLibwebsockets/Util.h create mode 100644 libs/ofxLibwebsockets/src/Client.cpp create mode 100644 libs/ofxLibwebsockets/src/Connection.cpp create mode 100644 libs/ofxLibwebsockets/src/Events.cpp create mode 100644 libs/ofxLibwebsockets/src/Protocol.cpp create mode 100644 libs/ofxLibwebsockets/src/Reactor.cpp create mode 100644 libs/ofxLibwebsockets/src/Server.cpp delete mode 100644 src/ofxWebSocketClient.cpp delete mode 100644 src/ofxWebSocketClient.h delete mode 100644 src/ofxWebSocketConnection.cpp delete mode 100644 src/ofxWebSocketConnection.h delete mode 100644 src/ofxWebSocketEvents.cpp delete mode 100644 src/ofxWebSocketEvents.h delete mode 100644 src/ofxWebSocketProtocol.cpp delete mode 100644 src/ofxWebSocketProtocol.h delete mode 100644 src/ofxWebSocketReactor.cpp delete mode 100644 src/ofxWebSocketReactor.h delete mode 100644 src/ofxWebSocketServer.cpp delete mode 100644 src/ofxWebSocketServer.h delete mode 100644 src/ofxWebSocketUtil.h diff --git a/example - client/LibwebsocketClient.xcodeproj/project.pbxproj b/example - client/LibwebsocketClient.xcodeproj/project.pbxproj index ad6957d..1da9c1c 100644 --- a/example - client/LibwebsocketClient.xcodeproj/project.pbxproj +++ b/example - client/LibwebsocketClient.xcodeproj/project.pbxproj @@ -8,12 +8,12 @@ /* Begin PBXBuildFile section */ A9587897153E6DFC0015A6B9 /* jsoncpp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A9587896153E6DFC0015A6B9 /* jsoncpp.cpp */; }; - A9C95A3815360A61000BEEF1 /* ofxWebSocketConnection.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A9C95A2B15360A61000BEEF1 /* ofxWebSocketConnection.cpp */; }; - A9C95A3915360A61000BEEF1 /* ofxWebSocketEvents.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A9C95A2D15360A61000BEEF1 /* ofxWebSocketEvents.cpp */; }; - A9C95A3A15360A61000BEEF1 /* ofxWebSocketProtocol.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A9C95A2F15360A61000BEEF1 /* ofxWebSocketProtocol.cpp */; }; - A9C95A3B15360A61000BEEF1 /* ofxWebSocketServer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A9C95A3115360A61000BEEF1 /* ofxWebSocketServer.cpp */; }; - A9C95A3F1537191B000BEEF1 /* ofxWebSocketClient.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A9C95A3E1537191B000BEEF1 /* ofxWebSocketClient.cpp */; }; - A9C95A4215371C36000BEEF1 /* ofxWebSocketReactor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A9C95A4115371C36000BEEF1 /* ofxWebSocketReactor.cpp */; }; + A98A96F21544D41000D50B3C /* Client.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A98A96EC1544D41000D50B3C /* Client.cpp */; }; + A98A96F31544D41000D50B3C /* Connection.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A98A96ED1544D41000D50B3C /* Connection.cpp */; }; + A98A96F41544D41000D50B3C /* Events.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A98A96EE1544D41000D50B3C /* Events.cpp */; }; + A98A96F51544D41000D50B3C /* Protocol.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A98A96EF1544D41000D50B3C /* Protocol.cpp */; }; + A98A96F61544D41000D50B3C /* Reactor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A98A96F01544D41000D50B3C /* Reactor.cpp */; }; + A98A96F71544D41000D50B3C /* Server.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A98A96F11544D41000D50B3C /* Server.cpp */; }; BBAB23CB13894F3D00AA2426 /* GLUT.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = BBAB23BE13894E4700AA2426 /* GLUT.framework */; }; E4328149138ABC9F0047C5CB /* openFrameworksDebug.a in Frameworks */ = {isa = PBXBuildFile; fileRef = E4328148138ABC890047C5CB /* openFrameworksDebug.a */; }; E45BE97B0E8CC7DD009D7055 /* AGL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E45BE9710E8CC7DD009D7055 /* AGL.framework */; }; @@ -68,21 +68,21 @@ A9587896153E6DFC0015A6B9 /* jsoncpp.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsoncpp.cpp; sourceTree = ""; }; A9587898153E6E640015A6B9 /* json-forwards.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "json-forwards.h"; sourceTree = ""; }; A9587899153FB2880015A6B9 /* ofxLibwebsockets.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ofxLibwebsockets.h; sourceTree = ""; }; + A98A96E41544D41000D50B3C /* Client.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Client.h; sourceTree = ""; }; + A98A96E51544D41000D50B3C /* Connection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Connection.h; sourceTree = ""; }; + A98A96E61544D41000D50B3C /* Events.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Events.h; sourceTree = ""; }; + A98A96E71544D41000D50B3C /* Protocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Protocol.h; sourceTree = ""; }; + A98A96E81544D41000D50B3C /* Reactor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Reactor.h; sourceTree = ""; }; + A98A96E91544D41000D50B3C /* Server.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Server.h; sourceTree = ""; }; + A98A96EA1544D41000D50B3C /* Util.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Util.h; sourceTree = ""; }; + A98A96EC1544D41000D50B3C /* Client.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Client.cpp; sourceTree = ""; }; + A98A96ED1544D41000D50B3C /* Connection.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Connection.cpp; sourceTree = ""; }; + A98A96EE1544D41000D50B3C /* Events.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Events.cpp; sourceTree = ""; }; + A98A96EF1544D41000D50B3C /* Protocol.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Protocol.cpp; sourceTree = ""; }; + A98A96F01544D41000D50B3C /* Reactor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Reactor.cpp; sourceTree = ""; }; + A98A96F11544D41000D50B3C /* Server.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Server.cpp; sourceTree = ""; }; A9C95A2215360A61000BEEF1 /* COPYING */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = COPYING; sourceTree = ""; }; A9C95A2415360A61000BEEF1 /* libwebsockets.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = libwebsockets.h; sourceTree = ""; }; - A9C95A2B15360A61000BEEF1 /* ofxWebSocketConnection.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ofxWebSocketConnection.cpp; sourceTree = ""; }; - A9C95A2C15360A61000BEEF1 /* ofxWebSocketConnection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ofxWebSocketConnection.h; sourceTree = ""; }; - A9C95A2D15360A61000BEEF1 /* ofxWebSocketEvents.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ofxWebSocketEvents.cpp; sourceTree = ""; }; - A9C95A2E15360A61000BEEF1 /* ofxWebSocketEvents.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ofxWebSocketEvents.h; sourceTree = ""; }; - A9C95A2F15360A61000BEEF1 /* ofxWebSocketProtocol.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ofxWebSocketProtocol.cpp; sourceTree = ""; }; - A9C95A3015360A61000BEEF1 /* ofxWebSocketProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ofxWebSocketProtocol.h; sourceTree = ""; }; - A9C95A3115360A61000BEEF1 /* ofxWebSocketServer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ofxWebSocketServer.cpp; sourceTree = ""; }; - A9C95A3215360A61000BEEF1 /* ofxWebSocketServer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ofxWebSocketServer.h; sourceTree = ""; }; - A9C95A3C15360ABB000BEEF1 /* ofxWebSocketClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ofxWebSocketClient.h; sourceTree = ""; }; - A9C95A3D153613BC000BEEF1 /* ofxWebSocketUtil.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ofxWebSocketUtil.h; sourceTree = ""; }; - A9C95A3E1537191B000BEEF1 /* ofxWebSocketClient.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ofxWebSocketClient.cpp; sourceTree = ""; }; - A9C95A4015371B4E000BEEF1 /* ofxWebSocketReactor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ofxWebSocketReactor.h; sourceTree = ""; }; - A9C95A4115371C36000BEEF1 /* ofxWebSocketReactor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ofxWebSocketReactor.cpp; sourceTree = ""; }; BBAB23BE13894E4700AA2426 /* GLUT.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GLUT.framework; path = ../../../libs/glut/lib/osx/GLUT.framework; sourceTree = ""; }; E4328143138ABC890047C5CB /* openFrameworksLib.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = openFrameworksLib.xcodeproj; path = ../../../libs/openFrameworksCompiled/project/osx/openFrameworksLib.xcodeproj; sourceTree = SOURCE_ROOT; }; E45BE9710E8CC7DD009D7055 /* AGL.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AGL.framework; path = /System/Library/Frameworks/AGL.framework; sourceTree = ""; }; @@ -149,6 +149,50 @@ path = json; sourceTree = ""; }; + A98A96E11544D41000D50B3C /* ofxLibwebsockets */ = { + isa = PBXGroup; + children = ( + A98A96E21544D41000D50B3C /* include */, + A98A96EB1544D41000D50B3C /* src */, + ); + path = ofxLibwebsockets; + sourceTree = ""; + }; + A98A96E21544D41000D50B3C /* include */ = { + isa = PBXGroup; + children = ( + A98A96E31544D41000D50B3C /* ofxLibwebsockets */, + ); + path = include; + sourceTree = ""; + }; + A98A96E31544D41000D50B3C /* ofxLibwebsockets */ = { + isa = PBXGroup; + children = ( + A98A96E41544D41000D50B3C /* Client.h */, + A98A96E51544D41000D50B3C /* Connection.h */, + A98A96E61544D41000D50B3C /* Events.h */, + A98A96E71544D41000D50B3C /* Protocol.h */, + A98A96E81544D41000D50B3C /* Reactor.h */, + A98A96E91544D41000D50B3C /* Server.h */, + A98A96EA1544D41000D50B3C /* Util.h */, + ); + path = ofxLibwebsockets; + sourceTree = ""; + }; + A98A96EB1544D41000D50B3C /* src */ = { + isa = PBXGroup; + children = ( + A98A96EC1544D41000D50B3C /* Client.cpp */, + A98A96ED1544D41000D50B3C /* Connection.cpp */, + A98A96EE1544D41000D50B3C /* Events.cpp */, + A98A96EF1544D41000D50B3C /* Protocol.cpp */, + A98A96F01544D41000D50B3C /* Reactor.cpp */, + A98A96F11544D41000D50B3C /* Server.cpp */, + ); + path = src; + sourceTree = ""; + }; A9C959F115360A60000BEEF1 /* ofxLibwebsockets */ = { isa = PBXGroup; children = ( @@ -162,6 +206,7 @@ A9C95A2015360A60000BEEF1 /* libs */ = { isa = PBXGroup; children = ( + A98A96E11544D41000D50B3C /* ofxLibwebsockets */, A9587892153E6DFC0015A6B9 /* jsoncpp */, A9C95A2115360A60000BEEF1 /* libwebsockets */, ); @@ -189,19 +234,6 @@ isa = PBXGroup; children = ( A9587899153FB2880015A6B9 /* ofxLibwebsockets.h */, - A9C95A3E1537191B000BEEF1 /* ofxWebSocketClient.cpp */, - A9C95A3C15360ABB000BEEF1 /* ofxWebSocketClient.h */, - A9C95A2B15360A61000BEEF1 /* ofxWebSocketConnection.cpp */, - A9C95A2C15360A61000BEEF1 /* ofxWebSocketConnection.h */, - A9C95A2D15360A61000BEEF1 /* ofxWebSocketEvents.cpp */, - A9C95A2E15360A61000BEEF1 /* ofxWebSocketEvents.h */, - A9C95A2F15360A61000BEEF1 /* ofxWebSocketProtocol.cpp */, - A9C95A3015360A61000BEEF1 /* ofxWebSocketProtocol.h */, - A9C95A3115360A61000BEEF1 /* ofxWebSocketServer.cpp */, - A9C95A3215360A61000BEEF1 /* ofxWebSocketServer.h */, - A9C95A3D153613BC000BEEF1 /* ofxWebSocketUtil.h */, - A9C95A4115371C36000BEEF1 /* ofxWebSocketReactor.cpp */, - A9C95A4015371B4E000BEEF1 /* ofxWebSocketReactor.h */, ); path = src; sourceTree = ""; @@ -376,13 +408,13 @@ files = ( E4B69E200A3A1BDC003C02F2 /* main.cpp in Sources */, E4B69E210A3A1BDC003C02F2 /* testApp.cpp in Sources */, - A9C95A3815360A61000BEEF1 /* ofxWebSocketConnection.cpp in Sources */, - A9C95A3915360A61000BEEF1 /* ofxWebSocketEvents.cpp in Sources */, - A9C95A3A15360A61000BEEF1 /* ofxWebSocketProtocol.cpp in Sources */, - A9C95A3B15360A61000BEEF1 /* ofxWebSocketServer.cpp in Sources */, - A9C95A3F1537191B000BEEF1 /* ofxWebSocketClient.cpp in Sources */, - A9C95A4215371C36000BEEF1 /* ofxWebSocketReactor.cpp in Sources */, A9587897153E6DFC0015A6B9 /* jsoncpp.cpp in Sources */, + A98A96F21544D41000D50B3C /* Client.cpp in Sources */, + A98A96F31544D41000D50B3C /* Connection.cpp in Sources */, + A98A96F41544D41000D50B3C /* Events.cpp in Sources */, + A98A96F51544D41000D50B3C /* Protocol.cpp in Sources */, + A98A96F61544D41000D50B3C /* Reactor.cpp in Sources */, + A98A96F71544D41000D50B3C /* Server.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/example - client/Project.xcconfig b/example - client/Project.xcconfig index 1f093f1..e5ce282 100644 --- a/example - client/Project.xcconfig +++ b/example - client/Project.xcconfig @@ -9,9 +9,10 @@ LIB_SSL = "$(OF_PATH)/addons/ofxLibwebsockets/libs/openssl/lib/osx/libssl.a" LIB_CRYPTO = "$(OF_PATH)/addons/ofxLibwebsockets/libs/openssl/lib/osx/libcrypto.a" SSL_INC = "$(OF_PATH)/addons/ofxLibwebsockets/libs/openssl/" LWS = "$(OF_PATH)/addons/ofxLibwebsockets/libs/libwebsockets/lib/osx/libwebsockets.a" +OFX_LWS_INCLUDE = "$(OF_PATH)/addons/ofxLibwebsockets/libs/ofxLibwebsockets/include" LIBWEBSOCKETS_LIBS = $(LIB_CRYPTO) $(LIB_SSL) $(LWS) LIBWEBSOCKETS_INCLUDES= $(SSL_INC) OTHER_LDFLAGS = $(OF_CORE_LIBS) $(LIBWEBSOCKETS_LIBS) -HEADER_SEARCH_PATHS = $(OF_CORE_HEADERS) $(SSL_INC) +HEADER_SEARCH_PATHS = $(OF_CORE_HEADERS) $(SSL_INC) $(OFX_LWS_INCLUDE) diff --git a/example - client/src/testApp.cpp b/example - client/src/testApp.cpp index 4ee1c11..162f11f 100644 --- a/example - client/src/testApp.cpp +++ b/example - client/src/testApp.cpp @@ -2,8 +2,6 @@ //-------------------------------------------------------------- void testApp::setup(){ - //http://localhost:9091/ - // connect to websocket.org's echo server on default port //bool connected = client.connect( "echo.websocket.org", true ); @@ -24,32 +22,32 @@ void testApp::draw(){ } //-------------------------------------------------------------- -void testApp::onConnect( ofxWebSocketEvent& args ){ +void testApp::onConnect( ofxLibwebsockets::Event& args ){ cout<<"on connected"< messages; // websocket methods - void onConnect( ofxWebSocketEvent& args ); - void onOpen( ofxWebSocketEvent& args ); - void onClose( ofxWebSocketEvent& args ); - void onIdle( ofxWebSocketEvent& args ); - void onMessage( ofxWebSocketEvent& args ); - void onBroadcast( ofxWebSocketEvent& args ); + void onConnect( ofxLibwebsockets::Event& args ); + void onOpen( ofxLibwebsockets::Event& args ); + void onClose( ofxLibwebsockets::Event& args ); + void onIdle( ofxLibwebsockets::Event& args ); + void onMessage( ofxLibwebsockets::Event& args ); + void onBroadcast( ofxLibwebsockets::Event& args ); }; diff --git a/libs/ofxLibwebsockets/include/ofxLibwebsockets/Client.h b/libs/ofxLibwebsockets/include/ofxLibwebsockets/Client.h new file mode 100644 index 0000000..17ac50e --- /dev/null +++ b/libs/ofxLibwebsockets/include/ofxLibwebsockets/Client.h @@ -0,0 +1,59 @@ +// +// ofxLibwebsockets::Client.h +// ofxLibwebsockets +// +// Created by Brett Renfer on 4/11/12. +// Copyright (c) 2012 Robotconscience. All rights reserved. +// + +#pragma once + +#include +#include "ofxLibwebsockets/Reactor.h" + +namespace ofxLibwebsockets { + + class Client : public Reactor { + friend class Protocol; + public: + + Client(); + + bool connect ( string _address = "localhost", bool bUseSSL=false ); + bool connect ( string _address, int _port, bool bUseSSL=false ); + bool connect ( string _address, int _port, bool bUseSSL, string _channel ); + + // this is ugly for now, should be a smarter way? + bool connect ( string _address, int _port, bool bUseSSL, string _channel, string protocol ); + + void onClose( Event& args ); + void close(); + + void send( string message ); + + template + void addListener(T * app){ + ofAddListener( clientProtocol.onconnectEvent, app, &T::onConnect); + ofAddListener( clientProtocol.onopenEvent, app, &T::onOpen); + ofAddListener( clientProtocol.oncloseEvent, app, &T::onClose); + ofAddListener( clientProtocol.onidleEvent, app, &T::onIdle); + ofAddListener( clientProtocol.onmessageEvent, app, &T::onMessage); + ofAddListener( clientProtocol.onbroadcastEvent, app, &T::onBroadcast); + } + + Connection * getConnection(){ + return connection; + } + + private: + + Connection * connection; + + struct libwebsocket * lwsconnection; + void threadedFunction(); + + //wrap protocol + Protocol clientProtocol; + + }; +}; \ No newline at end of file diff --git a/libs/ofxLibwebsockets/include/ofxLibwebsockets/Connection.h b/libs/ofxLibwebsockets/include/ofxLibwebsockets/Connection.h new file mode 100644 index 0000000..f311499 --- /dev/null +++ b/libs/ofxLibwebsockets/include/ofxLibwebsockets/Connection.h @@ -0,0 +1,53 @@ +// +// ofxLibwebsockets::Connection.h +// ofxLibwebsockets +// +// Created by Brett Renfer on 4/11/12. +// Copyright (c) 2012 Robotconscience. All rights reserved. +// + +#pragma once + +#include + +#include +#include + +namespace ofxLibwebsockets { + + class Reactor; + class Protocol; + + class Session { + public: + }; + + class Connection { + friend class Reactor; + public: + Connection(); + + Connection(Reactor* const _reactor=NULL, Protocol* const _protocol=NULL, const bool supportsBinary=false); + + ~Connection(); + void close(); + void send(const std::string& message); + const std::string recv(const std::string& message); + + libwebsocket* ws; + Reactor* reactor; + Protocol* protocol; + + protected: + Session* session; + + bool binary; + bool supportsBinary; + int buffersize; + unsigned char* buf; + int bufsize; + //std::vector buf; + }; + + +} \ No newline at end of file diff --git a/libs/ofxLibwebsockets/include/ofxLibwebsockets/Events.h b/libs/ofxLibwebsockets/include/ofxLibwebsockets/Events.h new file mode 100644 index 0000000..2a8fc90 --- /dev/null +++ b/libs/ofxLibwebsockets/include/ofxLibwebsockets/Events.h @@ -0,0 +1,38 @@ +// +// ofxLibwebsockets::Events.h +// ofxLibwebsockets +// +// Created by Brett Renfer on 4/11/12. +// Copyright (c) 2012 Robotconscience. All rights reserved. +// + +#pragma once + +#include +#include "json.h" + +namespace ofxLibwebsockets { + + class Connection; + + class Event { + public: + Event(Connection& _conn, std::string& _message); + + Connection& conn; + std::string& message; + Json::Value json; + }; +}; + +/* + class _Events { + public: + ofEvent onConnect; + ofEvent onOpen; + ofEvent onClose; + ofEvent onIdle; + ofEvent onMessage; + ofEvent onBroadcast; + }; + extern _ofxWebSocketEvents ofxWebSocketEvents;*/ \ No newline at end of file diff --git a/libs/ofxLibwebsockets/include/ofxLibwebsockets/Protocol.h b/libs/ofxLibwebsockets/include/ofxLibwebsockets/Protocol.h new file mode 100644 index 0000000..b884778 --- /dev/null +++ b/libs/ofxLibwebsockets/include/ofxLibwebsockets/Protocol.h @@ -0,0 +1,76 @@ +// +// ofxLibwebsockets::Protocol.h +// ofxLibwebsockets +// +// Created by Brett Renfer on 4/11/12. +// Copyright (c) 2012 Robotconscience. All rights reserved. +// + +#pragma once + +#include +#include + +#include "ofMain.h" +#include "ofxLibwebsockets/Events.h" + +namespace ofxLibwebsockets { + + class Reactor; + + #include "json.h" + + class Protocol + { + friend class Reactor; + friend class Server; + friend class Client; + + public: + Protocol(); + ~Protocol(); + + virtual bool allowClient(const std::string name, + const std::string ip) const; + + void broadcast(const std::string& message); + + unsigned int idx; + bool binary; + + protected: + virtual void execute() {} + + virtual void onconnect (Event& args); + virtual void onopen (Event& args); + virtual void onclose (Event& args); + virtual void onidle (Event& args); + virtual void onmessage (Event& args); + virtual void onbroadcast(Event& args); + + ofEvent onconnectEvent; + ofEvent onopenEvent; + ofEvent oncloseEvent; + ofEvent onidleEvent; + ofEvent onmessageEvent; + ofEvent onbroadcastEvent; + + bool defaultAllowPolicy; + std::map allowRules; + + Reactor* reactor; + + private: + Json::Reader reader; + + void _onconnect (Event& args); + void _onopen (Event& args); + void _onclose (Event& args); + void _onidle (Event& args); + void _onmessage (Event& args); + void _onbroadcast (Event& args); + + bool _allowClient(const std::string name, + const std::string ip) const; + }; +}; \ No newline at end of file diff --git a/libs/ofxLibwebsockets/include/ofxLibwebsockets/Reactor.h b/libs/ofxLibwebsockets/include/ofxLibwebsockets/Reactor.h new file mode 100644 index 0000000..253c53f --- /dev/null +++ b/libs/ofxLibwebsockets/include/ofxLibwebsockets/Reactor.h @@ -0,0 +1,68 @@ +// +// ofxLibwebsockets::Reactor.h +// ofxLibwebsockets +// +// Created by Brett Renfer on 4/11/12. +// Copyright (c) 2012 Robotconscience. All rights reserved. +// + +#pragma once + +#include "ofThread.h" +#include "ofEvents.h" +#include +#include "ofxLibwebsockets/Protocol.h" +#include "ofxLibwebsockets/Connection.h" + +namespace ofxLibwebsockets { + + class Reactor : public ofThread { + friend class Protocol; + + public: + Reactor(); + ~Reactor(); + + void close(Connection* const conn); + + void exit(); + void registerProtocol(const std::string& name, Protocol& protocol); + + Protocol* const protocol(const unsigned int idx); + + //private: + unsigned int _allow(Protocol* const protocol, const long fd); + + unsigned int _notify(Connection* const conn, + enum libwebsocket_callback_reasons const reason, + const char* const _message, + const unsigned int len); + + unsigned int _http(struct libwebsocket *ws, + const char* const url); + + std::string document_root; + std::vector > protocols; + + struct libwebsocket_context * getContext(); + + struct pollfd pollfds[100]; + int count_pollfds; + + protected: + unsigned int waitMillis; + std::string interface; + + virtual void threadedFunction(){}; + + string address; + string channel; + int port; + + struct libwebsocket_context *context; + + std::vector lws_protocols; + }; + + static vector reactors; +}; \ No newline at end of file diff --git a/libs/ofxLibwebsockets/include/ofxLibwebsockets/Server.h b/libs/ofxLibwebsockets/include/ofxLibwebsockets/Server.h new file mode 100644 index 0000000..f06fafb --- /dev/null +++ b/libs/ofxLibwebsockets/include/ofxLibwebsockets/Server.h @@ -0,0 +1,49 @@ +// +// ofxLibwebsockets::Server.h +// ofxLibwebsockets +// +// Created by Brett Renfer on 4/11/12. +// Copyright (c) 2012 Robotconscience. All rights reserved. +// + +#pragma once + +#include "ofThread.h" +#include "ofEvents.h" + +#include + +#include "ofxLibwebsockets/Reactor.h" + +namespace ofxLibwebsockets { + + class Connection; + class Protocol; + + class Server : public Reactor { + friend class Protocol; + + public: + Server(); + + bool setup( const short _port=80 ); + bool setup( const short _port, string protocol, bool bAllowAllProtocols = true ); + + template + void addListener(T * app){ + ofAddListener( serverProtocol.onconnectEvent, app, &T::onConnect); + ofAddListener( serverProtocol.onopenEvent, app, &T::onOpen); + ofAddListener( serverProtocol.oncloseEvent, app, &T::onClose); + ofAddListener( serverProtocol.onidleEvent, app, &T::onIdle); + ofAddListener( serverProtocol.onmessageEvent, app, &T::onMessage); + ofAddListener( serverProtocol.onbroadcastEvent, app, &T::onBroadcast); + } + + protected: + std::string interface; + + private: + Protocol serverProtocol; + void threadedFunction(); + }; +}; \ No newline at end of file diff --git a/libs/ofxLibwebsockets/include/ofxLibwebsockets/Util.h b/libs/ofxLibwebsockets/include/ofxLibwebsockets/Util.h new file mode 100644 index 0000000..d82d9ce --- /dev/null +++ b/libs/ofxLibwebsockets/include/ofxLibwebsockets/Util.h @@ -0,0 +1,195 @@ +// +// ofxLibwebsockets::Util.h +// ofxLibwebsockets +// +// Created by Brett Renfer on 4/11/12. +// Copyright (c) 2012 Robotconscience. All rights reserved. +// + +#pragma once + +#include +#include "ofxLibwebsockets/Connection.h" +#include "ofxLibwebsockets/Reactor.h" +#include "ofxLibwebsockets/Client.h" +#include "ofxLibwebsockets/Server.h" + +namespace ofxLibwebsockets { + + class Client; + class Server; + + // CLIENT CALLBACK + + static int lws_client_callback(struct libwebsocket_context* context, struct libwebsocket *ws, enum libwebsocket_callback_reasons reason, void *user, void *data, size_t len){ + const struct libwebsocket_protocols* lws_protocol = (ws == NULL ? NULL : libwebsockets_get_protocol(ws)); + int idx = lws_protocol? lws_protocol->protocol_index : 0; + + Connection* conn; + + Connection** conn_ptr = (Connection**)user; + Reactor* reactor = NULL; + Protocol* protocol; + + for (int i=0; igetContext() == context){ + reactor = reactors[i]; + protocol = reactor->protocol(idx-1); + conn = ((Client*) reactor)->getConnection(); + break; + } else { + } + } + + if (reason == LWS_CALLBACK_CLIENT_ESTABLISHED){ + } else if (reason == LWS_CALLBACK_CLOSED){ + } + + switch (reason) + { + case LWS_CALLBACK_CONFIRM_EXTENSION_OKAY: + return 0; + + case LWS_CALLBACK_FILTER_NETWORK_CONNECTION: + if (protocol != NULL){ + return reactor->_allow(protocol, (int)(long)user)? 0 : 1; + } else { + return 0; + } + case LWS_CALLBACK_HTTP: + if ( reactor != NULL){ + return reactor->_http(ws, (char*)data); + } else { + return 0; + } + case LWS_CALLBACK_CLIENT_ESTABLISHED: + case LWS_CALLBACK_CLIENT_WRITEABLE: + case LWS_CALLBACK_CLOSED: + case LWS_CALLBACK_CLIENT_RECEIVE: + if (reactor != NULL){ + //conn = *(Connection**)user; + if (conn && conn->ws != ws) conn->ws = ws; + return reactor->_notify(conn, reason, (char*)data, len); + } else { + return 0; + } + default: + return 0; + } + + return 1; // FAIL (e.g. unhandled case/break in switch) + } + + // SERVER CALLBACK + + static int lws_callback(struct libwebsocket_context* context, struct libwebsocket *ws, enum libwebsocket_callback_reasons reason, void *user, void *data, size_t len){ + const struct libwebsocket_protocols* lws_protocol = (ws == NULL ? NULL : libwebsockets_get_protocol(ws)); + int idx = lws_protocol? lws_protocol->protocol_index : 0; + + bool bAllowAllProtocls = (ws != NULL ? lws_protocol == NULL : false); + + Connection* conn; + Connection** conn_ptr = (Connection**)user; + Server* reactor = NULL; + Protocol* protocol = NULL; + + for (int i=0; igetContext() == context){ + reactor = (Server*) reactors[i]; + protocol = reactor->protocol( (idx > 0 ? idx-1 : 0) ); + break; + } else { + } + } + + if ( bAllowAllProtocls && reactor != NULL ){ + protocol = reactor->protocols[0].second; + } + + if (reason == LWS_CALLBACK_ESTABLISHED){ + cout<<"yes"<protocols[0].second; + //conn = new Connection( reactor, protocol ); + return reactor->_allow(protocol, (int)(long)user)? 0 : 1; + } + return reactor->_allow(protocol, (int)(long)user)? 0 : 1; + } else { + return 1; + } + + case LWS_CALLBACK_HTTP: + return reactor->_http(ws, (char*)data); + + case LWS_CALLBACK_ESTABLISHED: + case LWS_CALLBACK_CLOSED: + case LWS_CALLBACK_SERVER_WRITEABLE: + case LWS_CALLBACK_RECEIVE: + case LWS_CALLBACK_BROADCAST: + conn = *(Connection**)user; + if (conn && conn->ws != ws) conn->ws = ws; + if (reactor){ + return reactor->_notify(conn, reason, (char*)data, len); + } else { + return 0; + } + + default: + return 0; + } + + return 1; // FAIL (e.g. unhandled case/break in switch) + } + + static void dump_handshake_info(struct lws_tokens *lwst) + { + int n; + static const char *token_names[WSI_TOKEN_COUNT] = { + "GET URI", + "Host", + "Connection", + "key 1", + "key 2", + "Protocol", + "Upgrade", + "Origin", + "Draft", + "Challenge", + + /* new for 04 */ + "Key", + "Version", + "Sworigin", + + /* new for 05 */ + "Extensions", + + /* client receives these */ + "Accept", + "Nonce", + "Http", + "MuxURL", + }; + + for (n = 0; n < WSI_TOKEN_COUNT; n++) { + if (lwst[n].token == NULL) + continue; + + fprintf(stderr, " %s = %s\n", token_names[n], lwst[n].token); + } + } +} \ No newline at end of file diff --git a/libs/ofxLibwebsockets/src/Client.cpp b/libs/ofxLibwebsockets/src/Client.cpp new file mode 100644 index 0000000..5fdedad --- /dev/null +++ b/libs/ofxLibwebsockets/src/Client.cpp @@ -0,0 +1,186 @@ +// +// Client.cpp +// ofxLibwebsockets +// +// Created by Brett Renfer on 4/11/12. +// Copyright (c) 2012 Robotconscience. All rights reserved. +// + +#include "ofxLibwebsockets/Client.h" +#include "ofxLibwebsockets/Util.h" + +namespace ofxLibwebsockets { + + Client::Client(){ + context = NULL; + waitMillis = 1000; + count_pollfds = 0; + reactors.push_back(this); + ofAddListener( clientProtocol.oncloseEvent, this, &Client::onClose); + } + + //-------------------------------------------------------------- + bool Client::connect ( string _address, bool bUseSSL ){ + connect( _address, (bUseSSL ? 443 : 80), bUseSSL, "/" ); + } + + //-------------------------------------------------------------- + bool Client::connect ( string _address, int _port, bool bUseSSL ){ + connect( _address, _port, bUseSSL, "/" ); + } + + //-------------------------------------------------------------- + bool Client::connect ( string _address, int _port, bool bUseSSL, string _channel ){ + ofLog( OF_LOG_VERBOSE, "connect: "+_address+":"+ofToString(_port)+_channel+":"+ofToString(bUseSSL) ); + address = _address; + port = _port; + channel = _channel; + + // set up default protocols + struct libwebsocket_protocols null_protocol = { NULL, NULL, 0 }; + + // should this just be http? channel seems to just be path related? + registerProtocol( _channel, clientProtocol ); + + lws_protocols.clear(); + for (int i=0; iws = lwsconnection; + + std::cerr << "client connection success" << std::endl; + startThread(true, false); // blocking, non-verbose + return true; + } + } + } + + //-------------------------------------------------------------- + bool Client::connect ( string _address, int _port, bool bUseSSL, string _channel, string protocol ){ + address = _address; + port = _port; + channel = _channel; + + // set up default protocols + struct libwebsocket_protocols null_protocol = { NULL, NULL, 0 }; + + registerProtocol( protocol, clientProtocol ); + + lws_protocols.clear(); + for (int i=0; iws = lwsconnection; + + std::cerr << "client connection success" << std::endl; + startThread(true, false); // blocking, non-verbose + return true; + } + } + } + + //-------------------------------------------------------------- + void Client::close(){ + if (isThreadRunning()){ + stopThread(); + } + if ( context != NULL){ + libwebsocket_close_and_free_session( context, lwsconnection, LWS_CLOSE_STATUS_NORMAL); + libwebsocket_context_destroy( context ); + context = NULL; + lwsconnection = NULL; + delete connection; + connection = NULL; + } + } + + + //-------------------------------------------------------------- + void Client::onClose( Event& args ){ + close(); + } + + //-------------------------------------------------------------- + void Client::send( string message ){ + if ( connection != NULL){ + connection->send( message ); + } + } + + //-------------------------------------------------------------- + void Client::threadedFunction(){ + while ( isThreadRunning() ){ + for (int i=0; iexecute(); + //unlock(); + } + } + if (context != NULL && lwsconnection != NULL){ + //libwebsocket_callback_on_writable(context, lwsconnection); + libwebsocket_service(context, waitMillis); + } else { + stopThread(); + close(); + } + } + } +} \ No newline at end of file diff --git a/libs/ofxLibwebsockets/src/Connection.cpp b/libs/ofxLibwebsockets/src/Connection.cpp new file mode 100644 index 0000000..4a3930e --- /dev/null +++ b/libs/ofxLibwebsockets/src/Connection.cpp @@ -0,0 +1,106 @@ +#include "ofxLibwebsockets/Connection.h" +#include "ofxLibwebsockets/Reactor.h" +#include "ofxLibwebsockets/Protocol.h" + +namespace ofxLibwebsockets { + + //-------------------------------------------------------------- + Connection::Connection(){ + reactor = NULL; + protocol = NULL; + } + + //-------------------------------------------------------------- + Connection::Connection(Reactor* const _reactor, Protocol* const _protocol, const bool _supportsBinary) + : reactor(_reactor) + , protocol(_protocol) + , ws(NULL) + , session(NULL) + , supportsBinary(_supportsBinary) + //, buf(LWS_SEND_BUFFER_PRE_PADDING+1024+LWS_SEND_BUFFER_POST_PADDING) + { + if (_protocol != NULL){ + binary = _protocol->binary; + bufsize = 1024; + buf = (unsigned char*)calloc(LWS_SEND_BUFFER_PRE_PADDING+bufsize+LWS_SEND_BUFFER_POST_PADDING, sizeof(unsigned char)); + } + + } + + //-------------------------------------------------------------- + Connection::~Connection(){ + delete buf; + } + //-------------------------------------------------------------- + void + Connection::close() + { + if (reactor != NULL) + reactor->close(this); + + } + + //-------------------------------------------------------------- + void + Connection::send(const std::string& message) + { + int n = 0; + if(message.size() > 4096){ + return; + } + if(message.size() > bufsize){ + bufsize = bufsize+1024; + buf = (unsigned char*)realloc(buf, bufsize + LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING*sizeof(unsigned char)); + cout << "Connection -- received large message, resizing buffer to " << bufsize << endl; + } + unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING]; + + if (binary) + { + //TODO: when libwebsockets has an API supporting something this, we should use it + if (supportsBinary) + { + memcpy(p, message.c_str(), message.size()); + n = libwebsocket_write(ws, p, message.size(), LWS_WRITE_BINARY); + } + else { + int encoded_len; + //encoded_len = b64_encode_string(message.c_str(), message.size(), (char*)p, buf.size()); + cout<<"encode "< 0){ + n = libwebsocket_write(ws, p, encoded_len, LWS_WRITE_TEXT); + } + } + } + else { + memcpy(p, message.c_str(), message.size()); + n = libwebsocket_write(ws, p, message.size(), LWS_WRITE_TEXT); + } + + if (n < 0) + std::cout << "ERROR writing to socket" << std::endl; + } + + //-------------------------------------------------------------- + const std::string + Connection::recv(const std::string& message) + { + std::string decoded = message; + + //TODO: when libwebsockets has an API + // to detect binary support, we should use it + if (binary && !supportsBinary) + { + //TODO: libwebsockets base64 decode is broken @2011-06-19 + //len = lws_b64_decode_string(message, decoded, len); + int decoded_len = lws_b64_decode_string(message.c_str(), &decoded[0], message.size()); + decoded.resize(decoded_len); + } + + return decoded; + } +} + + diff --git a/libs/ofxLibwebsockets/src/Events.cpp b/libs/ofxLibwebsockets/src/Events.cpp new file mode 100644 index 0000000..e1cd721 --- /dev/null +++ b/libs/ofxLibwebsockets/src/Events.cpp @@ -0,0 +1,12 @@ +#include "ofxLibwebsockets/Events.h" + +namespace ofxLibwebsockets { + + //-------------------------------------------------------------- + Event::Event(Connection& _conn, std::string& _message) + : conn(_conn) + , message(_message) + {} + + //_Events ofxWebSocketEvents; +} \ No newline at end of file diff --git a/libs/ofxLibwebsockets/src/Protocol.cpp b/libs/ofxLibwebsockets/src/Protocol.cpp new file mode 100644 index 0000000..c1db5b9 --- /dev/null +++ b/libs/ofxLibwebsockets/src/Protocol.cpp @@ -0,0 +1,106 @@ +#include "ofxLibwebsockets/Protocol.h" +#include "ofxLibwebsockets/Reactor.h" + +namespace ofxLibwebsockets { + + //-------------------------------------------------------------- + Protocol::Protocol() + : defaultAllowPolicy(true){ + ofAddListener(onconnectEvent, this, &Protocol::_onconnect); + ofAddListener(onopenEvent, this, &Protocol::_onopen); + ofAddListener(oncloseEvent, this, &Protocol::_onclose); + ofAddListener(onidleEvent, this, &Protocol::_onidle); + ofAddListener(onmessageEvent, this, &Protocol::_onmessage); + ofAddListener(onbroadcastEvent, this, &Protocol::_onbroadcast); + binary = false; + } + + //-------------------------------------------------------------- + Protocol::~Protocol() { + ofRemoveListener(onconnectEvent, this, &Protocol::_onconnect); + ofRemoveListener(onopenEvent, this, &Protocol::_onopen); + ofRemoveListener(oncloseEvent, this, &Protocol::_onclose); + ofRemoveListener(onidleEvent, this, &Protocol::_onidle); + ofRemoveListener(onmessageEvent, this, &Protocol::_onmessage); + ofRemoveListener(onbroadcastEvent, this, &Protocol::_onbroadcast); + binary = false; + } + + //-------------------------------------------------------------- + bool Protocol::_allowClient(const std::string name, const std::string ip) const { + std::map::const_iterator allow_iter; + + allow_iter = allowRules.find(name); + if (allow_iter != allowRules.end()) + return allow_iter->second; + + allow_iter = allowRules.find(ip); + if (allow_iter != allowRules.end()) + return allow_iter->second; + + return allowClient(name, ip); + } + + //-------------------------------------------------------------- + bool Protocol::allowClient(const std::string name, const std::string ip) const { + return defaultAllowPolicy; + } + + //-------------------------------------------------------------- + void Protocol::_onconnect(Event& args){ onconnect(args); } + + void Protocol::onconnect(Event& args){} + + //-------------------------------------------------------------- + void Protocol::_onopen(Event& args){ onopen(args); } + + void Protocol::onopen(Event&args){} + + //-------------------------------------------------------------- + void Protocol::_onclose(Event& args){ onclose(args); } + + void Protocol::onclose(Event&args){} + + //-------------------------------------------------------------- + void Protocol::_onidle(Event& args){ onidle(args); } + + void Protocol::onidle(Event&args){} + + //-------------------------------------------------------------- + void Protocol::_onmessage(Event& args){ + args.message = args.conn.recv(args.message); + args.json = NULL; + + bool parsingSuccessful = reader.parse( args.message, args.json ); + if ( !parsingSuccessful ) + { + // report to the user the failure and their locations in the document. + ofLog( OF_LOG_VERBOSE, "Failed to parse JSON\n"+ reader.getFormatedErrorMessages() ); + args.json = NULL; + } + + //std::cout << args.message << std::endl; + onmessage(args); + } + + void Protocol::onmessage(Event&args){} + + //-------------------------------------------------------------- + void Protocol::_onbroadcast(Event& args){ onbroadcast(args); } + + void Protocol::onbroadcast(Event&args){ args.conn.send(args.message); } + + //-------------------------------------------------------------- + void Protocol::broadcast(const std::string& message){ + std::string buf(LWS_SEND_BUFFER_PRE_PADDING+1024+LWS_SEND_BUFFER_POST_PADDING, 0); + unsigned char *p = (unsigned char*)&buf[LWS_SEND_BUFFER_PRE_PADDING]; + + if (reactor != NULL) + { + memcpy(p, message.c_str(), message.size()); + int n = libwebsockets_broadcast(&reactor->lws_protocols[idx], p, message.size()); + if (n < 0) + fprintf(stderr, "ERROR writing to socket"); + } + } +} diff --git a/libs/ofxLibwebsockets/src/Reactor.cpp b/libs/ofxLibwebsockets/src/Reactor.cpp new file mode 100644 index 0000000..eac32e2 --- /dev/null +++ b/libs/ofxLibwebsockets/src/Reactor.cpp @@ -0,0 +1,145 @@ +// +// Reactor.cpp +// LibwebsocketClient +// +// Created by Brett Renfer on 4/12/12. +// Copyright (c) 2012 Robotconscience. All rights reserved. +// + +#include "ofxLibwebsockets/Reactor.h" + +namespace ofxLibwebsockets { + + //-------------------------------------------------------------- + Reactor::Reactor() + : context(NULL), waitMillis(50), count_pollfds(0){ + reactors.push_back(this); + } + + //-------------------------------------------------------------- + Reactor::~Reactor(){ + exit(); + } + + //-------------------------------------------------------------- + void Reactor::registerProtocol(const std::string& name, Protocol& protocol){ + protocol.idx = protocols.size()+1; // "http" is protocol 0 + protocol.reactor = this; + protocols.push_back(make_pair(name, &protocol)); + } + + //-------------------------------------------------------------- + Protocol* const Reactor::protocol(const unsigned int idx){ + return (idx < protocols.size())? protocols[idx].second : NULL; + } + + //-------------------------------------------------------------- + void Reactor::close(Connection* const conn){ + if (conn != NULL && conn->ws != NULL) + libwebsocket_close_and_free_session(context, conn->ws, LWS_CLOSE_STATUS_NORMAL); + } + + //-------------------------------------------------------------- + void Reactor::exit(){ + if (context != NULL) + { + stopThread(); + libwebsocket_context_destroy(context); + context = NULL; + } + } + + + //-------------------------------------------------------------- + struct libwebsocket_context * Reactor::getContext(){ + return context; + } + + //-------------------------------------------------------------- + unsigned int + Reactor::_allow(Protocol* const protocol, const long fd){ + std::string client_ip(128, 0); + std::string client_name(128, 0); + + libwebsockets_get_peer_addresses((int)fd, + &client_name[0], client_name.size(), + &client_ip[0], client_ip.size()); + return protocol->_allowClient(client_name, client_ip); + } + + //-------------------------------------------------------------- + unsigned int Reactor::_notify(Connection* const conn, + enum libwebsocket_callback_reasons const reason, + const char* const _message, + const unsigned int len){ + if (conn == NULL || conn->protocol == NULL){ + if (conn == NULL){ + cout<<"connection is null"< 0){ + message = std::string(_message, len); + } + + Event args(*conn, message); + + if (reason==LWS_CALLBACK_ESTABLISHED || reason == LWS_CALLBACK_CLIENT_ESTABLISHED){ + ofNotifyEvent(conn->protocol->onopenEvent, args); + } else if (reason==LWS_CALLBACK_CLOSED){ + ofNotifyEvent(conn->protocol->oncloseEvent, args); + } else if (reason==LWS_CALLBACK_SERVER_WRITEABLE){ + ofNotifyEvent(conn->protocol->onidleEvent, args); + } else if (reason==LWS_CALLBACK_BROADCAST){ + ofNotifyEvent(conn->protocol->onbroadcastEvent, args); + } else if (reason==LWS_CALLBACK_RECEIVE || reason == LWS_CALLBACK_CLIENT_RECEIVE){ + ofNotifyEvent(conn->protocol->onmessageEvent, args); + } + + return 0; + } + + //-------------------------------------------------------------- + unsigned int Reactor::_http(struct libwebsocket *ws, + const char* const _url){ + std::string url(_url); + if (url == "/") + url = "/index.html"; + + // why does this need to be done? + + if (document_root.empty()) + document_root = "web"; + + if (document_root.at(0) != '/') + document_root = ofToDataPath(document_root, true); + + std::string ext = url.substr(url.find_last_of(".")+1); + std::string file = document_root+url; + std::string mimetype = "text/html"; + + if (ext == "ico") + mimetype = "image/x-icon"; + if (ext == "manifest") + mimetype = "text/cache-manifest"; + if (ext == "swf") + mimetype = "application/x-shockwave-flash"; + if (ext == "js") + mimetype = "application/javascript"; + if (ext == "css") + mimetype = "text/css"; + + if (libwebsockets_serve_http_file(ws, file.c_str(), mimetype.c_str())) + { + std::cerr + << "Failed to send HTTP file " << file << " for " << url + << std::endl; + } else { + cout<<"served file "<execute(); + //unlock(); + } + libwebsocket_service(context, waitMillis); + } + } +} diff --git a/src/ofxLibwebsockets.h b/src/ofxLibwebsockets.h index 2c925db..186b6e6 100644 --- a/src/ofxLibwebsockets.h +++ b/src/ofxLibwebsockets.h @@ -1,7 +1,7 @@ #pragma once -#include "ofxWebSocketUtil.h" -#include "ofxWebSocketProtocol.h" -#include "ofxWebSocketClient.h" -#include "ofxWebSocketServer.h" -#include "ofxWebSocketEvents.h" +#include "ofxLibwebsockets/Util.h" +#include "ofxLibwebsockets/Protocol.h" +#include "ofxLibwebsockets/Client.h" +#include "ofxLibwebsockets/Server.h" +#include "ofxLibwebsockets/Events.h" diff --git a/src/ofxWebSocketClient.cpp b/src/ofxWebSocketClient.cpp deleted file mode 100644 index cd13030..0000000 --- a/src/ofxWebSocketClient.cpp +++ /dev/null @@ -1,184 +0,0 @@ -// -// ofxWebSocketClient.cpp -// LibwebsocketClient -// -// Created by Brett Renfer on 4/12/12. -// Copyright (c) 2012 Robotconscience. All rights reserved. -// - -#include "ofxWebSocketClient.h" -#include "ofxWebSocketUtil.h" - -ofxWebSocketClient::ofxWebSocketClient(){ - context = NULL; - waitMillis = 1000; - count_pollfds = 0; - reactors.push_back(this); - ofAddListener( clientProtocol.oncloseEvent, this, &ofxWebSocketClient::onClose); -} - -//-------------------------------------------------------------- -bool ofxWebSocketClient::connect ( string _address, bool bUseSSL ){ - connect( _address, (bUseSSL ? 443 : 80), bUseSSL, "/" ); -} - -//-------------------------------------------------------------- -bool ofxWebSocketClient::connect ( string _address, int _port, bool bUseSSL ){ - connect( _address, _port, bUseSSL, "/" ); -} - -//-------------------------------------------------------------- -bool ofxWebSocketClient::connect ( string _address, int _port, bool bUseSSL, string _channel ){ - ofLog( OF_LOG_VERBOSE, "connect: "+_address+":"+ofToString(_port)+_channel+":"+ofToString(bUseSSL) ); - address = _address; - port = _port; - channel = _channel; - - // set up default protocols - struct libwebsocket_protocols null_protocol = { NULL, NULL, 0 }; - - // should this just be http? channel seems to just be path related? - registerProtocol( _channel, clientProtocol ); - - lws_protocols.clear(); - for (int i=0; iws = lwsconnection; - - std::cerr << "client connection success" << std::endl; - startThread(true, false); // blocking, non-verbose - return true; - } - } -} - -//-------------------------------------------------------------- -bool ofxWebSocketClient::connect ( string _address, int _port, bool bUseSSL, string _channel, string protocol ){ - address = _address; - port = _port; - channel = _channel; - - // set up default protocols - struct libwebsocket_protocols null_protocol = { NULL, NULL, 0 }; - - registerProtocol( protocol, clientProtocol ); - - lws_protocols.clear(); - for (int i=0; iws = lwsconnection; - - std::cerr << "client connection success" << std::endl; - startThread(true, false); // blocking, non-verbose - return true; - } - } -} - -//-------------------------------------------------------------- -void ofxWebSocketClient::close(){ - if (isThreadRunning()){ - stopThread(); - } - if ( context != NULL){ - libwebsocket_close_and_free_session( context, lwsconnection, LWS_CLOSE_STATUS_NORMAL); - libwebsocket_context_destroy( context ); - context = NULL; - lwsconnection = NULL; - delete connection; - connection = NULL; - } -} - - -//-------------------------------------------------------------- -void ofxWebSocketClient::onClose( ofxWebSocketEvent& args ){ - close(); -} - -//-------------------------------------------------------------- -void ofxWebSocketClient::send( string message ){ - if ( connection != NULL){ - connection->send( message ); - } -} - -//-------------------------------------------------------------- -void ofxWebSocketClient::threadedFunction(){ - while ( isThreadRunning() ){ - for (int i=0; iexecute(); - //unlock(); - } - } - if (context != NULL && lwsconnection != NULL){ - //libwebsocket_callback_on_writable(context, lwsconnection); - libwebsocket_service(context, waitMillis); - } else { - stopThread(); - close(); - } - } - -} \ No newline at end of file diff --git a/src/ofxWebSocketClient.h b/src/ofxWebSocketClient.h deleted file mode 100644 index f302cbb..0000000 --- a/src/ofxWebSocketClient.h +++ /dev/null @@ -1,57 +0,0 @@ -// -// ofxWebSocketClient.h -// LibwebsocketClient -// -// Created by Brett Renfer on 4/11/12. -// Copyright (c) 2012 Robotconscience. All rights reserved. -// - -#pragma once - -#include -#include "ofxWebSocketReactor.h" - -class ofxWebSocketClient; -class ofxWebSocketClient : public ofxWebSocketReactor { - friend class ofxWebSocketProtocol; -public: - - ofxWebSocketClient(); - - bool connect ( string _address = "localhost", bool bUseSSL=false ); - bool connect ( string _address, int _port, bool bUseSSL=false ); - bool connect ( string _address, int _port, bool bUseSSL, string _channel ); - - // this is ugly for now, should be a smarter way? - bool connect ( string _address, int _port, bool bUseSSL, string _channel, string protocol ); - - void onClose( ofxWebSocketEvent& args ); - void close(); - - void send( string message ); - - template - void addListener(T * app){ - ofAddListener( clientProtocol.onconnectEvent, app, &T::onConnect); - ofAddListener( clientProtocol.onopenEvent, app, &T::onOpen); - ofAddListener( clientProtocol.oncloseEvent, app, &T::onClose); - ofAddListener( clientProtocol.onidleEvent, app, &T::onIdle); - ofAddListener( clientProtocol.onmessageEvent, app, &T::onMessage); - ofAddListener( clientProtocol.onbroadcastEvent, app, &T::onBroadcast); - } - - ofxWebSocketConnection * getConnection(){ - return connection; - } - -private: - - ofxWebSocketConnection * connection; - - struct libwebsocket * lwsconnection; - void threadedFunction(); - - //wrap protocol - ofxWebSocketProtocol clientProtocol; - -}; \ No newline at end of file diff --git a/src/ofxWebSocketConnection.cpp b/src/ofxWebSocketConnection.cpp deleted file mode 100644 index bd5fffc..0000000 --- a/src/ofxWebSocketConnection.cpp +++ /dev/null @@ -1,106 +0,0 @@ -#include "ofxWebSocketConnection.h" -#include "ofxWebSocketReactor.h" -#include "ofxWebSocketProtocol.h" - -//-------------------------------------------------------------- -ofxWebSocketConnection::ofxWebSocketConnection(){ - reactor = NULL; - protocol = NULL; -} - -//-------------------------------------------------------------- -ofxWebSocketConnection::ofxWebSocketConnection(ofxWebSocketReactor* const _reactor, - ofxWebSocketProtocol* const _protocol, - const bool _supportsBinary) -: reactor(_reactor) -, protocol(_protocol) -, ws(NULL) -, session(NULL) -, supportsBinary(_supportsBinary) -//, buf(LWS_SEND_BUFFER_PRE_PADDING+1024+LWS_SEND_BUFFER_POST_PADDING) -{ - if (_protocol != NULL){ - binary = _protocol->binary; - bufsize = 1024; - buf = (unsigned char*)calloc(LWS_SEND_BUFFER_PRE_PADDING+bufsize+LWS_SEND_BUFFER_POST_PADDING, sizeof(unsigned char)); - } - -} - -//-------------------------------------------------------------- -ofxWebSocketConnection::~ofxWebSocketConnection(){ - delete buf; -} -//-------------------------------------------------------------- -void -ofxWebSocketConnection::close() -{ - if (reactor != NULL) - reactor->close(this); - -} - -//-------------------------------------------------------------- -void -ofxWebSocketConnection::send(const std::string& message) -{ - int n = 0; - if(message.size() > 4096){ - return; - } - if(message.size() > bufsize){ - bufsize = bufsize+1024; - buf = (unsigned char*)realloc(buf, bufsize + LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING*sizeof(unsigned char)); - cout << "ofxWebSocketConnection -- received large message, resizing buffer to " << bufsize << endl; - } - unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING]; - - if (binary) - { - //TODO: when libwebsockets has an API supporting something this, we should use it - if (supportsBinary) - { - memcpy(p, message.c_str(), message.size()); - n = libwebsocket_write(ws, p, message.size(), LWS_WRITE_BINARY); - } - else { - int encoded_len; - //encoded_len = b64_encode_string(message.c_str(), message.size(), (char*)p, buf.size()); - cout<<"encode "< 0){ - n = libwebsocket_write(ws, p, encoded_len, LWS_WRITE_TEXT); - } - } - } - else { - memcpy(p, message.c_str(), message.size()); - n = libwebsocket_write(ws, p, message.size(), LWS_WRITE_TEXT); - } - - if (n < 0) - std::cout << "ERROR writing to socket" << std::endl; -} - -//-------------------------------------------------------------- -const std::string -ofxWebSocketConnection::recv(const std::string& message) -{ - std::string decoded = message; - - //TODO: when libwebsockets has an API - // to detect binary support, we should use it - if (binary && !supportsBinary) - { - //TODO: libwebsockets base64 decode is broken @2011-06-19 - //len = lws_b64_decode_string(message, decoded, len); - int decoded_len = lws_b64_decode_string(message.c_str(), &decoded[0], message.size()); - decoded.resize(decoded_len); - } - - return decoded; -} - - - diff --git a/src/ofxWebSocketConnection.h b/src/ofxWebSocketConnection.h deleted file mode 100644 index 2aae0dd..0000000 --- a/src/ofxWebSocketConnection.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include - -#include -#include - -class ofxWebSocketReactor; -class ofxWebSocketProtocol; - -class ofxWebSocketSession { - public: -}; - -class ofxWebSocketConnection { - friend class ofxWebSocketReactor; -public: - ofxWebSocketConnection(); - - ofxWebSocketConnection(ofxWebSocketReactor* const _reactor=NULL, - ofxWebSocketProtocol* const _protocol=NULL, - const bool supportsBinary=false); - - ~ofxWebSocketConnection(); - void close(); - void send(const std::string& message); - const std::string recv(const std::string& message); - - libwebsocket* ws; - ofxWebSocketReactor* reactor; - ofxWebSocketProtocol* protocol; - -protected: - ofxWebSocketSession* session; - - bool binary; - bool supportsBinary; - int buffersize; - unsigned char* buf; - int bufsize; - //std::vector buf; -}; - diff --git a/src/ofxWebSocketEvents.cpp b/src/ofxWebSocketEvents.cpp deleted file mode 100644 index 3fd39d8..0000000 --- a/src/ofxWebSocketEvents.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "ofxWebSocketEvents.h" - -//-------------------------------------------------------------- -ofxWebSocketEvent::ofxWebSocketEvent(ofxWebSocketConnection& _conn, - std::string& _message) -: conn(_conn) -, message(_message) -{} - -//_ofxWebSocketEvents ofxWebSocketEvents; \ No newline at end of file diff --git a/src/ofxWebSocketEvents.h b/src/ofxWebSocketEvents.h deleted file mode 100644 index a0499bf..0000000 --- a/src/ofxWebSocketEvents.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include -#include "json.h" - -class ofxWebSocketConnection; - -class ofxWebSocketEvent { -public: - ofxWebSocketEvent(ofxWebSocketConnection& _conn, std::string& _message); - - ofxWebSocketConnection& conn; - std::string& message; - Json::Value json; -}; - -/* - class _ofxWebSocketEvents { - public: - ofEvent onConnect; - ofEvent onOpen; - ofEvent onClose; - ofEvent onIdle; - ofEvent onMessage; - ofEvent onBroadcast; - }; - extern _ofxWebSocketEvents ofxWebSocketEvents;*/ \ No newline at end of file diff --git a/src/ofxWebSocketProtocol.cpp b/src/ofxWebSocketProtocol.cpp deleted file mode 100644 index cf7194a..0000000 --- a/src/ofxWebSocketProtocol.cpp +++ /dev/null @@ -1,113 +0,0 @@ -#include "ofxWebSocketProtocol.h" -#include "ofxWebSocketReactor.h" - -//-------------------------------------------------------------- -ofxWebSocketProtocol::ofxWebSocketProtocol() -: defaultAllowPolicy(true) -{ - ofAddListener(onconnectEvent, this, &ofxWebSocketProtocol::_onconnect); - ofAddListener(onopenEvent, this, &ofxWebSocketProtocol::_onopen); - ofAddListener(oncloseEvent, this, &ofxWebSocketProtocol::_onclose); - ofAddListener(onidleEvent, this, &ofxWebSocketProtocol::_onidle); - ofAddListener(onmessageEvent, this, &ofxWebSocketProtocol::_onmessage); - ofAddListener(onbroadcastEvent, this, &ofxWebSocketProtocol::_onbroadcast); - binary = false; -} - -//-------------------------------------------------------------- -ofxWebSocketProtocol::~ofxWebSocketProtocol() -{ - ofRemoveListener(onconnectEvent, this, &ofxWebSocketProtocol::_onconnect); - ofRemoveListener(onopenEvent, this, &ofxWebSocketProtocol::_onopen); - ofRemoveListener(oncloseEvent, this, &ofxWebSocketProtocol::_onclose); - ofRemoveListener(onidleEvent, this, &ofxWebSocketProtocol::_onidle); - ofRemoveListener(onmessageEvent, this, &ofxWebSocketProtocol::_onmessage); - ofRemoveListener(onbroadcastEvent, this, &ofxWebSocketProtocol::_onbroadcast); - binary = false; -} - -//-------------------------------------------------------------- -bool ofxWebSocketProtocol::_allowClient(const std::string name, - const std::string ip) const -{ - std::map::const_iterator allow_iter; - - allow_iter = allowRules.find(name); - if (allow_iter != allowRules.end()) - return allow_iter->second; - - allow_iter = allowRules.find(ip); - if (allow_iter != allowRules.end()) - return allow_iter->second; - - return allowClient(name, ip); -} - -//-------------------------------------------------------------- -bool ofxWebSocketProtocol::allowClient(const std::string name, - const std::string ip) const -{ - return defaultAllowPolicy; -} - -//-------------------------------------------------------------- -void ofxWebSocketProtocol::_onconnect(ofxWebSocketEvent& args){ onconnect(args); } - -void ofxWebSocketProtocol::onconnect(ofxWebSocketEvent& args){} - -//-------------------------------------------------------------- -void ofxWebSocketProtocol::_onopen(ofxWebSocketEvent& args){ onopen(args); } - -void ofxWebSocketProtocol::onopen(ofxWebSocketEvent&args){} - -//-------------------------------------------------------------- -void ofxWebSocketProtocol::_onclose(ofxWebSocketEvent& args){ onclose(args); } - -void ofxWebSocketProtocol::onclose(ofxWebSocketEvent&args){} - -//-------------------------------------------------------------- -void ofxWebSocketProtocol::_onidle(ofxWebSocketEvent& args){ onidle(args); } - -void ofxWebSocketProtocol::onidle(ofxWebSocketEvent&args){} - -//-------------------------------------------------------------- -void ofxWebSocketProtocol::_onmessage(ofxWebSocketEvent& args){ - args.message = args.conn.recv(args.message); - args.json = NULL; - - bool parsingSuccessful = reader.parse( args.message, args.json ); - if ( !parsingSuccessful ) - { - // report to the user the failure and their locations in the document. - ofLog( OF_LOG_VERBOSE, "Failed to parse JSON\n"+ reader.getFormatedErrorMessages() ); - args.json = NULL; - } - - //std::cout << args.message << std::endl; - onmessage(args); -} - -void ofxWebSocketProtocol::onmessage(ofxWebSocketEvent&args) -{} - -//-------------------------------------------------------------- -void ofxWebSocketProtocol::_onbroadcast(ofxWebSocketEvent& args) -{ onbroadcast(args); } - -void ofxWebSocketProtocol::onbroadcast(ofxWebSocketEvent&args) -{ args.conn.send(args.message); } - -//-------------------------------------------------------------- -void ofxWebSocketProtocol::broadcast(const std::string& message) -{ - std::string buf(LWS_SEND_BUFFER_PRE_PADDING+1024+LWS_SEND_BUFFER_POST_PADDING, 0); - unsigned char *p = (unsigned char*)&buf[LWS_SEND_BUFFER_PRE_PADDING]; - - if (reactor != NULL) - { - memcpy(p, message.c_str(), message.size()); - int n = libwebsockets_broadcast(&reactor->lws_protocols[idx], p, message.size()); - if (n < 0) - fprintf(stderr, "ERROR writing to socket"); - } -} diff --git a/src/ofxWebSocketProtocol.h b/src/ofxWebSocketProtocol.h deleted file mode 100644 index ada6aaf..0000000 --- a/src/ofxWebSocketProtocol.h +++ /dev/null @@ -1,65 +0,0 @@ -#pragma once - -#include -#include - -#include "ofMain.h" -#include "ofxWebSocketEvents.h" - -class ofxWebSocketReactor; - -#include "json.h" - -class ofxWebSocketProtocol -{ - friend class ofxWebSocketReactor; - friend class ofxWebSocketServer; - friend class ofxWebSocketClient; - -public: - ofxWebSocketProtocol(); - ~ofxWebSocketProtocol(); - - virtual bool allowClient(const std::string name, - const std::string ip) const; - - void broadcast(const std::string& message); - - unsigned int idx; - bool binary; - -protected: - virtual void execute() {} - - virtual void onconnect (ofxWebSocketEvent& args); - virtual void onopen (ofxWebSocketEvent& args); - virtual void onclose (ofxWebSocketEvent& args); - virtual void onidle (ofxWebSocketEvent& args); - virtual void onmessage (ofxWebSocketEvent& args); - virtual void onbroadcast(ofxWebSocketEvent& args); - - ofEvent onconnectEvent; - ofEvent onopenEvent; - ofEvent oncloseEvent; - ofEvent onidleEvent; - ofEvent onmessageEvent; - ofEvent onbroadcastEvent; - - bool defaultAllowPolicy; - std::map allowRules; - - ofxWebSocketReactor* reactor; - -private: - Json::Reader reader; - - void _onconnect (ofxWebSocketEvent& args); - void _onopen (ofxWebSocketEvent& args); - void _onclose (ofxWebSocketEvent& args); - void _onidle (ofxWebSocketEvent& args); - void _onmessage (ofxWebSocketEvent& args); - void _onbroadcast (ofxWebSocketEvent& args); - - bool _allowClient(const std::string name, - const std::string ip) const; -}; diff --git a/src/ofxWebSocketReactor.cpp b/src/ofxWebSocketReactor.cpp deleted file mode 100644 index 8a3a21a..0000000 --- a/src/ofxWebSocketReactor.cpp +++ /dev/null @@ -1,151 +0,0 @@ -// -// ofxWebSocketReactor.cpp -// LibwebsocketClient -// -// Created by Brett Renfer on 4/12/12. -// Copyright (c) 2012 Robotconscience. All rights reserved. -// - -#include "ofxWebSocketReactor.h" - -//-------------------------------------------------------------- -ofxWebSocketReactor::ofxWebSocketReactor() -: context(NULL), waitMillis(50), count_pollfds(0) -{ - reactors.push_back(this); -} - -//-------------------------------------------------------------- -ofxWebSocketReactor::~ofxWebSocketReactor() -{ - exit(); -} - -//-------------------------------------------------------------- -void ofxWebSocketReactor::registerProtocol(const std::string& name, ofxWebSocketProtocol& protocol) -{ - protocol.idx = protocols.size()+1; // "http" is protocol 0 - protocol.reactor = this; - protocols.push_back(make_pair(name, &protocol)); -} - -//-------------------------------------------------------------- -ofxWebSocketProtocol* const ofxWebSocketReactor::protocol(const unsigned int idx) -{ - return (idx < protocols.size())? protocols[idx].second : NULL; -} - -//-------------------------------------------------------------- -void ofxWebSocketReactor::close(ofxWebSocketConnection* const conn) -{ - if (conn != NULL && conn->ws != NULL) - libwebsocket_close_and_free_session(context, conn->ws, LWS_CLOSE_STATUS_NORMAL); -} - -//-------------------------------------------------------------- -void ofxWebSocketReactor::exit() -{ - if (context != NULL) - { - stopThread(); - libwebsocket_context_destroy(context); - context = NULL; - } -} - - -//-------------------------------------------------------------- -struct libwebsocket_context * ofxWebSocketReactor::getContext(){ - return context; -}; - -//-------------------------------------------------------------- -unsigned int -ofxWebSocketReactor::_allow(ofxWebSocketProtocol* const protocol, const long fd) -{ - std::string client_ip(128, 0); - std::string client_name(128, 0); - - libwebsockets_get_peer_addresses((int)fd, - &client_name[0], client_name.size(), - &client_ip[0], client_ip.size()); - return protocol->_allowClient(client_name, client_ip); -} - -//-------------------------------------------------------------- -unsigned int ofxWebSocketReactor::_notify(ofxWebSocketConnection* const conn, - enum libwebsocket_callback_reasons const reason, - const char* const _message, - const unsigned int len) -{ - if (conn == NULL || conn->protocol == NULL){ - if (conn == NULL){ - cout<<"connection is null"< 0){ - message = std::string(_message, len); - } - - ofxWebSocketEvent args(*conn, message); - - if (reason==LWS_CALLBACK_ESTABLISHED || reason == LWS_CALLBACK_CLIENT_ESTABLISHED){ - ofNotifyEvent(conn->protocol->onopenEvent, args); - } else if (reason==LWS_CALLBACK_CLOSED){ - ofNotifyEvent(conn->protocol->oncloseEvent, args); - } else if (reason==LWS_CALLBACK_SERVER_WRITEABLE){ - ofNotifyEvent(conn->protocol->onidleEvent, args); - } else if (reason==LWS_CALLBACK_BROADCAST){ - ofNotifyEvent(conn->protocol->onbroadcastEvent, args); - } else if (reason==LWS_CALLBACK_RECEIVE || reason == LWS_CALLBACK_CLIENT_RECEIVE){ - ofNotifyEvent(conn->protocol->onmessageEvent, args); - } - - return 0; -} - -//-------------------------------------------------------------- -unsigned int ofxWebSocketReactor::_http(struct libwebsocket *ws, - const char* const _url) -{ - std::string url(_url); - if (url == "/") - url = "/index.html"; - - // why does this need to be done? - - if (document_root.empty()) - document_root = "web"; - - if (document_root.at(0) != '/') - document_root = ofToDataPath(document_root, true); - - std::string ext = url.substr(url.find_last_of(".")+1); - std::string file = document_root+url; - std::string mimetype = "text/html"; - - if (ext == "ico") - mimetype = "image/x-icon"; - if (ext == "manifest") - mimetype = "text/cache-manifest"; - if (ext == "swf") - mimetype = "application/x-shockwave-flash"; - if (ext == "js") - mimetype = "application/javascript"; - if (ext == "css") - mimetype = "text/css"; - - if (libwebsockets_serve_http_file(ws, file.c_str(), mimetype.c_str())) - { - std::cerr - << "Failed to send HTTP file " << file << " for " << url - << std::endl; - } else { - cout<<"served file "< -#include "ofxWebSocketProtocol.h" -#include "ofxWebSocketConnection.h" - -class ofxWebSocketReactor : public ofThread { - friend class ofxWebSocketProtocol; - -public: - ofxWebSocketReactor(); - ~ofxWebSocketReactor(); - - void close(ofxWebSocketConnection* const conn); - - void exit(); - void registerProtocol(const std::string& name, ofxWebSocketProtocol& protocol); - - ofxWebSocketProtocol* const protocol(const unsigned int idx); - - //private: - unsigned int _allow(ofxWebSocketProtocol* const protocol, const long fd); - - unsigned int _notify(ofxWebSocketConnection* const conn, - enum libwebsocket_callback_reasons const reason, - const char* const _message, - const unsigned int len); - - unsigned int _http(struct libwebsocket *ws, - const char* const url); - - std::string document_root; - std::vector > protocols; - - struct libwebsocket_context * getContext(); - - struct pollfd pollfds[100]; - int count_pollfds; - -protected: - unsigned int waitMillis; - std::string interface; - - virtual void threadedFunction(){}; - - string address; - string channel; - int port; - - struct libwebsocket_context *context; - - std::vector lws_protocols; -}; - -static vector reactors; \ No newline at end of file diff --git a/src/ofxWebSocketServer.cpp b/src/ofxWebSocketServer.cpp deleted file mode 100644 index a25c373..0000000 --- a/src/ofxWebSocketServer.cpp +++ /dev/null @@ -1,108 +0,0 @@ -#include "ofxWebSocketServer.h" -#include "ofxWebSocketUtil.h" - -#include "ofEvents.h" -#include "ofUtils.h" - -//-------------------------------------------------------------- -ofxWebSocketServer::ofxWebSocketServer(){ - context = NULL; - waitMillis = 50; - count_pollfds = 0; - reactors.push_back(this); -} - -//-------------------------------------------------------------- -bool ofxWebSocketServer::setup( const short _port ) -{ - // setup with default protocol (http) and allow ALL other protocols - setup( _port, "/", true ); -} - -//-------------------------------------------------------------- -bool ofxWebSocketServer::setup( const short _port, string protocol, bool bAllowAllProtocols ){ - port = _port; - // libwebsockets isn't compiled with SSL support right now! - bool useSSL = false;//(!_sslCertFilename.empty() && !_sslKeyFilename.empty()); - - std::string sslCertPath, sslKeyPath; - const char *_sslCertPath = NULL; - const char *_sslKeyPath = NULL; - - /*if (useSSL) - { - if (_sslCertFilename.at(0) == '/') - sslCertPath = _sslCertFilename; - else - sslCertPath = ofToDataPath(_sslCertFilename, true); - _sslCertPath = sslCertPath.c_str(); - - if (_sslKeyFilename.at(0) == '/') - sslKeyPath = _sslKeyFilename; - else - sslKeyPath = ofToDataPath(_sslKeyFilename, true); - _sslKeyPath = sslKeyPath.c_str(); - }*/ - - // where our webserver is loading files from - if (document_root.empty()){ - document_root = "web"; - } - if (document_root.at(0) != '/'){ - document_root = ofToDataPath(document_root, true); - } - - // NULL protocol is required by LWS - struct libwebsocket_protocols null_protocol = { NULL, NULL, 0 }; - - // NULL name = any protocol - struct libwebsocket_protocols null_name_protocol = { NULL, lws_callback, sizeof(ofxWebSocketConnection) }; - - //setup protocols - lws_protocols.clear(); - - // register main protocol - registerProtocol( protocol, serverProtocol ); - for (int i=0; iexecute(); - //unlock(); - } - libwebsocket_service(context, waitMillis); - } -} diff --git a/src/ofxWebSocketServer.h b/src/ofxWebSocketServer.h deleted file mode 100644 index 1a61ddc..0000000 --- a/src/ofxWebSocketServer.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include "ofThread.h" -#include "ofEvents.h" - -#include - -#include "ofxWebSocketReactor.h" - -class ofxWebSocketConnection; -class ofxWebSocketProtocol; - -class ofxWebSocketServer; - -class ofxWebSocketServer : public ofxWebSocketReactor { - friend class ofxWebSocketProtocol; - -public: - ofxWebSocketServer(); - - bool setup( const short _port=80 ); - bool setup( const short _port, string protocol, bool bAllowAllProtocols = true ); - - template - void addListener(T * app){ - ofAddListener( serverProtocol.onconnectEvent, app, &T::onConnect); - ofAddListener( serverProtocol.onopenEvent, app, &T::onOpen); - ofAddListener( serverProtocol.oncloseEvent, app, &T::onClose); - ofAddListener( serverProtocol.onidleEvent, app, &T::onIdle); - ofAddListener( serverProtocol.onmessageEvent, app, &T::onMessage); - ofAddListener( serverProtocol.onbroadcastEvent, app, &T::onBroadcast); - } - -protected: - std::string interface; - -private: - ofxWebSocketProtocol serverProtocol; - void threadedFunction(); -}; diff --git a/src/ofxWebSocketUtil.h b/src/ofxWebSocketUtil.h deleted file mode 100644 index b182d97..0000000 --- a/src/ofxWebSocketUtil.h +++ /dev/null @@ -1,192 +0,0 @@ -// -// ofxWebSocketUtil.h -// LibwebsocketClient -// -// Created by Brett Renfer on 4/11/12. -// Copyright (c) 2012 Robotconscience. All rights reserved. -// - -#pragma once - -#include -#include "ofxWebSocketConnection.h" -#include "ofxWebSocketReactor.h" -#include "ofxWebSocketClient.h" -#include "ofxWebSocketServer.h" - -class ofxWebSocketClient; -class ofxWebSocketServer; - -// CLIENT CALLBACK - -static int lws_client_callback(struct libwebsocket_context* context, struct libwebsocket *ws, enum libwebsocket_callback_reasons reason, void *user, void *data, size_t len){ - const struct libwebsocket_protocols* lws_protocol = (ws == NULL ? NULL : libwebsockets_get_protocol(ws)); - int idx = lws_protocol? lws_protocol->protocol_index : 0; - - ofxWebSocketConnection* conn; - - ofxWebSocketConnection** conn_ptr = (ofxWebSocketConnection**)user; - ofxWebSocketReactor* reactor = NULL; - ofxWebSocketProtocol* protocol; - - for (int i=0; igetContext() == context){ - reactor = reactors[i]; - protocol = reactor->protocol(idx-1); - conn = ((ofxWebSocketClient*) reactor)->getConnection(); - break; - } else { - } - } - - if (reason == LWS_CALLBACK_CLIENT_ESTABLISHED){ - } else if (reason == LWS_CALLBACK_CLOSED){ - } - - switch (reason) - { - case LWS_CALLBACK_CONFIRM_EXTENSION_OKAY: - return 0; - - case LWS_CALLBACK_FILTER_NETWORK_CONNECTION: - if (protocol != NULL){ - return reactor->_allow(protocol, (int)(long)user)? 0 : 1; - } else { - return 0; - } - case LWS_CALLBACK_HTTP: - if ( reactor != NULL){ - return reactor->_http(ws, (char*)data); - } else { - return 0; - } - case LWS_CALLBACK_CLIENT_ESTABLISHED: - case LWS_CALLBACK_CLIENT_WRITEABLE: - case LWS_CALLBACK_CLOSED: - case LWS_CALLBACK_CLIENT_RECEIVE: - if (reactor != NULL){ - //conn = *(ofxWebSocketConnection**)user; - if (conn && conn->ws != ws) conn->ws = ws; - return reactor->_notify(conn, reason, (char*)data, len); - } else { - return 0; - } - default: - return 0; - } - - return 1; // FAIL (e.g. unhandled case/break in switch) -} - -// SERVER CALLBACK - -static int lws_callback(struct libwebsocket_context* context, struct libwebsocket *ws, enum libwebsocket_callback_reasons reason, void *user, void *data, size_t len){ - const struct libwebsocket_protocols* lws_protocol = (ws == NULL ? NULL : libwebsockets_get_protocol(ws)); - int idx = lws_protocol? lws_protocol->protocol_index : 0; - - bool bAllowAllProtocls = (ws != NULL ? lws_protocol == NULL : false); - - ofxWebSocketConnection* conn; - ofxWebSocketConnection** conn_ptr = (ofxWebSocketConnection**)user; - ofxWebSocketServer* reactor = NULL; - ofxWebSocketProtocol* protocol = NULL; - - for (int i=0; igetContext() == context){ - reactor = (ofxWebSocketServer*) reactors[i]; - protocol = reactor->protocol( (idx > 0 ? idx-1 : 0) ); - break; - } else { - } - } - - if ( bAllowAllProtocls && reactor != NULL ){ - protocol = reactor->protocols[0].second; - } - - if (reason == LWS_CALLBACK_ESTABLISHED){ - cout<<"yes"<protocols[0].second; - //conn = new ofxWebSocketConnection( reactor, protocol ); - return reactor->_allow(protocol, (int)(long)user)? 0 : 1; - } - return reactor->_allow(protocol, (int)(long)user)? 0 : 1; - } else { - return 1; - } - - case LWS_CALLBACK_HTTP: - return reactor->_http(ws, (char*)data); - - case LWS_CALLBACK_ESTABLISHED: - case LWS_CALLBACK_CLOSED: - case LWS_CALLBACK_SERVER_WRITEABLE: - case LWS_CALLBACK_RECEIVE: - case LWS_CALLBACK_BROADCAST: - conn = *(ofxWebSocketConnection**)user; - if (conn && conn->ws != ws) conn->ws = ws; - if (reactor){ - return reactor->_notify(conn, reason, (char*)data, len); - } else { - return 0; - } - - default: - return 0; - } - - return 1; // FAIL (e.g. unhandled case/break in switch) -} - -static void dump_handshake_info(struct lws_tokens *lwst) -{ - int n; - static const char *token_names[WSI_TOKEN_COUNT] = { - "GET URI", - "Host", - "Connection", - "key 1", - "key 2", - "Protocol", - "Upgrade", - "Origin", - "Draft", - "Challenge", - - /* new for 04 */ - "Key", - "Version", - "Sworigin", - - /* new for 05 */ - "Extensions", - - /* client receives these */ - "Accept", - "Nonce", - "Http", - "MuxURL", - }; - - for (n = 0; n < WSI_TOKEN_COUNT; n++) { - if (lwst[n].token == NULL) - continue; - - fprintf(stderr, " %s = %s\n", token_names[n], lwst[n].token); - } -} \ No newline at end of file