embedded-framework/utils/TcpModule/src/TcpServerImpl.h
2024-08-12 17:13:12 +08:00

73 lines
3.6 KiB
C++

/*
* 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 <map>
#include <mutex>
#include <thread>
/**
* @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<TcpClientAcceptImpl>
{
public:
TcpClientAcceptImpl(const hio_t *io, const void *object, const ClientAcceptParam &param);
virtual ~TcpClientAcceptImpl() = default;
void Close(void) override; ///<Disconnect the client
void Readed(const void *data, size_t length) override; ///<The server reads the data received by tcp connection.
ssize_t Write(const void *data, size_t length) override;///<The server writes data to the tcp connection.
void Closed(void) override; ///<Used to perform some cleaning work or notify upper layer logic when a TCP client accepts a connection that is closed
private:
const hio_t *mIo; ///< Connected client socket
const void *mObjectThis;///<Used to verify whether this type of instance is legal
const ClientAcceptParam mParam;
};
/**
* @brief The TcpServerImpl class is responsible for listening to connection requests from clients and creating an
* instance of TcpClientAcceptImpl for each accepted connection.
*
*/
class TcpServerImpl : public ITcpServer, public std::enable_shared_from_this<TcpServerImpl>
{
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<ITcpClientAccept> *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<int, std::shared_ptr<ITcpClientAccept> *> mClients;///< Container for storing clients connected to servers
};
std::shared_ptr<ITcpServer> *NewTcpServer(const TcpServerParam &param);
std::shared_ptr<ITcpClientAccept> *NewTcpClientAccept(const hio_t *io, const ClientAcceptParam &param);
#endif