add boolean and add missing protocol error close constant (#41)
This commit is contained in:
		
				
					committed by
					
						
						Benjamin Sergeant
					
				
			
			
				
	
			
			
			
						parent
						
							0caf875399
						
					
				
				
					commit
					28210ee31d
				
			@@ -45,11 +45,11 @@ namespace ix
 | 
				
			|||||||
        _pingTimeoutSecs(kDefaultPingTimeoutSecs)
 | 
					        _pingTimeoutSecs(kDefaultPingTimeoutSecs)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        _ws.setOnCloseCallback(
 | 
					        _ws.setOnCloseCallback(
 | 
				
			||||||
            [this](uint16_t code, const std::string& reason, size_t wireSize)
 | 
					            [this](uint16_t code, const std::string& reason, size_t wireSize, bool remote)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                _onMessageCallback(WebSocket_MessageType_Close, "", wireSize,
 | 
					                _onMessageCallback(WebSocket_MessageType_Close, "", wireSize,
 | 
				
			||||||
                                   WebSocketErrorInfo(), WebSocketOpenInfo(),
 | 
					                                   WebSocketErrorInfo(), WebSocketOpenInfo(),
 | 
				
			||||||
                                   WebSocketCloseInfo(code, reason));
 | 
					                                   WebSocketCloseInfo(code, reason, remote));
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -61,11 +61,14 @@ namespace ix
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        uint16_t code;
 | 
					        uint16_t code;
 | 
				
			||||||
        std::string reason;
 | 
					        std::string reason;
 | 
				
			||||||
 | 
					        bool remote;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        WebSocketCloseInfo(uint16_t c = 0,
 | 
					        WebSocketCloseInfo(uint16_t c = 0,
 | 
				
			||||||
                           const std::string& r = std::string())
 | 
					                           const std::string& r = std::string(),
 | 
				
			||||||
 | 
					                           bool rem = false)
 | 
				
			||||||
            : code(c)
 | 
					            : code(c)
 | 
				
			||||||
            , reason(r)
 | 
					            , reason(r)
 | 
				
			||||||
 | 
					            , remote(rem)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            ;
 | 
					            ;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -71,9 +71,11 @@ namespace ix
 | 
				
			|||||||
    constexpr size_t WebSocketTransport::kChunkSize;
 | 
					    constexpr size_t WebSocketTransport::kChunkSize;
 | 
				
			||||||
    const uint16_t WebSocketTransport::kInternalErrorCode(1011);
 | 
					    const uint16_t WebSocketTransport::kInternalErrorCode(1011);
 | 
				
			||||||
    const uint16_t WebSocketTransport::kAbnormalCloseCode(1006);
 | 
					    const uint16_t WebSocketTransport::kAbnormalCloseCode(1006);
 | 
				
			||||||
 | 
					    const uint16_t WebSocketTransport::kProtocolErrorCode(1002);
 | 
				
			||||||
    const std::string WebSocketTransport::kInternalErrorMessage("Internal error");
 | 
					    const std::string WebSocketTransport::kInternalErrorMessage("Internal error");
 | 
				
			||||||
    const std::string WebSocketTransport::kAbnormalCloseMessage("Abnormal closure");
 | 
					    const std::string WebSocketTransport::kAbnormalCloseMessage("Abnormal closure");
 | 
				
			||||||
    const std::string WebSocketTransport::kPingTimeoutMessage("Ping timeout");
 | 
					    const std::string WebSocketTransport::kPingTimeoutMessage("Ping timeout");
 | 
				
			||||||
 | 
					    const std::string WebSocketTransport::kProtocolErrorMessage("Protocol error");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    WebSocketTransport::WebSocketTransport() :
 | 
					    WebSocketTransport::WebSocketTransport() :
 | 
				
			||||||
        _useMask(true),
 | 
					        _useMask(true),
 | 
				
			||||||
@@ -81,6 +83,7 @@ namespace ix
 | 
				
			|||||||
        _closeCode(kInternalErrorCode),
 | 
					        _closeCode(kInternalErrorCode),
 | 
				
			||||||
        _closeReason(kInternalErrorMessage),
 | 
					        _closeReason(kInternalErrorMessage),
 | 
				
			||||||
        _closeWireSize(0),
 | 
					        _closeWireSize(0),
 | 
				
			||||||
 | 
					        _closeRemote(false),
 | 
				
			||||||
        _enablePerMessageDeflate(false),
 | 
					        _enablePerMessageDeflate(false),
 | 
				
			||||||
        _requestInitCancellation(false),
 | 
					        _requestInitCancellation(false),
 | 
				
			||||||
        _enablePong(kDefaultEnablePong),
 | 
					        _enablePong(kDefaultEnablePong),
 | 
				
			||||||
@@ -203,10 +206,11 @@ namespace ix
 | 
				
			|||||||
        if (readyStateValue == CLOSED)
 | 
					        if (readyStateValue == CLOSED)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            std::lock_guard<std::mutex> lock(_closeDataMutex);
 | 
					            std::lock_guard<std::mutex> lock(_closeDataMutex);
 | 
				
			||||||
            _onCloseCallback(_closeCode, _closeReason, _closeWireSize);
 | 
					            _onCloseCallback(_closeCode, _closeReason, _closeWireSize, _closeRemote);
 | 
				
			||||||
            _closeCode = kInternalErrorCode;
 | 
					            _closeCode = kInternalErrorCode;
 | 
				
			||||||
            _closeReason = kInternalErrorMessage;
 | 
					            _closeReason = kInternalErrorMessage;
 | 
				
			||||||
            _closeWireSize = 0;
 | 
					            _closeWireSize = 0;
 | 
				
			||||||
 | 
					            _closeRemote = false;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        _readyState = readyStateValue;
 | 
					        _readyState = readyStateValue;
 | 
				
			||||||
@@ -302,6 +306,7 @@ namespace ix
 | 
				
			|||||||
                        _closeCode = kAbnormalCloseCode;
 | 
					                        _closeCode = kAbnormalCloseCode;
 | 
				
			||||||
                        _closeReason = kAbnormalCloseMessage;
 | 
					                        _closeReason = kAbnormalCloseMessage;
 | 
				
			||||||
                        _closeWireSize = 0;
 | 
					                        _closeWireSize = 0;
 | 
				
			||||||
 | 
					                        _closeRemote = true;
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                    setReadyState(CLOSED);
 | 
					                    setReadyState(CLOSED);
 | 
				
			||||||
                    break;
 | 
					                    break;
 | 
				
			||||||
@@ -544,12 +549,16 @@ namespace ix
 | 
				
			|||||||
                // Get the reason.
 | 
					                // Get the reason.
 | 
				
			||||||
                std::string reason(_rxbuf.begin()+ws.header_size + 2,
 | 
					                std::string reason(_rxbuf.begin()+ws.header_size + 2,
 | 
				
			||||||
                                   _rxbuf.begin()+ws.header_size + (size_t) ws.N);
 | 
					                                   _rxbuf.begin()+ws.header_size + (size_t) ws.N);
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                bool remote = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                close(code, reason, _rxbuf.size());
 | 
					                close(code, reason, _rxbuf.size(), remote);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            else
 | 
					            else
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                close();
 | 
					                // Unexpected frame type
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                close(kProtocolErrorCode, kProtocolErrorMessage, _rxbuf.size());
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // Erase the message that has been processed from the input/read buffer
 | 
					            // Erase the message that has been processed from the input/read buffer
 | 
				
			||||||
@@ -850,7 +859,7 @@ namespace ix
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void WebSocketTransport::close(uint16_t code, const std::string& reason, size_t closeWireSize)
 | 
					    void WebSocketTransport::close(uint16_t code, const std::string& reason, size_t closeWireSize, bool remote)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        _requestInitCancellation = true;
 | 
					        _requestInitCancellation = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -878,6 +887,7 @@ namespace ix
 | 
				
			|||||||
            _closeCode = code;
 | 
					            _closeCode = code;
 | 
				
			||||||
            _closeReason = reason;
 | 
					            _closeReason = reason;
 | 
				
			||||||
            _closeWireSize = closeWireSize;
 | 
					            _closeWireSize = closeWireSize;
 | 
				
			||||||
 | 
					            _closeRemote = remote;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        setReadyState(CLOSED);
 | 
					        setReadyState(CLOSED);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -62,7 +62,8 @@ namespace ix
 | 
				
			|||||||
                                                     MessageKind)>;
 | 
					                                                     MessageKind)>;
 | 
				
			||||||
        using OnCloseCallback = std::function<void(uint16_t,
 | 
					        using OnCloseCallback = std::function<void(uint16_t,
 | 
				
			||||||
                                                   const std::string&,
 | 
					                                                   const std::string&,
 | 
				
			||||||
                                                   size_t)>;
 | 
					                                                   size_t,
 | 
				
			||||||
 | 
					                                                   bool)>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        WebSocketTransport();
 | 
					        WebSocketTransport();
 | 
				
			||||||
        ~WebSocketTransport();
 | 
					        ~WebSocketTransport();
 | 
				
			||||||
@@ -86,7 +87,8 @@ namespace ix
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        void close(uint16_t code = 1000,
 | 
					        void close(uint16_t code = 1000,
 | 
				
			||||||
                   const std::string& reason = "Normal closure",
 | 
					                   const std::string& reason = "Normal closure",
 | 
				
			||||||
                   size_t closeWireSize = 0);
 | 
					                   size_t closeWireSize = 0,
 | 
				
			||||||
 | 
					                   bool remote = false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        ReadyStateValues getReadyState() const;
 | 
					        ReadyStateValues getReadyState() const;
 | 
				
			||||||
        void setReadyState(ReadyStateValues readyStateValue);
 | 
					        void setReadyState(ReadyStateValues readyStateValue);
 | 
				
			||||||
@@ -150,6 +152,7 @@ namespace ix
 | 
				
			|||||||
        uint16_t _closeCode;
 | 
					        uint16_t _closeCode;
 | 
				
			||||||
        std::string _closeReason;
 | 
					        std::string _closeReason;
 | 
				
			||||||
        size_t _closeWireSize;
 | 
					        size_t _closeWireSize;
 | 
				
			||||||
 | 
					        bool _closeRemote;
 | 
				
			||||||
        mutable std::mutex _closeDataMutex;
 | 
					        mutable std::mutex _closeDataMutex;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Data used for Per Message Deflate compression (with zlib)
 | 
					        // Data used for Per Message Deflate compression (with zlib)
 | 
				
			||||||
@@ -163,9 +166,11 @@ namespace ix
 | 
				
			|||||||
        // Constants for dealing with closing conneections
 | 
					        // Constants for dealing with closing conneections
 | 
				
			||||||
        static const uint16_t kInternalErrorCode;
 | 
					        static const uint16_t kInternalErrorCode;
 | 
				
			||||||
        static const uint16_t kAbnormalCloseCode;
 | 
					        static const uint16_t kAbnormalCloseCode;
 | 
				
			||||||
 | 
					        static const uint16_t kProtocolErrorCode;
 | 
				
			||||||
        static const std::string kInternalErrorMessage;
 | 
					        static const std::string kInternalErrorMessage;
 | 
				
			||||||
        static const std::string kAbnormalCloseMessage;
 | 
					        static const std::string kAbnormalCloseMessage;
 | 
				
			||||||
        static const std::string kPingTimeoutMessage;
 | 
					        static const std::string kPingTimeoutMessage;
 | 
				
			||||||
 | 
					        static const std::string kProtocolErrorMessage;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // enable auto response to ping
 | 
					        // enable auto response to ping
 | 
				
			||||||
        bool _enablePong;
 | 
					        bool _enablePong;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user