/* * 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 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; void Readed(const void *data, size_t length) override; ssize_t Write(const void *data, size_t length) override; void Closed(void) override; private: const hio_t *mIo; const void *mObjectThis; const ClientAcceptParam mParam; }; class TcpServerImpl : public ITcpServer, public std::enable_shared_from_this { public: TcpServerImpl(const TcpServerParam param); virtual ~TcpServerImpl() = default; void Init(void) override; void UnInit(void) override; void Loop(void); void AddClient(hio_t *io); std::shared_ptr *GetClient(hio_t *io); void RemoveClient(hio_t *io); void FreeClients(void); void Closed(void); private: std::mutex mMutex; hloop_t *mLoop; hio_t *mIo; const TcpServerParam mParam; std::thread mTcpServerThread; std::map *> mClients; }; std::shared_ptr *NewTcpServer(const TcpServerParam ¶m); std::shared_ptr *NewTcpClientAccept(const hio_t *io, const ClientAcceptParam ¶m); #endif