Skip to content

Commit bbbc1e3

Browse files
committed
add boolean message type, fix crash of a synchronization bug
1 parent 9d85d3e commit bbbc1e3

File tree

3 files changed

+75
-8
lines changed

3 files changed

+75
-8
lines changed

src/internal/sio_packet.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ namespace sio
1919
using namespace std;
2020
void accept_message(message const& msg,Value& val, Document& doc,vector<shared_ptr<const string> >& buffers);
2121

22+
void accept_bool_message(bool_message const& msg, Value& val)
23+
{
24+
val.SetBool(msg.get_bool());
25+
}
26+
2227
void accept_int_message(int_message const& msg, Value& val)
2328
{
2429
val.SetInt64(msg.get_int());
@@ -95,6 +100,11 @@ namespace sio
95100
accept_string_message(*(static_cast<const string_message*>(msg_ptr)), val);
96101
break;
97102
}
103+
case message::flag_boolean:
104+
{
105+
accept_bool_message(*(static_cast<const bool_message*>(msg_ptr)), val);
106+
break;
107+
}
98108
case message::flag_binary:
99109
{
100110
accept_binary_message(*(static_cast<const binary_message*>(msg_ptr)), val,doc,buffers);
@@ -163,6 +173,10 @@ namespace sio
163173
}
164174
return ptr;
165175
}
176+
else if(value.IsBool())
177+
{
178+
return bool_message::create(value.GetBool());
179+
}
166180
return message::ptr();
167181
}
168182

src/sio_message.h

+30-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ namespace sio
2626
flag_string,
2727
flag_binary,
2828
flag_array,
29-
flag_object
29+
flag_object,
30+
flag_boolean
3031
};
3132

3233
virtual ~message(){};
@@ -39,6 +40,12 @@ namespace sio
3940
}
4041

4142
typedef shared_ptr<message> ptr;
43+
44+
virtual bool get_bool() const
45+
{
46+
assert(false);
47+
return false;
48+
}
4249

4350
virtual int64_t get_int() const
4451
{
@@ -105,6 +112,28 @@ namespace sio
105112
protected:
106113
message(flag f):_flag(f){}
107114
};
115+
116+
class bool_message : public message
117+
{
118+
bool _v;
119+
120+
protected:
121+
bool_message(bool v)
122+
:message(flag_boolean),_v(v)
123+
{
124+
}
125+
126+
public:
127+
static message::ptr create(bool v)
128+
{
129+
return ptr(new bool_message(v));
130+
}
131+
132+
bool get_bool() const
133+
{
134+
return _v;
135+
}
136+
};
108137

109138
class int_message : public message
110139
{

src/sio_socket.cpp

+31-7
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ namespace sio
183183
std::queue<packet> m_packet_queue;
184184

185185
std::mutex m_event_mutex;
186+
187+
std::mutex m_packet_mutex;
186188

187189
friend class socket;
188190
};
@@ -306,9 +308,18 @@ namespace sio
306308
{
307309
m_connected = true;
308310
m_client->on_socket_opened(m_nsp);
309-
while (!m_packet_queue.empty()) {
310-
m_client->send(m_packet_queue.front());
311+
312+
while (true) {
313+
m_packet_mutex.lock();
314+
if(m_packet_queue.empty())
315+
{
316+
m_packet_mutex.unlock();
317+
return;
318+
}
319+
sio::packet front_pack = std::move(m_packet_queue.front());
311320
m_packet_queue.pop();
321+
m_packet_mutex.unlock();
322+
m_client->send(front_pack);
312323
}
313324
}
314325
}
@@ -325,9 +336,12 @@ namespace sio
325336
m_connection_timer.reset();
326337
}
327338
m_connected = false;
328-
while (!m_packet_queue.empty()) {
329-
m_packet_queue.pop();
330-
}
339+
{
340+
std::lock_guard<std::mutex> guard(m_packet_mutex);
341+
while (!m_packet_queue.empty()) {
342+
m_packet_queue.pop();
343+
}
344+
}
331345
client->on_socket_closed(m_nsp);
332346
client->remove_socket(m_nsp);
333347
}
@@ -343,6 +357,7 @@ namespace sio
343357
if(m_connected)
344358
{
345359
m_connected = false;
360+
std::lock_guard<std::mutex> guard(m_packet_mutex);
346361
while (!m_packet_queue.empty()) {
347362
m_packet_queue.pop();
348363
}
@@ -479,14 +494,23 @@ namespace sio
479494
NULL_GUARD(m_client);
480495
if(m_connected)
481496
{
482-
while (!m_packet_queue.empty()) {
483-
m_client->send(m_packet_queue.front());
497+
while (true) {
498+
m_packet_mutex.lock();
499+
if(m_packet_queue.empty())
500+
{
501+
m_packet_mutex.unlock();
502+
break;
503+
}
504+
sio::packet front_pack = std::move(m_packet_queue.front());
484505
m_packet_queue.pop();
506+
m_packet_mutex.unlock();
507+
m_client->send(front_pack);
485508
}
486509
m_client->send(p);
487510
}
488511
else
489512
{
513+
std::lock_guard<std::mutex> guard(m_packet_mutex);
490514
m_packet_queue.push(p);
491515
}
492516
}

0 commit comments

Comments
 (0)