Skip to content

Commit ad0cd77

Browse files
committed
use socket to store pending packet, send them once connected.
1 parent c8aeb09 commit ad0cd77

8 files changed

Lines changed: 217 additions & 201 deletions

File tree

examples/QT/SioChatDemo/SioChatDemo.pro

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ SOURCES += main.cpp\
1818
mainwindow.cpp \
1919
../../../src/sio_client.cpp \
2020
../../../src/sio_packet.cpp \
21+
../../../src/sio_socket.cpp \
22+
../../../src/internal/sio_client_impl.cpp \
2123
nicknamedialog.cpp
2224

2325
HEADERS += mainwindow.h \
2426
../../../src/sio_client.h \
2527
../../../src/sio_message.h \
28+
../../../src/sio_packet.h \
29+
../../../src/sio_socket.h \
30+
../../../src/internal/sio_client_impl.h \
2631
nicknamedialog.h
2732

2833
FORMS += mainwindow.ui \

examples/QT/SioChatDemo/mainwindow.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
#define BIND_EVENT(IO,EV,FN) \
88
do{ \
99
client::event_listener_aux l = FN;\
10-
IO->bind_event(EV,l);\
10+
IO->on(EV,l);\
1111
} while(0)
1212

1313
#else
1414
#define BIND_EVENT(IO,EV,FN) \
15-
IO->bind_event(EV,FN)
15+
IO->on(EV,FN)
1616
#endif
1717

1818
MainWindow::MainWindow(QWidget *parent) :
@@ -22,28 +22,14 @@ MainWindow::MainWindow(QWidget *parent) :
2222
m_dialog()
2323
{
2424
ui->setupUi(this);
25-
using std::placeholders::_1;
26-
using std::placeholders::_2;
27-
using std::placeholders::_3;
28-
using std::placeholders::_4;
29-
BIND_EVENT(_io,"new message",std::bind(&MainWindow::OnNewMessage,this,_1,_2,_3,_4));
30-
BIND_EVENT(_io,"user joined",std::bind(&MainWindow::OnUserJoined,this,_1,_2,_3,_4));
31-
BIND_EVENT(_io,"user left",std::bind(&MainWindow::OnUserLeft,this,_1,_2,_3,_4));
32-
BIND_EVENT(_io,"typing",std::bind(&MainWindow::OnTyping,this,_1,_2,_3,_4));
33-
BIND_EVENT(_io,"stop typing",std::bind(&MainWindow::OnStopTyping,this,_1,_2,_3,_4));
34-
BIND_EVENT(_io,"login",std::bind(&MainWindow::OnLogin,this,_1,_2,_3,_4));
35-
_io->set_connect_listener(std::bind(&MainWindow::OnConnected,this));
36-
_io->set_fail_listener(std::bind(&MainWindow::OnFailed,this));
37-
_io->set_close_listener(std::bind(&MainWindow::OnClosed,this,_1));
38-
3925
connect(this,SIGNAL(RequestAddListItem(QListWidgetItem*)),this,SLOT(AddListItem(QListWidgetItem*)));
4026
connect(this,SIGNAL(RequestToggleInputs(bool)),this,SLOT(ToggleInputs(bool)));
4127
}
4228

4329
MainWindow::~MainWindow()
4430
{
45-
_io->clear_event_bindings();
46-
_io->clear_con_listeners();
31+
_io->socket()->all_off();
32+
_io->socket()->clear_listeners();
4733
delete ui;
4834
}
4935

@@ -55,7 +41,7 @@ void MainWindow::SendBtnClicked()
5541
{
5642
QByteArray bytes = text.toUtf8();
5743
std::string msg(bytes.data(),bytes.length());
58-
_io->emit("new message",msg);
44+
_io->socket()->emit("new message",msg);
5945
text.append(":You");
6046
QListWidgetItem *item = new QListWidgetItem(text);
6147
item->setTextAlignment(Qt::AlignRight);
@@ -86,7 +72,7 @@ void MainWindow::showEvent(QShowEvent *event)
8672
void MainWindow::TypingStop()
8773
{
8874
m_timer.reset();
89-
_io->emit("stop typing","");
75+
_io->socket()->emit("stop typing","");
9076
}
9177

9278
void MainWindow::TypingChanged()
@@ -97,7 +83,7 @@ void MainWindow::TypingChanged()
9783
}
9884
else
9985
{
100-
_io->emit("typing","");
86+
_io->socket()->emit("typing","");
10187
}
10288
m_timer.reset(new QTimer(this));
10389
connect(m_timer.get(),SIGNAL(timeout()),this,SLOT(TypingStop()));
@@ -110,6 +96,20 @@ void MainWindow::NicknameAccept()
11096
m_name = m_dialog->getNickname();
11197
if(m_name.length()>0)
11298
{
99+
using std::placeholders::_1;
100+
using std::placeholders::_2;
101+
using std::placeholders::_3;
102+
using std::placeholders::_4;
103+
socket::ptr sock = _io->socket();
104+
BIND_EVENT(sock,"new message",std::bind(&MainWindow::OnNewMessage,this,_1,_2,_3,_4));
105+
BIND_EVENT(sock,"user joined",std::bind(&MainWindow::OnUserJoined,this,_1,_2,_3,_4));
106+
BIND_EVENT(sock,"user left",std::bind(&MainWindow::OnUserLeft,this,_1,_2,_3,_4));
107+
BIND_EVENT(sock,"typing",std::bind(&MainWindow::OnTyping,this,_1,_2,_3,_4));
108+
BIND_EVENT(sock,"stop typing",std::bind(&MainWindow::OnStopTyping,this,_1,_2,_3,_4));
109+
BIND_EVENT(sock,"login",std::bind(&MainWindow::OnLogin,this,_1,_2,_3,_4));
110+
sock->set_connect_listener(std::bind(&MainWindow::OnConnected,this));
111+
112+
sock->set_close_listener(std::bind(&MainWindow::OnFailed,this));
113113
_io->connect("ws://localhost:3000");
114114
}
115115
}
@@ -234,7 +234,7 @@ void MainWindow::OnConnected()
234234
{
235235
QByteArray bytes = m_name.toUtf8();
236236
std::string nickName(bytes.data(),bytes.length());
237-
_io->emit("add user", nickName);
237+
_io->socket()->emit("add user", nickName);
238238
}
239239

240240
void MainWindow::OnClosed(client::close_reason const& reason)

src/internal/sio_client_impl.cpp

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ namespace sio {
158158

159159
if(m_close_listener)
160160
{
161-
161+
162162
m_close_listener(reason);
163163
}
164164
}
@@ -314,19 +314,8 @@ namespace sio {
314314
m_ping_timer->expires_from_now(milliseconds(m_ping_interval),timeout_ec);
315315
m_ping_timer->async_wait(lib::bind(&client_impl::__ping,this,lib::placeholders::_1));
316316
}
317-
while(m_message_queue.size()>0)
318-
{
319-
message_queue_element element = m_message_queue.front();
320-
m_message_queue.pop();
321-
m_client.send(m_con,*(element.payload_ptr),element.opcode);
322-
}
323317
m_client.send(m_con,*payload_ptr,opcode);
324318
}
325-
else
326-
{
327-
message_queue_element element = INTIALIZER(message_queue_element){opcode,payload_ptr};
328-
m_message_queue.push(element);
329-
}
330319
}
331320

332321
void client_impl::__connect(const std::string& uri)
@@ -380,11 +369,6 @@ namespace sio {
380369
m_client.reset();
381370
m_sid.clear();
382371
m_packet_mgr.reset();
383-
//clear all queued messages.
384-
while(!m_message_queue.empty())
385-
{
386-
m_message_queue.pop();
387-
}
388372
}
389373

390374
void client_impl::connect(const std::string& uri)
@@ -408,7 +392,7 @@ namespace sio {
408392
this->reset_states();
409393
m_client.get_io_service().dispatch(lib::bind(&client_impl::__connect,this,uri));
410394
m_network_thread.reset(new std::thread(lib::bind(&client_impl::run_loop,this)));//uri lifecycle?
411-
395+
412396
}
413397

414398
void client_impl::reconnect(const std::string& uri)
@@ -490,4 +474,4 @@ namespace sio {
490474
((*(it->second)).*fn)();
491475
}
492476
}
493-
}
477+
}

src/internal/sio_client_impl.h

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ typedef websocketpp::config::asio_client client_config;
2323

2424
#include <memory>
2525
#include <map>
26-
#include <queue>
2726
#include <thread>
2827
#include "../sio_client.h"
2928
#include "../sio_packet.h"
@@ -33,9 +32,9 @@ namespace sio
3332
using namespace websocketpp;
3433

3534
typedef websocketpp::client<client_config> client_type;
36-
35+
3736
class client_impl {
38-
37+
3938
protected:
4039
enum con_state
4140
{
@@ -46,126 +45,117 @@ namespace sio
4645
};
4746

4847
client_impl();
49-
48+
5049
~client_impl();
51-
50+
5251
//set listeners and event bindings.
5352
#define SYNTHESIS_SETTER(__TYPE__,__FIELD__) \
5453
void set_##__FIELD__(__TYPE__ const& l) \
5554
{ m_##__FIELD__ = l;}
56-
55+
5756
SYNTHESIS_SETTER(client::con_listener,open_listener)
58-
57+
5958
SYNTHESIS_SETTER(client::con_listener,fail_listener)
60-
59+
6160
SYNTHESIS_SETTER(client::close_listener,close_listener)
62-
61+
6362
#undef SYNTHESIS_SETTER
64-
63+
6564

6665
void clear_con_listeners()
6766
{
6867
m_open_listener = nullptr;
6968
m_close_listener = nullptr;
7069
m_fail_listener = nullptr;
7170
}
72-
71+
7372
// Client Functions - such as send, etc.
7473
void connect(const std::string& uri);
75-
74+
7675
void reconnect(const std::string& uri);
7776

7877
socket::ptr const& socket(const std::string& nsp);
79-
78+
8079
// Closes the connection
8180
void close();
82-
81+
8382
void sync_close();
84-
83+
8584
bool opened() const { return m_con_state == con_opened; }
86-
85+
8786
std::string const& get_sessionid() const { return m_sid; }
88-
87+
8988
friend class client;
9089
protected:
9190
void send(packet& p);
92-
91+
9392
void remove_socket(std::string const& nsp);
9493

9594
boost::asio::io_service& get_io_service();
96-
95+
9796
private:
9897
void __close(close::status::value const& code,std::string const& reason);
99-
98+
10099
void __connect(const std::string& uri);
101-
100+
102101
void __send(std::shared_ptr<const std::string> const& payload_ptr,frame::opcode::value opcode);
103-
104-
102+
105103
void __ping(const boost::system::error_code& ec);
106-
104+
107105
void __timeout_pong(const boost::system::error_code& ec);
108-
106+
109107
void __timeout_connection(const boost::system::error_code& ec);
110108

111109
socket::ptr const& get_socket_locked(std::string const& nsp);
112-
110+
113111
void sockets_invoke_void(void (sio::socket::*fn)(void));
114-
112+
115113
void run_loop();
116-
114+
117115
void on_decode(packet const& pack);
118116
void on_encode(bool isBinary,shared_ptr<const string> const& payload);
119-
117+
120118
// Callbacks
121119
void on_fail(connection_hdl con);
122120
void on_open(connection_hdl con);
123121
void on_close(connection_hdl con);
124122
void on_message(connection_hdl con, client_type::message_ptr msg);
125123
void on_handshake(message::ptr const& message);
126-
124+
127125
void on_pong();
128-
126+
129127
void on_pong_timeout();
130-
128+
131129
void reset_states();
132-
130+
133131
void clear_timers();
134-
132+
135133
// Connection pointer for client functions.
136134
connection_hdl m_con;
137135
client_type m_client;
138136
// Socket.IO server settings
139137
std::string m_sid;
140138
unsigned int m_ping_interval;
141139
unsigned int m_ping_timeout;
142-
140+
143141
std::unique_ptr<std::thread> m_network_thread;
144-
145-
struct message_queue_element
146-
{
147-
frame::opcode::value opcode;
148-
std::shared_ptr<const std::string> payload_ptr;
149-
};
150-
142+
151143
packet_manager m_packet_mgr;
152-
153-
std::queue<message_queue_element> m_message_queue;
154-
144+
155145
std::unique_ptr<boost::asio::deadline_timer> m_ping_timer;
156-
146+
157147
std::unique_ptr<boost::asio::deadline_timer> m_ping_timeout_timer;
158-
148+
159149
con_state m_con_state;
160-
150+
161151
client::con_listener m_open_listener;
162152
client::con_listener m_fail_listener;
163153
client::close_listener m_close_listener;
164154

165155
std::map<const std::string,socket::ptr> m_sockets;
166156

167157
std::mutex m_socket_mutex;
168-
158+
169159
friend class sio::client;
170160
friend class sio::socket;
171161
};

src/sio_client.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#include "sio_client.h"
88
#include "internal/sio_client_impl.h"
99

10-
11-
1210
using namespace websocketpp;
1311
using boost::posix_time::milliseconds;
1412
using std::stringstream;

src/sio_client.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ namespace sio {
2727

2828
typedef std::function<void(close_reason const& reason)> close_listener;
2929

30-
typedef std::function<bool(event& ev)> event_filter;
31-
32-
3330
client();
3431
~client();
3532

0 commit comments

Comments
 (0)