From 9af17daed688c6ccfe86d51ac7140d4ed51aa0bf Mon Sep 17 00:00:00 2001 From: Brad Arant Date: Tue, 26 Jul 2022 16:07:37 -0700 Subject: [PATCH] Fixing command list delimiter depth. --- CommandList.cpp | 35 +++++++++----------- CommandList.h | 2 +- Sihen.txt | 1 - TCPServer.cpp | 86 ++++++++++++++++++++++++------------------------- TCPSession.cpp | 82 +++++++++++++++++++++++----------------------- 5 files changed, 101 insertions(+), 105 deletions(-) delete mode 100644 Sihen.txt diff --git a/CommandList.cpp b/CommandList.cpp index df97968..a999c72 100644 --- a/CommandList.cpp +++ b/CommandList.cpp @@ -2,50 +2,47 @@ #include "Log.h" namespace core { - + CommandList::CommandList(std::string delimiter, int depth) : delimiter(delimiter), depth(depth) {} - + void CommandList::add(Command &command, std::string name) { commands.insert(std::make_pair(name, &command)); } - + void CommandList::remove(Command &command) {} - - bool CommandList::processRequest(coreutils::ZString &request, TCPSession &session) { + + int CommandList::processRequest(coreutils::ZString &request, TCPSession &session) { if(session.grab != NULL) - return session.grab->processCommand(request, session); + return session.grab->processCommand(request, session); else { if(request.equals("")) - return false; - request.split(delimiter, 10); + return 0; + request.split(delimiter, depth); request.reset(); try { auto command = commands.at(request[0].str()); - return command->processCommand(request, session); + return command->processCommand(request, session); } catch(...) { - return false; + return 0; } } return true; } - + bool CommandList::grabInput(TCPSession &session, Command &command) { session.grab = &command; return true; } - + void CommandList::clearGrab(TCPSession &session) { session.grab = NULL; } - + int CommandList::processCommand(coreutils::ZString &request, TCPSession &session) { -// for(Command *command : commands) -// session.out << command->getName() << std::endl; + // for(Command *command : commands) + // session.out << command->getName() << std::endl; return true; } - + } - - - diff --git a/CommandList.h b/CommandList.h index 5d62707..acbc99d 100644 --- a/CommandList.h +++ b/CommandList.h @@ -40,7 +40,7 @@ namespace core { /// then control is given to the process handler holding the grab on the input. /// - bool processRequest(coreutils::ZString &request, TCPSession &session); + int processRequest(coreutils::ZString &request, TCPSession &session); /// /// Use grabInput() within a Command object to force the requesting handler to receive diff --git a/Sihen.txt b/Sihen.txt deleted file mode 100644 index 3eaf00b..0000000 --- a/Sihen.txt +++ /dev/null @@ -1 +0,0 @@ -0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: 0.000000| pos: \ No newline at end of file diff --git a/TCPServer.cpp b/TCPServer.cpp index 24adf2d..f2347e5 100644 --- a/TCPServer.cpp +++ b/TCPServer.cpp @@ -8,31 +8,31 @@ namespace core { TCPServer::TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter, int depth, std::string text) : TCPSocket(ePoll, text), commands(delimiter, depth) { - - commands.add(subscriptions, "publish"); - commands.add(subscriptions, "unpublish"); - commands.add(subscriptions, "subscribe"); - commands.add(subscriptions, "unsubscribe"); - commands.add(subscriptions, "catalog"); - commands.add(subscriptions, "event"); - - setDescriptor(socket(AF_INET, SOCK_STREAM, 0)); - int yes = 1; - setsockopt(getDescriptor(), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); - - if(bind(getDescriptor(), address.getPointer(), address.addressLength) < 0) - throw coreutils::Exception("Error on bind to socket: " + std::to_string(errno)); - - if(listen(getDescriptor(), 20) < 0) - throw coreutils::Exception("Error on listen to socket"); - - } + + commands.add(subscriptions, "publish"); + commands.add(subscriptions, "unpublish"); + commands.add(subscriptions, "subscribe"); + commands.add(subscriptions, "unsubscribe"); + commands.add(subscriptions, "catalog"); + commands.add(subscriptions, "event"); + + setDescriptor(socket(AF_INET, SOCK_STREAM, 0)); + int yes = 1; + setsockopt(getDescriptor(), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); + + if(bind(getDescriptor(), address.getPointer(), address.addressLength) < 0) + throw coreutils::Exception("Error on bind to socket: " + std::to_string(errno)); + + if(listen(getDescriptor(), 20) < 0) + throw coreutils::Exception("Error on listen to socket"); + + } TCPServer::~TCPServer() { coreutils::Log(coreutils::LOG_DEBUG_2) << "Closing server socket " << getDescriptor() << "."; close(getDescriptor()); } - + void TCPServer::onDataReceived(std::string data) { lock.lock(); TCPSession *session = accept(); @@ -40,11 +40,11 @@ namespace core { sessions.push_back(session); lock.unlock(); } - + TCPSession * TCPServer::accept() { - + try { - + TCPSession *session = getSocketAccept(ePoll); session->setDescriptor(::accept(getDescriptor(), (struct sockaddr *)&session->ipAddress.addr, &session->ipAddress.addressLength)); // if(blackList && blackList->contains(session->ipAddress.getClientAddress())) { @@ -67,30 +67,30 @@ namespace core { } return NULL; } - + void TCPServer::removeFromSessionList(TCPSession *session) { std::vector::iterator cursor; - lock.lock(); + lock.lock(); for(cursor = sessions.begin(); cursor < sessions.end(); ++cursor) if(*cursor == session) { - sessions.erase(cursor); - break; - } - lock.unlock(); + sessions.erase(cursor); + break; + } + lock.unlock(); } - + void TCPServer::sessionErrorHandler(std::string errorString, std::stringstream &out) { throw coreutils::Exception(errorString); } - + TCPSession * TCPServer::getSocketAccept(EPoll &ePoll) { return new TCPSession(ePoll, *this); } - + 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 sequence = 0; for(auto *sessionx : sessions) { @@ -100,26 +100,26 @@ namespace core { } return 1; } - + void TCPServer::sendToAll(std::stringstream &data) { for(auto session : sessions) - session->write(data.str()); + session->write(data.str()); data.str(""); } - + void TCPServer::sendToAll(std::stringstream &data, TCPSession &sender) { for(auto session : sessions) - if(session != &sender) - session->write(data.str()); + if(session != &sender) + session->write(data.str()); data.str(""); } - + void TCPServer::sendToAll(std::stringstream &data, TCPSession &sender, SessionFilter filter) { for(auto session : sessions) - if(filter.test(*session)) - if(session != &sender) - session->write(data.str()); + if(filter.test(*session)) + if(session != &sender) + session->write(data.str()); data.str(""); } - + } diff --git a/TCPSession.cpp b/TCPSession.cpp index 59283c2..67c25ae 100644 --- a/TCPSession.cpp +++ b/TCPSession.cpp @@ -6,92 +6,92 @@ namespace core { TCPSession::TCPSession(EPoll &ePoll, TCPServer &server, std::string text) : TCPSocket(ePoll, text), server(server) {} - + TCPSession::~TCPSession() { server.removeFromSessionList(this); server.subscriptions.removeSessionSubscriptions(*this); } - + void TCPSession::output(std::stringstream &data) { data << "|" << ipAddress.getClientAddressAndPort(); } - + 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(); + if(server.commands.processRequest(data, *this) == 0) { + coreutils::Log(coreutils::LOG_DEBUG_1) << "Received data could not be parsed: " << data.str(); } } } - + void TCPSession::onRegistered() { onConnected(); coreutils::ZString blank(""); protocol(blank); send(); if(term) - shutdown("termination requested"); + shutdown("termination requested"); } - + void TCPSession::onConnected() {} - + void TCPSession::onDataReceived(coreutils::ZString &data) { if(data.getLength() > 0) { - lineBuffer = (char *)realloc(lineBuffer, lineBufferSize + data.getLength()); - memcpy(lineBuffer + lineBufferSize, data.getData(), data.getLength()); - lineBufferSize += data.getLength(); - while(lineBufferSize > 0) { - if(blockSize == 0) { - lineLength = strcspn(lineBuffer, "\r\n"); - if(lineLength == lineBufferSize) - break; + lineBuffer = (char *)realloc(lineBuffer, lineBufferSize + data.getLength()); + memcpy(lineBuffer + lineBufferSize, data.getData(), data.getLength()); + lineBufferSize += data.getLength(); + while(lineBufferSize > 0) { + if(blockSize == 0) { + lineLength = strcspn(lineBuffer, "\r\n"); + if(lineLength == lineBufferSize) + break; coreutils::ZString zLine(lineBuffer, lineLength); - onLineReceived(zLine); - if(lineBuffer[lineLength] == '\r') - ++lineLength; - if(lineBuffer[lineLength] == '\n') - ++lineLength; - lineBufferSize -= lineLength; - if(lineBufferSize > 0) - memmove(lineBuffer, lineBuffer + lineLength, lineBufferSize); - lineBuffer = (char *)realloc(lineBuffer, lineBufferSize); + onLineReceived(zLine); + if(lineBuffer[lineLength] == '\r') + ++lineLength; + if(lineBuffer[lineLength] == '\n') + ++lineLength; + lineBufferSize -= lineLength; + if(lineBufferSize > 0) + memmove(lineBuffer, lineBuffer + lineLength, lineBufferSize); + lineBuffer = (char *)realloc(lineBuffer, lineBufferSize); } else if(lineBufferSize >= blockLength) { - coreutils::ZString zBlock(lineBuffer, blockLength); - onBlockReceived(zBlock); - lineBufferSize -= blockLength; - if(lineBufferSize > 0) - memmove(lineBuffer, lineBuffer + blockLength, lineBufferSize); - lineBuffer = (char *)realloc(lineBuffer, lineBufferSize); - } - } + coreutils::ZString zBlock(lineBuffer, blockLength); + onBlockReceived(zBlock); + lineBufferSize -= blockLength; + if(lineBufferSize > 0) + memmove(lineBuffer, lineBuffer + blockLength, lineBufferSize); + lineBuffer = (char *)realloc(lineBuffer, lineBufferSize); + } + } } } - + void TCPSession::setBlockSize(int blockSize) { this->blockSize = blockSize; } - + void TCPSession::onLineReceived(coreutils::ZString &line) { protocol(line); send(); if(term) shutdown("termination requested"); } - + 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) - write(out.str()); + write(out.str()); out.str(""); } - + void TCPSession::terminate() { term = true; } - + }