diff --git a/.gitignore b/.gitignore index c032710..95ca25b 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,9 @@ # pyenv .idea/ +# vscode +.vscode + # Compiled Object files *.slo *.lo @@ -36,3 +39,4 @@ *.exe *.out *.app +main diff --git a/task/PingTask.cpp b/task/PingTask.cpp new file mode 100644 index 0000000..ee78a91 --- /dev/null +++ b/task/PingTask.cpp @@ -0,0 +1,48 @@ +#include "PingTask.h" +using namespace yazi::task; + +#include "Logger.h" +#include "Singleton.h" +using namespace yazi::utility; + +#include "SocketHandler.h" +using namespace yazi::socket; +#include +#include + + +PingTask::PingTask(Socket * socket) : Task(socket) +{ +} + +PingTask::~PingTask() +{ +} + +void PingTask::run() +{ + debug("Ping task run"); + SocketHandler * handler = Singleton::instance(); + + Socket * socket = static_cast(m_data); + char buf[8192]; + memset(buf, 0, 8192); + int len = socket->recv(buf, 8192); + if (len > 0) + { + debug("recv msg len: %d", len); + socket->send("+OK\r\n", 5); + handler->attach(socket); + } + else + { + debug("Ping task socket closed by peer"); + handler->remove(socket); + } +} + +void PingTask::destroy() +{ + debug("Ping task destroy"); + delete this; +} diff --git a/task/PingTask.h b/task/PingTask.h new file mode 100644 index 0000000..4bc0fe6 --- /dev/null +++ b/task/PingTask.h @@ -0,0 +1,23 @@ +#pragma once + +#include "Task.h" +using namespace yazi::thread; + +#include "Socket.h" +using namespace yazi::socket; + +namespace yazi { +namespace task { + +class PingTask : public Task +{ +public: + PingTask(Socket * socket); + virtual ~PingTask(); + + virtual void run(); + + virtual void destroy(); +}; + +}} diff --git a/task/TaskFactory.h b/task/TaskFactory.h index 185f571..0ceb365 100644 --- a/task/TaskFactory.h +++ b/task/TaskFactory.h @@ -8,6 +8,7 @@ using namespace yazi::thread; #include "EchoTask.h" #include "WorkTask.h" +#include "PingTask.h" using namespace yazi::task; namespace yazi { @@ -19,6 +20,8 @@ class TaskFactory static Task * create(Socket * socket) { return new WorkTask(socket); + // return new EchoTask(socket); + // return new PingTask(socket); } };