Skip to content

Commit e6cc789

Browse files
Fixed up sending, now more threadsafe
1 parent 718a873 commit e6cc789

File tree

1 file changed

+44
-34
lines changed

1 file changed

+44
-34
lines changed

libs/ofxLibwebsockets/src/Connection.cpp

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
// Connection.cpp
33
// ofxLibwebsockets
44
//
5-
// Created by Brett Renfer on 4/11/12.
5+
// Created by Brett Renfer on 4/11/12.
66
//
77

88
#include "ofxLibwebsockets/Connection.h"
99
#include "ofxLibwebsockets/Reactor.h"
1010
#include "ofxLibwebsockets/Protocol.h"
1111

1212
namespace ofxLibwebsockets {
13-
13+
1414
//--------------------------------------------------------------
1515
Connection::Connection(Reactor* const _reactor, Protocol* const _protocol)
1616
: reactor(_reactor)
@@ -25,7 +25,7 @@ namespace ofxLibwebsockets {
2525
binaryBuf = (unsigned char*)calloc(LWS_SEND_BUFFER_PRE_PADDING+bufferSize+LWS_SEND_BUFFER_POST_PADDING, sizeof(unsigned char));
2626
}
2727
}
28-
28+
2929
//--------------------------------------------------------------
3030
Connection::~Connection(){
3131
free(buf);
@@ -61,29 +61,32 @@ namespace ofxLibwebsockets {
6161
libwebsockets_get_peer_addresses(context, ws, fd, &client_name[0], client_name.size(),
6262
&client_ip[0], client_ip.size());
6363
}
64-
64+
6565
//--------------------------------------------------------------
6666
void Connection::send(const std::string& message)
6767
{
68+
if ( ws == NULL) return;
6869
if ( message.size() == 0 ) return;
6970
int n = 0;
7071

7172
// size packet based on either bufferSize (max) or passed 'size' (whichever is smaller)
7273
int dataSize = bufferSize > message.size() ? message.size() : bufferSize;
7374
memcpy(&buf[LWS_SEND_BUFFER_PRE_PADDING], message.c_str(), dataSize );
7475

75-
// we have a big frame, so we need to send a few times
76-
if ( bufferSize < message.size() ){
77-
// need to jump into thread
78-
TextPacket tp;
79-
tp.index = 0;
80-
tp.message = message;
81-
messages_text.push_back(tp);
82-
76+
// changed 3/6/15: buffer all messages to prevent threading errors
77+
TextPacket tp;
78+
tp.index = 0;
79+
tp.message = message;
80+
messages_text.push_back(tp);
81+
8382
// we have a nice small frame, just send it
84-
} else {
85-
n = libwebsocket_write(ws, &buf[LWS_SEND_BUFFER_PRE_PADDING], message.size(), LWS_WRITE_TEXT);
86-
}
83+
// } else {
84+
// if ( lws_partial_buffered(ws) == 0 ){
85+
// n = libwebsocket_write(ws, &buf[LWS_SEND_BUFFER_PRE_PADDING], message.size(), LWS_WRITE_TEXT);
86+
// } else {
87+
// n = -1;
88+
// }
89+
// }
8790

8891
if (n < 0)
8992
ofLogError() << "[ofxLibwebsockets] ERROR writing to socket" << std::endl;
@@ -106,26 +109,28 @@ namespace ofxLibwebsockets {
106109
int dataSize = bufferSize > size ? size : bufferSize;
107110
memcpy(&binaryBuf[LWS_SEND_BUFFER_PRE_PADDING], data, dataSize );
108111

112+
113+
// changed 3/6/15: buffer all messages to prevent threading errors
109114
// we have a big frame, so we need to send a few times
110-
if ( bufferSize < size ){
111-
112-
// need to split into packets
113-
BinaryPacket bp;
114-
bp.index = 0;
115-
bp.size = size;
116-
117-
// copy data into array, in case user frees it
118-
bp.data = (unsigned char*)calloc(size, sizeof(unsigned char));
119-
memcpy(bp.data, data, size);
120-
121-
messages_binary.push_back(bp);
122-
123-
n = 0;
124-
125-
// we have a nice small frame, just send it
126-
} else {
127-
n = libwebsocket_write(ws, &binaryBuf[LWS_SEND_BUFFER_PRE_PADDING], dataSize, LWS_WRITE_BINARY);
128-
}
115+
// if ( bufferSize < size ){
116+
117+
// need to split into packets
118+
BinaryPacket bp;
119+
bp.index = 0;
120+
bp.size = size;
121+
122+
// copy data into array, in case user frees it
123+
bp.data = (unsigned char*)calloc(size, sizeof(unsigned char));
124+
memcpy(bp.data, data, size);
125+
126+
messages_binary.push_back(bp);
127+
128+
n = 0;
129+
130+
// // we have a nice small frame, just send it
131+
// } else {
132+
// n = libwebsocket_write(ws, &binaryBuf[LWS_SEND_BUFFER_PRE_PADDING], dataSize, LWS_WRITE_BINARY);
133+
// }
129134

130135
if (n < 0){
131136
ofLogError() << "[ofxLibwebsockets] ERROR writing to socket" << std::endl;
@@ -148,6 +153,11 @@ namespace ofxLibwebsockets {
148153
protocol->idle = false;
149154

150155
int n = libwebsocket_write(ws, &buf[LWS_SEND_BUFFER_PRE_PADDING], dataSize, (libwebsocket_write_protocol) writeMode );
156+
157+
if ( n < -1 ){
158+
ofLogError()<<"[ofxLibwebsockets] ERROR writing to socket";
159+
}
160+
151161
libwebsocket_callback_on_writable(context, ws);
152162
packet.index = dataSize;
153163

0 commit comments

Comments
 (0)