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() {
-shutDown = false<
- Reimplemented in core::TCPServer, core::TCPSession, and core::UDPServerSocket. +Reimplemented in core::TCPServer, and core::UDPServerSocket. diff --git a/html/classcore_1_1TCPSession-members.html b/html/classcore_1_1TCPSession-members.html index 6fab674..4243b4a 100644 --- a/html/classcore_1_1TCPSession-members.html +++ b/html/classcore_1_1TCPSession-members.html @@ -83,34 +83,35 @@ $(function() { | ||
name (defined in core::Object) | core::Object | |
needsToWrite() (defined in core::Socket) | core::Socket | |
onConnected() | core::TCPSession | protectedvirtual |
onDataReceived(std::string data) override | core::TCPSession | protectedvirtual |
onDataReceived(char *buffer, int len) (defined in core::Socket) | core::Socket | protectedvirtual |
onRegister() | core::Socket | virtual |
onRegistered() override | core::TCPSession | protectedvirtual |
onUnregister() (defined in core::Socket) | core::Socket | virtual |
onUnregistered() | core::Socket | virtual |
out | core::TCPSession | |
output(std::stringstream &data) | core::TCPSession | virtual |
protocol(std::string data) | core::TCPSession | protectedvirtual |
receiveData(char *buffer, int bufferLength) | core::Socket | protectedvirtual |
send() | core::TCPSession | |
sendToAll() | core::TCPSession | |
sendToAll(SessionFilter filter) | core::TCPSession | |
server (defined in core::TCPSession) | core::TCPSession | |
setBufferSize(int length) (defined in core::Socket) | core::Socket | protected |
setDescriptor(int descriptor) | core::Socket | |
shutDown (defined in core::Socket) | core::Socket | protected |
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 | |
write(std::string data) | core::Socket | |
write(char *buffer, int length) (defined in core::Socket) | core::Socket | |
~Socket() | core::Socket | |
~TCPSession() (defined in core::TCPSession) | core::TCPSession | |
~TCPSocket() (defined in core::TCPSocket) | core::TCPSocket | |
onDataReceived(char *data, int len) override | core::TCPSession | protectedvirtual |
core::TCPSocket::onDataReceived(std::string data) | core::Socket | protectedvirtual |
onLineReceived(std::string line) | core::TCPSession | protectedvirtual |
onRegister() | core::Socket | virtual |
onRegistered() override | core::TCPSession | protectedvirtual |
onUnregister() (defined in core::Socket) | core::Socket | virtual |
onUnregistered() | core::Socket | virtual |
out | core::TCPSession | |
output(std::stringstream &data) | core::TCPSession | virtual |
protocol(std::string data) | core::TCPSession | protectedvirtual |
receiveData(char *buffer, int bufferLength) | core::Socket | protectedvirtual |
send() | core::TCPSession | |
sendToAll() | core::TCPSession | |
sendToAll(SessionFilter filter) | core::TCPSession | |
server (defined in core::TCPSession) | core::TCPSession | |
setBufferSize(int length) (defined in core::Socket) | core::Socket | protected |
setDescriptor(int descriptor) | core::Socket | |
shutDown (defined in core::Socket) | core::Socket | protected |
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 | |
write(std::string data) | core::Socket | |
write(char *buffer, int length) (defined in core::Socket) | core::Socket | |
~Socket() | core::Socket | |
~TCPSession() (defined in core::TCPSession) | core::TCPSession | |
~TCPSocket() (defined in core::TCPSocket) | core::TCPSocket |
Protected Member Functions | |
virtual void | onDataReceived (std::string data) override |
Called when data is received from the socket. More... | |
virtual void | onRegistered () override |
Called after the socket has been registered with epoll processing. | |
virtual void | onDataReceived (char *data, int len) override |
virtual void | onLineReceived (std::string line) |
virtual void | onConnected () |
virtual void | protocol (std::string data) | setBufferSize (in |
int | getBufferSize () |
-virtual void | onDataReceived (char *buffer, int len) |
virtual void | onDataReceived (std::string data) |
Called when data is received from the socket. More... | |
virtual void | receiveData (char *buffer, int bufferLength) |
shutDown = false<
-
-◆ onDataReceived()+ +◆ onDataReceived()
@@ -275,9 +276,19 @@ bool | shutDown = false< | |||||
void core::TCPSession::onDataReceived | ( | -std::string | -data | ) | +char * | +data, | +
+ | int | +len | +||||
+ | ) | +
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.
-Called when data is received from the socket.
-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 remote device. 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
-data | the data that has been received from the socket. |
Reimplemented from core::Socket.
-Reimplemented from core::Socket.
+
+
|
+ +protectedvirtual | +
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.
Additional Inherited Members | ||
needsToWrite() (defined in core::Socket) | core::Socket | |
NextLine(int lines) (defined in core::TerminalSession) | core::TerminalSession | |
onConnected() | core::TCPSession | protectedvirtual |
onDataReceived(std::string data) override | core::TCPSession | protectedvirtual |
onDataReceived(char *buffer, int len) (defined in core::Socket) | core::Socket | protectedvirtual |
onRegister() | core::Socket | virtual |
onRegistered() override | core::TCPSession | protectedvirtual |
onUnregister() (defined in core::Socket) | core::Socket | virtual |
onUnregistered() | core::Socket | virtual |
out | core::TCPSession | |
output(std::stringstream &data) | core::TCPSession | virtual |
PreviousLine(int lines) (defined in core::TerminalSession) | core::TerminalSession | |
protocol(std::string data) | core::TCPSession | protectedvirtual |
receiveData(char *buffer, int bufferLength) | core::Socket | protectedvirtual |
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::Socket | protected |
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::Socket | protected |
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 | |
~Socket() | core::Socket | |
~TCPSession() (defined in core::TCPSession) | core::TCPSession | |
~TCPSocket() (defined in core::TCPSocket) | core::TCPSocket | |
~TerminalSession() (defined in core::TerminalSession) | core::TerminalSession | |
onDataReceived(char *data, int len) override | core::TCPSession | protectedvirtual |
core::TCPSocket::onDataReceived(std::string data) | core::Socket | protectedvirtual |
onLineReceived(std::string line) | core::TCPSession | protectedvirtual |
onRegister() | core::Socket | virtual |
onRegistered() override | core::TCPSession | protectedvirtual |
onUnregister() (defined in core::Socket) | core::Socket | virtual |
onUnregistered() | core::Socket | virtual |
out | core::TCPSession | |
output(std::stringstream &data) | core::TCPSession | virtual |
PreviousLine(int lines) (defined in core::TerminalSession) | core::TerminalSession | |
protocol(std::string data) | core::TCPSession | protectedvirtual |
receiveData(char *buffer, int bufferLength) | core::Socket | protectedvirtual |
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::Socket | protected |
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::Socket | protected |
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 | |
~Socket() | core::Socket | |
~TCPSession() (defined in core::TCPSession) | core::TCPSession | |
~TCPSocket() (defined in core::TCPSocket) | core::TCPSocket | |
~TerminalSession() (defined in core::TerminalSession) | core::TerminalSession |