64 lines
2.1 KiB
C++
64 lines
2.1 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>
|
|
class TcpClientAcceptImpl : public ITcpClientAccept, public std::enable_shared_from_this<TcpClientAcceptImpl>
|
|
{
|
|
public:
|
|
TcpClientAcceptImpl(const hio_t *io, const void *object);
|
|
virtual ~TcpClientAcceptImpl() = default;
|
|
void SetParam(const ClientAcceptParam ¶m) 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:
|
|
std::mutex mMutex;
|
|
const hio_t *mIo;
|
|
ClientAcceptParam mParam;
|
|
const void *mObjectThis;
|
|
};
|
|
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);
|
|
void AddClient(hio_t *io, std::shared_ptr<ITcpClientAccept> *client);
|
|
std::shared_ptr<ITcpClientAccept> *GetClient(hio_t *io);
|
|
void RemoveClient(hio_t *io);
|
|
void FreeClients(void);
|
|
|
|
private:
|
|
std::mutex mMutex;
|
|
hloop_t *mLoop;
|
|
hio_t *mIo;
|
|
const TcpServerParam mParam;
|
|
std::thread mTcpServerThread;
|
|
std::map<int, std::shared_ptr<ITcpClientAccept> *> mClients;
|
|
};
|
|
std::shared_ptr<ITcpServer> *NewTcpServer(const TcpServerParam ¶m);
|
|
std::shared_ptr<ITcpClientAccept> *NewTcpClientAccept(const hio_t *io);
|
|
#endif |