Skip to content

Commit

Permalink
add boolean message type, fix crash of a synchronization bug
Browse files Browse the repository at this point in the history
  • Loading branch information
melode11 committed Aug 25, 2015
1 parent 9d85d3e commit bbbc1e3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 8 deletions.
14 changes: 14 additions & 0 deletions src/internal/sio_packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ namespace sio
using namespace std;
void accept_message(message const& msg,Value& val, Document& doc,vector<shared_ptr<const string> >& buffers);

void accept_bool_message(bool_message const& msg, Value& val)
{
val.SetBool(msg.get_bool());
}

void accept_int_message(int_message const& msg, Value& val)
{
val.SetInt64(msg.get_int());
Expand Down Expand Up @@ -95,6 +100,11 @@ namespace sio
accept_string_message(*(static_cast<const string_message*>(msg_ptr)), val);
break;
}
case message::flag_boolean:
{
accept_bool_message(*(static_cast<const bool_message*>(msg_ptr)), val);
break;
}
case message::flag_binary:
{
accept_binary_message(*(static_cast<const binary_message*>(msg_ptr)), val,doc,buffers);
Expand Down Expand Up @@ -163,6 +173,10 @@ namespace sio
}
return ptr;
}
else if(value.IsBool())
{
return bool_message::create(value.GetBool());
}
return message::ptr();
}

Expand Down
31 changes: 30 additions & 1 deletion src/sio_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ namespace sio
flag_string,
flag_binary,
flag_array,
flag_object
flag_object,
flag_boolean
};

virtual ~message(){};
Expand All @@ -39,6 +40,12 @@ namespace sio
}

typedef shared_ptr<message> ptr;

virtual bool get_bool() const
{
assert(false);
return false;
}

virtual int64_t get_int() const
{
Expand Down Expand Up @@ -105,6 +112,28 @@ namespace sio
protected:
message(flag f):_flag(f){}
};

class bool_message : public message
{
bool _v;

protected:
bool_message(bool v)
:message(flag_boolean),_v(v)
{
}

public:
static message::ptr create(bool v)
{
return ptr(new bool_message(v));
}

bool get_bool() const
{
return _v;
}
};

class int_message : public message
{
Expand Down
38 changes: 31 additions & 7 deletions src/sio_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ namespace sio
std::queue<packet> m_packet_queue;

std::mutex m_event_mutex;

std::mutex m_packet_mutex;

friend class socket;
};
Expand Down Expand Up @@ -306,9 +308,18 @@ namespace sio
{
m_connected = true;
m_client->on_socket_opened(m_nsp);
while (!m_packet_queue.empty()) {
m_client->send(m_packet_queue.front());

while (true) {
m_packet_mutex.lock();
if(m_packet_queue.empty())
{
m_packet_mutex.unlock();
return;
}
sio::packet front_pack = std::move(m_packet_queue.front());
m_packet_queue.pop();
m_packet_mutex.unlock();
m_client->send(front_pack);
}
}
}
Expand All @@ -325,9 +336,12 @@ namespace sio
m_connection_timer.reset();
}
m_connected = false;
while (!m_packet_queue.empty()) {
m_packet_queue.pop();
}
{
std::lock_guard<std::mutex> guard(m_packet_mutex);
while (!m_packet_queue.empty()) {
m_packet_queue.pop();
}
}
client->on_socket_closed(m_nsp);
client->remove_socket(m_nsp);
}
Expand All @@ -343,6 +357,7 @@ namespace sio
if(m_connected)
{
m_connected = false;
std::lock_guard<std::mutex> guard(m_packet_mutex);
while (!m_packet_queue.empty()) {
m_packet_queue.pop();
}
Expand Down Expand Up @@ -479,14 +494,23 @@ namespace sio
NULL_GUARD(m_client);
if(m_connected)
{
while (!m_packet_queue.empty()) {
m_client->send(m_packet_queue.front());
while (true) {
m_packet_mutex.lock();
if(m_packet_queue.empty())
{
m_packet_mutex.unlock();
break;
}
sio::packet front_pack = std::move(m_packet_queue.front());
m_packet_queue.pop();
m_packet_mutex.unlock();
m_client->send(front_pack);
}
m_client->send(p);
}
else
{
std::lock_guard<std::mutex> guard(m_packet_mutex);
m_packet_queue.push(p);
}
}
Expand Down

0 comments on commit bbbc1e3

Please sign in to comment.