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) {
|
TCPSession * ConsoleServer::getSocketAccept(EPoll &ePoll) {
|
||||||
return new ConsoleSession(ePoll, *this);
|
return new ConsoleSession(ePoll, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
namespace core {
|
namespace core {
|
||||||
|
|
||||||
ConsoleSession::ConsoleSession(EPoll &ePoll, TCPServer &server) : TerminalSession(ePoll, server) {}
|
ConsoleSession::ConsoleSession(EPoll &ePoll, TCPServer *server) : TerminalSession(ePoll, server) {}
|
||||||
|
|
||||||
ConsoleSession::~ConsoleSession() {}
|
ConsoleSession::~ConsoleSession() {}
|
||||||
|
|
||||||
@ -75,7 +75,7 @@ namespace core {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ConsoleSession::doCommand(coreutils::ZString &request) {
|
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 {
|
class ConsoleSession : public TerminalSession {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ConsoleSession(EPoll &ePoll, TCPServer &server);
|
ConsoleSession(EPoll &ePoll, TCPServer *server);
|
||||||
~ConsoleSession();
|
~ConsoleSession();
|
||||||
|
|
||||||
void writeLog(std::string data);
|
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;
|
std::stringstream out;
|
||||||
coreutils::Log(coreutils::LOG_DEBUG_1) << request[2];
|
coreutils::Log(coreutils::LOG_DEBUG_1) << request[2];
|
||||||
std::string invitee = request[2].str();
|
std::string invitee = request[2].str();
|
||||||
TCPSession *tempSession = session.server.getSessionByAlias(&invitee);
|
TCPSession *tempSession = session.server->getSessionByAlias(&invitee);
|
||||||
std::stringstream temp;
|
std::stringstream temp;
|
||||||
temp << "invite:" << request[1] << ":" << *(std::string *)session.alias;
|
temp << "invite:" << request[1] << ":" << *(std::string *)session.alias;
|
||||||
tempSession->write(temp.str());
|
tempSession->write(temp.str());
|
||||||
|
@ -9,13 +9,13 @@ namespace core {
|
|||||||
TCPServer::TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter, int depth, std::string text)
|
TCPServer::TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter, int depth, std::string text)
|
||||||
: TCPSocket(ePoll, text), commands(delimiter, depth) {
|
: TCPSocket(ePoll, text), commands(delimiter, depth) {
|
||||||
|
|
||||||
commands.add(subscriptions, "publish");
|
// commands.add(subscriptions, "publish"); // This needs to be removed and put into the servers that
|
||||||
commands.add(subscriptions, "unpublish");
|
// commands.add(subscriptions, "unpublish"); // Others make. Plus it should be in a manager object that
|
||||||
commands.add(subscriptions, "subscribe");
|
// commands.add(subscriptions, "subscribe"); // encapsulates all of the function.
|
||||||
commands.add(subscriptions, "unsubscribe");
|
// commands.add(subscriptions, "unsubscribe");
|
||||||
commands.add(subscriptions, "catalog");
|
// commands.add(subscriptions, "catalog");
|
||||||
commands.add(subscriptions, "event");
|
// commands.add(subscriptions, "event");
|
||||||
commands.add(subscriptions, "invite");
|
// commands.add(subscriptions, "invite");
|
||||||
|
|
||||||
setDescriptor(socket(AF_INET, SOCK_STREAM, 0));
|
setDescriptor(socket(AF_INET, SOCK_STREAM, 0));
|
||||||
int yes = 1;
|
int yes = 1;
|
||||||
@ -84,7 +84,7 @@ namespace core {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TCPSession * TCPServer::getSocketAccept(EPoll &ePoll) {
|
TCPSession * TCPServer::getSocketAccept(EPoll &ePoll) {
|
||||||
return new TCPSession(ePoll, *this);
|
return new TCPSession(ePoll, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TCPServer::output(std::stringstream &out) {
|
void TCPServer::output(std::stringstream &out) {
|
||||||
|
298
TCPServer.h
298
TCPServer.h
@ -1,155 +1,155 @@
|
|||||||
#ifndef __TCPServer_h__
|
#ifndef __TCPServer_h__
|
||||||
#define __TCPServer_h__
|
# define __TCPServer_h__
|
||||||
|
|
||||||
#include "Command.h"
|
# include "Command.h"
|
||||||
#include "CommandList.h"
|
# include "CommandList.h"
|
||||||
#include "IPAddressList.h"
|
# include "IPAddressList.h"
|
||||||
#include "Socket.h"
|
# include "Socket.h"
|
||||||
#include "SubscriptionManager.h"
|
# include "SubscriptionManager.h"
|
||||||
#include "TCPSession.h"
|
# include "TCPSession.h"
|
||||||
#include "TCPSocket.h"
|
# include "TCPSocket.h"
|
||||||
|
|
||||||
namespace core
|
|
||||||
{
|
|
||||||
|
|
||||||
///
|
|
||||||
/// TCPServer
|
|
||||||
///
|
|
||||||
/// Manage a socket connection as a TCP server type. Connections to the socket are processed through
|
|
||||||
/// the accept functionality.
|
|
||||||
///
|
|
||||||
/// A list of connections is maintained in a vector object.
|
|
||||||
///
|
|
||||||
/// This object extends the BMACommand object as well so it can be added to a Console object and
|
|
||||||
/// process commands to display status information.
|
|
||||||
///
|
|
||||||
|
|
||||||
class TCPServer : public TCPSocket, public Command {
|
|
||||||
|
|
||||||
public:
|
|
||||||
///
|
|
||||||
/// The constructor for the TCPServer object.
|
|
||||||
///
|
|
||||||
/// @param ePoll the EPoll instance that manages the socket.
|
|
||||||
/// @param url the IP address for the socket to receive connection requests.
|
|
||||||
/// @param port the port number that the socket will listen on.
|
|
||||||
/// @param commandName the name of the command used to invoke the status display for this object.
|
|
||||||
///
|
|
||||||
|
|
||||||
TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter = " ", int depth = 10, std::string text = "");
|
|
||||||
|
|
||||||
///
|
|
||||||
/// The destructor for this object.
|
|
||||||
///
|
|
||||||
|
|
||||||
virtual ~TCPServer();
|
|
||||||
|
|
||||||
virtual void sessionErrorHandler(std::string errorString, std::stringstream &out);
|
|
||||||
|
|
||||||
///
|
|
||||||
/// getSocketAccept is designed to allow a polymorphic extension of this object to
|
|
||||||
/// return a type of object that extends the definition of the server socket.
|
|
||||||
/// Returning the appropriate session object that extends from Session provides
|
|
||||||
/// the mechanism where the server can select the protocol dialog for the desired
|
|
||||||
/// service.
|
|
||||||
///
|
|
||||||
|
|
||||||
virtual TCPSession *getSocketAccept(EPoll &epoll);
|
|
||||||
|
|
||||||
///
|
|
||||||
/// The list of sessions that are currently open and being maintained by this object.
|
|
||||||
///
|
|
||||||
|
|
||||||
std::vector<TCPSession *> sessions;
|
|
||||||
|
|
||||||
///
|
|
||||||
/// The commands object is a CommandList and is used to store Command objects to be
|
|
||||||
/// parsed and run as data comes into the session.
|
|
||||||
///
|
|
||||||
|
|
||||||
CommandList commands;
|
|
||||||
|
|
||||||
///
|
|
||||||
/// If not NULL the blacklist object can be assigned to this server socket and the server
|
|
||||||
/// IP addresses connecting to the server attempting to accept a socket are contained in
|
|
||||||
/// this list then the connection is rejected and no accept is granted.
|
|
||||||
///
|
|
||||||
|
|
||||||
IPAddressList *blackList;
|
|
||||||
|
|
||||||
///
|
|
||||||
/// If not NULL the blacklist object can be assigned to this server socket and the server
|
|
||||||
/// IP addresses connecting to the server attempting to accept a socket are contained in
|
|
||||||
/// this list then the connection is rejected and no accept is granted.
|
|
||||||
///
|
|
||||||
|
|
||||||
IPAddressList *whiteList;
|
|
||||||
|
|
||||||
void removeFromSessionList(TCPSession *session);
|
|
||||||
|
|
||||||
void output(std::stringstream &out); ///< Output the consoles array to the console.
|
|
||||||
|
|
||||||
///
|
|
||||||
///
|
|
||||||
///
|
|
||||||
|
|
||||||
void sendToAll(std::stringstream &out);
|
|
||||||
|
|
||||||
///
|
|
||||||
/// Use this sendToAll method to output the contents of the out stream
|
|
||||||
/// to all the connections on the server excluding the sender session.
|
|
||||||
///
|
|
||||||
|
|
||||||
void sendToAll(std::stringstream &out, TCPSession &sender, SessionFilter filter);
|
|
||||||
|
|
||||||
///
|
|
||||||
/// Use this sendToAll method to output the contents of the out stream
|
|
||||||
/// to all the connections on the server excluding the sender session
|
|
||||||
/// and the entries identified by the passed in filter object.
|
|
||||||
///
|
|
||||||
|
|
||||||
void sendToAll(std::stringstream &out, TCPSession &sender);
|
|
||||||
|
|
||||||
///
|
|
||||||
/// The Subscription Manager tracks all subscriptions on the server.
|
|
||||||
///
|
|
||||||
|
|
||||||
SubscriptionManager subscriptions;
|
|
||||||
|
|
||||||
///
|
|
||||||
/// Use the getSessionByAlias to retrieve a session pointer by the value
|
|
||||||
/// of the alias pointer.
|
|
||||||
///
|
|
||||||
|
|
||||||
TCPSession *getSessionByAlias(void *alias);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
///
|
|
||||||
/// Override the virtual dataReceived since for the server these
|
|
||||||
/// are requests to accept the new connection socket.
|
|
||||||
/// No data is to be read or written when this method is called. It is the response to
|
|
||||||
/// the fact that a new connection is coming into the system
|
|
||||||
///
|
|
||||||
/// @param data the pointer to the buffer containing the received data.
|
|
||||||
/// @param length the length of the associated data buffer.
|
|
||||||
///
|
|
||||||
|
|
||||||
void onDataReceived(std::string data) override;
|
|
||||||
|
|
||||||
///
|
|
||||||
/// This method is called when the Command associated with this object is requested
|
|
||||||
/// because a user has typed in the associated command name on a command entry line.
|
|
||||||
///
|
|
||||||
/// @param the session object to write the output to.
|
|
||||||
///
|
|
||||||
|
|
||||||
int processCommand(coreutils::ZString &request, TCPSession &session) override;
|
|
||||||
|
|
||||||
private:
|
|
||||||
TCPSession *accept();
|
|
||||||
std::mutex lock;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
namespace core {
|
||||||
|
|
||||||
|
///
|
||||||
|
/// TCPServer
|
||||||
|
///
|
||||||
|
/// Manage a socket connection as a TCP server type. Connections to the socket are processed through
|
||||||
|
/// the accept functionality.
|
||||||
|
///
|
||||||
|
/// A list of connections is maintained in a vector object.
|
||||||
|
///
|
||||||
|
/// This object extends the BMACommand object as well so it can be added to a Console object and
|
||||||
|
/// process commands to display status information.
|
||||||
|
///
|
||||||
|
|
||||||
|
class TCPServer : public TCPSocket, public Command {
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
///
|
||||||
|
/// The constructor for the TCPServer object.
|
||||||
|
///
|
||||||
|
/// @param ePoll the EPoll instance that manages the socket.
|
||||||
|
/// @param url the IP address for the socket to receive connection requests.
|
||||||
|
/// @param port the port number that the socket will listen on.
|
||||||
|
/// @param commandName the name of the command used to invoke the status display for this object.
|
||||||
|
///
|
||||||
|
|
||||||
|
TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter = " ", int depth = 10, std::string text = "");
|
||||||
|
|
||||||
|
///
|
||||||
|
/// The destructor for this object.
|
||||||
|
///
|
||||||
|
|
||||||
|
virtual ~TCPServer();
|
||||||
|
|
||||||
|
virtual void sessionErrorHandler(std::string errorString, std::stringstream &out);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// getSocketAccept is designed to allow a polymorphic extension of this object to
|
||||||
|
/// return a type of object that extends the definition of the server socket.
|
||||||
|
/// Returning the appropriate session object that extends from Session provides
|
||||||
|
/// the mechanism where the server can select the protocol dialog for the desired
|
||||||
|
/// service.
|
||||||
|
///
|
||||||
|
|
||||||
|
virtual TCPSession *getSocketAccept(EPoll &epoll);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// The list of sessions that are currently open and being maintained by this object.
|
||||||
|
///
|
||||||
|
|
||||||
|
std::vector<TCPSession *> sessions;
|
||||||
|
|
||||||
|
///
|
||||||
|
/// The commands object is a CommandList and is used to store Command objects to be
|
||||||
|
/// parsed and run as data comes into the session.
|
||||||
|
///
|
||||||
|
|
||||||
|
CommandList commands;
|
||||||
|
|
||||||
|
///
|
||||||
|
/// If not NULL the blacklist object can be assigned to this server socket and the server
|
||||||
|
/// IP addresses connecting to the server attempting to accept a socket are contained in
|
||||||
|
/// this list then the connection is rejected and no accept is granted.
|
||||||
|
///
|
||||||
|
|
||||||
|
IPAddressList *blackList;
|
||||||
|
|
||||||
|
///
|
||||||
|
/// If not NULL the blacklist object can be assigned to this server socket and the server
|
||||||
|
/// IP addresses connecting to the server attempting to accept a socket are contained in
|
||||||
|
/// this list then the connection is rejected and no accept is granted.
|
||||||
|
///
|
||||||
|
|
||||||
|
IPAddressList *whiteList;
|
||||||
|
|
||||||
|
void removeFromSessionList(TCPSession *session);
|
||||||
|
|
||||||
|
void output(std::stringstream &out); ///< Output the consoles array to the console.
|
||||||
|
|
||||||
|
///
|
||||||
|
///
|
||||||
|
///
|
||||||
|
|
||||||
|
void sendToAll(std::stringstream &out);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Use this sendToAll method to output the contents of the out stream
|
||||||
|
/// to all the connections on the server excluding the sender session.
|
||||||
|
///
|
||||||
|
|
||||||
|
void sendToAll(std::stringstream &out, TCPSession &sender, SessionFilter filter);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Use this sendToAll method to output the contents of the out stream
|
||||||
|
/// to all the connections on the server excluding the sender session
|
||||||
|
/// and the entries identified by the passed in filter object.
|
||||||
|
///
|
||||||
|
|
||||||
|
void sendToAll(std::stringstream &out, TCPSession &sender);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// The Subscription Manager tracks all subscriptions on the server.
|
||||||
|
///
|
||||||
|
|
||||||
|
SubscriptionManager subscriptions;
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Use the getSessionByAlias to retrieve a session pointer by the value
|
||||||
|
/// of the alias pointer.
|
||||||
|
///
|
||||||
|
|
||||||
|
TCPSession *getSessionByAlias(void *alias);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
///
|
||||||
|
/// Override the virtual dataReceived since for the server these
|
||||||
|
/// are requests to accept the new connection socket.
|
||||||
|
/// No data is to be read or written when this method is called. It is the response to
|
||||||
|
/// the fact that a new connection is coming into the system
|
||||||
|
///
|
||||||
|
/// @param data the pointer to the buffer containing the received data.
|
||||||
|
/// @param length the length of the associated data buffer.
|
||||||
|
///
|
||||||
|
|
||||||
|
void onDataReceived(std::string data) override;
|
||||||
|
|
||||||
|
///
|
||||||
|
/// This method is called when the Command associated with this object is requested
|
||||||
|
/// because a user has typed in the associated command name on a command entry line.
|
||||||
|
///
|
||||||
|
/// @param the session object to write the output to.
|
||||||
|
///
|
||||||
|
|
||||||
|
int processCommand(coreutils::ZString &request, TCPSession &session) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
TCPSession *accept();
|
||||||
|
std::mutex lock;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -4,10 +4,16 @@
|
|||||||
#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, 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_t uuidObject;
|
||||||
uuid_generate(uuidObject);
|
uuid_generate(uuidObject);
|
||||||
coreutils::Log(coreutils::LOG_DEBUG_1) << uuidObject;
|
coreutils::Log(coreutils::LOG_DEBUG_1) << uuidObject;
|
||||||
@ -15,8 +21,8 @@ namespace core
|
|||||||
}
|
}
|
||||||
|
|
||||||
TCPSession::~TCPSession() {
|
TCPSession::~TCPSession() {
|
||||||
server.removeFromSessionList(this);
|
server->removeFromSessionList(this);
|
||||||
server.subscriptions.removeSessionSubscriptions(*this);
|
server->subscriptions.removeSessionSubscriptions(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TCPSession::output(std::stringstream &data) {
|
void TCPSession::output(std::stringstream &data) {
|
||||||
@ -25,7 +31,7 @@ namespace core
|
|||||||
|
|
||||||
void TCPSession::protocol(coreutils::ZString &data) {
|
void TCPSession::protocol(coreutils::ZString &data) {
|
||||||
if (data.getLength() != 0) {
|
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();
|
coreutils::Log(coreutils::LOG_DEBUG_1) << "Received data could not be parsed: " << data.str();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
262
TCPSession.h
262
TCPSession.h
@ -1,136 +1,144 @@
|
|||||||
#ifndef __Session_h__
|
#ifndef __Session_h__
|
||||||
#define __Session_h__
|
# define __Session_h__
|
||||||
|
|
||||||
#include "SessionFilter.h"
|
# include "SessionFilter.h"
|
||||||
#include "TCPSocket.h"
|
# include "TCPSocket.h"
|
||||||
#include <sstream>
|
# include <sstream>
|
||||||
|
|
||||||
namespace core {
|
namespace core {
|
||||||
|
|
||||||
class Command;
|
|
||||||
class TCPServer;
|
|
||||||
|
|
||||||
///
|
|
||||||
/// TCPSession
|
|
||||||
///
|
|
||||||
/// TCPSession defines the nature of the interaction with the client
|
|
||||||
/// and stores persistent data for a defined session. TCPSession objects
|
|
||||||
/// are not sockets but instead provide a communications control
|
|
||||||
/// mechanism. Protocol conversations are provided through extensions
|
|
||||||
/// from this object.
|
|
||||||
///
|
|
||||||
///
|
|
||||||
///
|
|
||||||
|
|
||||||
class TCPSession : public TCPSocket {
|
|
||||||
|
|
||||||
public:
|
|
||||||
///
|
|
||||||
///
|
|
||||||
///
|
|
||||||
|
|
||||||
TCPSession(EPoll &ePoll, TCPServer &server, std::string text = "");
|
|
||||||
|
|
||||||
///
|
|
||||||
///
|
|
||||||
///
|
|
||||||
|
|
||||||
virtual ~TCPSession();
|
|
||||||
|
|
||||||
Command *grab = NULL;
|
|
||||||
|
|
||||||
virtual void output(std::stringstream &data);
|
|
||||||
|
|
||||||
///
|
|
||||||
/// The send method is used to output the contents of the out stream
|
|
||||||
/// to the session containing the stream.
|
|
||||||
///
|
|
||||||
|
|
||||||
void send();
|
|
||||||
|
|
||||||
///
|
|
||||||
/// Use this method to terminate this TCPSession.
|
|
||||||
///
|
|
||||||
|
|
||||||
void terminate();
|
|
||||||
|
|
||||||
///
|
|
||||||
///
|
|
||||||
///
|
|
||||||
|
|
||||||
TCPServer &server;
|
|
||||||
|
|
||||||
///
|
|
||||||
/// Use out to send data to the session socket or other session sockets.
|
|
||||||
///
|
|
||||||
|
|
||||||
std::stringstream out;
|
|
||||||
|
|
||||||
///
|
|
||||||
/// uuid is generated automatically when the session object is instantiated. This
|
|
||||||
/// value can be used to uniquely identify a session and is the default value
|
|
||||||
/// pointed to by the alias pointer.
|
|
||||||
///
|
|
||||||
|
|
||||||
char uuid[37];
|
|
||||||
|
|
||||||
///
|
|
||||||
/// alias is a void pointer that can be set to point to any object that identifies
|
|
||||||
/// this session uniquely. Using this approach, inheriting objects can determine
|
|
||||||
/// how it knows the contacts that this server manages.
|
|
||||||
///
|
|
||||||
|
|
||||||
void *alias;
|
|
||||||
|
|
||||||
///
|
|
||||||
///
|
|
||||||
///
|
|
||||||
|
|
||||||
virtual bool compareAlias(void *alias);
|
|
||||||
|
|
||||||
virtual void outputAlias(std::stringstream &out);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
///
|
|
||||||
///
|
|
||||||
///
|
|
||||||
|
|
||||||
virtual void onRegistered() 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;
|
|
||||||
|
|
||||||
///
|
|
||||||
/// 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();
|
|
||||||
|
|
||||||
///
|
|
||||||
/// Override the protocol method to manage and control the session communications
|
|
||||||
/// in your inherited session. If you do not override this method then the Session
|
|
||||||
/// default will process the 'commands' added to the server object using the
|
|
||||||
/// processRequest method on the session input.
|
|
||||||
///
|
|
||||||
/// When data is received within the session two modes are available to pass the
|
|
||||||
/// data through the protocol method: LINE or BLOCK.
|
|
||||||
///
|
|
||||||
|
|
||||||
virtual void protocol(coreutils::ZString &data);
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::mutex mtx;
|
|
||||||
|
|
||||||
};
|
class Command;
|
||||||
|
class TCPServer;
|
||||||
|
|
||||||
|
///
|
||||||
|
/// TCPSession
|
||||||
|
///
|
||||||
|
/// TCPSession defines the nature of the interaction with the client
|
||||||
|
/// and stores persistent data for a defined session. TCPSession objects
|
||||||
|
/// are not sockets but instead provide a communications control
|
||||||
|
/// mechanism. Protocol conversations are provided through extensions
|
||||||
|
/// from this object.
|
||||||
|
///
|
||||||
|
///
|
||||||
|
///
|
||||||
|
|
||||||
|
class TCPSession : public TCPSocket {
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
///
|
||||||
|
///
|
||||||
|
///
|
||||||
|
|
||||||
|
TCPSession(EPoll &ePoll, std::string text = "");
|
||||||
|
|
||||||
|
///
|
||||||
|
///
|
||||||
|
///
|
||||||
|
|
||||||
|
TCPSession(EPoll &ePoll, TCPServer *server, std::string text = "");
|
||||||
|
|
||||||
|
///
|
||||||
|
///
|
||||||
|
///
|
||||||
|
|
||||||
|
virtual ~TCPSession();
|
||||||
|
|
||||||
|
Command *grab = NULL;
|
||||||
|
|
||||||
|
virtual void output(std::stringstream &data);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// The send method is used to output the contents of the out stream
|
||||||
|
/// to the session containing the stream.
|
||||||
|
///
|
||||||
|
|
||||||
|
void send();
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Use this method to terminate this TCPSession.
|
||||||
|
///
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Use out to send data to the session socket or other session sockets.
|
||||||
|
///
|
||||||
|
|
||||||
|
std::stringstream out;
|
||||||
|
|
||||||
|
///
|
||||||
|
/// uuid is generated automatically when the session object is instantiated. This
|
||||||
|
/// value can be used to uniquely identify a session and is the default value
|
||||||
|
/// pointed to by the alias pointer.
|
||||||
|
///
|
||||||
|
|
||||||
|
char uuid[37];
|
||||||
|
|
||||||
|
///
|
||||||
|
/// alias is a void pointer that can be set to point to any object that identifies
|
||||||
|
/// this session uniquely. Using this approach, inheriting objects can determine
|
||||||
|
/// how it knows the contacts that this server manages.
|
||||||
|
///
|
||||||
|
|
||||||
|
void *alias;
|
||||||
|
|
||||||
|
///
|
||||||
|
///
|
||||||
|
///
|
||||||
|
|
||||||
|
virtual bool compareAlias(void *alias);
|
||||||
|
|
||||||
|
virtual void outputAlias(std::stringstream &out);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
///
|
||||||
|
///
|
||||||
|
///
|
||||||
|
|
||||||
|
virtual void onRegistered() 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;
|
||||||
|
|
||||||
|
///
|
||||||
|
/// 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();
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Override the protocol method to manage and control the session communications
|
||||||
|
/// in your inherited session. If you do not override this method then the Session
|
||||||
|
/// default will process the 'commands' added to the server object using the
|
||||||
|
/// processRequest method on the session input.
|
||||||
|
///
|
||||||
|
/// When data is received within the session two modes are available to pass the
|
||||||
|
/// data through the protocol method: LINE or BLOCK.
|
||||||
|
///
|
||||||
|
|
||||||
|
virtual void protocol(coreutils::ZString &data);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::mutex mtx;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -23,55 +23,11 @@ namespace core {
|
|||||||
|
|
||||||
void TCPSession2::onConnected() {}
|
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) {
|
void TCPSession2::setBlockSize(int blockSize) {
|
||||||
this->blockSize = 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() {
|
void TCPSession2::send() {
|
||||||
if(out.tellp() > 0)
|
if(out.tellp() > 0)
|
||||||
TCPSocket::write(out.str());
|
TCPSocket::write(out.str());
|
||||||
|
@ -70,31 +70,6 @@ namespace core {
|
|||||||
|
|
||||||
virtual void onRegistered() override;
|
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
|
/// 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
|
/// 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:
|
public:
|
||||||
|
|
||||||
|
///
|
||||||
|
///
|
||||||
|
///
|
||||||
|
|
||||||
TCPSocket(EPoll &ePoll);
|
TCPSocket(EPoll &ePoll);
|
||||||
|
|
||||||
|
///
|
||||||
|
///
|
||||||
|
///
|
||||||
|
|
||||||
TCPSocket(EPoll &ePoll, std::string text);
|
TCPSocket(EPoll &ePoll, std::string text);
|
||||||
|
|
||||||
|
///
|
||||||
|
///
|
||||||
|
///
|
||||||
|
|
||||||
TCPSocket(EPoll &ePoll, SSL_CTX *ctx, std::string text);
|
TCPSocket(EPoll &ePoll, SSL_CTX *ctx, std::string text);
|
||||||
|
|
||||||
|
///
|
||||||
|
///
|
||||||
|
///
|
||||||
|
|
||||||
virtual ~TCPSocket();
|
virtual ~TCPSocket();
|
||||||
|
|
||||||
|
///
|
||||||
|
///
|
||||||
|
///
|
||||||
|
|
||||||
void connect(IPAddress &address);
|
void connect(IPAddress &address);
|
||||||
|
|
||||||
IPAddress ipAddress;
|
IPAddress ipAddress;
|
||||||
@ -34,7 +57,7 @@ namespace core {
|
|||||||
///
|
///
|
||||||
/// The output method is called by a socket session (TCPSession) and
|
/// The output method is called by a socket session (TCPSession) and
|
||||||
/// will output the detail information for the client socket. When extending
|
/// 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.
|
/// to the list.
|
||||||
///
|
///
|
||||||
|
|
||||||
|
@ -3,11 +3,9 @@
|
|||||||
|
|
||||||
namespace core {
|
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() {
|
int TerminalSession::getLines() {
|
||||||
struct winsize size;
|
struct winsize size;
|
||||||
|
@ -29,7 +29,7 @@ namespace core {
|
|||||||
class TerminalSession : public TCPSession {
|
class TerminalSession : public TCPSession {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TerminalSession(EPoll &ePoll, TCPServer &server);
|
TerminalSession(EPoll &ePoll, TCPServer *server);
|
||||||
~TerminalSession();
|
~TerminalSession();
|
||||||
|
|
||||||
int getLines();
|
int getLines();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user