Skip to content

Commit bd89d13

Browse files
committed
unify emit interface
1 parent f6b4846 commit bd89d13

File tree

4 files changed

+87
-101
lines changed

4 files changed

+87
-101
lines changed

examples/QT/SioChatDemo/mainwindow.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void MainWindow::TypingChanged()
102102
}
103103
else
104104
{
105-
_io->socket()->emit("typing","");
105+
_io->socket()->emit("typing",nullptr);
106106
}
107107
m_timer.reset(new QTimer(this));
108108
connect(m_timer.get(),SIGNAL(timeout()),this,SLOT(TypingStop()));
@@ -234,6 +234,11 @@ void MainWindow::OnStopTyping(std::string const& name,message::ptr const& data,b
234234

235235
void MainWindow::OnLogin(std::string const& name,message::ptr const& data,bool hasAck,message::ptr &ack_resp)
236236
{
237+
message::ptr array = array_message::create();
238+
array->get_vector().push_back(string_message::create("arg1"));
239+
array->get_vector().push_back(string_message::create("arg2"));
240+
_io->socket()->emit("test va",array);
241+
237242
Q_EMIT RequestToggleInputs(true);
238243
int numUser = data->get_map()["numUsers"]->get_int();
239244

src/sio_message.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ namespace sio
2828
flag_object
2929
};
3030

31+
class list;
32+
3133
flag get_flag() const
3234
{
3335
return _flag;
@@ -226,6 +228,76 @@ namespace sio
226228
return _v;
227229
}
228230
};
231+
232+
class message::list
233+
{
234+
public:
235+
list(nullptr_t)
236+
{
237+
}
238+
239+
list(message::ptr const& message)
240+
{
241+
if(message)
242+
{
243+
m_vector.push_back(message);
244+
}
245+
}
246+
247+
list(string& text)
248+
{
249+
m_vector.push_back(string_message::create(text));
250+
}
251+
252+
list(const char* text)
253+
{
254+
if(text)
255+
{
256+
m_vector.push_back(string_message::create(text));
257+
}
258+
}
259+
260+
list(shared_ptr<const string> const& binary)
261+
{
262+
if(binary)
263+
{
264+
m_vector.push_back(binary_message::create(binary));
265+
}
266+
}
267+
268+
void push(message::ptr const& message)
269+
{
270+
if(message)
271+
{
272+
m_vector.push_back(message);
273+
}
274+
}
275+
276+
void insert(size_t pos,message::ptr const& message)
277+
{
278+
m_vector.insert(m_vector.begin()+pos, message);
279+
}
280+
281+
size_t size() const
282+
{
283+
return m_vector.size();
284+
}
285+
286+
const message::ptr& at(size_t i) const
287+
{
288+
return m_vector[i];
289+
}
290+
291+
message::ptr to_array_message(string const& event_name) const
292+
{
293+
message::ptr arr = array_message::create();
294+
arr->get_vector().push_back(string_message::create(event_name));
295+
arr->get_vector().insert(arr->get_vector().end(),m_vector.begin(),m_vector.end());
296+
}
297+
298+
private:
299+
vector<message::ptr> m_vector;
300+
};
229301

230302
inline
231303
message::ptr make_message(string const& event_name, message::ptr const& event_args)

src/sio_socket.cpp

Lines changed: 7 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <boost/asio/deadline_timer.hpp>
55
#include <boost/system/error_code.hpp>
66
#include <queue>
7+
#include <cstdarg>
78

89
#if DEBUG || _DEBUG
910
#define LOG(x) std::cout << x
@@ -114,17 +115,7 @@ void set_##__FIELD__(__TYPE__ const& l) \
114115

115116
void close();
116117

117-
void emit(std::string const& name, std::string const& message);
118-
119-
void emit(std::string const& name, std::string const& message, std::function<void (message::ptr const&)> const& ack);
120-
121-
void emit(std::string const& name, message::ptr const& args);
122-
123-
void emit(std::string const& name, message::ptr const& args, std::function<void (message::ptr const&)> const& ack);
124-
125-
void emit(std::string const& name, std::shared_ptr<const std::string> const& binary_ptr);
126-
127-
void emit(std::string const& name, std::shared_ptr<const std::string> const& binary_ptr, std::function<void (message::ptr const&)> const& ack);
118+
void emit(std::string const& name, message::list const& msglist, std::function<void (message::ptr const&)> const& ack);
128119

129120
std::string const& get_namespace() const {return m_nsp;}
130121

@@ -236,59 +227,12 @@ void set_##__FIELD__(__TYPE__ const& l) \
236227

237228
unsigned int socket::impl::s_global_event_id = 1;
238229

239-
void socket::impl::emit(std::string const& name, std::string const& message)
240-
{
241-
NULL_GUARD(m_client);
242-
message::ptr msg_ptr = make_message(name, message);
243-
packet p(m_nsp, msg_ptr);
244-
__send_packet(p);
245-
}
246-
247-
void socket::impl::emit(std::string const& name, std::string const& message, std::function<void (message::ptr const&)> const& ack)
248-
{
249-
NULL_GUARD(m_client);
250-
message::ptr msg_ptr = make_message(name, message);
251-
packet p(m_nsp, msg_ptr,s_global_event_id);
252-
{
253-
std::lock_guard<std::mutex> guard(m_event_mutex);
254-
m_acks[s_global_event_id++] = ack;
255-
}
256-
__send_packet(p);
257-
}
258-
259-
void socket::impl::emit(std::string const& name, message::ptr const& args)
260-
{
261-
NULL_GUARD(m_client);
262-
message::ptr msg_ptr = make_message(name, args);
263-
packet p(m_nsp, msg_ptr);
264-
__send_packet(p);
265-
}
266-
267-
void socket::impl::emit(std::string const& name, message::ptr const& args, std::function<void (message::ptr const&)> const& ack)
230+
void socket::impl::emit(std::string const& name, message::list const& msglist, std::function<void (message::ptr const&)> const& ack)
268231
{
269232
NULL_GUARD(m_client);
270-
message::ptr msg_ptr = make_message(name, args);
271-
packet p(m_nsp, msg_ptr,s_global_event_id);
272-
{
273-
std::lock_guard<std::mutex> guard(m_event_mutex);
274-
m_acks[s_global_event_id++] = ack;
275-
}
276-
__send_packet(p);
277-
}
278-
279-
void socket::impl::emit(std::string const& name, std::shared_ptr<const std::string> const& binary_ptr)
280-
{
281-
NULL_GUARD(m_client);
282-
message::ptr msg_ptr = make_message(name, binary_ptr);
283-
packet p(m_nsp, msg_ptr);
284-
__send_packet(p);
285-
}
286-
287-
void socket::impl::emit(std::string const& name, std::shared_ptr<const std::string> const& binary_ptr, std::function<void (message::ptr const&)> const& ack)
288-
{
289-
NULL_GUARD(m_client);
290-
message::ptr msg_ptr = make_message(name, binary_ptr);
233+
message::ptr msg_ptr = msglist.to_array_message(name);
291234
packet p(m_nsp, msg_ptr,s_global_event_id);
235+
if(ack)
292236
{
293237
std::lock_guard<std::mutex> guard(m_event_mutex);
294238
m_acks[s_global_event_id++] = ack;
@@ -579,41 +523,16 @@ void set_##__FIELD__(__TYPE__ const& l) \
579523
m_impl->off_error();
580524
}
581525

582-
void socket::emit(std::string const& name, std::string const& message)
526+
void socket::emit(std::string const& name, message::list const& msglist, std::function<void (message::ptr const&)> const& ack)
583527
{
584-
m_impl->emit(name, message);
585-
}
586-
587-
void socket::emit(std::string const& name, std::string const& message, std::function<void (message::ptr const&)> const& ack)
588-
{
589-
m_impl->emit(name, message,ack);
590-
}
591-
592-
void socket::emit(std::string const& name, message::ptr const& args)
593-
{
594-
m_impl->emit(name, args);
595-
}
596-
597-
void socket::emit(std::string const& name, message::ptr const& args, std::function<void (message::ptr const&)> const& ack)
598-
{
599-
m_impl->emit(name, args,ack);
600-
}
601-
602-
void socket::emit(std::string const& name, std::shared_ptr<const std::string> const& binary_ptr){
603-
m_impl->emit(name, binary_ptr);
604-
}
605-
606-
void socket::emit(std::string const& name, std::shared_ptr<const std::string> const& binary_ptr, std::function<void (message::ptr const&)> const& ack)
607-
{
608-
m_impl->emit(name, binary_ptr,ack);
528+
m_impl->emit(name, msglist,ack);
609529
}
610530

611531
std::string const& socket::get_namespace() const
612532
{
613533
return m_impl->get_namespace();
614534
}
615535

616-
617536
void socket::on_connected()
618537
{
619538
m_impl->on_connected();

src/sio_socket.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,8 @@ namespace sio
6666
void on_error(error_listener const& l);
6767

6868
void off_error();
69-
70-
void emit(std::string const& name, std::string const& message);
71-
72-
void emit(std::string const& name, std::string const& message, std::function<void (message::ptr const&)> const& ack);
73-
74-
void emit(std::string const& name, message::ptr const& args);
75-
76-
void emit(std::string const& name, message::ptr const& args, std::function<void (message::ptr const&)> const& ack);
77-
78-
void emit(std::string const& name, std::shared_ptr<const std::string> const& binary_ptr);
79-
80-
void emit(std::string const& name, std::shared_ptr<const std::string> const& binary_ptr, std::function<void (message::ptr const&)> const& ack);
69+
70+
void emit(std::string const& name, message::list const& msglist, std::function<void (message::ptr const&)> const& ack = nullptr);
8171

8272
std::string const& get_namespace() const;
8373

0 commit comments

Comments
 (0)