/***************************************************************************************** * * * OpenSpace * * * * Copyright (c) 2014-2023 * * * * Permission is hereby granted, free of charge, to any person obtaining a copy of this * * software and associated documentation files (the "Software"), to deal in the Software * * without restriction, including without limitation the rights to use, copy, modify, * * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * * permit persons to whom the Software is furnished to do so, subject to the following * * conditions: * * * * The above copyright notice and this permission notice shall be included in all copies * * or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ #include #include #include #include #include #include #include namespace { constexpr const char* _loggerCat = "NetworkEngine"; } // namespace namespace openspace::softwareintegration::network { namespace { void eventLoop(std::weak_ptr networkStateWeakPtr) { while (!networkStateWeakPtr.expired()) { auto networkState = networkStateWeakPtr.lock(); if (networkState->shouldStopThreads) break; // The call to "pop" below will block execution // on this thread until interrupt is called try { auto pm = networkState->incomingMessages.pop(); messagehandler::handleMessage(pm); } catch (const ghoul::RuntimeError&) { break; } } } void serverLoop(std::weak_ptr networkStateWeakPtr) { while (!networkStateWeakPtr.expired()) { auto networkState = networkStateWeakPtr.lock(); if (networkState->shouldStopThreads|| !networkState->server || !networkState->server->isListening()) break; std::unique_ptr socket = networkState->server->awaitPendingTcpSocket(); if (!socket) return; socket->startStreams(); auto p = std::make_shared(std::move(socket)); std::lock_guard guard(networkState->softwareConnectionsMutex); auto [it, peerInserted] = networkState->softwareConnections.emplace(p->id(), std::move(p)); if (peerInserted) { auto connectionWeak = std::weak_ptr{ it->second }; auto thread = std::thread{ [connectionWeak, networkStateWeakPtr] { connection::eventLoop(connectionWeak, networkStateWeakPtr); } }; it->second->setThread(thread); } } } } // namespace std::shared_ptr serve(const int port) { auto networkState = std::make_shared(); // 4700, is the defualt port where the tcp socket will be opened to the ext. software networkState->server = std::make_unique(); networkState->server->listen(port); std::weak_ptr networkStateWeakPtr = networkState; networkState->serverThread = std::make_unique([networkStateWeakPtr] { serverLoop(networkStateWeakPtr); }); networkState->eventLoopThread = std::make_unique([networkStateWeakPtr] { eventLoop(networkStateWeakPtr); }); return networkState; }; void stopServer(std::shared_ptr networkState) { if (networkState->hasStopped) return; networkState->hasStopped = true; networkState->shouldStopThreads = true; networkState->incomingMessages.interrupt(); networkState->server->close(); { std::lock_guard guardSoftwareConnections(networkState->softwareConnectionsMutex); networkState->softwareConnections.clear(); } networkState->serverThread->join(); networkState->eventLoopThread->join(); } SoftwareConnectionLostError::SoftwareConnectionLostError(const std::string& msg) : ghoul::RuntimeError(std::format("{}{}", "Software connection lost", msg), "SoftwareConnection") {} } // namespace openspace::softwareintegration::network