1 #ifndef DALI_INTERNAL_ADAPTOR_NETWORK_PERFORMANCE_SERVER_H
2 #define DALI_INTERNAL_ADAPTOR_NETWORK_PERFORMANCE_SERVER_H
5 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/devel-api/threading/mutex.h>
23 #include <dali/public-api/common/dali-vector.h>
27 #include <dali/devel-api/adaptor-framework/event-thread-callback.h>
28 #include <dali/internal/adaptor/common/adaptor-internal-services.h>
29 #include <dali/internal/network/common/network-performance-client.h>
30 #include <dali/internal/system/common/environment-options.h>
38 class SocketInterface;
39 class PerformanceMarker;
42 * @brief The class listens for incoming connections on a dedicated thread.
44 * When a new connection is established a client thread is spawned to
45 * handle that connection, along with a NetworkPerformanceClient object.
46 * The NetworkPerformanceClient object performs processing of incoming
47 * commands and holds the per-client state information for performance monitoring.
51 * - Spawns a thread to listen for incoming connections
52 * <---- New connection
53 * - Spawns a client thread to communicate with new client
56 * - Stops listening thread
57 * - Stops all client threads
59 class NetworkPerformanceServer : public ClientSendDataInterface
64 * @param[in] adaptorServices adaptor internal services
65 * @param[in] logOptions log options
67 NetworkPerformanceServer(AdaptorInternalServices& adaptorServices, const EnvironmentOptions& logOptions);
70 * @brief Start the server, to be called form Dali main thread
71 * @pre Can only be called form Dali main thread
76 * @brief Stop the server
77 * @pre Can only be called form Dali main thread
82 * @return true if the server is running
84 bool IsRunning() const;
87 * @brief Transmit a marker to any clients are listening for this marker.
88 * @param[in] marker performance marker
89 * @param[in] description marker description
90 * @pre Can be called from any thread
93 void TransmitMarker(const PerformanceMarker& marker, const char* const description);
98 ~NetworkPerformanceServer();
100 protected: // ClientSendDataInterface
102 * @copydoc ClientSendDataInterface::TriggerMainThreadAutomation()
104 void TriggerMainThreadAutomation(CallbackBase* callback) override;
107 * @copydoc ClientSendDataInterface::ClientSendDataInterface()
109 void SendData(const char* const data, unsigned int bufferSizeInBytes, unsigned int clientId) override;
111 void AutomationCallback();
115 * Helper for the thread calling the entry function.
116 * @param[in] This A pointer to the current RenderThread object
118 static void* ConnectionListenerFunc(void* This)
120 (static_cast<NetworkPerformanceServer*>(This))->ConnectionListener();
125 * Helper for the thread calling the entry function.
126 * @param[in] This A pointer to the current RenderThread object
128 static void* ClientThreadFunc(void* data);
131 * @brief Client thread function
132 * @param client network client object
134 void ClientThread(NetworkPerformanceClient* client);
137 * @brief Stop all client threads
142 * @brief Waits for new connections to be made
144 void ConnectionListener();
147 * @brief Add a new client to the client list
148 * @param clientSocket client socket
149 * @param clientThread client thread
152 NetworkPerformanceClient* AddClient(SocketInterface* clientSocket, pthread_t* clientThread);
155 * @brief Delete a client from the client list
156 * @param client network client
158 void DeleteClient(NetworkPerformanceClient* client);
160 NetworkPerformanceServer(const NetworkPerformanceServer&); ///< undefined copy constructor
161 NetworkPerformanceServer& operator=(const NetworkPerformanceServer&); ///< undefined assignment operator
163 SocketFactoryInterface& mSocketFactory; ///< used to create sockets
164 std::unique_ptr<EventThreadCallback> mTrigger; ///< For waking up main thread
165 CallbackBase* mClientCallback; ///< Wrong thread callback!
167 const EnvironmentOptions& mLogOptions; ///< log options
168 Dali::Vector<NetworkPerformanceClient*> mClients; ///< list of connected clients
169 pthread_t mServerThread; ///< thread that listens for new connections
170 SocketInterface* mListeningSocket; ///< socket used to listen for new connections
171 Dali::Mutex mClientListMutex; ///< mutex
172 unsigned int mClientUniqueId; ///< increments for every client connection
173 volatile unsigned int mClientCount; ///< client count
174 bool mLogFunctionInstalled; ///< whether the log function is installed
177 } // namespace Adaptor
179 } // namespace Internal
183 #endif //DALI_INTERNAL_ADAPTOR_NETWORK_PERFORMANCE_SERVER_H