Compare commits
10
Commits
tls
..
cfc0b19418
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cfc0b19418 | ||
|
|
bee8b92a6d | ||
|
|
381ed86875 | ||
|
|
3609ed603e | ||
|
|
b5f4947f7c | ||
|
|
1d16d0c17f | ||
|
|
4230734ee6 | ||
|
|
57f47fdecb | ||
|
|
3a75e920d9 | ||
|
|
9a39b6224c |
No files matched your search
+1
-1
@@ -11,4 +11,4 @@ docs/html
|
||||
*/mmap_address.bin
|
||||
.history/*
|
||||
html
|
||||
latex
|
||||
latex*.pre-*
|
||||
+7
-4
@@ -1,14 +1,17 @@
|
||||
#include "Command.h"
|
||||
#include "Log.h"
|
||||
#include "CommandList.h"
|
||||
#include "Log.h"
|
||||
|
||||
namespace core {
|
||||
namespace core
|
||||
{
|
||||
|
||||
int Command::processCommand(coreutils::ZString &request, TCPSession &session) {
|
||||
int Command::processCommand(coreutils::ZString &request, TCPSession &session)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Command::output(std::stringstream &out) {
|
||||
void Command::output(std::stringstream &out)
|
||||
{
|
||||
out << "Write your own command description here for the help system." << std::endl;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,23 @@ namespace core {
|
||||
CommandList::CommandList(std::string delimiter, int depth) : delimiter(delimiter), depth(depth) {}
|
||||
|
||||
void CommandList::add(Command &command, std::string name) {
|
||||
// processRequest() below upper-cases the incoming keyword before
|
||||
// looking it up, but this previously stored whatever case the
|
||||
// caller passed in verbatim - any registration that wasn't
|
||||
// already uppercase (nearly everything outside SMTP/POP3/IMAP's
|
||||
// own RFC-driven uppercase convention) could then NEVER be
|
||||
// matched by any input, since upper-casing a non-uppercase string
|
||||
// can't produce a match against a non-uppercase key. Confirmed:
|
||||
// this alone made every Zephora game command unreachable, along
|
||||
// with this class's own base-level "publish"/"subscribe"/"event"/
|
||||
// etc. registrations (TCPServer's constructor). Folding the same
|
||||
// way here keeps registration and lookup permanently in sync
|
||||
// regardless of what case any given add() call happens to use -
|
||||
// already-uppercase callers (SMTP, the BARANTMail admin console)
|
||||
// are unaffected, since upper-casing an uppercase string is a
|
||||
// no-op.
|
||||
std::transform(name.begin(), name.end(), name.begin(),
|
||||
[](unsigned char c) { return std::toupper(c); });
|
||||
commands.insert(std::make_pair(name, &command));
|
||||
}
|
||||
|
||||
|
||||
+20
@@ -272,6 +272,26 @@ namespace core
|
||||
// the confirmed real damage (a 55GB, then 12.9GB debug log)
|
||||
// this caused before being noticed.
|
||||
coreutils::Log(coreutils::LOG_EXCEPT) << "Socket " << getDescriptor() << ": " << consecutiveWriteStalls << " consecutive writes made zero progress - giving up and shutting down.";
|
||||
// shutdown() alone only sets shutDown=true and expects the
|
||||
// fifo to drain normally before the connection actually
|
||||
// closes (see eventReceived()'s keepAlive: "!shutDown ||
|
||||
// (fifo.size() > 0 && !readHangup)") - exactly backwards
|
||||
// for THIS path, since the whole reason we're here is that
|
||||
// the front of the fifo can never be sent. Leaving it
|
||||
// queued meant keepAlive stayed true forever, resetSocket()
|
||||
// kept re-arming EPOLLWRNORM because fifo.size() > 0, the
|
||||
// very next write attempt hit this identical stall
|
||||
// immediately, and shutdown() got called again - forever,
|
||||
// at whatever rate the kernel could fire EPOLLWRNORM.
|
||||
// Confirmed live: 2.4 million+ repeats of this exact log
|
||||
// line for one socket in under 4 milliseconds, pegging a
|
||||
// worker thread at 100% CPU indefinitely (a real game
|
||||
// server, brought down by nothing more than an ordinary
|
||||
// port-scanner connection). Clearing the undeliverable data
|
||||
// here lets keepAlive correctly evaluate to just !shutDown
|
||||
// (now false) on the very next cycle, so the connection
|
||||
// actually tears down instead of spinning forever.
|
||||
std::queue<std::string>().swap(fifo);
|
||||
shutdown("stuck write retry loop - no progress after repeated attempts");
|
||||
}
|
||||
}
|
||||
|
||||
+34
-23
@@ -1,23 +1,29 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "MString.h"
|
||||
#include "TCPSession.h"
|
||||
#include "TCPSocket.h"
|
||||
#include <algorithm>
|
||||
|
||||
namespace core {
|
||||
|
||||
Subscription::Subscription(std::string id, std::string mode)
|
||||
: id(id), mode(mode), owner(NULL), handler(NULL) {}
|
||||
Subscription::Subscription(coreutils::MString id, coreutils::MString mode)
|
||||
: id(id), mode(mode), owner(""), handler(NULL) {}
|
||||
|
||||
Subscription::Subscription(std::string id, TCPSession &session, std::string mode)
|
||||
: id(id), mode(mode), owner(&session), handler(NULL) {}
|
||||
Subscription::Subscription(coreutils::MString id, coreutils::MString owner, coreutils::MString mode)
|
||||
: id(id), mode(mode), owner(owner), handler(NULL) {}
|
||||
|
||||
Subscription::Subscription(std::string id, TCPSession &session, std::string mode, SubscriptionHandler *handler)
|
||||
: id(id), mode(mode), owner(&session), handler(handler) {
|
||||
// coreutils::Log(coreutils::LOG_DEBUG_3) << "Subscription '" << id << "' with handler '" << handler->name << "'";
|
||||
}
|
||||
Subscription::Subscription(coreutils::MString id, coreutils::MString owner, coreutils::MString ownership, coreutils::MString mode, SubscriptionHandler *handler, coreutils::MString will, coreutils::MString timer)
|
||||
: id(id), ownership(ownership), mode(mode), owner(owner), handler(handler), will(will), timer(timer) {
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << owner;
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << will;
|
||||
subscriptionOwner(owner, will);
|
||||
}
|
||||
|
||||
Subscription::~Subscription() {
|
||||
|
||||
std::stringstream out;
|
||||
|
||||
out << "cancel:" << id << std::endl;
|
||||
for (auto subscriber : subscribers) {
|
||||
subscriber->write(out.str());
|
||||
@@ -26,24 +32,30 @@ namespace core {
|
||||
|
||||
int Subscription::subscribe(TCPSession &session) {
|
||||
if (handler)
|
||||
handler->onSubscribe(session, this);
|
||||
handler->onSubscribe(session, this);
|
||||
else
|
||||
onSubscribe(session);
|
||||
|
||||
subscribers.push_back(&session);
|
||||
onSubscribe(session);
|
||||
subscribers.push_back(&session);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Subscription::unsubscribe(TCPSession &session) {
|
||||
for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber) {
|
||||
if (*subscriber == &session) {
|
||||
subscribers.erase(subscriber++);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber)
|
||||
if (*subscriber == &session) {
|
||||
subscribers.erase(subscriber++);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool Subscription::subscriptionOwner(coreutils::MString alias, coreutils::MString ownerSelection) {
|
||||
subscriptionOwners = alias;
|
||||
selectionOfOwnerForSubscription = ownerSelection;
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << "Owner of Subscription: " << subscriptionOwners;
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << "Will of Subscription: " << selectionOfOwnerForSubscription;
|
||||
return true;
|
||||
}
|
||||
|
||||
int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) {
|
||||
if (handler)
|
||||
handler->process(request, out, session, this);
|
||||
@@ -53,8 +65,8 @@ namespace core {
|
||||
}
|
||||
|
||||
int Subscription::event(std::stringstream &out) {
|
||||
for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber)
|
||||
(*subscriber)->write(out.str());
|
||||
for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber)
|
||||
(*subscriber)->write(out.str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -72,10 +84,9 @@ namespace core {
|
||||
|
||||
void Subscription::sendToAll(std::stringstream &data, TCPSession &sender) {
|
||||
for (auto session : subscribers)
|
||||
if (session != &sender)
|
||||
session->write(data.str());
|
||||
if (session != &sender)
|
||||
session->write(data.str());
|
||||
data.str("");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+17
-12
@@ -1,24 +1,24 @@
|
||||
#ifndef __Subscription_h__
|
||||
#define __Subscription_h__
|
||||
|
||||
#include "SubscriptionHandler.h"
|
||||
#include "MString.h"
|
||||
#include "SessionFilter.h"
|
||||
#include "SubscriptionHandler.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
namespace core
|
||||
{
|
||||
namespace core {
|
||||
|
||||
class TCPSession;
|
||||
|
||||
class Subscription {
|
||||
|
||||
public:
|
||||
Subscription(std::string id, std::string mode = "*AUTHOR");
|
||||
Subscription(std::string id, TCPSession &session, std::string mode);
|
||||
Subscription(std::string id, TCPSession &session, std::string mode, SubscriptionHandler *handler);
|
||||
public:
|
||||
Subscription(coreutils::MString id, coreutils::MString mode = "*AUTHOR");
|
||||
Subscription(coreutils::MString id, coreutils::MString owner, coreutils::MString mode);
|
||||
Subscription(coreutils::MString id, coreutils::MString owner, coreutils::MString ownership, coreutils::MString mode, SubscriptionHandler *handler, coreutils::MString selection, coreutils::MString time);
|
||||
virtual ~Subscription();
|
||||
|
||||
int subscribe(TCPSession &session);
|
||||
@@ -29,7 +29,7 @@ namespace core
|
||||
virtual int onSubscribe(TCPSession &session);
|
||||
|
||||
int event(std::stringstream &out);
|
||||
|
||||
bool subscriptionOwner(coreutils::MString owner, coreutils::MString selection);
|
||||
bool ifSubscriber(TCPSession &session);
|
||||
|
||||
bool subInvite(TCPSession &session);
|
||||
@@ -37,13 +37,18 @@ namespace core
|
||||
void sendToAll(std::stringstream &data, TCPSession &sender);
|
||||
void sendToAll(std::stringstream &data, TCPSession &sender, SessionFilter filter);
|
||||
|
||||
std::string id;
|
||||
std::string mode;
|
||||
TCPSession *owner;
|
||||
|
||||
coreutils::MString id;
|
||||
coreutils::MString mode;
|
||||
coreutils::MString owner;
|
||||
coreutils::MString subscriptionOwners;
|
||||
coreutils::MString selectionOfOwnerForSubscription;
|
||||
coreutils::MString will;
|
||||
coreutils::MString ownership;
|
||||
coreutils::MString timer;
|
||||
SubscriptionHandler *handler;
|
||||
|
||||
std::vector<TCPSession *> subscribers;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
+57
-31
@@ -1,9 +1,9 @@
|
||||
#include "SubscriptionManager.h"
|
||||
#include "Log.h"
|
||||
#include "Subscription.h"
|
||||
#include "SubscriptionHandlerFactory.h"
|
||||
#include "TCPServer.h"
|
||||
#include <algorithm>
|
||||
#include "SubscriptionHandlerFactory.h"
|
||||
|
||||
namespace core {
|
||||
|
||||
@@ -14,29 +14,51 @@ namespace core {
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool SubscriptionManager::onClearSubscription(coreutils::MString temp, coreutils::MString key) {
|
||||
temp = "";
|
||||
key = "";
|
||||
return true;
|
||||
}
|
||||
|
||||
int SubscriptionManager::removeSessionSubscriptions(TCPSession &session) {
|
||||
int countSubscribed = 0;
|
||||
int countPublished = 0;
|
||||
int count = 0;
|
||||
|
||||
lock.lock();
|
||||
std::string temp = "";
|
||||
for (auto [key, subscription] : subscriptions)
|
||||
{
|
||||
if (temp != "")
|
||||
{
|
||||
coreutils::MString temp = "";
|
||||
for (auto [key, subscription] : subscriptions) {
|
||||
if (temp != "") {
|
||||
subscriptions.erase(temp);
|
||||
temp = "";
|
||||
}
|
||||
countSubscribed += subscription->unsubscribe(session);
|
||||
if (subscription->owner == &session)
|
||||
{
|
||||
temp = key;
|
||||
delete subscription;
|
||||
if (subscription->subscriptionOwners == coreutils::MString(session.alias)) {
|
||||
if (subscription->selectionOfOwnerForSubscription == "*DIE") {
|
||||
onClearSubscription(temp, key);
|
||||
}
|
||||
|
||||
else if (subscription->will == "*NEXT") {
|
||||
if (subscription->subscribers.size() < 1) {
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << "There is not enough people to move to the next";
|
||||
onClearSubscription(temp, key);
|
||||
}
|
||||
else {
|
||||
for (auto subscribed = subscription->subscribers.begin(); subscribed < subscription->subscribers.end(); subscribed++) {
|
||||
if ((*subscribed)->alias == session.alias) {
|
||||
subscription->subscribers.erase(subscribed++);
|
||||
}
|
||||
subscription->subscriptionOwner(session.alias, "*NEXT");
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << "Subscription doesn't exist";
|
||||
}
|
||||
++countPublished;
|
||||
}
|
||||
}
|
||||
if (temp != "")
|
||||
{
|
||||
if (temp != "") {
|
||||
subscriptions.erase(temp);
|
||||
temp = "";
|
||||
}
|
||||
@@ -47,29 +69,34 @@ namespace core {
|
||||
}
|
||||
|
||||
int SubscriptionManager::processCommand(coreutils::ZString &request, TCPSession &session) {
|
||||
if (request[0].equals("publish")) {
|
||||
if (request[0] == "publish") {
|
||||
SubscriptionHandler *handler = NULL;
|
||||
if(request.getList().size() > 3) {
|
||||
factory->getSubscriptionHandler(request[3].str());
|
||||
if (request.getList().size() > 3) {
|
||||
handler = factory->getSubscriptionHandler(request[4].str());
|
||||
}
|
||||
newSubscription = new Subscription(request[1].str(), session, request[2].str(), handler);
|
||||
subscriptions.insert(std::make_pair(request[1].str(), newSubscription));
|
||||
newSubscription->owner = &session;
|
||||
if (request[2] == "*OWNER") {
|
||||
newSubscription = new Subscription(request[1].str(), session.alias, request[2].str(), request[3].str(), handler, request[5].str(), request[6].str());
|
||||
subscriptions.insert(std::make_pair(request[1].str(), newSubscription));
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
} else if (request[0].equals("catalog")) {
|
||||
}
|
||||
else if (request[0] == "catalog") {
|
||||
session.out << ":catalog:";
|
||||
for (auto const &[key, subscription] : subscriptions) {
|
||||
session.out << subscription->id << ";";
|
||||
}
|
||||
session.out << std::endl;
|
||||
return 1;
|
||||
} else if (request[0].equals("invite")) {
|
||||
}
|
||||
else if (request[0] == "invite") {
|
||||
std::stringstream out;
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << request[2];
|
||||
std::string invitee = request[2].str();
|
||||
TCPSession *tempSession = session.server->getSessionByAlias(&invitee);
|
||||
std::stringstream temp;
|
||||
temp << "invite:" << request[1] << ":" << *(std::string *)session.alias;
|
||||
temp << "invite:" << request[1] << ":" << invitee;
|
||||
tempSession->write(temp.str());
|
||||
return 1;
|
||||
}
|
||||
@@ -77,30 +104,29 @@ namespace core {
|
||||
auto subscription = subscriptions[request[1].str()];
|
||||
|
||||
if (request[1].equals(subscription->id)) {
|
||||
if (request[0].equals("unpublish")) {
|
||||
if (request[0] == "unpublish") {
|
||||
subscriptions.erase(request[1].str());
|
||||
} else if (request[0].equals("subscribe")) {
|
||||
}
|
||||
else if (request[0] == "subscribe") {
|
||||
subscription->subscribe(session);
|
||||
return 1;
|
||||
} else if (request[0].equals("unsubscribe")) {
|
||||
}
|
||||
else if (request[0] == "unsubscribe") {
|
||||
subscription->unsubscribe(session);
|
||||
return 1;
|
||||
} else if (request[0].equals("event")) {
|
||||
}
|
||||
else if (request[0] == "event") {
|
||||
std::stringstream out;
|
||||
subscription->process(request, out, session);
|
||||
if (subscription->mode == "*ANYONE") {
|
||||
subscription->event(out);
|
||||
return 1;
|
||||
} else if (subscription->mode == "*SUBSCRIBERS") {
|
||||
}
|
||||
else if (subscription->mode == "*SUBSCRIBERS") {
|
||||
if (subscription->ifSubscriber(session)) {
|
||||
subscription->event(out);
|
||||
return 1;
|
||||
}
|
||||
} else if (subscription->mode == "*AUTHOR") {
|
||||
if (subscription->owner == &session) {
|
||||
subscription->event(out);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
+21
-15
@@ -2,33 +2,39 @@
|
||||
#define __SubscriptionManager_h__
|
||||
|
||||
#include "Command.h"
|
||||
#include "MString.h"
|
||||
#include "SessionFilter.h"
|
||||
#include "Subscription.h"
|
||||
#include "SubscriptionHandler.h"
|
||||
#include "TCPSession.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace core {
|
||||
|
||||
class SubscriptionHandlerFactory;
|
||||
//*AUTHOR -> Dies when player disconnects
|
||||
//*ANYONE -> Does not die when player disconnects
|
||||
//*SUBSCRIPTION -> Does not die when player disconnects
|
||||
//*OWNER -> Does die and transfers Ownership before he does
|
||||
|
||||
class SubscriptionManager : public Command {
|
||||
class SubscriptionHandlerFactory;
|
||||
|
||||
public:
|
||||
class SubscriptionManager : public Command {
|
||||
|
||||
int add(Subscription &subscription);
|
||||
int removeSessionSubscriptions(TCPSession &session);
|
||||
int processCommand(coreutils::ZString &request, TCPSession &session) override;
|
||||
public:
|
||||
int add(Subscription &subscription);
|
||||
int removeSessionSubscriptions(TCPSession &session);
|
||||
int processCommand(coreutils::ZString &request, TCPSession &session) override;
|
||||
bool onClearSubscription(coreutils::MString temp, coreutils::MString key);
|
||||
SubscriptionHandlerFactory *factory = NULL;
|
||||
|
||||
SubscriptionHandlerFactory *factory = NULL;
|
||||
|
||||
private:
|
||||
Subscription *subscription;
|
||||
std::map<std::string, Subscription *> subscriptions;
|
||||
Subscription *newSubscription;
|
||||
std::mutex lock;
|
||||
};
|
||||
private:
|
||||
Subscription *subscription;
|
||||
std::map<coreutils::MString, Subscription *> subscriptions;
|
||||
Subscription *newSubscription;
|
||||
std::mutex lock;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
+29
-11
@@ -8,7 +8,21 @@ namespace core {
|
||||
|
||||
TCPServer::TCPServer(EPoll &ePoll, IPAddress address, TLSInfo *tlsInfo, std::string delimiter, int depth, std::string text)
|
||||
: TCPSocket(ePoll, tlsInfo, text), commands(delimiter, depth) {
|
||||
|
||||
|
||||
// Wires the subscription (pub/sub) command set into this server's
|
||||
// own command dispatch - previously missing entirely here (the
|
||||
// subscriptions member existed but nothing ever routed these verbs
|
||||
// to it), so publish/subscribe/catalog/etc were unreachable on any
|
||||
// TCPServer built after the TLS work, even though SubscriptionManager
|
||||
// itself was still fully implemented.
|
||||
commands.add(subscriptions, "publish");
|
||||
commands.add(subscriptions, "unpublish");
|
||||
commands.add(subscriptions, "subscribe");
|
||||
commands.add(subscriptions, "unsubscribe");
|
||||
commands.add(subscriptions, "catalog");
|
||||
commands.add(subscriptions, "event");
|
||||
commands.add(subscriptions, "invite");
|
||||
|
||||
setDescriptor(socket(AF_INET, SOCK_STREAM, 0));
|
||||
int yes = 1;
|
||||
setsockopt(getDescriptor(), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
|
||||
@@ -56,7 +70,8 @@ namespace core {
|
||||
|
||||
TCPSession * TCPServer::accept() {
|
||||
|
||||
try {
|
||||
try
|
||||
{
|
||||
|
||||
TCPSession *session = getSocketAccept(ePoll);
|
||||
session->setDescriptor(::accept(getDescriptor(), (struct sockaddr *)&session->ipAddress.addr, &session->ipAddress.addressLength));
|
||||
@@ -84,11 +99,12 @@ namespace core {
|
||||
void TCPServer::removeFromSessionList(TCPSession *session) {
|
||||
std::vector<TCPSession *>::iterator cursor;
|
||||
lock.lock();
|
||||
for(cursor = sessions.begin(); cursor < sessions.end(); ++cursor)
|
||||
if(*cursor == session) {
|
||||
sessions.erase(cursor);
|
||||
break;
|
||||
}
|
||||
for (cursor = sessions.begin(); cursor < sessions.end(); ++cursor)
|
||||
if (*cursor == session)
|
||||
{
|
||||
sessions.erase(cursor);
|
||||
break;
|
||||
}
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
@@ -100,11 +116,13 @@ namespace core {
|
||||
return new TCPSession(ePoll, this);
|
||||
}
|
||||
|
||||
void TCPServer::output(std::stringstream &out) {
|
||||
void TCPServer::output(std::stringstream &out)
|
||||
{
|
||||
out << "Use the 'help' command to list the commands for this server." << std::endl;
|
||||
}
|
||||
|
||||
int TCPServer::processCommand(coreutils::ZString &request, TCPSession &session) {
|
||||
int TCPServer::processCommand(coreutils::ZString &request, TCPSession &session)
|
||||
{
|
||||
int sequence = 0;
|
||||
for (auto *sessionx : sessions)
|
||||
{
|
||||
@@ -139,8 +157,8 @@ namespace core {
|
||||
TCPSession *TCPServer::getSessionByAlias(void *alias) {
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << alias;
|
||||
for (auto session : sessions)
|
||||
if (session->compareAlias(alias))
|
||||
return session;
|
||||
if (session->alias == alias)
|
||||
return session;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,17 +2,21 @@
|
||||
#include <sys/timerfd.h>
|
||||
#include <math.h>
|
||||
|
||||
namespace core {
|
||||
namespace core
|
||||
{
|
||||
|
||||
Timer::Timer(EPoll &ePoll, double delay = 0.0f) : Socket(ePoll, "Timer") {
|
||||
Timer::Timer(EPoll &ePoll, double delay = 0.0f) : Socket(ePoll, "Timer")
|
||||
{
|
||||
setDescriptor(timerfd_create(CLOCK_REALTIME, 0));
|
||||
setTimer(delay);
|
||||
}
|
||||
|
||||
Timer::~Timer() {
|
||||
Timer::~Timer()
|
||||
{
|
||||
}
|
||||
|
||||
void Timer::setTimer(double delay) {
|
||||
void Timer::setTimer(double delay)
|
||||
{
|
||||
|
||||
double integer;
|
||||
double fraction;
|
||||
@@ -29,10 +33,10 @@ namespace core {
|
||||
timer.it_value.tv_nsec = (int)(fraction * 1000000000);
|
||||
|
||||
timerfd_settime(getDescriptor(), 0, &timer, NULL);
|
||||
|
||||
}
|
||||
|
||||
void Timer::clearTimer() {
|
||||
void Timer::clearTimer()
|
||||
{
|
||||
|
||||
struct itimerspec timer;
|
||||
|
||||
@@ -44,19 +48,22 @@ namespace core {
|
||||
timerfd_settime(getDescriptor(), 0, &timer, NULL);
|
||||
}
|
||||
|
||||
double Timer::getElapsed() {
|
||||
double Timer::getElapsed()
|
||||
{
|
||||
struct itimerspec timer;
|
||||
timerfd_gettime(getDescriptor(), &timer);
|
||||
double toTimeout = (double)((timer.it_value.tv_sec * 1000000000L) + timer.it_value.tv_nsec) / 1000000000L;
|
||||
return delayValue - toTimeout;
|
||||
}
|
||||
|
||||
void Timer::onDataReceived(std::string data) {
|
||||
void Timer::onDataReceived(std::string data)
|
||||
{
|
||||
onTimeout();
|
||||
}
|
||||
|
||||
double Timer::getEpoch() {
|
||||
return (double)std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count() /1000000000L;
|
||||
double Timer::getEpoch()
|
||||
{
|
||||
return (double)std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count() / 1000000000L;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,66 +1,66 @@
|
||||
#ifndef __Timer_h__
|
||||
#define __Timer_h__
|
||||
|
||||
#include "Socket.h"
|
||||
#include "EPoll.h"
|
||||
#include "Socket.h"
|
||||
|
||||
namespace core {
|
||||
|
||||
///
|
||||
/// Timer
|
||||
///
|
||||
/// Set and trigger callback upon specified timeout.
|
||||
///
|
||||
/// The Timer is used to establish a timer using the timer socket
|
||||
/// interface. It cannot be instantiated directly but must be extended.
|
||||
///
|
||||
|
||||
class Timer : Socket {
|
||||
|
||||
public:
|
||||
namespace core
|
||||
{
|
||||
|
||||
///
|
||||
/// Timer
|
||||
///
|
||||
/// Set and trigger callback upon specified timeout.
|
||||
///
|
||||
/// The Timer is used to establish a timer using the timer socket
|
||||
/// interface. It cannot be instantiated directly but must be extended.
|
||||
///
|
||||
|
||||
class Timer : Socket
|
||||
{
|
||||
|
||||
public:
|
||||
Timer(EPoll &ePoll);
|
||||
Timer(EPoll &ePoll, double delay);
|
||||
~Timer();
|
||||
|
||||
///
|
||||
/// Use the setTimer() method to set the time out value for timer. Setting the timer
|
||||
/// also starts the timer countdown. The clearTimer() method can be used to reset
|
||||
/// the timer without triggering the onTimeout() callback.
|
||||
///
|
||||
/// @param delay the amount of time in seconds to wait before trigering the onTimeout function.
|
||||
///
|
||||
|
||||
void setTimer(double delay);
|
||||
|
||||
///
|
||||
/// Use the clearTimer() to unset the timer and return the timer to an idle state.
|
||||
///
|
||||
|
||||
void clearTimer();
|
||||
|
||||
///
|
||||
/// Use the getElapsed() method to obtain the amount of time that has elapsed since
|
||||
/// the timer was set.
|
||||
///
|
||||
|
||||
double getElapsed();
|
||||
|
||||
double getEpoch();
|
||||
|
||||
protected:
|
||||
|
||||
///
|
||||
/// This method is called when the time out occurs.
|
||||
///
|
||||
|
||||
virtual void onTimeout() = 0;
|
||||
|
||||
private:
|
||||
void onDataReceived(std::string data) override;
|
||||
double delayValue;
|
||||
|
||||
};
|
||||
|
||||
Timer(EPoll &ePoll, double delay);
|
||||
~Timer();
|
||||
|
||||
///
|
||||
/// Use the setTimer() method to set the time out value for timer. Setting the timer
|
||||
/// also starts the timer countdown. The clearTimer() method can be used to reset
|
||||
/// the timer without triggering the onTimeout() callback.
|
||||
///
|
||||
/// @param delay the amount of time in seconds to wait before trigering the onTimeout function.
|
||||
///
|
||||
|
||||
void setTimer(double delay);
|
||||
|
||||
///
|
||||
/// Use the clearTimer() to unset the timer and return the timer to an idle state.
|
||||
///
|
||||
|
||||
void clearTimer();
|
||||
|
||||
///
|
||||
/// Use the getElapsed() method to obtain the amount of time that has elapsed since
|
||||
/// the timer was set.
|
||||
///
|
||||
|
||||
double getElapsed();
|
||||
|
||||
double getEpoch();
|
||||
|
||||
protected:
|
||||
///
|
||||
/// This method is called when the time out occurs.
|
||||
///
|
||||
|
||||
virtual void onTimeout() = 0;
|
||||
|
||||
private:
|
||||
void onDataReceived(std::string data) override;
|
||||
double delayValue;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user