Skip to content

Commit

Permalink
add varialbe args support in event callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
melode11 committed Apr 22, 2015
1 parent 3440d58 commit 0b0f265
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 16 deletions.
17 changes: 17 additions & 0 deletions src/sio_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,18 @@ namespace sio
{
}

list(message::list&& rhs):
m_vector(std::move(rhs.m_vector))
{

}

list(message::list const& rhs):
m_vector(rhs.m_vector)
{

}

list(message::ptr const& message)
{
if(message)
Expand Down Expand Up @@ -285,6 +297,11 @@ namespace sio
return m_vector[i];
}

const message::ptr& operator[] (size_t i) const
{
return m_vector[i];
}

message::ptr to_array_message(string const& event_name) const
{
message::ptr arr = array_message::create();
Expand Down
43 changes: 32 additions & 11 deletions src/sio_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace sio
return std::bind(&event_adapter::adapt_func, func,std::placeholders::_1);
}

static inline event create_event(std::string const& nsp,std::string const& name,message::ptr const& message,bool need_ack)
static inline event create_event(std::string const& nsp,std::string const& name,message::list&& message,bool need_ack)
{
return event(nsp,name,message,need_ack);
}
Expand All @@ -51,7 +51,19 @@ namespace sio
inline
const message::ptr& event::get_message() const
{
return m_message;
if(m_messages.size()>0)
return m_messages[0];
else
{
static message::ptr null_ptr;
return null_ptr;
}
}

inline
const message::list& event::get_messages() const
{
return m_messages;
}

inline
Expand All @@ -68,10 +80,19 @@ namespace sio
}

inline
event::event(std::string const& nsp,std::string const& name,message::ptr const& message,bool need_ack):
event::event(std::string const& nsp,std::string const& name,message::list&& messages,bool need_ack):
m_nsp(nsp),
m_name(name),
m_message(message),
m_messages(std::move(messages)),
m_need_ack(need_ack)
{
}

inline
event::event(std::string const& nsp,std::string const& name,message::list const& messages,bool need_ack):
m_nsp(nsp),
m_name(name),
m_messages(messages),
m_need_ack(need_ack)
{
}
Expand Down Expand Up @@ -133,7 +154,7 @@ namespace sio
private:

// Message Parsing callbacks.
void on_socketio_event(const std::string& nsp, int msgId,const std::string& name, message::ptr const& message);
void on_socketio_event(const std::string& nsp, int msgId,const std::string& name, message::list&& message);
void on_socketio_ack(int msgId, message::ptr const& message);
void on_socketio_error(message::ptr const& err_message);

Expand Down Expand Up @@ -359,12 +380,12 @@ namespace sio
if(array_ptr->get_vector().size() >= 1&&array_ptr->get_vector()[0]->get_flag() == message::flag_string)
{
const string_message* name_ptr = static_cast<const string_message*>(array_ptr->get_vector()[0].get());
message::ptr value_ptr;
if(array_ptr->get_vector().size()>1)
message::list mlist;
for(size_t i = 1;i<array_ptr->get_vector().size();++i)
{
value_ptr = array_ptr->get_vector()[1];
mlist.push(array_ptr->get_vector()[i]);
}
this->on_socketio_event(p.get_nsp(), p.get_pack_id(),name_ptr->get_string(), value_ptr);
this->on_socketio_event(p.get_nsp(), p.get_pack_id(),name_ptr->get_string(), std::move(mlist));
}
}

Expand Down Expand Up @@ -406,10 +427,10 @@ namespace sio
}
}

void socket::impl::on_socketio_event(const std::string& nsp,int msgId,const std::string& name, message::ptr const& message)
void socket::impl::on_socketio_event(const std::string& nsp,int msgId,const std::string& name, message::list && message)
{
bool needAck = msgId >= 0;
event ev = event_adapter::create_event(nsp,name, message,needAck);
event ev = event_adapter::create_event(nsp,name, std::move(message),needAck);
event_listener func = this->get_bind_listener_locked(name);
if(func)func(ev);
if(needAck)
Expand Down
9 changes: 6 additions & 3 deletions src/sio_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace sio
const std::string& get_name() const;

const message::ptr& get_message() const;

const message::list& get_messages() const;

bool need_ack() const;

Expand All @@ -22,14 +24,15 @@ namespace sio
message::ptr const& get_ack_message() const;

protected:
event(std::string const& nsp,std::string const& name,message::ptr const& message,bool need_ack);

event(std::string const& nsp,std::string const& name,message::list const& messages,bool need_ack);
event(std::string const& nsp,std::string const& name,message::list&& messages,bool need_ack);

message::ptr& get_ack_message_impl();

private:
const std::string m_nsp;
const std::string m_name;
const message::ptr m_message;
const message::list m_messages;
const bool m_need_ack;
message::ptr m_ack_message;

Expand Down
4 changes: 2 additions & 2 deletions test/sio_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ BOOST_AUTO_TEST_CASE( test_packet_accept_4 )
std::string json = payload.substr(json_start);
nlohmann::json j = nlohmann::json::parse(json);
BOOST_CHECK_MESSAGE(j["desc"].get<std::string>() == "Bin of 100 bytes", std::string("outputing payload desc:") + j["desc"].get<std::string>());
BOOST_CHECK_MESSAGE(j["bin1"]["_placeholder"] == true , std::string("outputing payload bin1:") + j["bin1"].dump());
BOOST_CHECK_MESSAGE(j["bin2"]["_placeholder"] == true , std::string("outputing payload bin2:") + j["bin2"].dump());
BOOST_CHECK_MESSAGE((bool)j["bin1"]["_placeholder"] , std::string("outputing payload bin1:") + j["bin1"].dump());
BOOST_CHECK_MESSAGE((bool)j["bin2"]["_placeholder"] , std::string("outputing payload bin2:") + j["bin2"].dump());
int bin1Num = j["bin1"]["num"].get<int>();
char numchar[] = {0,0};
numchar[0] = bin1Num+'0';
Expand Down

0 comments on commit 0b0f265

Please sign in to comment.