/* * Copyright (c) 2023 Fancy Code. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef TCP_SERVER_IMPL_H #define TCP_SERVER_IMPL_H #include "ITcpServer.h" #include "TcpModule.h" #include "hloop.h" #include "hsocket.h" #include "hssl.h" #include #include #include /** * @brief The TcpClient Accept class instance is used to associate with the socket after connecting to the server and client, responsible for a series of operations (read/write, etc.) after the connection * */ class TcpClientAcceptImpl : public ITcpClientAccept, public std::enable_shared_from_this { public: TcpClientAcceptImpl(const hio_t *io, const void *object, const ClientAcceptParam ¶m); virtual ~TcpClientAcceptImpl() = default; void Close(void) override; /// { public: TcpServerImpl(const TcpServerParam param); virtual ~TcpServerImpl() = default; void Init(void) override; void UnInit(void) override; void Loop(void); ///< Run an event loop and release resources after completion or error. void AddClient(hio_t *io); ///< Add a newly connected client to the server connection management list. std::shared_ptr *GetClient(hio_t *io);///< Retrieve the corresponding client acceptance object based on the socket handle. void RemoveClient(hio_t *io); ///< Remove the data element in the map, that is, the connected client. void FreeClients(void); ///< Clear all connected clients. void Closed(void); ///< Stop listening to the event loop(mLoop) private: std::mutex mMutex; ///< A mutex lock used to synchronize access to shared resources. hloop_t *mLoop; ///< Event loop, listening for all io objects hio_t *mIo; ///< A server socket to listen whether a new client sends a connection request. const TcpServerParam mParam; std::thread mTcpServerThread; ///< A separate thread used to run event loops std::map *> mClients;///< Container for storing clients connected to servers }; std::shared_ptr *NewTcpServer(const TcpServerParam ¶m); std::shared_ptr *NewTcpClientAccept(const hio_t *io, const ClientAcceptParam ¶m); #endif