Compare commits

..

No commits in common. "develop" and "master" have entirely different histories.

9 changed files with 116 additions and 92 deletions

View File

@ -48,6 +48,17 @@ namespace core
return descriptor; 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::onRegister() {}
void Socket::onRegistered() {} void Socket::onRegistered() {}
@ -59,8 +70,8 @@ namespace core
bool Socket::eventReceived(struct epoll_event event, long long eventId) { bool Socket::eventReceived(struct epoll_event event, long long eventId) {
// coreutils::Log(coreutils::LOG_DEBUG_1) << "Event process beginning for socket " << getDescriptor(); // coreutils::Log(coreutils::LOG_DEBUG_1) << "Event process beginning for socket " << getDescriptor();
if(inHandler) if(inHandler)
// coreutils::Log(coreutils::LOG_DEBUG_2) << "inHandler was already true."; // coreutils::Log(coreutils::LOG_DEBUG_2) << "inHandler was already true.";
inHandler = true; inHandler = true;
if(event.events & EPOLLRDHUP) { if(event.events & EPOLLRDHUP) {
// coreutils::Log(coreutils::LOG_DEBUG_2) << "EPOLLRDHUP"; // coreutils::Log(coreutils::LOG_DEBUG_2) << "EPOLLRDHUP";
readHangup = true; readHangup = true;
@ -68,8 +79,9 @@ namespace core
} }
if(event.events & EPOLLIN) { if(event.events & EPOLLIN) {
// coreutils::Log(coreutils::LOG_DEBUG_2) << "EPOLLIN"; // coreutils::Log(coreutils::LOG_DEBUG_2) << "EPOLLIN";
coreutils::ZString zbuffer(buffer, length);
lock.lock(); lock.lock();
receiveDataNotification(); receiveData(zbuffer);
if(!shutDown) { if(!shutDown) {
inHandler = false; inHandler = false;
lock.unlock(); lock.unlock();
@ -95,11 +107,8 @@ namespace core
onDataReceived(std::string(data.getData(), data.getLength())); onDataReceived(std::string(data.getData(), data.getLength()));
} }
void Socket::receiveDataNotification() {} void Socket::receiveData(coreutils::ZString &buffer) {
coreutils::ZString blank("");
coreutils::ZString blank("");
if(buffer.getLength() <= 0) if(buffer.getLength() <= 0)
throw coreutils::Exception("Request to receive data with a zero buffer length.", __FILE__, __LINE__, -1); throw coreutils::Exception("Request to receive data with a zero buffer length.", __FILE__, __LINE__, -1);
int len; int len;

View File

@ -124,6 +124,10 @@ namespace core {
EPoll &ePoll; // The EPoll control object. EPoll &ePoll; // The EPoll control object.
void setBufferSize(int length);
int getBufferSize();
/// ///
/// The onConnected method is called when the socket is ready to communicate. /// 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 /// Writing to the socket can begin on this call to initiate a contact with the
@ -139,10 +143,27 @@ namespace core {
// virtual void onDisconnected(); ///< Called when socket is closing and no longer ready to communicate. // virtual void onDisconnected(); ///< Called when socket is closing and no longer ready to communicate.
/// ///
/// This method is called when data is ready to be read from the socket. /// 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.
/// ///
virtual void readDataNotification(); 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);
private: private:
@ -150,7 +171,7 @@ namespace core {
int descriptor = -1; int descriptor = -1;
std::mutex outlock; std::mutex outlock;
bool readHangup = false; 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. // the writeSocket is called when epoll has received a write request for a socket.
@ -166,6 +187,9 @@ namespace core {
// int_type pbackfail(int_type ch); // int_type pbackfail(int_type ch);
// streamsize showmanyc(); // 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; std::queue<std::string> fifo;
void resetSocket(); void resetSocket();

View File

@ -1,29 +0,0 @@
#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) {}
}

View File

@ -1,43 +0,0 @@
#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,7 +4,8 @@
#include "TCPServer.h" #include "TCPServer.h"
#include "uuid/uuid.h" #include "uuid/uuid.h"
namespace core { namespace core
{
TCPSession::TCPSession(EPoll &ePoll, TCPServer &server, std::string text) : TCPSocket(ePoll, text), server(server) { TCPSession::TCPSession(EPoll &ePoll, TCPServer &server, std::string text) : TCPSocket(ePoll, text), server(server) {
uuid_t uuid; uuid_t uuid;

View File

@ -1,12 +1,11 @@
#ifndef __Session_h__ #ifndef __Session_h__
#define __Session_h__ #define __Session_h__
#include "TCPLineBufferParser.h"
#include "MString.h" #include "MString.h"
#include "SessionFilter.h" #include "SessionFilter.h"
#include "TCPSocket.h" #include "TCPSocket.h"
namespace core
namespace core { {
class Command; class Command;
class TCPServer; class TCPServer;
@ -23,7 +22,8 @@ namespace core {
/// ///
/// ///
class TCPSession : public TCPLineBufferParser { class TCPSession : public TCPSocket
{
public: public:
/// ///
@ -103,8 +103,7 @@ namespace core {
/// received. If you need data split by line termination characters then /// received. If you need data split by line termination characters then
/// override the onLineReceived method instead. /// 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 /// Override the onLineReceived method to receive a string of characters that
@ -113,6 +112,23 @@ namespace core {
/// this method explicitly using the class and member name. /// 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(); virtual void onConnected();
/// ///

View File

@ -5,7 +5,8 @@
#include "Socket.h" #include "Socket.h"
#include "includes" #include "includes"
namespace core { namespace core
{
/// ///
/// TCPSocket /// TCPSocket
@ -17,7 +18,8 @@ namespace core {
/// synchronous data connection. /// synchronous data connection.
/// ///
class TCPSocket : public Socket { class TCPSocket : public Socket
{
public: public:
TCPSocket(EPoll &ePoll); TCPSocket(EPoll &ePoll);

6
src/main.cpp Normal file
View File

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

View File

@ -0,0 +1,38 @@
{
"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
}
]
}
]
}
}