mirror of
https://gitee.com/jiuyilian/embedded-framework.git
synced 2025-01-06 10:16:51 -05:00
Fixed:TcpModule.
This commit is contained in:
parent
7ebd2e3aa7
commit
7fd6c72864
|
@ -62,16 +62,14 @@ void TcpClientImpl::Init(void)
|
||||||
}
|
}
|
||||||
void TcpClientImpl::UnInit(void)
|
void TcpClientImpl::UnInit(void)
|
||||||
{
|
{
|
||||||
if (nullptr != mLoop) {
|
if (nullptr != mIo) {
|
||||||
LogInfo("Stop loop.\n");
|
LogInfo("Close io.\n");
|
||||||
hloop_stop(mLoop);
|
hio_close(mIo);
|
||||||
|
mIo = nullptr;
|
||||||
}
|
}
|
||||||
if (mTcpClientThread.joinable()) {
|
if (mTcpClientThread.joinable()) {
|
||||||
mTcpClientThread.join();
|
mTcpClientThread.join();
|
||||||
}
|
}
|
||||||
hloop_free(&mLoop);
|
|
||||||
mLoop = nullptr;
|
|
||||||
mIo = nullptr;
|
|
||||||
}
|
}
|
||||||
void TcpClientImpl::Readed(const void *data, size_t length)
|
void TcpClientImpl::Readed(const void *data, size_t length)
|
||||||
{
|
{
|
||||||
|
@ -93,9 +91,11 @@ void TcpClientImpl::Closed(void)
|
||||||
{
|
{
|
||||||
if (nullptr != mParam.mClosedFunc) {
|
if (nullptr != mParam.mClosedFunc) {
|
||||||
mParam.mClosedFunc(mObjectThis);
|
mParam.mClosedFunc(mObjectThis);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
LogWarning("mParam.mClosedFunc is null\n");
|
if (nullptr != mLoop) {
|
||||||
|
LogInfo("Stop loop.\n");
|
||||||
|
hloop_stop(mLoop);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
void TcpClientImpl::Loop(void)
|
void TcpClientImpl::Loop(void)
|
||||||
{
|
{
|
||||||
|
@ -104,6 +104,8 @@ void TcpClientImpl::Loop(void)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
hloop_run(mLoop);
|
hloop_run(mLoop);
|
||||||
|
hloop_free(&mLoop);
|
||||||
|
mLoop = nullptr;
|
||||||
}
|
}
|
||||||
std::shared_ptr<ITcpClient> *NewTcpClient(const TcpClientParam ¶m)
|
std::shared_ptr<ITcpClient> *NewTcpClient(const TcpClientParam ¶m)
|
||||||
{
|
{
|
||||||
|
|
|
@ -21,6 +21,12 @@ static void on_close(hio_t *io)
|
||||||
TcpServerImpl *server = (TcpServerImpl *)hevent_userdata(io);
|
TcpServerImpl *server = (TcpServerImpl *)hevent_userdata(io);
|
||||||
server->RemoveClient(io);
|
server->RemoveClient(io);
|
||||||
}
|
}
|
||||||
|
static void server_on_close(hio_t *io)
|
||||||
|
{
|
||||||
|
LogInfo("server_on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
|
||||||
|
TcpServerImpl *server = (TcpServerImpl *)hevent_userdata(io);
|
||||||
|
server->Closed();
|
||||||
|
}
|
||||||
static void on_recv(hio_t *io, void *buf, int readbytes)
|
static void on_recv(hio_t *io, void *buf, int readbytes)
|
||||||
{
|
{
|
||||||
LogInfo("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes);
|
LogInfo("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes);
|
||||||
|
@ -104,6 +110,7 @@ void TcpServerImpl::Init(void)
|
||||||
LogInfo("listenfd=%d\n", hio_fd(listenio));
|
LogInfo("listenfd=%d\n", hio_fd(listenio));
|
||||||
hevent_set_userdata(listenio, this);
|
hevent_set_userdata(listenio, this);
|
||||||
mIo = listenio;
|
mIo = listenio;
|
||||||
|
hio_setcb_close(mIo, server_on_close);
|
||||||
std::shared_ptr<ITcpServer> server = shared_from_this();
|
std::shared_ptr<ITcpServer> server = shared_from_this();
|
||||||
std::shared_ptr<TcpServerImpl> impl = std::dynamic_pointer_cast<TcpServerImpl>(server);
|
std::shared_ptr<TcpServerImpl> impl = std::dynamic_pointer_cast<TcpServerImpl>(server);
|
||||||
auto recvThread = [](std::shared_ptr<TcpServerImpl> tcpServer) {
|
auto recvThread = [](std::shared_ptr<TcpServerImpl> tcpServer) {
|
||||||
|
@ -115,16 +122,13 @@ void TcpServerImpl::UnInit(void)
|
||||||
{
|
{
|
||||||
LogInfo("UnInit TcpServerImpl\n");
|
LogInfo("UnInit TcpServerImpl\n");
|
||||||
FreeClients();
|
FreeClients();
|
||||||
if (nullptr != mLoop) {
|
if (nullptr != mIo) {
|
||||||
LogInfo("Stop loop.\n");
|
hio_close(mIo);
|
||||||
hloop_stop(mLoop);
|
mIo = nullptr;
|
||||||
}
|
}
|
||||||
if (mTcpServerThread.joinable()) {
|
if (mTcpServerThread.joinable()) {
|
||||||
mTcpServerThread.join();
|
mTcpServerThread.join();
|
||||||
}
|
}
|
||||||
hloop_free(&mLoop);
|
|
||||||
mLoop = nullptr;
|
|
||||||
mIo = nullptr;
|
|
||||||
}
|
}
|
||||||
void TcpServerImpl::Loop(void)
|
void TcpServerImpl::Loop(void)
|
||||||
{
|
{
|
||||||
|
@ -133,6 +137,8 @@ void TcpServerImpl::Loop(void)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
hloop_run(mLoop);
|
hloop_run(mLoop);
|
||||||
|
hloop_free(&mLoop);
|
||||||
|
mLoop = nullptr;
|
||||||
}
|
}
|
||||||
void TcpServerImpl::AddClient(hio_t *io)
|
void TcpServerImpl::AddClient(hio_t *io)
|
||||||
{
|
{
|
||||||
|
@ -200,6 +206,13 @@ void TcpServerImpl::FreeClients(void)
|
||||||
}
|
}
|
||||||
mClients.clear();
|
mClients.clear();
|
||||||
}
|
}
|
||||||
|
void TcpServerImpl::Closed(void)
|
||||||
|
{
|
||||||
|
if (nullptr != mLoop) {
|
||||||
|
LogInfo("Stop loop.\n");
|
||||||
|
hloop_stop(mLoop);
|
||||||
|
}
|
||||||
|
}
|
||||||
std::shared_ptr<ITcpServer> *NewTcpServer(const TcpServerParam ¶m)
|
std::shared_ptr<ITcpServer> *NewTcpServer(const TcpServerParam ¶m)
|
||||||
{
|
{
|
||||||
LogInfo("Create tcp server object.\n");
|
LogInfo("Create tcp server object.\n");
|
||||||
|
|
|
@ -49,6 +49,7 @@ public:
|
||||||
std::shared_ptr<ITcpClientAccept> *GetClient(hio_t *io);
|
std::shared_ptr<ITcpClientAccept> *GetClient(hio_t *io);
|
||||||
void RemoveClient(hio_t *io);
|
void RemoveClient(hio_t *io);
|
||||||
void FreeClients(void);
|
void FreeClients(void);
|
||||||
|
void Closed(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::mutex mMutex;
|
std::mutex mMutex;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user