diff --git a/CommandList.cpp b/CommandList.cpp index 750cf84..2ac92cc 100644 --- a/CommandList.cpp +++ b/CommandList.cpp @@ -4,41 +4,41 @@ namespace core { CommandList::CommandList(std::string delimiter) : delimiter(delimiter) {} - + void CommandList::add(Command &command, std::string name) { command.setName(name); commands.push_back(&command); } - + void CommandList::remove(Command &command) {} - + bool CommandList::processRequest(coreutils::ZString &request, TCPSession &session) { if(session.grab != NULL) - return session.grab->processCommand(request, session); + return session.grab->processCommand(request, session); else { request.split(delimiter); - for(auto *command : commands) - if(command->check(request)) - return command->processCommand(request, session); + for(auto *command : commands) + if(command->check(request)) + return command->processCommand(request, session); } return false; } - + 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; + session.out << command->getName() << std::endl; return true; } - + } diff --git a/ConsoleServer.cpp b/ConsoleServer.cpp index 16cdc4f..93a13dc 100644 --- a/ConsoleServer.cpp +++ b/ConsoleServer.cpp @@ -5,7 +5,7 @@ namespace core { - ConsoleServer::ConsoleServer(EPoll &ePoll, IPAddress address) : TCPServer(ePoll, address, " ", "Console") { + ConsoleServer::ConsoleServer(EPoll &ePoll, IPAddress address) : TCPServer(ePoll, address, " ", "Console Server") { coreutils::Log(this); } diff --git a/Socket.cpp b/Socket.cpp index 5dbd0b1..5173b7c 100644 --- a/Socket.cpp +++ b/Socket.cpp @@ -7,7 +7,7 @@ namespace core { Socket::Socket(EPoll &ePoll, std::string text) : ePoll(ePoll), text(text) { - coreutils::Log(coreutils::LOG_DEBUG_2) << "BMASocket object created [" << text << "]."; + coreutils::Log(coreutils::LOG_DEBUG_2) << "Socket object created [" << text << "]."; buffer = (char *)malloc(4096); length = 4096; } diff --git a/TCPServer.cpp b/TCPServer.cpp index cc89dd6..eeeb269 100644 --- a/TCPServer.cpp +++ b/TCPServer.cpp @@ -5,49 +5,48 @@ #include "Log.h" namespace core { - + TCPServer::TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter, std::string text) : TCPSocket(ePoll, text), commands(delimiter) { - - 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"); - } - + + 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(); if(session) - sessions.push_back(session); + 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())) { - // session->shutdown(); - // Log(LOG_WARN) << "Client at IP address " << session->ipAddress.getClientAddress() << " is blacklisted and was denied a connection."; - // return NULL; - // } - // if(whiteList && !whiteList->contains(session->ipAddress.getClientAddress())) { - // session->shutdown(); - // Log(LOG_WARN) << "Client at IP address " << session->ipAddress.getClientAddress() << " is not authorized and was denied a connection."; - // return NULL; - // } - coreutils::Log(coreutils::LOG_DEBUG_2) << "Session started on socket " << session->getDescriptor() << "."; + // if(blackList && blackList->contains(session->ipAddress.getClientAddress())) { + // session->shutdown(); + // Log(LOG_WARN) << "Client at IP address " << session->ipAddress.getClientAddress() << " is blacklisted and was denied a connection."; + // return NULL; + // } + // if(whiteList && !whiteList->contains(session->ipAddress.getClientAddress())) { + // session->shutdown(); + // Log(LOG_WARN) << "Client at IP address " << session->ipAddress.getClientAddress() << " is not authorized and was denied a connection."; + // return NULL; + // } return session; } catch(coreutils::Exception e) { @@ -58,26 +57,26 @@ namespace core { } return NULL; } - + void TCPServer::removeFromSessionList(TCPSession *session) { std::vector::iterator cursor; for(cursor = sessions.begin(); cursor < sessions.end(); ++cursor) if(*cursor == session) sessions.erase(cursor); } - + 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) { @@ -87,26 +86,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 99e7c8d..be1be3c 100644 --- a/TCPSession.cpp +++ b/TCPSession.cpp @@ -1,5 +1,6 @@ #include "TCPSession.h" #include "TCPServer.h" +#include "Exception.h" #include "Log.h" namespace core { @@ -16,78 +17,77 @@ namespace core { void TCPSession::protocol(coreutils::ZString &data) { if(!server.commands.processRequest(data, *this)) - if(data.getLength() != 0) - server.sessionErrorHandler("Invalid data received.", out); + throw coreutils::Exception("Data received is not valid."); } - + 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); - } - } - } + 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; } - + } diff --git a/TCPSession.h b/TCPSession.h index aee1db8..e0a9edf 100644 --- a/TCPSession.h +++ b/TCPSession.h @@ -41,12 +41,6 @@ namespace core { virtual void output(std::stringstream &data); - /// - /// Use out to send data to the session socket or other session sockets. - /// - - std::stringstream out; - /// /// The send method is used to output the contents of the out stream /// to the session containing the stream. @@ -66,6 +60,12 @@ namespace core { TCPServer &server; + /// + /// Use out to send data to the session socket or other session sockets. + /// + + std::stringstream out; + protected: /// diff --git a/html/CommandList_8h_source.html b/html/CommandList_8h_source.html index a07a5b2..cbc3b9b 100644 --- a/html/CommandList_8h_source.html +++ b/html/CommandList_8h_source.html @@ -74,48 +74,52 @@ $(function() {
7 
8 namespace core {
9 
-
17 
+
17 
18  class CommandList : public Command {
-
19 
+
19 
20  public:
-
21 
-
25 
-
26  void add(Command &command, std::string name = "");
-
27 
-
31 
-
32  void remove(Command &command);
-
33 
-
40 
-
41  bool processRequest(coreutils::ZString request, TCPSession *session, std::stringstream &data);
+
21 
+
22  CommandList(std::string delimiter = "");
+
23 
+
27 
+
28  void add(Command &command, std::string name = "");
+
29 
+
33 
+
34  void remove(Command &command);
+
35 
42 
-
48 
-
49  bool grabInput(TCPSession *session, Command &command);
+
43  bool processRequest(coreutils::ZString &request, TCPSession &session);
+
44 
50 
-
54 
-
55  void clearGrab(TCPSession *session);
-
56 
-
60 
-
61  int processCommand(std::string request, TCPSession *session, std::stringstream &data);
-
62 
-
63  protected:
-
64 
-
68 
-
69  std::vector<Command *> commands;
-
70 
-
71  };
-
72 
-
73 }
-
74 
-
75 #endif
+
51  bool grabInput(TCPSession &session, Command &command);
+
52 
+
56 
+
57  void clearGrab(TCPSession &session);
+
58 
+
62 
+
63  int processCommand(coreutils::ZString &request, TCPSession &session);
+
64 
+
65  protected:
+
66 
+
70 
+
71  std::vector<Command *> commands;
+
72  std::string delimiter;
+
73 
+
74  };
+
75 
+
76 }
+
77 
+
78 #endif
-
bool grabInput(TCPSession *session, Command &command)
Definition: CommandList.cpp:27
-
bool processRequest(coreutils::ZString request, TCPSession *session, std::stringstream &data)
Definition: CommandList.cpp:15
Definition: TCPSession.h:24
-
std::vector< Command * > commands
Definition: CommandList.h:69
-
void add(Command &command, std::string name="")
Definition: CommandList.cpp:6
-
void remove(Command &command)
Definition: CommandList.cpp:11
+
std::vector< Command * > commands
Definition: CommandList.h:71
+
void add(Command &command, std::string name="")
Definition: CommandList.cpp:8
+
bool processRequest(coreutils::ZString &request, TCPSession &session)
Definition: CommandList.cpp:15
+
bool grabInput(TCPSession &session, Command &command)
Definition: CommandList.cpp:27
+
void remove(Command &command)
Definition: CommandList.cpp:13
Definition: Command.h:22
Definition: CommandList.h:18
+
int processCommand(coreutils::ZString &request, TCPSession &session)
Definition: CommandList.cpp:36