1 #ifndef DALI_INTERNAL_ADAPTOR_NETWORK_PERFORMANCE_SERVER_H
2 #define DALI_INTERNAL_ADAPTOR_NETWORK_PERFORMANCE_SERVER_H
5 * Copyright (c) 2019 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.
23 #include <dali/devel-api/threading/mutex.h>
24 #include <dali/public-api/common/dali-vector.h>
27 #include <dali/internal/system/common/environment-options.h>
28 #include <dali/internal/network/common/network-performance-client.h>
29 #include <dali/internal/adaptor/common/adaptor-internal-services.h>
40 class SocketInterface;
41 class PerformanceMarker;
44 * @brief The class listens for incoming connections on a dedicated thread.
46 * When a new connection is established a client thread is spawned to
47 * handle that connection, along with a NetworkPerformanceClient object.
48 * The NetworkPerformanceClient object performs processing of incoming
49 * commands and holds the per-client state information for performance monitoring.
53 * - Spawns a thread to listen for incoming connections
54 * <---- New connection
55 * - Spawns a client thread to communicate with new client
58 * - Stops listening thread
59 * - Stops all client threads
61 class NetworkPerformanceServer : public ClientSendDataInterface
68 * @param[in] adaptorServices adaptor internal services
69 * @param[in] logOptions log options
71 NetworkPerformanceServer( AdaptorInternalServices& adaptorServices, const EnvironmentOptions& logOptions );
75 * @brief Start the server, to be called form Dali main thread
76 * @pre Can only be called form Dali main thread
81 * @brief Stop the server
82 * @pre Can only be called form Dali main thread
87 * @return true if the server is running
89 bool IsRunning() const;
92 * @brief Transmit a marker to any clients are listening for this marker.
93 * @param[in] marker performance marker
94 * @param[in] description marker description
95 * @pre Can be called from any thread
98 void TransmitMarker( const PerformanceMarker& marker, const char* const description );
103 ~NetworkPerformanceServer();
105 protected: // ClientSendDataInterface
108 * @copydoc ClientSendDataInterface::ClientSendDataInterface()
110 virtual void SendData( const char* const data, unsigned int bufferSizeInBytes, unsigned int clientId );
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 const EnvironmentOptions& mLogOptions; ///< log options
165 Dali::Vector< NetworkPerformanceClient* > mClients; ///< list of connected clients
166 pthread_t mServerThread; ///< thread that listens for new connections
167 SocketInterface* mListeningSocket; ///< socket used to listen for new connections
168 Dali::Mutex mClientListMutex; ///< mutex
169 unsigned int mClientUniqueId; ///< increments for every client connection
170 volatile unsigned int mClientCount; ///< client count
171 bool mLogFunctionInstalled; ///< whether the log function is installed
176 } // namespace Internal
178 } // namespace Adaptor
182 #endif //DALI_INTERNAL_ADAPTOR_NETWORK_PERFORMANCE_SERVER_H