Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
275c7298ee |
21
Socket.cpp
21
Socket.cpp
@ -48,17 +48,6 @@ 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() {}
|
||||||
@ -70,7 +59,7 @@ 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";
|
||||||
@ -79,9 +68,8 @@ 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();
|
||||||
receiveData(zbuffer);
|
receiveDataNotification();
|
||||||
if(!shutDown) {
|
if(!shutDown) {
|
||||||
inHandler = false;
|
inHandler = false;
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
@ -107,7 +95,10 @@ namespace core
|
|||||||
onDataReceived(std::string(data.getData(), data.getLength()));
|
onDataReceived(std::string(data.getData(), data.getLength()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::receiveData(coreutils::ZString &buffer) {
|
void Socket::receiveDataNotification() {}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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);
|
||||||
|
28
Socket.h
28
Socket.h
@ -124,10 +124,6 @@ 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
|
||||||
@ -143,27 +139,10 @@ 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.
|
||||||
|
|
||||||
///
|
///
|
||||||
/// The onDataReceived method is called when the socket has received an event from
|
/// This method is called when data is ready to be read from the socket.
|
||||||
/// 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 onDataReceived(std::string data); ///< Called when data is received from the socket.
|
virtual void readDataNotification();
|
||||||
|
|
||||||
///
|
|
||||||
///
|
|
||||||
///
|
|
||||||
|
|
||||||
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:
|
||||||
|
|
||||||
@ -187,9 +166,6 @@ 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();
|
||||||
|
29
TCPLineBufferParser.cpp
Normal file
29
TCPLineBufferParser.cpp
Normal 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
43
TCPLineBufferParser.h
Normal 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
|
||||||
|
|
@ -4,8 +4,7 @@
|
|||||||
#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;
|
||||||
|
28
TCPSession.h
28
TCPSession.h
@ -1,11 +1,12 @@
|
|||||||
#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;
|
||||||
@ -22,8 +23,7 @@ namespace core
|
|||||||
///
|
///
|
||||||
///
|
///
|
||||||
|
|
||||||
class TCPSession : public TCPSocket
|
class TCPSession : public TCPLineBufferParser {
|
||||||
{
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
///
|
///
|
||||||
@ -103,7 +103,8 @@ 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
|
||||||
@ -112,23 +113,6 @@ 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();
|
||||||
|
|
||||||
///
|
///
|
||||||
|
@ -5,8 +5,7 @@
|
|||||||
#include "Socket.h"
|
#include "Socket.h"
|
||||||
#include "includes"
|
#include "includes"
|
||||||
|
|
||||||
namespace core
|
namespace core {
|
||||||
{
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// TCPSocket
|
/// TCPSocket
|
||||||
@ -18,8 +17,7 @@ namespace core
|
|||||||
/// synchronous data connection.
|
/// synchronous data connection.
|
||||||
///
|
///
|
||||||
|
|
||||||
class TCPSocket : public Socket
|
class TCPSocket : public Socket {
|
||||||
{
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TCPSocket(EPoll &ePoll);
|
TCPSocket(EPoll &ePoll);
|
||||||
|
@ -1,6 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
std::cout << "Hello world!" << std::endl;
|
|
||||||
}
|
|
@ -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
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user