Skip to content

Commit da82fc6

Browse files
General cleanup, comments, and addition of onError event
1 parent 99f5d15 commit da82fc6

File tree

11 files changed

+94
-66
lines changed

11 files changed

+94
-66
lines changed

libs/ofxLibwebsockets/include/ofxLibwebsockets/Client.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace ofxLibwebsockets {
2222
int version;
2323
};
2424

25+
// call this function to set up a vanilla client options object
2526
static ClientOptions defaultClientOptions(){
2627
ClientOptions opts;
2728
opts.host = "localhost";
@@ -47,9 +48,10 @@ namespace ofxLibwebsockets {
4748
bool connect ( string _address, int _port, bool bUseSSL=false );
4849
bool connect ( ClientOptions options );
4950

50-
void onClose( Event& args );
51+
// force close this client
5152
void close();
5253

54+
// most basic send function
5355
void send( string message );
5456

5557
// send anything that has pixels
@@ -71,12 +73,14 @@ namespace ofxLibwebsockets {
7173
ofAddListener( clientProtocol.onbroadcastEvent, app, &T::onBroadcast);
7274
}
7375

76+
// get pointer to libwebsockets connection wrapper
7477
Connection * getConnection(){
7578
return connection;
7679
}
7780

7881
protected:
7982
ClientOptions defaultOptions;
83+
void onClose( Event& args );
8084

8185
private:
8286

libs/ofxLibwebsockets/include/ofxLibwebsockets/Connection.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ namespace ofxLibwebsockets {
2020
class Reactor;
2121
class Protocol;
2222

23-
class Session {
24-
public:
25-
};
26-
2723
struct TextPacket {
2824
string message;
2925
int index;
@@ -38,7 +34,7 @@ namespace ofxLibwebsockets {
3834
class Connection {
3935
friend class Reactor;
4036
public:
41-
Connection(Reactor* const _reactor=NULL, Protocol* const _protocol=NULL, const bool supportsBinary=true);
37+
Connection(Reactor* const _reactor=NULL, Protocol* const _protocol=NULL);
4238

4339
~Connection();
4440
void close();
@@ -72,13 +68,10 @@ namespace ofxLibwebsockets {
7268
void update();
7369

7470
protected:
75-
Session* session;
76-
7771
std::string client_ip;
7872
std::string client_name;
7973

8074
bool binary; // is this connection sending / receiving binary?
81-
bool supportsBinary; // does this connection support binary?
8275

8376
int bufferSize;
8477
unsigned char* buf;

libs/ofxLibwebsockets/include/ofxLibwebsockets/Events.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace ofxLibwebsockets {
2020
Event(Connection& _conn, std::string& _message, bool isBinary=false);
2121

2222
Connection& conn;
23-
std::string& message;
23+
std::string& message; // message from ws OR error message if error
2424
Json::Value json;
2525

2626
// binary data
@@ -38,5 +38,4 @@ namespace ofxLibwebsockets {
3838
ofEvent<ofxLibwebsockets::Event> onIdle;
3939
ofEvent<ofxLibwebsockets::Event> onMessage;
4040
ofEvent<ofxLibwebsockets::Event> onBroadcast;
41-
};
42-
extern _ofxWebSocketEvents ofxWebSocketEvents;*/
41+
};*/

libs/ofxLibwebsockets/include/ofxLibwebsockets/Protocol.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,25 @@ namespace ofxLibwebsockets {
4242

4343
unsigned int idx;
4444
unsigned int rx_buffer_size;
45-
bool binary;
4645

4746
protected:
47+
// override these methods if/when creating
48+
// a custom protocol
4849
virtual void execute() {}
4950

5051
virtual void onconnect (Event& args);
5152
virtual void onopen (Event& args);
5253
virtual void onclose (Event& args);
54+
virtual void onerror (Event& args);
5355
virtual void onidle (Event& args);
5456
virtual void onmessage (Event& args);
5557
virtual void onbroadcast(Event& args);
5658

59+
// internal events: called by Reactor
5760
ofEvent<Event> onconnectEvent;
5861
ofEvent<Event> onopenEvent;
5962
ofEvent<Event> oncloseEvent;
63+
ofEvent<Event> onerrorEvent;
6064
ofEvent<Event> onidleEvent;
6165
ofEvent<Event> onmessageEvent;
6266
ofEvent<Event> onbroadcastEvent;
@@ -72,6 +76,7 @@ namespace ofxLibwebsockets {
7276
void _onconnect (Event& args);
7377
void _onopen (Event& args);
7478
void _onclose (Event& args);
79+
void _onerror (Event& args);
7580
void _onidle (Event& args);
7681
void _onmessage (Event& args);
7782
void _onbroadcast (Event& args);

libs/ofxLibwebsockets/include/ofxLibwebsockets/Server.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ namespace ofxLibwebsockets {
2828
struct ServerOptions {
2929
int port;
3030
bool bUseSSL; // if you use ssl, you must connect clients to wss:// instead of ws://
31-
bool bBinaryProtocol; // only matters as far as receiving; if rec. binary & is false will try to base64 decode it
3231
string protocol; // if you specify this, you must connect with a protocol on the client side
3332
string sslCertPath; // data path to ssl certificate
3433
string sslKeyPath; // data path to ssl key
@@ -41,7 +40,6 @@ namespace ofxLibwebsockets {
4140
opts.port = 80;
4241
opts.protocol = "NULL";
4342
opts.bUseSSL = false;
44-
opts.bBinaryProtocol = false;
4543
opts.sslCertPath = ofToDataPath("ssl/libwebsockets-test-server.pem", true);
4644
opts.sslKeyPath = ofToDataPath("ssl/libwebsockets-test-server.key.pem", true);
4745
opts.documentRoot = ofToDataPath("web", true);

libs/ofxLibwebsockets/include/ofxLibwebsockets/Util.h

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Util.h
33
// ofxLibwebsockets
44
//
5+
// This class is primarily the static callbacks needed by libwebsockets
6+
//
57
// Created by Brett Renfer on 4/11/12.
68
// Copyright (c) 2012 Robotconscience. All rights reserved.
79
//
@@ -33,7 +35,7 @@ namespace ofxLibwebsockets {
3335

3436
Reactor* reactor = NULL;
3537
Protocol* protocol;
36-
38+
3739
for (int i=0; i<(int)reactors.size(); i++){
3840
if (reactors[i]->getContext() == context){
3941
reactor = reactors[i];
@@ -47,7 +49,7 @@ namespace ofxLibwebsockets {
4749
}
4850
}
4951

50-
ofLog( OF_LOG_VERBOSE, getCallbackReason(reason) );
52+
ofLog( OF_LOG_VERBOSE, "[ofxLibwebsockets] " + getCallbackReason(reason) );
5153

5254
if (reason == LWS_CALLBACK_CLIENT_ESTABLISHED ){
5355
libwebsocket_callback_on_writable(context, ws);
@@ -56,26 +58,38 @@ namespace ofxLibwebsockets {
5658

5759
switch (reason)
5860
{
61+
// cases we may use in the future
5962
case LWS_CALLBACK_CONFIRM_EXTENSION_OKAY:
6063
case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
61-
case LWS_CALLBACK_PROTOCOL_INIT:
64+
case LWS_CALLBACK_PROTOCOL_INIT: // this may be useful, says we're OK to allocate protocol data
6265
case LWS_CALLBACK_WSI_CREATE:
66+
67+
case LWS_CALLBACK_HTTP_BODY_COMPLETION:
68+
case LWS_CALLBACK_HTTP_FILE_COMPLETION:
69+
case LWS_CALLBACK_HTTP_WRITEABLE:
70+
71+
case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
6372
return 0;
6473

74+
// check if we allow this connection (default is always yes)
75+
case LWS_CALLBACK_FILTER_HTTP_CONNECTION:
6576
case LWS_CALLBACK_FILTER_NETWORK_CONNECTION:
6677
if (protocol != NULL){
6778
return reactor->_allow(ws, protocol, (int)(long)user)? 0 : 1;
6879
} else {
6980
return 0;
7081
}
82+
83+
// need to serve up an HTTP file
7184
case LWS_CALLBACK_HTTP:
7285
if ( reactor != NULL){
7386
return reactor->_http(ws, (char*)data);
7487
} else {
7588
return 0;
7689
}
7790

78-
// we're not really worried about this at the moment
91+
// we're not really worried about these at the moment
92+
case LWS_CALLBACK_CLOSED_HTTP:
7993
case LWS_CALLBACK_ADD_POLL_FD:
8094
case LWS_CALLBACK_DEL_POLL_FD:
8195
case LWS_CALLBACK_CHANGE_MODE_POLL_FD:
@@ -86,6 +100,13 @@ namespace ofxLibwebsockets {
86100
case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER:
87101
return 1;
88102

103+
// catch-all for most important events:
104+
// LWS_CALLBACK_CLIENT_CONNECTION_ERROR
105+
// LWS_CALLBACK_CLIENT_ESTABLISHED
106+
// LWS_CALLBACK_RECEIVE
107+
// LWS_CALLBACK_CLIENT_RECEIVE
108+
// LWS_CALLBACK_CLIENT_RECEIVE_PONG
109+
// LWS_CALLBACK_CLIENT_WRITEABLE
89110
default:
90111
if ( reactor != NULL ){
91112
//conn = *(Connection**)user;
@@ -129,8 +150,9 @@ namespace ofxLibwebsockets {
129150
}
130151
}
131152

132-
ofLog( OF_LOG_VERBOSE, getCallbackReason(reason) );
153+
ofLog( OF_LOG_VERBOSE, "[ofxLibwebsockets] "+ getCallbackReason(reason) );
133154

155+
// server completed handshake, need to ask for next "writable" callback
134156
if (reason == LWS_CALLBACK_ESTABLISHED){
135157
libwebsocket_callback_on_writable(context, ws);
136158

libs/ofxLibwebsockets/src/Connection.cpp

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,14 @@
1313
namespace ofxLibwebsockets {
1414

1515
//--------------------------------------------------------------
16-
Connection::Connection(Reactor* const _reactor, Protocol* const _protocol, const bool _supportsBinary)
16+
Connection::Connection(Reactor* const _reactor, Protocol* const _protocol)
1717
: reactor(_reactor)
1818
, protocol(_protocol)
1919
, context(NULL)
2020
, ws(NULL)
21-
, session(NULL)
22-
, supportsBinary(_supportsBinary)
2321
//, buf(LWS_SEND_BUFFER_PRE_PADDING+1024+LWS_SEND_BUFFER_POST_PADDING)
2422
{
2523
if (_protocol != NULL){
26-
binary = _protocol->binary;
2724
bufferSize = _protocol->rx_buffer_size;
2825
buf = (unsigned char*)calloc(LWS_SEND_BUFFER_PRE_PADDING+bufferSize+LWS_SEND_BUFFER_POST_PADDING, sizeof(unsigned char));
2926
binaryBuf = (unsigned char*)calloc(LWS_SEND_BUFFER_PRE_PADDING+bufferSize+LWS_SEND_BUFFER_POST_PADDING, sizeof(unsigned char));
@@ -106,35 +103,29 @@ namespace ofxLibwebsockets {
106103
//--------------------------------------------------------------
107104
void Connection::sendBinary( char * data, unsigned int size ){
108105
int n = -1;
106+
// size binary packet based on either bufferSize (max) or passed 'size' (whichever is smaller)
107+
int dataSize = bufferSize > size ? size : bufferSize;
108+
memcpy(&binaryBuf[LWS_SEND_BUFFER_PRE_PADDING], data, dataSize );
109109

110-
// TODO: when libwebsockets has an API supporting something this, we should use it
111-
// for now we are assuming that if you send binary your client supports it
112-
113-
if ( supportsBinary ){
114-
// size binary packet based on either bufferSize (max) or passed 'size' (whichever is smaller)
115-
int dataSize = bufferSize > size ? size : bufferSize;
116-
memcpy(&binaryBuf[LWS_SEND_BUFFER_PRE_PADDING], data, dataSize );
110+
// we have a big frame, so we need to send a few times
111+
if ( bufferSize < size ){
117112

118-
// we have a big frame, so we need to send a few times
119-
if ( bufferSize < size ){
120-
121-
// need to split into packets
122-
BinaryPacket bp;
123-
bp.index = 0;
124-
bp.size = size;
125-
126-
// copy data into array, in case user frees it
127-
bp.data = (unsigned char*)calloc(size, sizeof(unsigned char));
128-
memcpy(bp.data, data, size);
129-
130-
messages_binary.push_back(bp);
131-
132-
n = 0;
133-
134-
// we have a nice small frame, just send it
135-
} else {
136-
n = libwebsocket_write(ws, &binaryBuf[LWS_SEND_BUFFER_PRE_PADDING], dataSize, LWS_WRITE_BINARY);
137-
}
113+
// need to split into packets
114+
BinaryPacket bp;
115+
bp.index = 0;
116+
bp.size = size;
117+
118+
// copy data into array, in case user frees it
119+
bp.data = (unsigned char*)calloc(size, sizeof(unsigned char));
120+
memcpy(bp.data, data, size);
121+
122+
messages_binary.push_back(bp);
123+
124+
n = 0;
125+
126+
// we have a nice small frame, just send it
127+
} else {
128+
n = libwebsocket_write(ws, &binaryBuf[LWS_SEND_BUFFER_PRE_PADDING], dataSize, LWS_WRITE_BINARY);
138129
}
139130

140131
if (n < 0){

libs/ofxLibwebsockets/src/Events.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,4 @@ namespace ofxLibwebsockets {
1616
, message(_message)
1717
, isBinary(isBinary)
1818
{}
19-
20-
//_Events ofxWebSocketEvents;
2119
}

libs/ofxLibwebsockets/src/Protocol.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace ofxLibwebsockets {
1313

14+
#pragma mark constructor
15+
1416
//--------------------------------------------------------------
1517
Protocol::Protocol()
1618
: defaultAllowPolicy(true){
@@ -20,7 +22,7 @@ namespace ofxLibwebsockets {
2022
ofAddListener(onidleEvent, this, &Protocol::_onidle);
2123
ofAddListener(onmessageEvent, this, &Protocol::_onmessage);
2224
ofAddListener(onbroadcastEvent, this, &Protocol::_onbroadcast);
23-
binary = false;
25+
ofAddListener(onerrorEvent, this, &Protocol::_onerror);
2426
rx_buffer_size = OFX_LWS_MAX_BUFFER;
2527
idle = false;
2628
}
@@ -33,7 +35,7 @@ namespace ofxLibwebsockets {
3335
ofRemoveListener(onidleEvent, this, &Protocol::_onidle);
3436
ofRemoveListener(onmessageEvent, this, &Protocol::_onmessage);
3537
ofRemoveListener(onbroadcastEvent, this, &Protocol::_onbroadcast);
36-
binary = false;
38+
ofRemoveListener(onerrorEvent, this, &Protocol::_onerror);
3739
rx_buffer_size = OFX_LWS_MAX_BUFFER;
3840
idle = false;
3941
}
@@ -57,6 +59,8 @@ namespace ofxLibwebsockets {
5759
bool Protocol::allowClient(const std::string name, const std::string ip) const {
5860
return defaultAllowPolicy;
5961
}
62+
63+
#pragma mark events
6064

6165
//--------------------------------------------------------------
6266
void Protocol::_onconnect(Event& args){ onconnect(args); }
@@ -72,6 +76,11 @@ namespace ofxLibwebsockets {
7276
void Protocol::_onclose(Event& args){ onclose(args); }
7377

7478
void Protocol::onclose(Event&args){}
79+
80+
//--------------------------------------------------------------
81+
void Protocol::_onerror(Event& args){ onerror(args); }
82+
83+
void Protocol::onerror(Event&args){}
7584

7685
//--------------------------------------------------------------
7786
bool Protocol::isIdle(){

0 commit comments

Comments
 (0)