close method change and fix code (#28)

* close method change and fix code

* missing mutex
This commit is contained in:
Kumamon38 2019-04-16 17:58:34 +02:00 committed by Benjamin Sergeant
parent 401fc39879
commit b2aca491b6
2 changed files with 20 additions and 18 deletions

View File

@ -474,14 +474,8 @@ namespace ix
std::string reason(_rxbuf.begin()+ws.header_size + 2,
_rxbuf.begin()+ws.header_size + 2 + (size_t) ws.N);
{
std::lock_guard<std::mutex> lock(_closeDataMutex);
_closeCode = code;
_closeReason = reason;
_closeWireSize = _rxbuf.size();
}
close();
close(code, reason, _rxbuf.size());
}
else
{
@ -781,7 +775,12 @@ namespace ix
_lastSendTimePoint = std::chrono::steady_clock::now();
}
void WebSocketTransport::close()
void WebSocketTransport::close(uint16_t code, const std::string& reason)
{
close(code, reason, 0);
}
void WebSocketTransport::close(uint16_t code, const std::string& reason, size_t closeWireSize)
{
_requestInitCancellation = true;
@ -789,21 +788,22 @@ namespace ix
// See list of close events here:
// https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
// We use 1000: normal closure.
//
// >>> struct.pack('!H', 1000)
// b'\x03\xe8'
//
const std::string normalClosure = std::string("\x03\xe8");
const std::string closure{(char)(code >> 8), (char)(code & 0xff)};
bool compress = false;
sendData(wsheader_type::CLOSE, normalClosure, compress);
sendData(wsheader_type::CLOSE, closure, compress);
setReadyState(CLOSING);
_socket->wakeUpFromPoll(Socket::kCloseRequest);
_socket->close();
_closeCode = 1000;
_closeReason = "Normal Closure";
{
std::lock_guard<std::mutex> lock(_closeDataMutex);
_closeCode = code;
_closeReason = reason;
_closeWireSize = closeWireSize;
}
setReadyState(CLOSED);
}

View File

@ -81,7 +81,7 @@ namespace ix
WebSocketSendInfo sendText(const std::string& message,
const OnProgressCallback& onProgressCallback);
WebSocketSendInfo sendPing(const std::string& message);
void close();
void close(uint16_t code = 1000, const std::string& reason = "Normal closure");
ReadyStateValues getReadyState() const;
void setReadyState(ReadyStateValues readyStateValue);
void setOnCloseCallback(const OnCloseCallback& onCloseCallback);
@ -164,6 +164,8 @@ namespace ix
// No data was send through the socket for longer than the heartbeat period
bool heartBeatPeriodExceeded();
void close(uint16_t code, const std::string& reason, size_t closeWireSize);
void sendOnSocket();
WebSocketSendInfo sendData(wsheader_type::opcode_type type,
const std::string& message,