diff --git a/CommandList.cpp b/CommandList.cpp index 451a517..9ba6265 100644 --- a/CommandList.cpp +++ b/CommandList.cpp @@ -13,20 +13,15 @@ namespace core { } bool CommandList::processRequest(std::string request, TCPSession *session, std::stringstream &data) { - std::stringstream input = std::stringstream(request); - while(!input.eof()) { - std::string requests; - std::getline(input, requests); - if(session->grab != NULL) - session->grab->processCommand(requests, session, data); - else { - int pos = requests.find(" "); - std::string function = pos == requests.npos ? requests: requests.substr(0, pos); - for(auto *command : commands) - if(command->check(function)) - command->processCommand(requests, session, data); - } - } + if(session->grab != NULL) + session->grab->processCommand(request, session, data); + else { + int pos = request.find(" "); + std::string function = pos == request.npos ? request: request.substr(0, pos); + for(auto *command : commands) + if(command->check(function)) + command->processCommand(request, session, data); + } return true; } diff --git a/Socket.cpp b/Socket.cpp index df89c8b..403741a 100644 --- a/Socket.cpp +++ b/Socket.cpp @@ -138,30 +138,26 @@ namespace core { } } - void Socket::writeSocket() { - if(shutDown) - return; - + void Socket::writeSocket() { if(fifo.size() > 0) { outlock.lock(); + coreutils::Log(coreutils::LOG_DEBUG_3) << "Writing data to socket " << getDescriptor() << " [" << fifo.front() << "]."; ::write(descriptor, fifo.front().c_str(), fifo.front().length()); fifo.pop(); coreutils::Log(coreutils::LOG_DEBUG_4) << "resetSocket from writeSocket."; - if(active) - ePoll.resetSocket(this); + ePoll.resetSocket(this); + if(shutDown && !needsToWrite()) + delete this; outlock.unlock(); } } int Socket::write(std::string data) { - if(!active) - return -1; - + coreutils::Log(coreutils::LOG_DEBUG_3) << "Writing data to socket " << getDescriptor() << " buffer [" << data << "]."; outlock.lock(); fifo.emplace(data); - coreutils::Log(coreutils::LOG_DEBUG_4) << "resetSocket from write. active is " << active; - if(active) - ePoll.resetSocket(this); + coreutils::Log(coreutils::LOG_DEBUG_4) << "Enabling write on socket " << getDescriptor() << " with " << fifo.size() << " entries to write."; + ePoll.resetSocket(this); outlock.unlock(); return 1; } @@ -171,6 +167,7 @@ namespace core { } bool Socket::needsToWrite() { + coreutils::Log(coreutils::LOG_DEBUG_4) << "Socket " << getDescriptor() << " needs to write is " << (fifo.size() > 0) << "."; return fifo.size() > 0; } @@ -178,7 +175,11 @@ namespace core { coreutils::Log(coreutils::LOG_DEBUG_2) << "Shutdown requested on socket " << descriptor << " with reason " << text << "."; shutDown = true; active = false; - delete this; + if(!needsToWrite()) { + active = false; + delete this; + } + } } diff --git a/TCPSession.cpp b/TCPSession.cpp index 35f54cd..b984a3b 100644 --- a/TCPSession.cpp +++ b/TCPSession.cpp @@ -30,8 +30,33 @@ namespace core { void TCPSession::onConnected() {} - void TCPSession::onDataReceived(std::string data) { - protocol(data); + void TCPSession::onDataReceived(char *data, int len) { + if(len > 0) { + lineBuffer = (char *)realloc(lineBuffer, lineBufferSize + len); + memcpy(lineBuffer + lineBufferSize, data, len); + lineBufferSize += len; + while(lineBufferSize > 0) { + int lineLength = strcspn(lineBuffer, "\r\n"); + if(lineLength == lineBufferSize) + break; + onLineReceived(std::string(lineBuffer, lineLength)); + if(lineBuffer[lineLength] == '\r') + ++lineLength; + if(lineBuffer[lineLength] == '\n') + ++lineLength; + lineBufferSize -= lineLength; + if(lineBufferSize > 0) + memmove(lineBuffer, lineBuffer + lineLength, lineBufferSize); + coreutils::Log(coreutils::LOG_DEBUG_3) << "lineBufferSize=" << lineBufferSize << "; lineLength=" << lineLength << ";"; + lineBuffer = (char *)realloc(lineBuffer, lineBufferSize); +// coreutils::Log(coreutils::LOG_DEBUG_3) << "lineBuffer=" << std::string(lineBuffer, lineBufferSize); + } + } + } + + void TCPSession::onLineReceived(std::string line) { + coreutils::Log(coreutils::LOG_DEBUG_3) << "[" << line << "]"; + protocol(line); send(); } diff --git a/TCPSession.h b/TCPSession.h index 10fd19a..782f611 100644 --- a/TCPSession.h +++ b/TCPSession.h @@ -61,9 +61,29 @@ namespace core { TCPServer &server; protected: - - virtual void onDataReceived(std::string data) override; + + /// + /// + /// + virtual void onRegistered() override; + + /// + /// Override this method to receive data directly from the socket as data is + /// received. If you need data split by line termination characters then + /// override the onLineReceived method instead. + /// + + virtual void onDataReceived(char *data, int len) override; + + /// + /// Override the onLineReceived method to receive a string of characters that + /// represents a single line of data terminated by a LF or CRLF. 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 onLineReceived(std::string line); /// /// This method is called from within the protocol method when protocol is called @@ -83,7 +103,8 @@ namespace core { virtual void protocol(std::string data); private: - + char *lineBuffer = NULL; + int lineBufferSize = 0; std::mutex mtx; }; diff --git a/TLSSession.cpp b/TLSSession.cpp index 503a24a..56ee2e3 100644 --- a/TLSSession.cpp +++ b/TLSSession.cpp @@ -50,8 +50,8 @@ namespace core { 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."); +// if(!SSL_set_generate_session_id(ssl, generate_session_id)) +// throw std::string("Error setting session identifier callback."); } @@ -91,7 +91,7 @@ namespace core { if((len = ::SSL_read(ssl, buffer, bufferLength)) >= 0) { std::cout << "receiveData TLS...len=" << len << ":" << buffer << std::endl; - onDataReceived(std::string(buffer, len)); + onDataReceived(buffer, len); } else { switch (SSL_get_error(ssl, len)) { diff --git a/html/CommandList_8h_source.html b/html/CommandList_8h_source.html index 168899d..6ed2e8b 100644 --- a/html/CommandList_8h_source.html +++ b/html/CommandList_8h_source.html @@ -108,10 +108,10 @@ $(function() {
74 
75 #endif
-
core::CommandList::grabInput
bool grabInput(TCPSession *session, Command &command)
Definition: CommandList.cpp:33
+
core::CommandList::grabInput
bool grabInput(TCPSession *session, Command &command)
Definition: CommandList.cpp:28
core::TCPSession
Definition: TCPSession.h:23
core::CommandList::commands
std::vector< Command * > commands
Definition: CommandList.h:69
-
core::CommandList::processCommand
int processCommand(std::string request, TCPSession *session, std::stringstream &data)
Definition: CommandList.cpp:42
+
core::CommandList::processCommand
int processCommand(std::string request, TCPSession *session, std::stringstream &data)
Definition: CommandList.cpp:37
core::CommandList::add
void add(Command &command, std::string name="")
Definition: CommandList.cpp:6
core::CommandList::remove
void remove(Command &command)
Definition: CommandList.cpp:11
core::Command
Definition: Command.h:20
diff --git a/html/Socket_8h_source.html b/html/Socket_8h_source.html index d236f37..b302722 100644 --- a/html/Socket_8h_source.html +++ b/html/Socket_8h_source.html @@ -181,13 +181,13 @@ $(function() {
196 
core::Socket
Definition: Socket.h:33
-
core::Socket::write
int write(std::string data)
Definition: Socket.cpp:156
+
core::Socket::write
int write(std::string data)
Definition: Socket.cpp:155
core::EPoll
Definition: EPoll.h:31
core::Socket::onRegistered
virtual void onRegistered()
Called after the socket has been registered with epoll processing.
Definition: Socket.cpp:55
core::Socket::onDataReceived
virtual void onDataReceived(std::string data)
Called when data is received from the socket.
Definition: Socket.cpp:99
core::Socket::Socket
Socket(EPoll &ePoll, std::string text="")
Definition: Socket.cpp:8
core::Socket::eventReceived
bool eventReceived(struct epoll_event event, pid_t threadId)
Parse epoll event and call specified callbacks.
Definition: Socket.cpp:61
-
core::Socket::shutdown
void shutdown(std::string text="unknown")
Definition: Socket.cpp:177
+
core::Socket::shutdown
void shutdown(std::string text="unknown")
Definition: Socket.cpp:174
core::Socket::onRegister
virtual void onRegister()
Called before the socket has registered with the epoll processing.
Definition: Socket.cpp:53
core::Socket::receiveData
virtual void receiveData(char *buffer, int bufferLength)
Definition: Socket.cpp:107
core::Socket::onUnregistered
virtual void onUnregistered()
Called when the socket has finished unregistering for the epoll processing.
Definition: Socket.cpp:59
diff --git a/html/TCPSession_8h_source.html b/html/TCPSession_8h_source.html index d12dee1..39cc1b7 100644 --- a/html/TCPSession_8h_source.html +++ b/html/TCPSession_8h_source.html @@ -103,37 +103,45 @@ $(function() {
61  TCPServer &server;
62 
63  protected:
-
64 
-
65  virtual void onDataReceived(std::string data) override;
-
66  virtual void onRegistered() override;
-
67 
-
73 
-
74  virtual void onConnected();
-
75 
-
82 
-
83  virtual void protocol(std::string data);
-
84 
-
85  private:
-
86 
-
87  std::mutex mtx;
-
88 
-
89  };
-
90 
-
91 }
-
92 
-
93 #endif
+
64 
+
68 
+
69  virtual void onRegistered() override;
+
70 
+
76 
+
77  virtual void onDataReceived(char *data, int len) override;
+
78 
+
85 
+
86  virtual void onLineReceived(std::string line);
+
87 
+
93 
+
94  virtual void onConnected();
+
95 
+
102 
+
103  virtual void protocol(std::string data);
+
104 
+
105  private:
+
106  char *lineBuffer = NULL;
+
107  int lineBufferSize = 0;
+
108  std::mutex mtx;
+
109 
+
110  };
+
111 
+
112 }
+
113 
+
114 #endif
-
core::TCPSession::send
void send()
Definition: TCPSession.cpp:53
+
core::TCPSession::send
void send()
Definition: TCPSession.cpp:78
core::SessionFilter
Definition: SessionFilter.h:10
core::TCPSession
Definition: TCPSession.h:23
core::TCPSession::protocol
virtual void protocol(std::string data)
Definition: TCPSession.cpp:18
+
core::TCPSession::onDataReceived
virtual void onDataReceived(char *data, int len) override
Definition: TCPSession.cpp:33
core::EPoll
Definition: EPoll.h:31
-
core::TCPSession::sendToAll
void sendToAll()
Definition: TCPSession.cpp:38
+
core::TCPSession::sendToAll
void sendToAll()
Definition: TCPSession.cpp:63
+
core::TCPSession::onLineReceived
virtual void onLineReceived(std::string line)
Definition: TCPSession.cpp:57
core::TCPSession::out
std::stringstream out
Definition: TCPSession.h:37
core::TCPSession::onRegistered
virtual void onRegistered() override
Called after the socket has been registered with epoll processing.
Definition: TCPSession.cpp:25
core::Command
Definition: Command.h:20
core::TCPSocket
Definition: TCPSocket.h:20
-
core::TCPSession::onDataReceived
virtual void onDataReceived(std::string data) override
Called when data is received from the socket.
Definition: TCPSession.cpp:33
core::TCPSession::output
virtual void output(std::stringstream &data)
Definition: TCPSession.cpp:14
core::TCPSession::onConnected
virtual void onConnected()
Definition: TCPSession.cpp:31
core::TCPServer
Definition: TCPServer.h:24
diff --git a/html/classcore_1_1ConsoleSession-members.html b/html/classcore_1_1ConsoleSession-members.html index e1f69ff..1bb0ce6 100644 --- a/html/classcore_1_1ConsoleSession-members.html +++ b/html/classcore_1_1ConsoleSession-members.html @@ -88,45 +88,46 @@ $(function() { needsToWrite() (defined in core::Socket)core::Socket NextLine(int lines) (defined in core::TerminalSession)core::TerminalSession onConnected()core::TCPSessionprotectedvirtual - onDataReceived(std::string data) overridecore::TCPSessionprotectedvirtual - onDataReceived(char *buffer, int len) (defined in core::Socket)core::Socketprotectedvirtual - onRegister()core::Socketvirtual - onRegistered() overridecore::TCPSessionprotectedvirtual - onUnregister() (defined in core::Socket)core::Socketvirtual - onUnregistered()core::Socketvirtual - outcore::TCPSession - output(std::stringstream &data)core::TCPSessionvirtual - PreviousLine(int lines) (defined in core::TerminalSession)core::TerminalSession - protocol(std::string data) overridecore::ConsoleSessionprotectedvirtual - receiveData(char *buffer, int bufferLength)core::Socketprotectedvirtual - restoreCursor() (defined in core::TerminalSession)core::TerminalSession - saveCursor() (defined in core::TerminalSession)core::TerminalSession - scrollArea(int start, int end) (defined in core::TerminalSession)core::TerminalSession - send()core::TCPSession - sendToAll()core::TCPSession - sendToAll(SessionFilter filter)core::TCPSession - server (defined in core::TCPSession)core::TCPSession - setBackColor(int color) (defined in core::TerminalSession)core::TerminalSession - setBufferSize(int length) (defined in core::Socket)core::Socketprotected - setColor(int color) (defined in core::TerminalSession)core::TerminalSession - setCursorLocation(int x, int y)core::TerminalSession - setDescriptor(int descriptor)core::Socket - shutDown (defined in core::Socket)core::Socketprotected + onDataReceived(char *data, int len) overridecore::TCPSessionprotectedvirtual + core::TCPSocket::onDataReceived(std::string data)core::Socketprotectedvirtual + onLineReceived(std::string line)core::TCPSessionprotectedvirtual + onRegister()core::Socketvirtual + onRegistered() overridecore::TCPSessionprotectedvirtual + onUnregister() (defined in core::Socket)core::Socketvirtual + onUnregistered()core::Socketvirtual + outcore::TCPSession + output(std::stringstream &data)core::TCPSessionvirtual + PreviousLine(int lines) (defined in core::TerminalSession)core::TerminalSession + protocol(std::string data) overridecore::ConsoleSessionprotectedvirtual + receiveData(char *buffer, int bufferLength)core::Socketprotectedvirtual + restoreCursor() (defined in core::TerminalSession)core::TerminalSession + saveCursor() (defined in core::TerminalSession)core::TerminalSession + scrollArea(int start, int end) (defined in core::TerminalSession)core::TerminalSession + send()core::TCPSession + sendToAll()core::TCPSession + sendToAll(SessionFilter filter)core::TCPSession + server (defined in core::TCPSession)core::TCPSession + setBackColor(int color) (defined in core::TerminalSession)core::TerminalSession + setBufferSize(int length) (defined in core::Socket)core::Socketprotected + setColor(int color) (defined in core::TerminalSession)core::TerminalSession + setCursorLocation(int x, int y)core::TerminalSession + setDescriptor(int descriptor)core::Socket shutdown(std::string text="unknown")core::Socket - Socket(EPoll &ePoll, std::string text="")core::Socket - tag (defined in core::Object)core::Object - TCPSession(EPoll &ePoll, TCPServer &server, std::string text="") (defined in core::TCPSession)core::TCPSession - TCPSocket(EPoll &ePoll) (defined in core::TCPSocket)core::TCPSocket - TCPSocket(EPoll &ePoll, std::string text) (defined in core::TCPSocket)core::TCPSocket - TerminalSession(EPoll &ePoll, TCPServer &server) (defined in core::TerminalSession)core::TerminalSession - write(std::string data)core::Socket - write(char *buffer, int length) (defined in core::Socket)core::Socket - writeLog(std::string data) (defined in core::ConsoleSession)core::ConsoleSession - ~ConsoleSession() (defined in core::ConsoleSession)core::ConsoleSession - ~Socket()core::Socket - ~TCPSession() (defined in core::TCPSession)core::TCPSession - ~TCPSocket() (defined in core::TCPSocket)core::TCPSocket - ~TerminalSession() (defined in core::TerminalSession)core::TerminalSession + shutDown (defined in core::Socket)core::Socketprotected + Socket(EPoll &ePoll, std::string text="")core::Socket + tag (defined in core::Object)core::Object + TCPSession(EPoll &ePoll, TCPServer &server, std::string text="") (defined in core::TCPSession)core::TCPSession + TCPSocket(EPoll &ePoll) (defined in core::TCPSocket)core::TCPSocket + TCPSocket(EPoll &ePoll, std::string text) (defined in core::TCPSocket)core::TCPSocket + TerminalSession(EPoll &ePoll, TCPServer &server) (defined in core::TerminalSession)core::TerminalSession + write(std::string data)core::Socket + write(char *buffer, int length) (defined in core::Socket)core::Socket + writeLog(std::string data) (defined in core::ConsoleSession)core::ConsoleSession + ~ConsoleSession() (defined in core::ConsoleSession)core::ConsoleSession + ~Socket()core::Socket + ~TCPSession() (defined in core::TCPSession)core::TCPSession + ~TCPSocket() (defined in core::TCPSocket)core::TCPSocket + ~TerminalSession() (defined in core::TerminalSession)core::TerminalSession