1 #ifndef DALI_INTERNAL_ADAPTOR_NETWORK_PERFORMANCE_SERVER_H
2 #define DALI_INTERNAL_ADAPTOR_NETWORK_PERFORMANCE_SERVER_H
5 * Copyright (c) 2021 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/internal/adaptor/common/adaptor-internal-services.h>
28 #include <dali/internal/network/common/network-performance-client.h>
29 #include <dali/internal/system/common/environment-options.h>
37 class SocketInterface;
38 class PerformanceMarker;
41 * @brief The class listens for incoming connections on a dedicated thread.
43 * When a new connection is established a client thread is spawned to
44 * handle that connection, along with a NetworkPerformanceClient object.
45 * The NetworkPerformanceClient object performs processing of incoming
46 * commands and holds the per-client state information for performance monitoring.
50 * - Spawns a thread to listen for incoming connections
51 * <---- New connection
52 * - Spawns a client thread to communicate with new client
55 * - Stops listening thread
56 * - Stops all client threads
58 class NetworkPerformanceServer : public ClientSendDataInterface
63 * @param[in] adaptorServices adaptor internal services
64 * @param[in] logOptions log options
66 NetworkPerformanceServer(AdaptorInternalServices& adaptorServices, const EnvironmentOptions& logOptions);
69 * @brief Start the server, to be called form Dali main thread
70 * @pre Can only be called form Dali main thread
75 * @brief Stop the server
76 * @pre Can only be called form Dali main thread
81 * @return true if the server is running
83 bool IsRunning() const;
86 * @brief Transmit a marker to any clients are listening for this marker.
87 * @param[in] marker performance marker
88 * @param[in] description marker description
89 * @pre Can be called from any thread
92 void TransmitMarker(const PerformanceMarker& marker, const char* const description);
97 ~NetworkPerformanceServer();
99 protected: // ClientSendDataInterface
101 * @copydoc ClientSendDataInterface::ClientSendDataInterface()
103 void SendData(const char* const data, unsigned int bufferSizeInBytes, unsigned int clientId) override;
107 * Helper for the thread calling the entry function.
108 * @param[in] This A pointer to the current RenderThread object
110 static void* ConnectionListenerFunc(void* This)
112 (static_cast<NetworkPerformanceServer*>(This))->ConnectionListener();
117 * Helper for the thread calling the entry function.
118 * @param[in] This A pointer to the current RenderThread object
120 static void* ClientThreadFunc(void* data);
123 * @brief Client thread function
124 * @param client network client object
126 void ClientThread(NetworkPerformanceClient* client);
129 * @brief Stop all client threads
134 * @brief Waits for new connections to be made
136 void ConnectionListener();
139 * @brief Add a new client to the client list
140 * @param clientSocket client socket
141 * @param clientThread client thread
144 NetworkPerformanceClient* AddClient(SocketInterface* clientSocket, pthread_t* clientThread);
147 * @brief Delete a client from the client list
148 * @param client network client
150 void DeleteClient(NetworkPerformanceClient* client);
152 NetworkPerformanceServer(const NetworkPerformanceServer&); ///< undefined copy constructor
153 NetworkPerformanceServer& operator=(const NetworkPerformanceServer&); ///< undefined assignment operator
155 SocketFactoryInterface& mSocketFactory; ///< used to create sockets
156 const EnvironmentOptions& mLogOptions; ///< log options
157 Dali::Vector<NetworkPerformanceClient*> mClients; ///< list of connected clients
158 pthread_t mServerThread; ///< thread that listens for new connections
159 SocketInterface* mListeningSocket; ///< socket used to listen for new connections
160 Dali::Mutex mClientListMutex; ///< mutex
161 unsigned int mClientUniqueId; ///< increments for every client connection
162 volatile unsigned int mClientCount; ///< client count
163 bool mLogFunctionInstalled; ///< whether the log function is installed
166 } // namespace Adaptor
168 } // namespace Internal
172 #endif //DALI_INTERNAL_ADAPTOR_NETWORK_PERFORMANCE_SERVER_H