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 : #ifndef EXTRA_WEBSERVER_WEBSERVER_WEBSOCKET_SPWEBWEBSOCKETCONNECTION_H_ 24 : #define EXTRA_WEBSERVER_WEBSERVER_WEBSOCKET_SPWEBWEBSOCKETCONNECTION_H_ 25 : 26 : #include "SPWebWebsocket.h" 27 : #include "SPWebAsyncTask.h" 28 : 29 : namespace STAPPLER_VERSIONIZED stappler::web { 30 : 31 : class Request; 32 : class WebserverHandler; 33 : 34 : class WebsocketConnection : public AllocBase { 35 : public: 36 : static void destroy(WebsocketConnection *); 37 : 38 : virtual ~WebsocketConnection(); 39 : 40 0 : bool isEnabled() const { return _enabled; } 41 : 42 : virtual bool write(WebsocketFrameType t, const uint8_t *bytes = nullptr, size_t count = 0) = 0; 43 : 44 : virtual bool run(WebsocketHandler *, const Callback<void()> &beginCb, const Callback<void()> &endCb) = 0; 45 : 46 : virtual void wakeup() = 0; 47 : 48 : virtual void terminate(); 49 : 50 : db::AccessRoleId getAccessRole() const { return _accessRole; } 51 : void setAccessRole(db::AccessRoleId); 52 : 53 : void setStatusCode(WebsocketStatusCode, StringView = StringView()); 54 : 55 25 : pool_t *getPool() const { return _pool; } 56 : 57 : pool_t *getHandlePool() const; 58 : 59 : Host getHost() const; 60 : 61 : bool performAsync(const Callback<void(AsyncTask &)> &cb) const; 62 : 63 : protected: 64 : // updates with last read status 65 : WebsocketStatusCode resolveStatus(WebsocketStatusCode code); 66 : 67 : WebsocketConnection(allocator_t *, pool_t *, HostController *); 68 : 69 : allocator_t *_allocator = nullptr; 70 : pool_t *_pool = nullptr; 71 : Mutex _mutex; 72 : std::atomic_flag _shouldTerminate; 73 : 74 : bool _enabled = false; 75 : bool _connected = true; 76 : 77 : String _serverReason; 78 : WebsocketStatusCode _clientCloseCode = WebsocketStatusCode::None; 79 : WebsocketStatusCode _serverCloseCode = WebsocketStatusCode::Auto; 80 : 81 : db::AccessRoleId _accessRole = db::AccessRoleId::Nobody; 82 : 83 : mutable AsyncTaskGroup _group; 84 : 85 : WebsocketFrameReader *_commonReader = nullptr; 86 : WebsocketFrameWriter *_commonWriter = nullptr; 87 : }; 88 : 89 : } 90 : 91 : #endif /* EXTRA_WEBSERVER_WEBSERVER_WEBSOCKET_SPWEBWEBSOCKETCONNECTION_H_ */