Line data Source code
1 : /** 2 : Copyright (c) 2024 Stappler LLC <admin@stappler.dev> 3 : 4 : Permission is hereby granted, free of charge, to any person obtaining a copy 5 : of this software and associated documentation files (the "Software"), to deal 6 : in the Software without restriction, including without limitation the rights 7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 : copies of the Software, and to permit persons to whom the Software is 9 : furnished to do so, subject to the following conditions: 10 : 11 : The above copyright notice and this permission notice shall be included in 12 : all copies or substantial portions of the Software. 13 : 14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 : THE SOFTWARE. 21 : **/ 22 : 23 : #include "SPWebWebsocketConnection.h" 24 : 25 : namespace STAPPLER_VERSIONIZED stappler::web { 26 : 27 25 : void WebsocketConnection::destroy(WebsocketConnection *conn) { 28 25 : auto p = conn->_pool; 29 25 : auto a = conn->_allocator; 30 : 31 25 : pool::destroy(p); 32 25 : allocator::destroy(a); 33 25 : } 34 : 35 0 : WebsocketConnection::~WebsocketConnection() { } 36 : 37 0 : void WebsocketConnection::terminate() { 38 0 : _shouldTerminate.clear(); 39 0 : wakeup(); 40 0 : } 41 : 42 0 : pool_t *WebsocketConnection::getHandlePool() const { 43 0 : return _commonReader->pool; 44 : } 45 : 46 750 : Host WebsocketConnection::getHost() const { 47 750 : return _group.getHost(); 48 : } 49 : 50 0 : bool WebsocketConnection::performAsync(const Callback<void(AsyncTask &)> &cb) const { 51 0 : if (!_enabled) { 52 0 : return false; 53 : } 54 0 : return _group.perform(cb); 55 : } 56 : 57 0 : void WebsocketConnection::setStatusCode(WebsocketStatusCode s, StringView r) { 58 0 : _serverCloseCode = s; 59 0 : if (!r.empty()) { 60 0 : _serverReason = r.str<Interface>(); 61 : } 62 0 : } 63 : 64 25 : void WebsocketConnection::setAccessRole(db::AccessRoleId id) { 65 25 : _accessRole = id; 66 25 : } 67 : 68 0 : WebsocketStatusCode WebsocketConnection::resolveStatus(WebsocketStatusCode code) { 69 0 : if (code == WebsocketStatusCode::Auto) { 70 0 : switch (_commonReader->error) { 71 0 : case WebsocketFrameReader::Error::NotInitialized: return WebsocketStatusCode::UnexceptedCondition; break; 72 0 : case WebsocketFrameReader::Error::ExtraIsNotEmpty: return WebsocketStatusCode::ProtocolError; break; 73 0 : case WebsocketFrameReader::Error::NotMasked: return WebsocketStatusCode::ProtocolError; break; 74 0 : case WebsocketFrameReader::Error::UnknownOpcode: return WebsocketStatusCode::ProtocolError; break; 75 0 : case WebsocketFrameReader::Error::InvalidSegment: return WebsocketStatusCode::ProtocolError; break; 76 0 : case WebsocketFrameReader::Error::InvalidSize: return WebsocketStatusCode::TooLarge; break; 77 0 : case WebsocketFrameReader::Error::InvalidAction: return WebsocketStatusCode::UnexceptedCondition; break; 78 0 : default: return WebsocketStatusCode::Ok; break; 79 : } 80 0 : } else if (code == WebsocketStatusCode::None) { 81 0 : return WebsocketStatusCode::Ok; 82 : } 83 0 : return code; 84 : } 85 : 86 25 : WebsocketConnection::WebsocketConnection(allocator_t *a, pool_t *p, HostController *c) 87 25 : : _allocator(a), _pool(p), _group(Host(c), [this] { 88 0 : wakeup(); 89 50 : }) { } 90 : 91 : }