Skip to content

Commit 2e687cb

Browse files
committed
working on close connection
1 parent dbde506 commit 2e687cb

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

include/internal/WebSocketProtocol.h

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,26 @@ namespace SL {
5656
}
5757
template<class PARENTTYPE, class SOCKETTYPE>inline void startwrite(const PARENTTYPE& parent, const SOCKETTYPE& socket) {
5858
if (!socket->SendMessageQueue.empty()) {
59-
auto msg(socket->SendMessageQueue.front());
60-
if (socket->SocketStatus_ == SocketStatus::CONNECTED) {
61-
//only send messages if the socket is in a connected state
62-
write(parent, socket, msg.msg);
63-
//update the socket status to reflect it is closing to prevent other messages from being sent.. this is the last valid message
64-
//make sure to do this after a call to write so the write process sends the close message, but no others
65-
if (msg.msg.code == OpCode::CLOSE) {
66-
socket->SocketStatus_ = SocketStatus::CLOSING;
59+
if(socket->SocketStatus_ == SocketStatus::CONNECTED){
60+
auto msg(socket->SendMessageQueue.front());
61+
write(parent, socket, msg.msg);
62+
} else if (socket->SocketStatus_ == SocketStatus::CLOSING){
63+
//find the close message and discard all others
64+
auto discardedcount=0;
65+
while(!socket->SendMessageQueue.empty()){
66+
auto msg(socket->SendMessageQueue.front());
67+
if(msg.msg.code == OpCode::CLOSE){
68+
socket->SendMessageQueue.clear();//remove any remaining messages
69+
write(parent, socket, msg.msg);
70+
} else {
71+
socket->SendMessageQueue.pop_front();
72+
}
73+
discardedcount+=1;
6774
}
75+
SL_WS_LITE_LOG(Logging_Levels::INFO_log_level, "discardedcount " <<discardedcount<< "remianing "<<socket->SendMessageQueue.size());
76+
socket->SendMessageQueue.clear();//just in case
6877
}
78+
6979
}
7080
else {
7181
writeexpire_from_now(parent, socket, std::chrono::seconds(0));// make sure the write timer doesnt kick off
@@ -78,6 +88,11 @@ namespace SL {
7888

7989
socket->strand.post([socket, msg, parent, compressmessage]() {
8090
if (socket->SocketStatus_ == SocketStatus::CONNECTED) {
91+
//update the socket status to reflect it is closing to prevent other messages from being sent.. this is the last valid message
92+
//make sure to do this after a call to write so the write process sends the close message, but no others
93+
if (msg.code == OpCode::CLOSE) {
94+
socket->SocketStatus_ = SocketStatus::CLOSING;
95+
}
8196
socket->SendMessageQueue.emplace_back(SendQueueItem{ msg, compressmessage });
8297
if (socket->SendMessageQueue.size() == 1) {
8398
SL::WS_LITE::startwrite(parent, socket);

0 commit comments

Comments
 (0)