Skip to content

Commit

Permalink
Add redis-ping demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Codesire-Deng committed Jul 21, 2022
1 parent e4a0aab commit 88f8ae4
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
# pyenv
.idea/

# vscode
.vscode

# Compiled Object files
*.slo
*.lo
Expand Down Expand Up @@ -36,3 +39,4 @@
*.exe
*.out
*.app
main
48 changes: 48 additions & 0 deletions task/PingTask.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdio>
#include <unistd.h>


PingTask::PingTask(Socket * socket) : Task(socket)
{
}

PingTask::~PingTask()
{
}

void PingTask::run()
{
debug("Ping task run");
SocketHandler * handler = Singleton<SocketHandler>::instance();

Socket * socket = static_cast<Socket *>(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;
}
23 changes: 23 additions & 0 deletions task/PingTask.h
Original file line number Diff line number Diff line change
@@ -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();
};

}}
3 changes: 3 additions & 0 deletions task/TaskFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using namespace yazi::thread;

#include "EchoTask.h"
#include "WorkTask.h"
#include "PingTask.h"
using namespace yazi::task;

namespace yazi {
Expand All @@ -19,6 +20,8 @@ class TaskFactory
static Task * create(Socket * socket)
{
return new WorkTask(socket);
// return new EchoTask(socket);
// return new PingTask(socket);
}
};

Expand Down

0 comments on commit 88f8ae4

Please sign in to comment.