From 275c7298eefcdda1f2c6151ad3993c2a4f68c40e Mon Sep 17 00:00:00 2001 From: Brad Arant Date: Fri, 20 Jun 2025 01:22:06 +0000 Subject: [PATCH] Adding LineParser layer. --- Socket.cpp | 25 +++++++-------------- Socket.h | 30 +++---------------------- TCPLineBufferParser.cpp | 29 ++++++++++++++++++++++++ TCPLineBufferParser.h | 43 ++++++++++++++++++++++++++++++++++++ TCPSession.cpp | 3 +-- TCPSession.h | 28 +++++------------------ TCPSocket.h | 6 ++--- src/main.cpp | 6 ----- src/workspace.code-workspace | 38 ------------------------------- 9 files changed, 92 insertions(+), 116 deletions(-) create mode 100644 TCPLineBufferParser.cpp create mode 100644 TCPLineBufferParser.h delete mode 100644 src/main.cpp delete mode 100644 src/workspace.code-workspace diff --git a/Socket.cpp b/Socket.cpp index f268233..0a1f79a 100644 --- a/Socket.cpp +++ b/Socket.cpp @@ -48,17 +48,6 @@ namespace core return descriptor; } - void Socket::setBufferSize(int length) - { - this->length = length; - buffer = (char *)realloc(buffer, length); - } - - int Socket::getBufferSize() - { - return length; - } - void Socket::onRegister() {} void Socket::onRegistered() {} @@ -70,8 +59,8 @@ namespace core bool Socket::eventReceived(struct epoll_event event, long long eventId) { // coreutils::Log(coreutils::LOG_DEBUG_1) << "Event process beginning for socket " << getDescriptor(); if(inHandler) - // coreutils::Log(coreutils::LOG_DEBUG_2) << "inHandler was already true."; - inHandler = true; +// coreutils::Log(coreutils::LOG_DEBUG_2) << "inHandler was already true."; + inHandler = true; if(event.events & EPOLLRDHUP) { // coreutils::Log(coreutils::LOG_DEBUG_2) << "EPOLLRDHUP"; readHangup = true; @@ -79,9 +68,8 @@ namespace core } if(event.events & EPOLLIN) { // coreutils::Log(coreutils::LOG_DEBUG_2) << "EPOLLIN"; - coreutils::ZString zbuffer(buffer, length); lock.lock(); - receiveData(zbuffer); + receiveDataNotification(); if(!shutDown) { inHandler = false; lock.unlock(); @@ -107,8 +95,11 @@ namespace core onDataReceived(std::string(data.getData(), data.getLength())); } - void Socket::receiveData(coreutils::ZString &buffer) { - coreutils::ZString blank(""); + void Socket::receiveDataNotification() {} + + + + coreutils::ZString blank(""); if(buffer.getLength() <= 0) throw coreutils::Exception("Request to receive data with a zero buffer length.", __FILE__, __LINE__, -1); int len; diff --git a/Socket.h b/Socket.h index fbd6395..e529433 100644 --- a/Socket.h +++ b/Socket.h @@ -124,10 +124,6 @@ namespace core { EPoll &ePoll; // The EPoll control object. - void setBufferSize(int length); - - int getBufferSize(); - /// /// 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 @@ -143,27 +139,10 @@ namespace core { // virtual void onDisconnected(); ///< Called when socket is closing and no longer ready to communicate. /// - /// 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 - /// - /// @param data the data that has been received from the socket. + /// This method is called when data is ready to be read from the socket. /// - virtual void onDataReceived(std::string data); ///< Called when data is received from the socket. - - /// - /// - /// - - virtual void onDataReceived(coreutils::ZString &data); - - /// - /// receiveData will read the data from the socket and place it in the socket buffer. - /// TLS layer overrides this to be able to read from SSL. - /// - - virtual void receiveData(coreutils::ZString &buffer); + virtual void readDataNotification(); private: @@ -171,7 +150,7 @@ namespace core { int descriptor = -1; std::mutex outlock; bool readHangup = false; - volatile bool inHandler = false; + volatile bool inHandler = false; //------------------------------------------------------------------------------------- // the writeSocket is called when epoll has received a write request for a socket. @@ -187,9 +166,6 @@ namespace core { // int_type pbackfail(int_type ch); // streamsize showmanyc(); - char *buffer; // This is a pointer to the managed buffer space. - int length; // This is the length of the buffer. - std::queue fifo; void resetSocket(); diff --git a/TCPLineBufferParser.cpp b/TCPLineBufferParser.cpp new file mode 100644 index 0000000..34906e8 --- /dev/null +++ b/TCPLineBufferParser.cpp @@ -0,0 +1,29 @@ +#include "TCPLineBufferParser.h" + +namespace core { + + TCPLineBufferParser::TCPLineBufferParser() { + buffer = nullptr; + bufferSize = 4096; + buffer = realloc(buffer, bufferSize); + cursor = buffer; + } + + void TCPLineBufferParser::readDataNotification() { + + len = ::read(getDescriptor(), buffer, bufferSize); + + for(unsigned int index = 0; index < bufferSize; ++index) { + if(cursor[index] == '\n') { + coreutils::ZString data(cursor, index); + dataReceived(data); + } + + } + + + } + + void TCPLineBufferParser::onDataReceived(coreutils::ZString data) {} + +} diff --git a/TCPLineBufferParser.h b/TCPLineBufferParser.h new file mode 100644 index 0000000..fb627c5 --- /dev/null +++ b/TCPLineBufferParser.h @@ -0,0 +1,43 @@ +#ifndef __TCPLineBufferParser_h__ +#define __TCPLineBufferParser_h__ + +#include "TCPSocket.h" + +namespace core { + + /// + /// TCPLineBufferParser + /// + /// This object will read lines of data from the source one line at a time. + /// + /// + + class TCPLineBufferParser : public TCPSocket { + + protected: + + /// + /// This method is called when a line is ready to be processed. + /// + + virtual void onDataReceived(); + + private: + + /// + /// Read the data for the socket and split into lines and call dataReceived + /// for each line parsed. + /// + + void readDataNotification() override; + + char *buffer; + size_t bufferSize; + char *cursor; + + }; + +} + +#endif + diff --git a/TCPSession.cpp b/TCPSession.cpp index 56f8c8c..bf4e4d2 100644 --- a/TCPSession.cpp +++ b/TCPSession.cpp @@ -4,8 +4,7 @@ #include "TCPServer.h" #include "uuid/uuid.h" -namespace core -{ +namespace core { TCPSession::TCPSession(EPoll &ePoll, TCPServer &server, std::string text) : TCPSocket(ePoll, text), server(server) { uuid_t uuid; diff --git a/TCPSession.h b/TCPSession.h index 7f419b0..9fb397e 100644 --- a/TCPSession.h +++ b/TCPSession.h @@ -1,11 +1,12 @@ #ifndef __Session_h__ #define __Session_h__ +#include "TCPLineBufferParser.h" #include "MString.h" #include "SessionFilter.h" #include "TCPSocket.h" -namespace core -{ + +namespace core { class Command; class TCPServer; @@ -22,8 +23,7 @@ namespace core /// /// - class TCPSession : public TCPSocket - { + class TCPSession : public TCPLineBufferParser { public: /// @@ -103,7 +103,8 @@ namespace core /// received. If you need data split by line termination characters then /// override the onLineReceived method instead. /// - virtual void onDataReceived(coreutils::ZString &data) override; + + void onDataReceived(coreutils::ZString &data) override; /// /// Override the onLineReceived method to receive a string of characters that @@ -112,23 +113,6 @@ namespace core /// this method explicitly using the class and member name. /// - virtual void onLineReceived(coreutils::ZString &line); - - /// - /// Override the onBlockReceived method to receive a string of characters that - /// represents a single block of data of length determined by the block length value. 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 onBlockReceived(coreutils::ZString &block); - - /// - /// This method is called from within the protocol method when protocol is called - /// on the initial connection where the data is an empty string. Use this method - /// to deliver a message to the connection upon connection. - /// - virtual void onConnected(); /// diff --git a/TCPSocket.h b/TCPSocket.h index 856bb57..15ea1da 100644 --- a/TCPSocket.h +++ b/TCPSocket.h @@ -5,8 +5,7 @@ #include "Socket.h" #include "includes" -namespace core -{ +namespace core { /// /// TCPSocket @@ -18,8 +17,7 @@ namespace core /// synchronous data connection. /// - class TCPSocket : public Socket - { + class TCPSocket : public Socket { public: TCPSocket(EPoll &ePoll); diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index d8e796c..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include - -int main(int argc, char *argv[]) -{ - std::cout << "Hello world!" << std::endl; -} \ No newline at end of file diff --git a/src/workspace.code-workspace b/src/workspace.code-workspace deleted file mode 100644 index 0221949..0000000 --- a/src/workspace.code-workspace +++ /dev/null @@ -1,38 +0,0 @@ -{ - "folders": [ - { - "path": "../../CoreUtils" - }, - { - "path": ".." - }, - { - "path": "../../HTTPServer" - } - ], - "settings": {}, - "launch": { - "version": "0.2.0", - "configurations": [ - { - "name": "(gdb) Launch", - "type": "cppdbg", - "request": "launch", - "program": "/home/barant/Development/HTTPServer/HTTPServer", - "args": [], - "stopAtEntry": false, - "cwd": "/home/barant/Development/HTTPServer/", - "environment": [], - "externalConsole": false, - "MIMode": "gdb", - "setupCommands": [ - { - "description": "Enable pretty-printing for gdb", - "text": "-enable-pretty-printing", - "ignoreFailures": true - } - ] - } - ] - } -} \ No newline at end of file