From 28292e924a5524afe5e4d03f10597b4843915323 Mon Sep 17 00:00:00 2001 From: Brad Arant Date: Thu, 23 Apr 2020 14:25:41 -0700 Subject: [PATCH] Continued improvements to shutdown. --- CommandList.cpp | 23 +++--- Socket.cpp | 27 +++---- TCPSession.cpp | 29 ++++++- TCPSession.h | 27 ++++++- TLSSession.cpp | 6 +- html/CommandList_8h_source.html | 4 +- html/Socket_8h_source.html | 4 +- html/TCPSession_8h_source.html | 52 +++++++------ html/classcore_1_1ConsoleSession-members.html | 77 ++++++++++--------- html/classcore_1_1ConsoleSession.html | 13 ++-- html/classcore_1_1Socket.html | 2 +- html/classcore_1_1TCPSession-members.html | 57 +++++++------- html/classcore_1_1TCPSession.html | 68 +++++++++++----- html/classcore_1_1TLSSession-members.html | 61 +++++++-------- html/classcore_1_1TLSSession.html | 13 ++-- .../classcore_1_1TerminalSession-members.html | 75 +++++++++--------- html/classcore_1_1TerminalSession.html | 13 ++-- html/functions.html | 5 +- html/functions_func.html | 5 +- html/search/all_7.js | 15 ++-- html/search/all_8.js | 6 +- html/search/all_9.js | 6 +- html/search/all_a.js | 24 +++--- html/search/all_b.js | 16 ++-- html/search/all_c.js | 6 +- html/search/all_d.js | 4 +- html/search/all_e.js | 8 +- html/search/classes_0.js | 8 +- html/search/classes_1.js | 2 +- html/search/classes_2.js | 6 +- html/search/classes_3.js | 2 +- html/search/classes_4.js | 4 +- html/search/classes_5.js | 16 ++-- html/search/classes_6.js | 4 +- html/search/functions_0.js | 2 +- html/search/functions_1.js | 8 +- html/search/functions_2.js | 4 +- html/search/functions_3.js | 14 ++-- html/search/functions_4.js | 2 +- html/search/functions_5.js | 15 ++-- html/search/functions_6.js | 6 +- html/search/functions_7.js | 6 +- html/search/functions_8.js | 20 ++--- html/search/functions_9.js | 4 +- html/search/functions_a.js | 2 +- html/search/functions_b.js | 2 +- html/search/functions_c.js | 8 +- html/search/variables_0.js | 2 +- html/search/variables_1.js | 2 +- html/search/variables_2.js | 2 +- html/search/variables_3.js | 2 +- html/search/variables_4.js | 2 +- html/search/variables_5.js | 2 +- latex/classcore_1_1Socket.tex | 2 +- latex/classcore_1_1TCPSession.tex | 31 ++++---- 55 files changed, 459 insertions(+), 367 deletions(-) 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
-
bool grabInput(TCPSession *session, Command &command)
Definition: CommandList.cpp:33
+
bool grabInput(TCPSession *session, Command &command)
Definition: CommandList.cpp:28
Definition: TCPSession.h:23
std::vector< Command * > commands
Definition: CommandList.h:69
-
int processCommand(std::string request, TCPSession *session, std::stringstream &data)
Definition: CommandList.cpp:42
+
int processCommand(std::string request, TCPSession *session, std::stringstream &data)
Definition: CommandList.cpp:37
void add(Command &command, std::string name="")
Definition: CommandList.cpp:6
void remove(Command &command)
Definition: CommandList.cpp:11
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 
Definition: Socket.h:33
-
int write(std::string data)
Definition: Socket.cpp:156
+
int write(std::string data)
Definition: Socket.cpp:155
Definition: EPoll.h:31
virtual void onRegistered()
Called after the socket has been registered with epoll processing.
Definition: Socket.cpp:55
virtual void onDataReceived(std::string data)
Called when data is received from the socket.
Definition: Socket.cpp:99
Socket(EPoll &ePoll, std::string text="")
Definition: Socket.cpp:8
bool eventReceived(struct epoll_event event, pid_t threadId)
Parse epoll event and call specified callbacks.
Definition: Socket.cpp:61
-
void shutdown(std::string text="unknown")
Definition: Socket.cpp:177
+
void shutdown(std::string text="unknown")
Definition: Socket.cpp:174
virtual void onRegister()
Called before the socket has registered with the epoll processing.
Definition: Socket.cpp:53
virtual void receiveData(char *buffer, int bufferLength)
Definition: Socket.cpp:107
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
-
void send()
Definition: TCPSession.cpp:53
+
void send()
Definition: TCPSession.cpp:78
Definition: SessionFilter.h:10
Definition: TCPSession.h:23
virtual void protocol(std::string data)
Definition: TCPSession.cpp:18
+
virtual void onDataReceived(char *data, int len) override
Definition: TCPSession.cpp:33
Definition: EPoll.h:31
-
void sendToAll()
Definition: TCPSession.cpp:38
+
void sendToAll()
Definition: TCPSession.cpp:63
+
virtual void onLineReceived(std::string line)
Definition: TCPSession.cpp:57
std::stringstream out
Definition: TCPSession.h:37
virtual void onRegistered() override
Called after the socket has been registered with epoll processing.
Definition: TCPSession.cpp:25
Definition: Command.h:20
Definition: TCPSocket.h:20
-
virtual void onDataReceived(std::string data) override
Called when data is received from the socket.
Definition: TCPSession.cpp:33
virtual void output(std::stringstream &data)
Definition: TCPSession.cpp:14
virtual void onConnected()
Definition: TCPSession.cpp:31
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