Compare commits

...

1 Commits

Author SHA1 Message Date
Brad Arant
275c7298ee Adding LineParser layer. 2025-06-20 01:22:06 +00:00
9 changed files with 92 additions and 116 deletions

View File

@ -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() {}
@ -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,7 +95,10 @@ namespace core
onDataReceived(std::string(data.getData(), data.getLength()));
}
void Socket::receiveData(coreutils::ZString &buffer) {
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);

View File

@ -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:
@ -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<std::string> fifo;
void resetSocket();

29
TCPLineBufferParser.cpp Normal file
View File

@ -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) {}
}

43
TCPLineBufferParser.h Normal file
View File

@ -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

View File

@ -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;

View File

@ -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();
///

View File

@ -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);

View File

@ -1,6 +0,0 @@
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "Hello world!" << std::endl;
}

View File

@ -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
}
]
}
]
}
}