Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
519faa535b |
No files matched your search
+4
-7
@@ -1,17 +1,14 @@
|
||||
#include "Command.h"
|
||||
#include "CommandList.h"
|
||||
#include "Log.h"
|
||||
#include "CommandList.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;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
#ifndef __Command_h__
|
||||
#define __Command_h__
|
||||
|
||||
#include "includes"
|
||||
#include "Object.h"
|
||||
#include "TCPSession.h"
|
||||
#include "ZString.h"
|
||||
#include "includes"
|
||||
|
||||
namespace core
|
||||
{
|
||||
namespace core {
|
||||
|
||||
class CommandList;
|
||||
|
||||
@@ -20,10 +19,10 @@ namespace core
|
||||
/// a list of functions that can be invoked as a result of processing a request.
|
||||
///
|
||||
|
||||
class Command
|
||||
{
|
||||
class Command {
|
||||
|
||||
public:
|
||||
|
||||
///
|
||||
/// This method is used to implement the functionality of the requested command.
|
||||
/// This pure virtual function must be implemented in your inheriting object.
|
||||
@@ -44,6 +43,7 @@ namespace core
|
||||
///
|
||||
|
||||
virtual void output(std::stringstream &out);
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
+19
-8
@@ -48,6 +48,17 @@ namespace core
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
void Socket::setBufferSize(int length)
|
||||
{
|
||||
this->length = length;
|
||||
buffer = (char *)realloc(buffer, length);
|
||||
}
|
||||
|
||||
int Socket::getBufferSize()
|
||||
{
|
||||
return length;
|
||||
}
|
||||
|
||||
void Socket::onRegister() {}
|
||||
|
||||
void Socket::onRegistered() {}
|
||||
@@ -59,7 +70,7 @@ namespace core
|
||||
bool Socket::eventReceived(struct epoll_event event, long long eventId) {
|
||||
// coreutils::Log(coreutils::LOG_DEBUG_1) << "Event process beginning for socket " << getDescriptor();
|
||||
if(inHandler)
|
||||
// coreutils::Log(coreutils::LOG_DEBUG_2) << "inHandler was already true.";
|
||||
// coreutils::Log(coreutils::LOG_DEBUG_2) << "inHandler was already true.";
|
||||
inHandler = true;
|
||||
if(event.events & EPOLLRDHUP) {
|
||||
// coreutils::Log(coreutils::LOG_DEBUG_2) << "EPOLLRDHUP";
|
||||
@@ -68,8 +79,9 @@ namespace core
|
||||
}
|
||||
if(event.events & EPOLLIN) {
|
||||
// coreutils::Log(coreutils::LOG_DEBUG_2) << "EPOLLIN";
|
||||
coreutils::ZString zbuffer(buffer, length);
|
||||
lock.lock();
|
||||
receiveDataNotification();
|
||||
receiveData(zbuffer);
|
||||
if(!shutDown) {
|
||||
inHandler = false;
|
||||
lock.unlock();
|
||||
@@ -87,18 +99,17 @@ namespace core
|
||||
return !shutDown;
|
||||
}
|
||||
|
||||
void Socket::onDataReceived(std::string data) {
|
||||
void Socket::onDataReceived(std::string data)
|
||||
{
|
||||
throw coreutils::Exception("Need to override onDataReceived.", __FILE__, __LINE__, -1);
|
||||
}
|
||||
|
||||
void Socket::onDataReceived(coreutils::ZString &data) {
|
||||
void Socket::onDataReceived(coreutils::ZString &data)
|
||||
{
|
||||
onDataReceived(std::string(data.getData(), data.getLength()));
|
||||
}
|
||||
|
||||
void Socket::receiveDataNotification() {}
|
||||
|
||||
|
||||
|
||||
void Socket::receiveData(coreutils::ZString &buffer) {
|
||||
coreutils::ZString blank("");
|
||||
if(buffer.getLength() <= 0)
|
||||
throw coreutils::Exception("Request to receive data with a zero buffer length.", __FILE__, __LINE__, -1);
|
||||
|
||||
@@ -124,6 +124,10 @@ namespace core {
|
||||
|
||||
EPoll &ePoll; // The EPoll control object.
|
||||
|
||||
void setBufferSize(int length);
|
||||
|
||||
int getBufferSize();
|
||||
|
||||
///
|
||||
/// The onConnected method is called when the socket is ready to communicate.
|
||||
/// Writing to the socket can begin on this call to initiate a contact with the
|
||||
@@ -139,10 +143,27 @@ namespace core {
|
||||
// virtual void onDisconnected(); ///< Called when socket is closing and no longer ready to communicate.
|
||||
|
||||
///
|
||||
/// This method is called when data is ready to be read from the socket.
|
||||
/// The onDataReceived method is called when the socket has received an event from
|
||||
/// epoll and there is data ready to be read from the socket. The default handler
|
||||
/// will pull the data and put it into the streambuf for the socket. EPOLLIN
|
||||
///
|
||||
/// @param data the data that has been received from the socket.
|
||||
///
|
||||
|
||||
virtual void readDataNotification();
|
||||
virtual void onDataReceived(std::string data); ///< Called when data is received from the socket.
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
virtual void onDataReceived(coreutils::ZString &data);
|
||||
|
||||
///
|
||||
/// receiveData will read the data from the socket and place it in the socket buffer.
|
||||
/// TLS layer overrides this to be able to read from SSL.
|
||||
///
|
||||
|
||||
virtual void receiveData(coreutils::ZString &buffer);
|
||||
|
||||
private:
|
||||
|
||||
@@ -166,6 +187,9 @@ namespace core {
|
||||
// int_type pbackfail(int_type ch);
|
||||
// streamsize showmanyc();
|
||||
|
||||
char *buffer; // This is a pointer to the managed buffer space.
|
||||
int length; // This is the length of the buffer.
|
||||
|
||||
std::queue<std::string> fifo;
|
||||
|
||||
void resetSocket();
|
||||
|
||||
+11
-22
@@ -1,29 +1,23 @@
|
||||
#include "Subscription.h"
|
||||
#include "Log.h"
|
||||
#include "MString.h"
|
||||
#include "TCPSession.h"
|
||||
#include "TCPSocket.h"
|
||||
#include <algorithm>
|
||||
|
||||
namespace core {
|
||||
|
||||
Subscription::Subscription(coreutils::MString id, coreutils::MString mode)
|
||||
: id(id), mode(mode), owner(""), handler(NULL) {}
|
||||
Subscription::Subscription(std::string id, std::string mode)
|
||||
: id(id), mode(mode), owner(NULL), 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)
|
||||
: id(id), mode(mode), owner(&session), handler(NULL) {}
|
||||
|
||||
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::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() {
|
||||
|
||||
std::stringstream out;
|
||||
|
||||
out << "cancel:" << id << std::endl;
|
||||
for (auto subscriber : subscribers) {
|
||||
subscriber->write(out.str());
|
||||
@@ -35,25 +29,19 @@ namespace core {
|
||||
handler->onSubscribe(session, this);
|
||||
else
|
||||
onSubscribe(session);
|
||||
|
||||
subscribers.push_back(&session);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Subscription::unsubscribe(TCPSession &session) {
|
||||
for (auto subscriber = subscribers.begin(); subscriber < subscribers.end(); ++subscriber)
|
||||
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;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Subscription::process(coreutils::ZString &request, std::stringstream &out, TCPSession &session) {
|
||||
@@ -89,4 +77,5 @@ namespace core {
|
||||
data.str("");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+11
-16
@@ -1,23 +1,23 @@
|
||||
#ifndef __Subscription_h__
|
||||
#define __Subscription_h__
|
||||
|
||||
#include "MString.h"
|
||||
#include "SessionFilter.h"
|
||||
#include "SubscriptionHandler.h"
|
||||
#include "SessionFilter.h"
|
||||
#include "ZString.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace core {
|
||||
namespace core
|
||||
{
|
||||
|
||||
class TCPSession;
|
||||
|
||||
class Subscription {
|
||||
|
||||
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);
|
||||
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);
|
||||
virtual ~Subscription();
|
||||
|
||||
int subscribe(TCPSession &session);
|
||||
@@ -28,7 +28,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);
|
||||
@@ -36,18 +36,13 @@ namespace core {
|
||||
void sendToAll(std::stringstream &data, TCPSession &sender);
|
||||
void sendToAll(std::stringstream &data, TCPSession &sender, SessionFilter filter);
|
||||
|
||||
coreutils::MString id;
|
||||
coreutils::MString mode;
|
||||
coreutils::MString owner;
|
||||
coreutils::MString subscriptionOwners;
|
||||
coreutils::MString selectionOfOwnerForSubscription;
|
||||
coreutils::MString will;
|
||||
coreutils::MString ownership;
|
||||
coreutils::MString timer;
|
||||
std::string id;
|
||||
std::string mode;
|
||||
TCPSession *owner;
|
||||
|
||||
SubscriptionHandler *handler;
|
||||
|
||||
std::vector<TCPSession *> subscribers;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
+32
-58
@@ -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,51 +14,29 @@ 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();
|
||||
coreutils::MString temp = "";
|
||||
for (auto [key, subscription] : subscriptions) {
|
||||
if (temp != "") {
|
||||
std::string temp = "";
|
||||
for (auto [key, subscription] : subscriptions)
|
||||
{
|
||||
if (temp != "")
|
||||
{
|
||||
subscriptions.erase(temp);
|
||||
temp = "";
|
||||
}
|
||||
countSubscribed += subscription->unsubscribe(session);
|
||||
if (subscription->subscriptionOwners == 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";
|
||||
}
|
||||
if (subscription->owner == &session)
|
||||
{
|
||||
temp = key;
|
||||
delete subscription;
|
||||
++countPublished;
|
||||
}
|
||||
}
|
||||
if (temp != "") {
|
||||
if (temp != "")
|
||||
{
|
||||
subscriptions.erase(temp);
|
||||
temp = "";
|
||||
}
|
||||
@@ -69,34 +47,29 @@ namespace core {
|
||||
}
|
||||
|
||||
int SubscriptionManager::processCommand(coreutils::ZString &request, TCPSession &session) {
|
||||
if (request[0] == "publish") {
|
||||
if (request[0].equals("publish")) {
|
||||
SubscriptionHandler *handler = NULL;
|
||||
if (request.getList().size() > 3) {
|
||||
handler = factory->getSubscriptionHandler(request[4].str());
|
||||
if(request.getList().size() > 3) {
|
||||
factory->getSubscriptionHandler(request[3].str());
|
||||
}
|
||||
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());
|
||||
newSubscription = new Subscription(request[1].str(), session, request[2].str(), handler);
|
||||
subscriptions.insert(std::make_pair(request[1].str(), newSubscription));
|
||||
newSubscription->owner = &session;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
else if (request[0] == "catalog") {
|
||||
} else if (request[0].equals("catalog")) {
|
||||
session.out << ":catalog:";
|
||||
for (auto const &[key, subscription] : subscriptions) {
|
||||
session.out << subscription->id << ";";
|
||||
}
|
||||
session.out << std::endl;
|
||||
return 1;
|
||||
}
|
||||
else if (request[0] == "invite") {
|
||||
} else if (request[0].equals("invite")) {
|
||||
std::stringstream out;
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << request[2];
|
||||
invitee = (coreutils::MString *)&request[2];
|
||||
TCPSession *tempSession = session.server.getSessionByAlias(invitee);
|
||||
std::string invitee = request[2].str();
|
||||
TCPSession *tempSession = session.server.getSessionByAlias(&invitee);
|
||||
std::stringstream temp;
|
||||
temp << "invite:" << request[1] << ":" << invitee;
|
||||
temp << "invite:" << request[1] << ":" << *(std::string *)session.alias;
|
||||
tempSession->write(temp.str());
|
||||
return 1;
|
||||
}
|
||||
@@ -104,29 +77,30 @@ namespace core {
|
||||
auto subscription = subscriptions[request[1].str()];
|
||||
|
||||
if (request[1].equals(subscription->id)) {
|
||||
if (request[0] == "unpublish") {
|
||||
if (request[0].equals("unpublish")) {
|
||||
subscriptions.erase(request[1].str());
|
||||
}
|
||||
else if (request[0] == "subscribe") {
|
||||
} else if (request[0].equals("subscribe")) {
|
||||
subscription->subscribe(session);
|
||||
return 1;
|
||||
}
|
||||
else if (request[0] == "unsubscribe") {
|
||||
} else if (request[0].equals("unsubscribe")) {
|
||||
subscription->unsubscribe(session);
|
||||
return 1;
|
||||
}
|
||||
else if (request[0] == "event") {
|
||||
} else if (request[0].equals("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;
|
||||
|
||||
+4
-11
@@ -2,39 +2,32 @@
|
||||
#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 {
|
||||
|
||||
//*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 SubscriptionHandlerFactory;
|
||||
|
||||
class SubscriptionManager : public Command {
|
||||
|
||||
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;
|
||||
|
||||
private:
|
||||
Subscription *subscription;
|
||||
std::map<coreutils::MString, Subscription *> subscriptions;
|
||||
std::map<std::string, Subscription *> subscriptions;
|
||||
Subscription *newSubscription;
|
||||
std::mutex lock;
|
||||
coreutils::MString invitee;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
#include "TCPLineBufferParser.h"
|
||||
|
||||
namespace core {
|
||||
|
||||
TCPLineBufferParser::TCPLineBufferParser() {
|
||||
buffer = nullptr;
|
||||
bufferSize = 4096;
|
||||
buffer = realloc(buffer, bufferSize);
|
||||
cursor = buffer;
|
||||
}
|
||||
|
||||
void TCPLineBufferParser::readDataNotification() {
|
||||
|
||||
len = ::read(getDescriptor(), buffer, bufferSize);
|
||||
|
||||
for(unsigned int index = 0; index < bufferSize; ++index) {
|
||||
if(cursor[index] == '\n') {
|
||||
coreutils::ZString data(cursor, index);
|
||||
dataReceived(data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void TCPLineBufferParser::onDataReceived(coreutils::ZString data) {}
|
||||
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
#ifndef __TCPLineBufferParser_h__
|
||||
#define __TCPLineBufferParser_h__
|
||||
|
||||
#include "TCPSocket.h"
|
||||
|
||||
namespace core {
|
||||
|
||||
///
|
||||
/// TCPLineBufferParser
|
||||
///
|
||||
/// This object will read lines of data from the source one line at a time.
|
||||
///
|
||||
///
|
||||
|
||||
class TCPLineBufferParser : public TCPSocket {
|
||||
|
||||
protected:
|
||||
|
||||
///
|
||||
/// This method is called when a line is ready to be processed.
|
||||
///
|
||||
|
||||
virtual void onDataReceived();
|
||||
|
||||
private:
|
||||
|
||||
///
|
||||
/// Read the data for the socket and split into lines and call dataReceived
|
||||
/// for each line parsed.
|
||||
///
|
||||
|
||||
void readDataNotification() override;
|
||||
|
||||
char *buffer;
|
||||
size_t bufferSize;
|
||||
char *cursor;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
+15
-23
@@ -8,8 +8,7 @@ namespace core
|
||||
{
|
||||
|
||||
TCPServer::TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter, int depth, std::string text)
|
||||
: TCPSocket(ePoll, text), commands(delimiter, depth)
|
||||
{
|
||||
: TCPSocket(ePoll, text), commands(delimiter, depth) {
|
||||
|
||||
commands.add(subscriptions, "publish");
|
||||
commands.add(subscriptions, "unpublish");
|
||||
@@ -36,8 +35,7 @@ namespace core
|
||||
close(getDescriptor());
|
||||
}
|
||||
|
||||
void TCPServer::onDataReceived(std::string data)
|
||||
{
|
||||
void TCPServer::onDataReceived(std::string data) {
|
||||
lock.lock();
|
||||
TCPSession *session = accept();
|
||||
if (session)
|
||||
@@ -45,11 +43,9 @@ namespace core
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
TCPSession *TCPServer::accept()
|
||||
{
|
||||
TCPSession * TCPServer::accept() {
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
|
||||
TCPSession *session = getSocketAccept(ePoll);
|
||||
session->setDescriptor(::accept(getDescriptor(), (struct sockaddr *)&session->ipAddress.addr, &session->ipAddress.addressLength));
|
||||
@@ -76,36 +72,30 @@ namespace core
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void TCPServer::removeFromSessionList(TCPSession *session)
|
||||
{
|
||||
void TCPServer::removeFromSessionList(TCPSession *session) {
|
||||
std::vector<TCPSession *>::iterator cursor;
|
||||
lock.lock();
|
||||
for (cursor = sessions.begin(); cursor < sessions.end(); ++cursor)
|
||||
if (*cursor == session)
|
||||
{
|
||||
for(cursor = sessions.begin(); cursor < sessions.end(); ++cursor)
|
||||
if(*cursor == session) {
|
||||
sessions.erase(cursor);
|
||||
break;
|
||||
}
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
void TCPServer::sessionErrorHandler(std::string errorString, std::stringstream &out)
|
||||
{
|
||||
void TCPServer::sessionErrorHandler(std::string errorString, std::stringstream &out) {
|
||||
throw coreutils::Exception(errorString);
|
||||
}
|
||||
|
||||
TCPSession *TCPServer::getSocketAccept(EPoll &ePoll)
|
||||
{
|
||||
TCPSession * TCPServer::getSocketAccept(EPoll &ePoll) {
|
||||
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)
|
||||
{
|
||||
@@ -140,9 +130,11 @@ namespace core
|
||||
data.str("");
|
||||
}
|
||||
|
||||
TCPSession *TCPServer::getSessionByAlias(coreutils::MString alias) {
|
||||
TCPSession *TCPServer::getSessionByAlias(void *alias)
|
||||
{
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << alias;
|
||||
for (auto session : sessions)
|
||||
if (session->alias == alias)
|
||||
if (session->compareAlias(alias))
|
||||
return session;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
+1
-1
@@ -122,7 +122,7 @@ namespace core
|
||||
/// of the alias pointer.
|
||||
///
|
||||
|
||||
TCPSession *getSessionByAlias(coreutils::MString alias);
|
||||
TCPSession *getSessionByAlias(void *alias);
|
||||
|
||||
protected:
|
||||
///
|
||||
|
||||
+36
-17
@@ -4,32 +4,46 @@
|
||||
#include "TCPServer.h"
|
||||
#include "uuid/uuid.h"
|
||||
|
||||
namespace core {
|
||||
namespace core
|
||||
{
|
||||
|
||||
TCPSession::TCPSession(EPoll &ePoll, TCPServer &server, std::string text) : TCPSocket(ePoll, text), server(server) {
|
||||
uuid_t uuid;
|
||||
uuid_generate(uuid);
|
||||
alias = coreutils::MString((char *)uuid, 26);
|
||||
TCPSession::TCPSession(EPoll &ePoll, TCPServer &server, std::string text) : TCPSocket(ePoll, text), server(server)
|
||||
{
|
||||
uuid_t uuidObject;
|
||||
uuid_generate(uuidObject);
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << uuidObject;
|
||||
alias = (void *)uuidObject;
|
||||
}
|
||||
|
||||
TCPSession::~TCPSession() {
|
||||
TCPSession::~TCPSession()
|
||||
{
|
||||
server.removeFromSessionList(this);
|
||||
server.subscriptions.removeSessionSubscriptions(*this);
|
||||
}
|
||||
|
||||
void TCPSession::output(std::stringstream &data) {
|
||||
void TCPSession::output(std::stringstream &data)
|
||||
{
|
||||
data << "|" << ipAddress.getClientAddressAndPort();
|
||||
}
|
||||
|
||||
void TCPSession::protocol(coreutils::ZString &data) {
|
||||
void TCPSession::protocol(coreutils::ZString &data)
|
||||
{
|
||||
if (data.getLength() != 0)
|
||||
{
|
||||
if (!server.commands.processRequest(data, *this))
|
||||
{
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << "Received data could not be parsed: " << data.str();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// bool TCPSession::compareAlias(coreutils::MString alias) {
|
||||
// return this->alias == alias;
|
||||
// }
|
||||
bool TCPSession::compareAlias(void *alias) {
|
||||
return this->alias = alias;
|
||||
}
|
||||
|
||||
void TCPSession::outputAlias(std::stringstream &out) {
|
||||
out << alias;
|
||||
}
|
||||
|
||||
void TCPSession::onRegistered() {
|
||||
onConnected();
|
||||
@@ -61,7 +75,8 @@ namespace core {
|
||||
memmove(lineBuffer, lineBuffer + lineLength, lineBufferSize);
|
||||
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize);
|
||||
}
|
||||
else if (lineBufferSize >= blockLength) {
|
||||
else if (lineBufferSize >= blockLength)
|
||||
{
|
||||
coreutils::ZString zBlock(lineBuffer, blockLength);
|
||||
onBlockReceived(zBlock);
|
||||
lineBufferSize -= blockLength;
|
||||
@@ -73,30 +88,34 @@ namespace core {
|
||||
}
|
||||
}
|
||||
|
||||
void TCPSession::setBlockSize(int blockSize) {
|
||||
void TCPSession::setBlockSize(int blockSize)
|
||||
{
|
||||
this->blockSize = blockSize;
|
||||
}
|
||||
|
||||
void TCPSession::onLineReceived(coreutils::ZString &line) {
|
||||
void TCPSession::onLineReceived(coreutils::ZString &line)
|
||||
{
|
||||
protocol(line);
|
||||
send();
|
||||
if (term)
|
||||
shutdown("termination requested");
|
||||
}
|
||||
|
||||
void TCPSession::onBlockReceived(coreutils::ZString &block) {
|
||||
void TCPSession::onBlockReceived(coreutils::ZString &block)
|
||||
{
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "[" << block.getLength() << "]";
|
||||
if (term)
|
||||
shutdown("termination requested");
|
||||
}
|
||||
|
||||
void TCPSession::send() {
|
||||
if (out.tellp() > 0)
|
||||
if(out.tellp() > 0)
|
||||
write(out.str());
|
||||
out.str("");
|
||||
}
|
||||
|
||||
void TCPSession::terminate() {
|
||||
void TCPSession::terminate()
|
||||
{
|
||||
term = true;
|
||||
}
|
||||
|
||||
|
||||
+29
-13
@@ -1,12 +1,11 @@
|
||||
#ifndef __Session_h__
|
||||
#define __Session_h__
|
||||
|
||||
#include "TCPLineBufferParser.h"
|
||||
#include "MString.h"
|
||||
#include "SessionFilter.h"
|
||||
#include "TCPSocket.h"
|
||||
|
||||
namespace core {
|
||||
namespace core
|
||||
{
|
||||
|
||||
class Command;
|
||||
class TCPServer;
|
||||
@@ -23,7 +22,8 @@ namespace core {
|
||||
///
|
||||
///
|
||||
|
||||
class TCPSession : public TCPLineBufferParser {
|
||||
class TCPSession : public TCPSocket
|
||||
{
|
||||
|
||||
public:
|
||||
///
|
||||
@@ -76,20 +76,20 @@ namespace core {
|
||||
char uuid[37];
|
||||
|
||||
///
|
||||
/// alias is a MString that can be set to any string of characters that identifies
|
||||
/// this session uniquely. Alias's should be unique between sessions.
|
||||
/// alias is a void pointer that can be set to point to any object that identifies
|
||||
/// this session uniquely. Using this approach, inheriting objects can determine
|
||||
/// how it knows the contacts that this server manages.
|
||||
///
|
||||
|
||||
coreutils::MString alias;
|
||||
///
|
||||
///
|
||||
///
|
||||
void *alias;
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
// bool compareAlias(coreutils::MString alias);
|
||||
virtual bool compareAlias(void *alias);
|
||||
|
||||
virtual void outputAlias(std::stringstream &out);
|
||||
|
||||
protected:
|
||||
///
|
||||
@@ -103,8 +103,7 @@ namespace core {
|
||||
/// received. If you need data split by line termination characters then
|
||||
/// override the onLineReceived method instead.
|
||||
///
|
||||
|
||||
void onDataReceived(coreutils::ZString &data) override;
|
||||
virtual void onDataReceived(coreutils::ZString &data) override;
|
||||
|
||||
///
|
||||
/// Override the onLineReceived method to receive a string of characters that
|
||||
@@ -113,6 +112,23 @@ namespace core {
|
||||
/// this method explicitly using the class and member name.
|
||||
///
|
||||
|
||||
virtual void onLineReceived(coreutils::ZString &line);
|
||||
|
||||
///
|
||||
/// Override the onBlockReceived method to receive a string of characters that
|
||||
/// represents a single block of data of length determined by the block length value. If
|
||||
/// onDataReceived was overriden this method will not be called unless the onDataReceived
|
||||
/// calls this method explicitly using the class and member name.
|
||||
///
|
||||
|
||||
virtual void onBlockReceived(coreutils::ZString &block);
|
||||
|
||||
///
|
||||
/// This method is called from within the protocol method when protocol is called
|
||||
/// on the initial connection where the data is an empty string. Use this method
|
||||
/// to deliver a message to the connection upon connection.
|
||||
///
|
||||
|
||||
virtual void onConnected();
|
||||
|
||||
///
|
||||
|
||||
+4
-2
@@ -1,9 +1,9 @@
|
||||
#ifndef __TCPSocket_h__
|
||||
#define __TCPSocket_h__
|
||||
|
||||
#include "IPAddress.h"
|
||||
#include "Socket.h"
|
||||
#include "includes"
|
||||
#include "Socket.h"
|
||||
#include "IPAddress.h"
|
||||
|
||||
namespace core {
|
||||
|
||||
@@ -20,6 +20,7 @@ namespace core {
|
||||
class TCPSocket : public Socket {
|
||||
|
||||
public:
|
||||
|
||||
TCPSocket(EPoll &ePoll);
|
||||
TCPSocket(EPoll &ePoll, std::string text);
|
||||
virtual ~TCPSocket();
|
||||
@@ -36,6 +37,7 @@ namespace core {
|
||||
///
|
||||
|
||||
virtual void output(std::stringstream &out);
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
#include "TLS.h"
|
||||
#include "EPoll.h"
|
||||
#include "TCPSession.h"
|
||||
#include "Exception.h"
|
||||
|
||||
namespace core {
|
||||
|
||||
static pthread_mutex_t *lockarray;
|
||||
|
||||
static unsigned long thread_id(void) {
|
||||
return ((unsigned long) pthread_self());
|
||||
}
|
||||
|
||||
static void lock_callback(int mode, int type, const char *file, int line) {
|
||||
if(mode & CRYPTO_LOCK)
|
||||
pthread_mutex_lock(&(lockarray[type]));
|
||||
else
|
||||
pthread_mutex_unlock(&(lockarray[type]));
|
||||
}
|
||||
|
||||
TLS::TLS() {
|
||||
|
||||
SSL_library_init();
|
||||
SSL_load_error_strings();
|
||||
|
||||
lockarray = (pthread_mutex_t *)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
|
||||
for(int i = 0; i < CRYPTO_num_locks(); ++i)
|
||||
pthread_mutex_init(&(lockarray[i]), NULL);
|
||||
|
||||
CRYPTO_set_id_callback((unsigned long (*)())thread_id);
|
||||
CRYPTO_set_locking_callback((void (*)(int, int, const char *, int))lock_callback);
|
||||
|
||||
SSLeay_add_ssl_algorithms();
|
||||
RAND_load_file("/dev/hwrng", 1024);
|
||||
|
||||
if(!(ctx = SSL_CTX_new(SSLv23_server_method())))
|
||||
throw coreutils::Exception("Error while setting server method SSLv23.");
|
||||
SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
|
||||
SSL_CTX_set_options(ctx, SSL_OP_NO_TICKET);
|
||||
SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER);
|
||||
// SSL_CTX_set_generate_session_id(ctx, generate_session_id);
|
||||
SSL_CTX_set_cipher_list(ctx, "ECDH-ECDSA-AES256-GCM-SHA384:DHE-DSS-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA256:DHE-RSA-AES256-SHA:DHE-DSS-AES256-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA");
|
||||
if(SSL_CTX_use_certificate_file(ctx, sip_cert, SSL_FILETYPE_PEM) <= 0)
|
||||
throw coreutils::Exception("Error looking up certificate.");
|
||||
if(SSL_CTX_use_PrivateKey_file(ctx, sip_key, SSL_FILETYPE_PEM) < 0)
|
||||
throw coreutils::Exception("Error with private key.");
|
||||
if(SSL_CTX_check_private_key(ctx) != 1)
|
||||
throw coreutils::Exception("Private key does not match certificate.");
|
||||
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
|
||||
SSL_CTX_set_verify_depth(ctx, 1);
|
||||
if(!SSL_CTX_load_verify_locations(ctx, sip_cacert, NULL))
|
||||
throw coreutils::Exception("Cannot verify locations.");
|
||||
SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(sip_cacert));
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << "Server key authenticated.";
|
||||
}
|
||||
|
||||
TLS::~TLS() {}
|
||||
|
||||
static int generate_session_id(const SSL *ssl, unsigned char *id, unsigned int *id_len) {
|
||||
char *session_id_prefix = (char *)"BARANT";
|
||||
unsigned int count = 0;
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "Generating unique session id.";
|
||||
do {
|
||||
RAND_bytes(id, *id_len);
|
||||
memcpy(id, session_id_prefix, (strlen(session_id_prefix) < *id_len));
|
||||
} while(SSL_has_matching_session_id(ssl, id, *id_len) && (++count < 10));
|
||||
return 1;
|
||||
}
|
||||
|
||||
void handshake_complete(const SSL *ssl, int where, int ret) {
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "==>" << SSL_state_string_long(ssl) << "<==" << ret;
|
||||
if(where & SSL_CB_HANDSHAKE_DONE) {
|
||||
X509 *ssl_client_cert = SSL_get_peer_certificate(ssl);
|
||||
if(!ssl_client_cert)
|
||||
throw std::string("Unable to get peer certificate.");
|
||||
X509_free(ssl_client_cert);
|
||||
if(SSL_get_verify_result(ssl) != X509_V_OK)
|
||||
throw std::string("Certificate verification failed.");
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "Certificate verified successfully.";
|
||||
}
|
||||
else
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "No client certificate.";
|
||||
}
|
||||
|
||||
TLSSession::TLSSession(EPoll &ePoll, TCPServer &server) : TCPSession(ePoll, server) {}
|
||||
|
||||
void TLSSession::onRegister() {
|
||||
initialized = true;
|
||||
int ret;
|
||||
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "TLS socket initializing on socket " << getDescriptor() << "...";
|
||||
|
||||
fcntl(getDescriptor(), F_SETFL, fcntl(getDescriptor(), F_GETFL, 0) | O_NONBLOCK);
|
||||
|
||||
ssl = SSL_new(static_cast<TLSServer &>(server).ctx);
|
||||
// if(ssl <= 0)
|
||||
// throw std::string("Error creating new TLS socket.");
|
||||
|
||||
SSL_set_info_callback(ssl, handshake_complete);
|
||||
|
||||
if((ret = SSL_set_fd(ssl, getDescriptor())) == 0)
|
||||
throw std::string("Error setting TLS socket descriptor.");
|
||||
|
||||
// if(!SSL_set_generate_session_id(ssl, generate_session_id))
|
||||
// throw std::string("Error setting session identifier callback.");
|
||||
|
||||
}
|
||||
|
||||
void TLSSession::onRegistered() {
|
||||
|
||||
switch (SSL_get_error(ssl, SSL_accept(ssl))) {
|
||||
case SSL_ERROR_SSL:
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "ERROR_SSL on ssl_accept. errno=" << errno;
|
||||
break;
|
||||
case SSL_ERROR_WANT_READ:
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "ERROR_WANT_READ on ssl_accept.";
|
||||
break;
|
||||
case SSL_ERROR_WANT_WRITE:
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "ERROR_WANT_WRITE on ssl_accept.";
|
||||
break;
|
||||
case SSL_ERROR_SYSCALL:
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "ERROR_SYSCALL on ssl_accept. errno=" << errno;
|
||||
shutdown();
|
||||
break;
|
||||
default:
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "Unknown ERROR on ssl_accept.";
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TLSSession::~TLSSession() {}
|
||||
|
||||
void TLSSession::protocol(coreutils::ZString &data) {}
|
||||
|
||||
void TLSSession::receiveData(coreutils::ZString &buffer) {
|
||||
|
||||
int len;
|
||||
// int error = -1;
|
||||
//
|
||||
std::cout << "receiveData TLS" << std::endl;
|
||||
|
||||
if((len = ::SSL_read(ssl, buffer.getData(), buffer.getLength())) >= 0) {
|
||||
std::cout << "receiveData TLS...len=" << len << ":" << buffer << std::endl;
|
||||
onDataReceived(buffer);
|
||||
}
|
||||
else {
|
||||
switch (SSL_get_error(ssl, len)) {
|
||||
case SSL_ERROR_SSL:
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "ERROR_SSL on ssl_read. error=" << errno;
|
||||
break;
|
||||
case SSL_ERROR_WANT_READ:
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "ERROR_WANT_READ on ssl_read.";
|
||||
break;
|
||||
case SSL_ERROR_WANT_WRITE:
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "ERROR_WANT_WRITE on ssl_read.";
|
||||
break;
|
||||
case SSL_ERROR_SYSCALL:
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "ERROR_SYSCALL on ssl_read. errno=" << errno;
|
||||
break;
|
||||
default:
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "Unknown ERROR on ssl_read.";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void TLSSession::output(std::stringstream &out) {
|
||||
out << "|" << ipAddress.getClientAddressAndPort();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
#ifndef TLSServerSocket_h__
|
||||
#define TLSServerSocket_h__
|
||||
|
||||
#include "Socket.h"
|
||||
#include "TCPServer.h"
|
||||
#include "Command.h"
|
||||
#include "TCPSession.h"
|
||||
#include "IPAddress.h"
|
||||
|
||||
namespace core {
|
||||
|
||||
///
|
||||
/// TLS
|
||||
///
|
||||
/// This object provides the support methods to handle TLS on the server core and
|
||||
/// session environment.
|
||||
///
|
||||
|
||||
class TLS {
|
||||
|
||||
public:
|
||||
|
||||
TLS();
|
||||
|
||||
///
|
||||
/// The destructor for this object.
|
||||
///
|
||||
|
||||
~TLS();
|
||||
|
||||
SSL_CTX *ctx;
|
||||
|
||||
private:
|
||||
|
||||
char *sip_cacert = (char *)"../testkeys/certs/pbxca.crt";
|
||||
char *sip_cert = (char *)"../testkeys/certs/pbxserver.crt";
|
||||
char *sip_key = (char *)"../testkeys/certs/pbxserver.key";
|
||||
|
||||
|
||||
public:
|
||||
|
||||
///
|
||||
/// The output method is called by a socket session (Session) and
|
||||
/// will output the detail information for the client socket. When extending
|
||||
/// TLSSocket or Session you can override the method to add attributes
|
||||
/// to the list.
|
||||
///
|
||||
|
||||
virtual void output(std::stringstream &out);
|
||||
virtual void protocol(coreutils::ZString &data) override;
|
||||
|
||||
protected:
|
||||
void receiveData(coreutils::ZString &buffer) override;
|
||||
void onRegister();
|
||||
void onRegistered();
|
||||
|
||||
private:
|
||||
bool initialized = false;
|
||||
SSL *ssl;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,20 +1,16 @@
|
||||
#include "Timer.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;
|
||||
@@ -31,10 +27,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;
|
||||
|
||||
@@ -46,22 +42,19 @@ 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,11 +1,10 @@
|
||||
#ifndef __Timer_h__
|
||||
#define __Timer_h__
|
||||
|
||||
#include "EPoll.h"
|
||||
#include "Socket.h"
|
||||
#include "EPoll.h"
|
||||
|
||||
namespace core
|
||||
{
|
||||
namespace core {
|
||||
|
||||
///
|
||||
/// Timer
|
||||
@@ -16,8 +15,7 @@ namespace core
|
||||
/// interface. It cannot be instantiated directly but must be extended.
|
||||
///
|
||||
|
||||
class Timer : Socket
|
||||
{
|
||||
class Timer : Socket {
|
||||
|
||||
public:
|
||||
Timer(EPoll &ePoll);
|
||||
@@ -50,6 +48,7 @@ namespace core
|
||||
double getEpoch();
|
||||
|
||||
protected:
|
||||
|
||||
///
|
||||
/// This method is called when the time out occurs.
|
||||
///
|
||||
@@ -59,6 +58,7 @@ namespace core
|
||||
private:
|
||||
void onDataReceived(std::string data) override;
|
||||
double delayValue;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
BIN
Binary file not shown.
@@ -0,0 +1,6 @@
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
std::cout << "Hello world!" << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
{
|
||||
"folders": [
|
||||
{
|
||||
"path": "../../CoreUtils"
|
||||
},
|
||||
{
|
||||
"path": ".."
|
||||
},
|
||||
{
|
||||
"path": "../../HTTPServer"
|
||||
}
|
||||
],
|
||||
"settings": {},
|
||||
"launch": {
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "(gdb) Launch",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "/home/barant/Development/HTTPServer/HTTPServer",
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "/home/barant/Development/HTTPServer/",
|
||||
"environment": [],
|
||||
"externalConsole": false,
|
||||
"MIMode": "gdb",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user