Made server a pointer for eliminating TCPSession2.
This commit is contained in:
parent
6c7fc7d28f
commit
a391882541
@ -15,7 +15,7 @@ namespace core {
|
||||
}
|
||||
|
||||
TCPSession * ConsoleServer::getSocketAccept(EPoll &ePoll) {
|
||||
return new ConsoleSession(ePoll, *this);
|
||||
return new ConsoleSession(ePoll, this);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
namespace core {
|
||||
|
||||
ConsoleSession::ConsoleSession(EPoll &ePoll, TCPServer &server) : TerminalSession(ePoll, server) {}
|
||||
ConsoleSession::ConsoleSession(EPoll &ePoll, TCPServer *server) : TerminalSession(ePoll, server) {}
|
||||
|
||||
ConsoleSession::~ConsoleSession() {}
|
||||
|
||||
@ -75,7 +75,7 @@ namespace core {
|
||||
}
|
||||
|
||||
void ConsoleSession::doCommand(coreutils::ZString &request) {
|
||||
server.commands.processRequest(request, *this);
|
||||
server->commands.processRequest(request, *this);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ namespace core {
|
||||
class ConsoleSession : public TerminalSession {
|
||||
|
||||
public:
|
||||
ConsoleSession(EPoll &ePoll, TCPServer &server);
|
||||
ConsoleSession(EPoll &ePoll, TCPServer *server);
|
||||
~ConsoleSession();
|
||||
|
||||
void writeLog(std::string data);
|
||||
|
91
Makefile
91
Makefile
@ -1,91 +0,0 @@
|
||||
#
|
||||
# 'make' build executable file 'main'
|
||||
# 'make clean' removes all .o and executable files
|
||||
#
|
||||
|
||||
# define the Cpp compiler to use
|
||||
CXX = g++
|
||||
|
||||
# define any compile-time flags
|
||||
CXXFLAGS := -std=c++17 -Wall -Wextra -g
|
||||
|
||||
# define library paths in addition to /usr/lib
|
||||
# if I wanted to include libraries not in /usr/lib I'd specify
|
||||
# their path using -Lpath, something like:
|
||||
LFLAGS =
|
||||
|
||||
# define output directory
|
||||
OUTPUT := output
|
||||
|
||||
# define source directory
|
||||
SRC := src
|
||||
|
||||
# define include directory
|
||||
INCLUDE := include
|
||||
|
||||
# define lib directory
|
||||
LIB := lib
|
||||
|
||||
ifeq ($(OS),Windows_NT)
|
||||
MAIN := main.exe
|
||||
SOURCEDIRS := $(SRC)
|
||||
INCLUDEDIRS := $(INCLUDE)
|
||||
LIBDIRS := $(LIB)
|
||||
FIXPATH = $(subst /,\,$1)
|
||||
RM := del /q /f
|
||||
MD := mkdir
|
||||
else
|
||||
MAIN := main
|
||||
SOURCEDIRS := $(shell find $(SRC) -type d)
|
||||
INCLUDEDIRS := $(shell find $(INCLUDE) -type d)
|
||||
LIBDIRS := $(shell find $(LIB) -type d)
|
||||
FIXPATH = $1
|
||||
RM = rm -f
|
||||
MD := mkdir -p
|
||||
endif
|
||||
|
||||
# define any directories containing header files other than /usr/include
|
||||
INCLUDES := $(patsubst %,-I%, $(INCLUDEDIRS:%/=%))
|
||||
|
||||
# define the C libs
|
||||
LIBS := $(patsubst %,-L%, $(LIBDIRS:%/=%))
|
||||
|
||||
# define the C source files
|
||||
SOURCES := $(wildcard $(patsubst %,%/*.cpp, $(SOURCEDIRS)))
|
||||
|
||||
# define the C object files
|
||||
OBJECTS := $(SOURCES:.cpp=.o)
|
||||
|
||||
#
|
||||
# The following part of the makefile is generic; it can be used to
|
||||
# build any executable just by changing the definitions above and by
|
||||
# deleting dependencies appended to the file from 'make depend'
|
||||
#
|
||||
|
||||
OUTPUTMAIN := $(call FIXPATH,$(OUTPUT)/$(MAIN))
|
||||
|
||||
all: $(OUTPUT) $(MAIN)
|
||||
@echo Executing 'all' complete!
|
||||
|
||||
$(OUTPUT):
|
||||
$(MD) $(OUTPUT)
|
||||
|
||||
$(MAIN): $(OBJECTS)
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDES) -o $(OUTPUTMAIN) $(OBJECTS) $(LFLAGS) $(LIBS)
|
||||
|
||||
# this is a suffix replacement rule for building .o's from .c's
|
||||
# it uses automatic variables $<: the name of the prerequisite of
|
||||
# the rule(a .c file) and $@: the name of the target of the rule (a .o file)
|
||||
# (see the gnu make manual section about automatic variables)
|
||||
.cpp.o:
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
$(RM) $(OUTPUTMAIN)
|
||||
$(RM) $(call FIXPATH,$(OBJECTS))
|
||||
@echo Cleanup complete!
|
||||
|
||||
run: all
|
||||
./$(OUTPUTMAIN)
|
||||
@echo Executing 'run: all' complete!
|
@ -67,7 +67,7 @@ namespace core {
|
||||
std::stringstream out;
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << request[2];
|
||||
std::string invitee = request[2].str();
|
||||
TCPSession *tempSession = session.server.getSessionByAlias(&invitee);
|
||||
TCPSession *tempSession = session.server->getSessionByAlias(&invitee);
|
||||
std::stringstream temp;
|
||||
temp << "invite:" << request[1] << ":" << *(std::string *)session.alias;
|
||||
tempSession->write(temp.str());
|
||||
|
@ -9,13 +9,13 @@ namespace core {
|
||||
TCPServer::TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter, int depth, std::string text)
|
||||
: TCPSocket(ePoll, text), commands(delimiter, depth) {
|
||||
|
||||
commands.add(subscriptions, "publish");
|
||||
commands.add(subscriptions, "unpublish");
|
||||
commands.add(subscriptions, "subscribe");
|
||||
commands.add(subscriptions, "unsubscribe");
|
||||
commands.add(subscriptions, "catalog");
|
||||
commands.add(subscriptions, "event");
|
||||
commands.add(subscriptions, "invite");
|
||||
// commands.add(subscriptions, "publish"); // This needs to be removed and put into the servers that
|
||||
// commands.add(subscriptions, "unpublish"); // Others make. Plus it should be in a manager object that
|
||||
// commands.add(subscriptions, "subscribe"); // encapsulates all of the function.
|
||||
// commands.add(subscriptions, "unsubscribe");
|
||||
// commands.add(subscriptions, "catalog");
|
||||
// commands.add(subscriptions, "event");
|
||||
// commands.add(subscriptions, "invite");
|
||||
|
||||
setDescriptor(socket(AF_INET, SOCK_STREAM, 0));
|
||||
int yes = 1;
|
||||
@ -84,7 +84,7 @@ namespace core {
|
||||
}
|
||||
|
||||
TCPSession * TCPServer::getSocketAccept(EPoll &ePoll) {
|
||||
return new TCPSession(ePoll, *this);
|
||||
return new TCPSession(ePoll, this);
|
||||
}
|
||||
|
||||
void TCPServer::output(std::stringstream &out) {
|
||||
|
@ -9,8 +9,7 @@
|
||||
# include "TCPSession.h"
|
||||
# include "TCPSocket.h"
|
||||
|
||||
namespace core
|
||||
{
|
||||
namespace core {
|
||||
|
||||
///
|
||||
/// TCPServer
|
||||
@ -27,6 +26,7 @@ namespace core
|
||||
class TCPServer : public TCPSocket, public Command {
|
||||
|
||||
public:
|
||||
|
||||
///
|
||||
/// The constructor for the TCPServer object.
|
||||
///
|
||||
|
@ -4,10 +4,16 @@
|
||||
#include "TCPServer.h"
|
||||
#include "uuid/uuid.h"
|
||||
|
||||
namespace core
|
||||
{
|
||||
namespace core {
|
||||
|
||||
TCPSession::TCPSession(EPoll &ePoll, TCPServer &server, std::string text) : TCPSocket(ePoll, server.ctx, text), server(server) {
|
||||
TCPSession::TCPSession(EPoll &ePoll, std::string text) : TCPSocket(ePoll, text) {
|
||||
uuid_t uuidObject;
|
||||
uuid_generate(uuidObject);
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << uuidObject;
|
||||
alias = (void *)uuidObject;
|
||||
}
|
||||
|
||||
TCPSession::TCPSession(EPoll &ePoll, TCPServer *server, std::string text) : TCPSocket(ePoll, server->ctx, text), server(server) {
|
||||
uuid_t uuidObject;
|
||||
uuid_generate(uuidObject);
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << uuidObject;
|
||||
@ -15,8 +21,8 @@ namespace core
|
||||
}
|
||||
|
||||
TCPSession::~TCPSession() {
|
||||
server.removeFromSessionList(this);
|
||||
server.subscriptions.removeSessionSubscriptions(*this);
|
||||
server->removeFromSessionList(this);
|
||||
server->subscriptions.removeSessionSubscriptions(*this);
|
||||
}
|
||||
|
||||
void TCPSession::output(std::stringstream &data) {
|
||||
@ -25,7 +31,7 @@ namespace core
|
||||
|
||||
void TCPSession::protocol(coreutils::ZString &data) {
|
||||
if (data.getLength() != 0) {
|
||||
if (!server.commands.processRequest(data, *this)) {
|
||||
if (!server->commands.processRequest(data, *this)) {
|
||||
coreutils::Log(coreutils::LOG_DEBUG_1) << "Received data could not be parsed: " << data.str();
|
||||
}
|
||||
}
|
||||
|
14
TCPSession.h
14
TCPSession.h
@ -25,11 +25,18 @@ namespace core {
|
||||
class TCPSession : public TCPSocket {
|
||||
|
||||
public:
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
TCPSession(EPoll &ePoll, TCPServer &server, std::string text = "");
|
||||
TCPSession(EPoll &ePoll, std::string text = "");
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
TCPSession(EPoll &ePoll, TCPServer *server, std::string text = "");
|
||||
|
||||
///
|
||||
///
|
||||
@ -55,10 +62,11 @@ namespace core {
|
||||
void terminate();
|
||||
|
||||
///
|
||||
///
|
||||
/// This is a pointer to the server that was used to 'spawn' the server connection.
|
||||
/// If the session is just client side connection this pointer should be NULL;
|
||||
///
|
||||
|
||||
TCPServer &server;
|
||||
TCPServer *server;
|
||||
|
||||
///
|
||||
/// Use out to send data to the session socket or other session sockets.
|
||||
|
@ -23,55 +23,11 @@ namespace core {
|
||||
|
||||
void TCPSession2::onConnected() {}
|
||||
|
||||
void TCPSession2::onDataReceived(coreutils::ZString &data) {
|
||||
if(data.getLength() > 0) {
|
||||
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize + data.getLength());
|
||||
memcpy(lineBuffer + lineBufferSize, data.getData(), data.getLength());
|
||||
lineBufferSize += data.getLength();
|
||||
while(lineBufferSize > 0) {
|
||||
if(blockSize == 0) {
|
||||
lineLength = strcspn(lineBuffer, "\r\n");
|
||||
if(lineLength == lineBufferSize)
|
||||
break;
|
||||
coreutils::ZString zLine(lineBuffer, lineLength);
|
||||
onLineReceived(zLine);
|
||||
if(lineBuffer[lineLength] == '\r')
|
||||
++lineLength;
|
||||
if(lineBuffer[lineLength] == '\n')
|
||||
++lineLength;
|
||||
lineBufferSize -= lineLength;
|
||||
if(lineBufferSize > 0)
|
||||
memmove(lineBuffer, lineBuffer + lineLength, lineBufferSize);
|
||||
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize);
|
||||
} else if(lineBufferSize >= blockLength) {
|
||||
coreutils::ZString zBlock(lineBuffer, blockLength);
|
||||
onBlockReceived(zBlock);
|
||||
lineBufferSize -= blockLength;
|
||||
if(lineBufferSize > 0)
|
||||
memmove(lineBuffer, lineBuffer + blockLength, lineBufferSize);
|
||||
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TCPSession2::setBlockSize(int blockSize) {
|
||||
this->blockSize = blockSize;
|
||||
}
|
||||
|
||||
void TCPSession2::onLineReceived(coreutils::ZString &line) {
|
||||
protocol(line);
|
||||
send();
|
||||
if(term)
|
||||
TCPSocket::shutdown("termination requested");
|
||||
}
|
||||
|
||||
void TCPSession2::onBlockReceived(coreutils::ZString &block) {
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "[" << block.getLength() << "]";
|
||||
if(term)
|
||||
TCPSocket::shutdown("termination requested");
|
||||
}
|
||||
|
||||
void TCPSession2::send() {
|
||||
if(out.tellp() > 0)
|
||||
TCPSocket::write(out.str());
|
||||
|
@ -70,31 +70,6 @@ namespace core {
|
||||
|
||||
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(coreutils::ZString &data) 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(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
|
||||
|
25
TCPSocket.h
25
TCPSocket.h
@ -22,11 +22,34 @@ namespace core {
|
||||
|
||||
public:
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
TCPSocket(EPoll &ePoll);
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
TCPSocket(EPoll &ePoll, std::string text);
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
TCPSocket(EPoll &ePoll, SSL_CTX *ctx, std::string text);
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
virtual ~TCPSocket();
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
void connect(IPAddress &address);
|
||||
|
||||
IPAddress ipAddress;
|
||||
@ -34,7 +57,7 @@ namespace core {
|
||||
///
|
||||
/// The output method is called by a socket session (TCPSession) and
|
||||
/// will output the detail information for the client socket. When extending
|
||||
/// BMATCPSocket or BMASession you can override the method to add attributes
|
||||
/// TCPSocket or TCPSession you can override the method to add attributes
|
||||
/// to the list.
|
||||
///
|
||||
|
||||
|
@ -3,11 +3,9 @@
|
||||
|
||||
namespace core {
|
||||
|
||||
TerminalSession::TerminalSession(EPoll &ePoll, TCPServer &server) : TCPSession(ePoll, server) {
|
||||
}
|
||||
TerminalSession::TerminalSession(EPoll &ePoll, TCPServer *server) : TCPSession(ePoll, server) {}
|
||||
|
||||
TerminalSession::~TerminalSession() {
|
||||
}
|
||||
TerminalSession::~TerminalSession() {}
|
||||
|
||||
int TerminalSession::getLines() {
|
||||
struct winsize size;
|
||||
|
@ -29,7 +29,7 @@ namespace core {
|
||||
class TerminalSession : public TCPSession {
|
||||
|
||||
public:
|
||||
TerminalSession(EPoll &ePoll, TCPServer &server);
|
||||
TerminalSession(EPoll &ePoll, TCPServer *server);
|
||||
~TerminalSession();
|
||||
|
||||
int getLines();
|
||||
|
Loading…
x
Reference in New Issue
Block a user