Skip to content

Commit 8f2b169

Browse files
Final changes for 0.8.4
1 parent 176e17f commit 8f2b169

File tree

7 files changed

+60
-12
lines changed

7 files changed

+60
-12
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,15 @@ make
143143
* Version 0.8.4 (current)
144144
* + Added iOS library
145145
* + Added RPi library (thanks to @rc1)
146+
* + Added libwebsockets' rudimentary keep-alive
147+
* See issue #38
146148
* \ Updated to latest version of libwebsockets
147149
* \ Bug fixes for newest libwebsockets version
148150
* \ testApp changed to ofApp
149151
* \ Better comments in general
150152
* \ Removed unnecessary checks for binary support (always enabled in libwebsockets)
153+
* \ Removed "of-protocol" protocol from examples (you don't need it!)
154+
* + Added adjustable image quality to binary video example for demonstration
151155
* Version 0.8.1
152156
* Overhauled all messaging, supporting sending/receiving string and binary messages of indefinite size via continuation frames
153157
* Added examples from @zebradog of sending Blob data over websockets: way faster, way cleaner!
-256 KB
Binary file not shown.

libs/ofxLibwebsockets/include/ofxLibwebsockets/Connection.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,15 @@ namespace ofxLibwebsockets {
5050
void sendBinary( unsigned char * data, unsigned int size );
5151
void sendBinary( char * data, unsigned int size );
5252

53-
void setupAddress();
54-
53+
// gets IP address *relative to system*
54+
// e.g. localhost could be ::1, 127.0.0.1, your IP, etc...
5555
std::string getClientIP();
5656
std::string getClientName();
5757

58+
// 'private' accessor for static libwebsockets loop stuff
59+
60+
void setupAddress();
61+
5862
libwebsocket_context* context;
5963
libwebsocket* ws;
6064
Reactor* reactor;

libs/ofxLibwebsockets/include/ofxLibwebsockets/Util.h

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ namespace ofxLibwebsockets {
125125

126126
// SERVER CALLBACK
127127

128-
static int lws_callback(struct libwebsocket_context* context, struct libwebsocket *ws, enum libwebsocket_callback_reasons reason, void *user, void *data, size_t len){
128+
static int lws_callback(struct libwebsocket_context* context, struct libwebsocket *ws,
129+
enum libwebsocket_callback_reasons reason, void *user, void *data, size_t len)
130+
{
129131
const struct libwebsocket_protocols* lws_protocol = (ws == NULL ? NULL : libwebsockets_get_protocol(ws));
130132
int idx = lws_protocol? lws_protocol->protocol_index : 0;
131133

@@ -152,10 +154,12 @@ namespace ofxLibwebsockets {
152154

153155
ofLog( OF_LOG_VERBOSE, "[ofxLibwebsockets] "+ getCallbackReason(reason) );
154156

155-
// server completed handshake, need to ask for next "writable" callback
156157
if (reason == LWS_CALLBACK_ESTABLISHED){
158+
// server completed handshake, need to ask for next "writable" callback
157159
libwebsocket_callback_on_writable(context, ws);
158160

161+
// now is when you can set the "user" data,
162+
// which we use to instantiate / refer to the ofxLibwebsockets::Connection
159163
if ( reactor != NULL ){
160164
*conn_ptr = new Connection(reactor, protocol);
161165
}
@@ -168,25 +172,32 @@ namespace ofxLibwebsockets {
168172
{
169173
// we may use these in the future!
170174
case LWS_CALLBACK_WSI_CREATE:
171-
case LWS_CALLBACK_PROTOCOL_INIT:
175+
case LWS_CALLBACK_WSI_DESTROY:
172176

173177
case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
178+
174179
case LWS_CALLBACK_HTTP_BODY_COMPLETION:
175180
case LWS_CALLBACK_HTTP_FILE_COMPLETION:
176181
case LWS_CALLBACK_HTTP_WRITEABLE:
182+
case LWS_CALLBACK_CLOSED_HTTP:
183+
177184
case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
178185

179186
case LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED:
187+
case LWS_CALLBACK_FILTER_NETWORK_CONNECTION:
180188
return 0;
181189

182190
case LWS_CALLBACK_FILTER_HTTP_CONNECTION:
183-
case LWS_CALLBACK_FILTER_NETWORK_CONNECTION:
184191
if (protocol != NULL ){
185-
return reactor->_allow(ws, protocol, (int)(long)user)? 0 : 1;
192+
// return 0 == allow, 1 == block
193+
return reactor->_allow(ws, protocol, (int)(long)data) ? 0 : 1;
186194
} else {
187-
return 1;
195+
return 0;
188196
}
189197

198+
case LWS_CALLBACK_PROTOCOL_INIT:
199+
return 0;
200+
190201
case LWS_CALLBACK_HTTP:
191202
return reactor->_http(ws, (char*)data);
192203

@@ -201,7 +212,15 @@ namespace ofxLibwebsockets {
201212
case LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH:
202213
return 0;
203214

204-
default:
215+
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
216+
case LWS_CALLBACK_ESTABLISHED:
217+
case LWS_CALLBACK_CLIENT_ESTABLISHED:
218+
case LWS_CALLBACK_CLOSED:
219+
case LWS_CALLBACK_SERVER_WRITEABLE:
220+
case LWS_CALLBACK_CLIENT_WRITEABLE:
221+
case LWS_CALLBACK_RECEIVE: // server receive
222+
case LWS_CALLBACK_CLIENT_RECEIVE: // client receive
223+
case LWS_CALLBACK_CLIENT_RECEIVE_PONG:
205224
if ( user != NULL ){
206225
conn = *(Connection**)user;
207226
}
@@ -215,9 +234,12 @@ namespace ofxLibwebsockets {
215234
} else {
216235
return 0;
217236
}
237+
238+
default:
239+
break;
218240
}
219241

220-
return 1; // FAIL (e.g. unhandled case/break in switch)
242+
return 0;
221243
}
222244

223245
static void dump_handshake_info(struct lws_tokens *lwst)

libs/ofxLibwebsockets/src/Client.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ namespace ofxLibwebsockets {
9797
info.extensions = libwebsocket_get_internal_extensions();
9898
info.gid = -1;
9999
info.uid = -1;
100+
101+
if ( options.ka_time != 0 ){
102+
ofLogVerbose()<<"[ofxLibwebsockets] Setting timeout "<<options.ka_time;
103+
info.ka_time = options.ka_time;
104+
info.ka_probes = options.ka_probes;
105+
info.ka_interval = options.ka_interval;
106+
}
100107

101108
context = libwebsocket_create_context(&info);
102109

libs/ofxLibwebsockets/src/Reactor.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ namespace ofxLibwebsockets {
9292
std::string client_ip(128, 0);
9393
std::string client_name(128, 0);
9494

95-
libwebsockets_get_peer_addresses(context, ws, (int)fd,
95+
libwebsockets_get_peer_addresses(context, ws, libwebsocket_get_socket_fd(ws),
9696
&client_name[0], client_name.size(),
9797
&client_ip[0], client_ip.size());
9898
return protocol->_allowClient(client_name, client_ip);
@@ -139,14 +139,18 @@ namespace ofxLibwebsockets {
139139

140140
// last thing that happens before connection goes dark
141141
case LWS_CALLBACK_WSI_DESTROY:
142+
{
143+
bool bFound = false;
142144
for (int i=0; i<connections.size(); i++){
143145
if ( connections[i] == conn ){
146+
bFound = true; // valid connection
144147
connections.erase( connections.begin() + i );
145148
break;
146149
}
147150
}
148151

149-
ofNotifyEvent(conn->protocol->oncloseEvent, args);
152+
if ( bFound ) ofNotifyEvent(conn->protocol->oncloseEvent, args);
153+
}
150154
break;
151155

152156
case LWS_CALLBACK_ESTABLISHED: // server connected with client

libs/ofxLibwebsockets/src/Server.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ namespace ofxLibwebsockets {
106106
info.ssl_private_key_filepath = sslKey;
107107
info.gid = -1;
108108
info.uid = -1;
109+
110+
if ( options.ka_time != 0 ){
111+
info.ka_time = options.ka_time;
112+
info.ka_probes = options.ka_probes;
113+
info.ka_interval = options.ka_interval;
114+
}
115+
109116
info.options = opts;
110117

111118
context = libwebsocket_create_context(&info);

0 commit comments

Comments
 (0)