Got it compiling and somewhat working, except post.
This commit is contained in:
parent
7ee13e87d2
commit
bcea21cf96
31
.vscode/launch.json
vendored
Normal file
31
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Debug",
|
||||||
|
"type": "cppdbg",
|
||||||
|
"request": "launch",
|
||||||
|
"args": [],
|
||||||
|
"stopAtEntry": false,
|
||||||
|
"cwd": "${workspaceFolder}",
|
||||||
|
"environment": [],
|
||||||
|
"externalConsole": false,
|
||||||
|
"linux": {
|
||||||
|
"MIMode": "gdb",
|
||||||
|
"miDebuggerPath": "gdb",
|
||||||
|
"program": "${workspaceFolder}/output/main"
|
||||||
|
},
|
||||||
|
"osx": {
|
||||||
|
"MIMode": "lldb",
|
||||||
|
"miDebuggerPath": "lldb-mi",
|
||||||
|
"program": "${workspaceFolder}/output/main"
|
||||||
|
},
|
||||||
|
"windows": {
|
||||||
|
"MIMode": "gdb",
|
||||||
|
"miDebuggerPath": "gdb.exe",
|
||||||
|
"program": "${workspaceFolder}/output/main.exe"
|
||||||
|
},
|
||||||
|
"preLaunchTask": "build"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
84
.vscode/tasks.json
vendored
Normal file
84
.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
{
|
||||||
|
"version": "2.0.0",
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"label": "build",
|
||||||
|
"type": "shell",
|
||||||
|
"group": {
|
||||||
|
"kind": "build",
|
||||||
|
"isDefault": true
|
||||||
|
},
|
||||||
|
"windows": {
|
||||||
|
"command": "powershell",
|
||||||
|
"args": [
|
||||||
|
"-c",
|
||||||
|
"mingw32-make"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"linux": {
|
||||||
|
"command": "bash",
|
||||||
|
"args": [
|
||||||
|
"-c",
|
||||||
|
"make"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"osx": {
|
||||||
|
"command": "bash",
|
||||||
|
"args": [
|
||||||
|
"-c",
|
||||||
|
"make"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "build & run",
|
||||||
|
"type": "shell",
|
||||||
|
"windows": {
|
||||||
|
"command": "powershell",
|
||||||
|
"args": [
|
||||||
|
"-c",
|
||||||
|
"'mingw32-make run'"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"linux": {
|
||||||
|
"command": "bash",
|
||||||
|
"args": [
|
||||||
|
"-c",
|
||||||
|
"'make run'"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"osx": {
|
||||||
|
"command": "bash",
|
||||||
|
"args": [
|
||||||
|
"-c",
|
||||||
|
"'make run'"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "clean",
|
||||||
|
"type": "shell",
|
||||||
|
"windows": {
|
||||||
|
"command": "powershell",
|
||||||
|
"args": [
|
||||||
|
"-c",
|
||||||
|
"'mingw32-make clean'"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"linux": {
|
||||||
|
"command": "bash",
|
||||||
|
"args": [
|
||||||
|
"-c",
|
||||||
|
"'make clean'"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"osx": {
|
||||||
|
"command": "bash",
|
||||||
|
"args": [
|
||||||
|
"-c",
|
||||||
|
"'make clean'"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
91
Makefile
Normal file
91
Makefile
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
#
|
||||||
|
# '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!
|
@ -6,35 +6,33 @@
|
|||||||
|
|
||||||
namespace core {
|
namespace core {
|
||||||
|
|
||||||
TCPServer::TCPServer(EPoll &ePoll, IPAddress address, std::string text)
|
TCPServer::TCPServer(EPoll &ePoll, IPAddress address, std::string text)
|
||||||
: TCPSocket(ePoll, text) {
|
: TCPSocket(ePoll, text) {
|
||||||
setDescriptor(socket(AF_INET, SOCK_STREAM, 0));
|
setDescriptor(socket(AF_INET, SOCK_STREAM, 0));
|
||||||
int yes = 1;
|
int yes = 1;
|
||||||
setsockopt(getDescriptor(), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
|
setsockopt(getDescriptor(), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
|
||||||
if(bind(getDescriptor(), address.getPointer(), address.addressLength) < 0)
|
if(bind(getDescriptor(), address.getPointer(), address.addressLength) < 0)
|
||||||
throw coreutils::Exception("Error on bind to socket: " + std::to_string(errno));
|
throw coreutils::Exception("Error on bind to socket: " + std::to_string(errno));
|
||||||
if(listen(getDescriptor(), 20) < 0)
|
if(listen(getDescriptor(), 20) < 0)
|
||||||
throw coreutils::Exception("Error on listen to socket");
|
throw coreutils::Exception("Error on listen to socket");
|
||||||
}
|
}
|
||||||
|
|
||||||
TCPServer::~TCPServer() {
|
TCPServer::~TCPServer() {
|
||||||
coreutils::Log(coreutils::LOG_DEBUG_2) << "Closing server socket " << getDescriptor() << ".";
|
coreutils::Log(coreutils::LOG_DEBUG_2) << "Closing server socket " << getDescriptor() << ".";
|
||||||
close(getDescriptor());
|
close(getDescriptor());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TCPServer::onDataReceived(std::string data) {
|
void TCPServer::onDataReceived(std::string data) {
|
||||||
coreutils::Log(coreutils::LOG_DEBUG_2) << "entering TCPServer::onDataReceived socket " << getDescriptor() << ".";
|
|
||||||
lock.lock();
|
lock.lock();
|
||||||
TCPSession *session = accept();
|
TCPSession *session = accept();
|
||||||
if(session)
|
if(session)
|
||||||
sessions.push_back(session);
|
sessions.push_back(session);
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
coreutils::Log(coreutils::LOG_DEBUG_2) << "Leaving TCPServer::onDataReceived socket " << getDescriptor() << ".";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TCPSession * TCPServer::accept() {
|
TCPSession * TCPServer::accept() {
|
||||||
TCPSession *session = getSocketAccept(ePoll);
|
TCPSession *session = getSocketAccept(ePoll);
|
||||||
session->setDescriptor(::accept(getDescriptor(), (struct sockaddr *)&session->ipAddress.addr, &session->ipAddress.addressLength));
|
session->setDescriptor(::accept(getDescriptor(), (struct sockaddr *)&session->ipAddress.addr, &session->ipAddress.addressLength));
|
||||||
// if(blackList && blackList->contains(session->ipAddress.getClientAddress())) {
|
// if(blackList && blackList->contains(session->ipAddress.getClientAddress())) {
|
||||||
// session->shutdown();
|
// session->shutdown();
|
||||||
// Log(LOG_WARN) << "Client at IP address " << session->ipAddress.getClientAddress() << " is blacklisted and was denied a connection.";
|
// Log(LOG_WARN) << "Client at IP address " << session->ipAddress.getClientAddress() << " is blacklisted and was denied a connection.";
|
||||||
@ -48,36 +46,36 @@ namespace core {
|
|||||||
coreutils::Log(coreutils::LOG_DEBUG_2) << "Session started on socket " << session->getDescriptor() << ".";
|
coreutils::Log(coreutils::LOG_DEBUG_2) << "Session started on socket " << session->getDescriptor() << ".";
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TCPServer::removeFromSessionList(TCPSession *session) {
|
void TCPServer::removeFromSessionList(TCPSession *session) {
|
||||||
std::vector<TCPSession *>::iterator cursor;
|
std::vector<TCPSession *>::iterator cursor;
|
||||||
for(cursor = sessions.begin(); cursor < sessions.end(); ++cursor)
|
for(cursor = sessions.begin(); cursor < sessions.end(); ++cursor)
|
||||||
if(*cursor == session)
|
if(*cursor == session)
|
||||||
sessions.erase(cursor);
|
sessions.erase(cursor);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TCPServer::sessionErrorHandler(std::string errorString, std::stringstream &out) {
|
void TCPServer::sessionErrorHandler(std::string errorString, std::stringstream &out) {
|
||||||
throw coreutils::Exception(errorString);
|
throw coreutils::Exception(errorString);
|
||||||
}
|
}
|
||||||
|
|
||||||
TCPSession * TCPServer::getSocketAccept(EPoll &ePoll) {
|
TCPSession * TCPServer::getSocketAccept(EPoll &ePoll) {
|
||||||
return new TCPSession(ePoll, *this);
|
return new TCPSession(ePoll, *this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TCPServer::output(TCPSession *session) {
|
void TCPServer::output(TCPSession *session) {
|
||||||
std::stringstream out;
|
std::stringstream out;
|
||||||
out << "|" << session->ipAddress.getClientAddressAndPort();
|
out << "|" << session->ipAddress.getClientAddressAndPort();
|
||||||
session->send();
|
session->send();
|
||||||
}
|
}
|
||||||
|
|
||||||
int TCPServer::processCommand(std::string command, TCPSession *session, std::stringstream &data) {
|
int TCPServer::processCommand(std::string command, TCPSession *session, std::stringstream &data) {
|
||||||
int sequence = 0;
|
int sequence = 0;
|
||||||
for(auto *sessionx : sessions) {
|
for(auto *sessionx : sessions) {
|
||||||
data << "|" << ++sequence;
|
data << "|" << ++sequence;
|
||||||
sessionx->output(data);
|
sessionx->output(data);
|
||||||
data << "|" << std::endl;
|
data << "|" << std::endl;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
86
TCPServer.h
86
TCPServer.h
@ -8,23 +8,23 @@
|
|||||||
#include "CommandList.h"
|
#include "CommandList.h"
|
||||||
|
|
||||||
namespace core {
|
namespace core {
|
||||||
|
|
||||||
///
|
///
|
||||||
/// TCPServer
|
/// TCPServer
|
||||||
///
|
///
|
||||||
/// Manage a socket connection as a TCP server type. Connections to the socket are processed through
|
/// Manage a socket connection as a TCP server type. Connections to the socket are processed through
|
||||||
/// the accept functionality.
|
/// the accept functionality.
|
||||||
///
|
///
|
||||||
/// A list of connections is maintained in a vector object.
|
/// 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
|
/// This object extends the BMACommand object as well so it can be added to a Console object and
|
||||||
/// process commands to display status information.
|
/// process commands to display status information.
|
||||||
///
|
///
|
||||||
|
|
||||||
class TCPServer : public TCPSocket, public Command {
|
class TCPServer : public TCPSocket, public Command {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
///
|
///
|
||||||
/// The constructor for the BMATCPSocket object.
|
/// The constructor for the BMATCPSocket object.
|
||||||
///
|
///
|
||||||
@ -34,90 +34,90 @@ namespace core {
|
|||||||
/// @param commandName the name of the command used to invoke the status display for this object.
|
/// @param commandName the name of the command used to invoke the status display for this object.
|
||||||
/// @return the instance of the BMATCPServerSocket.
|
/// @return the instance of the BMATCPServerSocket.
|
||||||
///
|
///
|
||||||
|
|
||||||
TCPServer(EPoll &ePoll, IPAddress address, std::string text = "");
|
TCPServer(EPoll &ePoll, IPAddress address, std::string text = "");
|
||||||
|
|
||||||
///
|
///
|
||||||
/// The destructor for this object.
|
/// The destructor for this object.
|
||||||
///
|
///
|
||||||
|
|
||||||
~TCPServer();
|
~TCPServer();
|
||||||
|
|
||||||
///
|
|
||||||
/// 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
|
/// 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
|
/// 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.
|
/// 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;
|
IPAddressList *whiteList;
|
||||||
|
|
||||||
void removeFromSessionList(TCPSession *session);
|
void removeFromSessionList(TCPSession *session);
|
||||||
|
|
||||||
virtual void sessionErrorHandler(std::string errorString, std::stringstream &out);
|
virtual void sessionErrorHandler(std::string errorString, std::stringstream &out);
|
||||||
|
|
||||||
///
|
///
|
||||||
/// getSocketAccept is designed to allow a polymorphic extension of this object to
|
/// 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.
|
/// return a type of object that extends the definition of the server socket.
|
||||||
/// Returning the appropriate session object that extends from Session provides
|
/// Returning the appropriate session object that extends from Session provides
|
||||||
/// the mechanism where the server can select the protocol dialog for the desired
|
/// the mechanism where the server can select the protocol dialog for the desired
|
||||||
/// service.
|
/// service.
|
||||||
///
|
///
|
||||||
|
|
||||||
virtual TCPSession * getSocketAccept(EPoll &epoll);
|
virtual TCPSession * getSocketAccept(EPoll &epoll);
|
||||||
|
|
||||||
void output(TCPSession *session); ///<Output the consoles array to the console.
|
void output(TCPSession *session); ///<Output the consoles array to the console.
|
||||||
|
|
||||||
///
|
///
|
||||||
/// The list of sessions that are currently open and being maintained by this object.
|
/// The list of sessions that are currently open and being maintained by this object.
|
||||||
///
|
///
|
||||||
|
|
||||||
std::vector<TCPSession *> sessions;
|
std::vector<TCPSession *> sessions;
|
||||||
|
|
||||||
///
|
///
|
||||||
/// The commands object is a CommandList and is used to store Command objects to be
|
/// The commands object is a CommandList and is used to store Command objects to be
|
||||||
/// parsed and run as data comes into the session.
|
/// parsed and run as data comes into the session.
|
||||||
///
|
///
|
||||||
|
|
||||||
CommandList commands;
|
CommandList commands;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Override the virtual dataReceived since for the server these
|
/// Override the virtual dataReceived since for the server these
|
||||||
/// are requests to accept the new connection socket.
|
/// 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
|
/// 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
|
/// the fact that a new connection is coming into the system
|
||||||
///
|
///
|
||||||
/// @param data the pointer to the buffer containing the received data.
|
/// @param data the pointer to the buffer containing the received data.
|
||||||
/// @param length the length of the associated data buffer.
|
/// @param length the length of the associated data buffer.
|
||||||
///
|
///
|
||||||
|
|
||||||
void onDataReceived(std::string data) override;
|
void onDataReceived(std::string data) override;
|
||||||
|
|
||||||
///
|
///
|
||||||
/// This method is called when the Command associated with this object is requested
|
/// 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.
|
/// because a user has typed in the associated command name on a command entry line.
|
||||||
///
|
///
|
||||||
/// @param the session object to write the output to.
|
/// @param the session object to write the output to.
|
||||||
///
|
///
|
||||||
|
|
||||||
int processCommand(std::string command, TCPSession *session, std::stringstream &data) override;
|
int processCommand(std::string command, TCPSession *session, std::stringstream &data) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
TCPSession * accept();
|
TCPSession * accept();
|
||||||
std::mutex lock;
|
std::mutex lock;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -33,46 +33,41 @@ namespace core {
|
|||||||
|
|
||||||
void TCPSession::onDataReceived(char *data, int len) {
|
void TCPSession::onDataReceived(char *data, int len) {
|
||||||
if(len > 0) {
|
if(len > 0) {
|
||||||
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize + len);
|
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize + len);
|
||||||
memcpy(lineBuffer + lineBufferSize, data, len);
|
memcpy(lineBuffer + lineBufferSize, data, len);
|
||||||
lineBufferSize += len;
|
lineBufferSize += len;
|
||||||
while(lineBufferSize > 0) {
|
while(lineBufferSize > 0) {
|
||||||
switch(mode) {
|
if(blockSize == 0) {
|
||||||
case LINE:
|
lineLength = strcspn(lineBuffer, "\r\n");
|
||||||
lineLength = strcspn(lineBuffer, "\r\n");
|
if(lineLength == lineBufferSize)
|
||||||
if(lineLength == lineBufferSize)
|
break;
|
||||||
break;
|
onLineReceived(std::string(lineBuffer, lineLength));
|
||||||
onLineReceived(std::string(lineBuffer, lineLength));
|
if(lineBuffer[lineLength] == '\r')
|
||||||
if(lineBuffer[lineLength] == '\r')
|
++lineLength;
|
||||||
++lineLength;
|
if(lineBuffer[lineLength] == '\n')
|
||||||
if(lineBuffer[lineLength] == '\n')
|
++lineLength;
|
||||||
++lineLength;
|
lineBufferSize -= lineLength;
|
||||||
lineBufferSize -= lineLength;
|
if(lineBufferSize > 0)
|
||||||
if(lineBufferSize > 0)
|
memmove(lineBuffer, lineBuffer + lineLength, lineBufferSize);
|
||||||
memmove(lineBuffer, lineBuffer + lineLength, lineBufferSize);
|
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize);
|
||||||
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize);
|
} else {
|
||||||
break;
|
if(lineBufferSize >= blockLength) {
|
||||||
case BLOCK:
|
onBlockReceived(std::string(lineBuffer, blockLength));
|
||||||
if(lineBufferSize >= blockLength) {
|
lineBufferSize -= blockLength;
|
||||||
onBlockReceived(std::string(lineBuffer, blockLength));
|
if(lineBufferSize > 0)
|
||||||
lineBufferSize -= blockLength;
|
memmove(lineBuffer, lineBuffer + blockLength, lineBufferSize);
|
||||||
if(lineBufferSize > 0)
|
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize);
|
||||||
memmove(lineBuffer, lineBuffer + blockLength, lineBufferSize);
|
}
|
||||||
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize);
|
}
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TCPSession::setMode(core::Mode mode, int blockSize) {
|
void TCPSession::setBlockSize(int blockSize) {
|
||||||
this->mode = mode;
|
|
||||||
this->blockSize = blockSize;
|
this->blockSize = blockSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TCPSession::onLineReceived(std::string line) {
|
void TCPSession::onLineReceived(std::string line) {
|
||||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "[" << line << "]";
|
|
||||||
protocol(line);
|
protocol(line);
|
||||||
send();
|
send();
|
||||||
if(term)
|
if(term)
|
||||||
|
13
TCPSession.h
13
TCPSession.h
@ -7,7 +7,6 @@
|
|||||||
namespace core {
|
namespace core {
|
||||||
|
|
||||||
class Command;
|
class Command;
|
||||||
enum Mode {LINE, BLOCK, PACKET};
|
|
||||||
class TCPServer;
|
class TCPServer;
|
||||||
|
|
||||||
///
|
///
|
||||||
@ -137,13 +136,12 @@ namespace core {
|
|||||||
virtual void protocol(std::string data);
|
virtual void protocol(std::string data);
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Use the setMode method to set the receiving mode for the data on this socket.
|
/// Use setBlockSize to set the amount of data that should be read at once from the
|
||||||
/// Data can be received in LINE mode, which will receive data from the socket one
|
/// session data buffer.
|
||||||
/// line at a time, or BLOCK mode where a certain specified data block is received
|
/// If this value is set to 0 then the data will be retrieved
|
||||||
/// before calling the onBlockReceived method.
|
|
||||||
///
|
///
|
||||||
|
|
||||||
void setMode(core::Mode mode, int size = 0);
|
void setBlockSize(int size = 0);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
char *lineBuffer = NULL;
|
char *lineBuffer = NULL;
|
||||||
@ -152,8 +150,7 @@ namespace core {
|
|||||||
int blockLength = 0;
|
int blockLength = 0;
|
||||||
std::mutex mtx;
|
std::mutex mtx;
|
||||||
bool term = false;
|
bool term = false;
|
||||||
core::Mode mode = LINE;
|
int blockSize = 0;
|
||||||
int blockSize;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -4,23 +4,23 @@
|
|||||||
#include "Exception.h"
|
#include "Exception.h"
|
||||||
|
|
||||||
namespace core {
|
namespace core {
|
||||||
|
|
||||||
TCPSocket::TCPSocket(EPoll &ePoll) : Socket(ePoll) {}
|
TCPSocket::TCPSocket(EPoll &ePoll) : Socket(ePoll) {}
|
||||||
|
|
||||||
TCPSocket::TCPSocket(EPoll &ePoll, std::string text) : Socket(ePoll, text) {}
|
TCPSocket::TCPSocket(EPoll &ePoll, std::string text) : Socket(ePoll, text) {}
|
||||||
|
|
||||||
TCPSocket::~TCPSocket() {}
|
TCPSocket::~TCPSocket() {}
|
||||||
|
|
||||||
void TCPSocket::connect(IPAddress &address) {
|
void TCPSocket::connect(IPAddress &address) {
|
||||||
setDescriptor(socket(AF_INET, SOCK_STREAM, 0));
|
setDescriptor(socket(AF_INET, SOCK_STREAM, 0));
|
||||||
if(::connect(getDescriptor(), (struct sockaddr *)&address.addr, address.addressLength) == -1)
|
if(::connect(getDescriptor(), (struct sockaddr *)&address.addr, address.addressLength) == -1)
|
||||||
throw coreutils::Exception("Error on connect to TCP socket.");
|
throw coreutils::Exception("Error on connect to TCP socket.");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TCPSocket::output(std::stringstream &out) {
|
void TCPSocket::output(std::stringstream &out) {
|
||||||
out << "|" << ipAddress.getClientAddressAndPort();
|
out << "|" << ipAddress.getClientAddressAndPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
24
TCPSocket.h
24
TCPSocket.h
@ -9,12 +9,12 @@ namespace core {
|
|||||||
|
|
||||||
///
|
///
|
||||||
/// TCPSocket
|
/// TCPSocket
|
||||||
///
|
///
|
||||||
/// Provides a network TCP socket.
|
/// Provides a network TCP socket.
|
||||||
///
|
///
|
||||||
/// For accessing TCP network functions use this object. The connection oriented nature of TCP
|
/// For accessing TCP network functions use this object. The connection oriented nature of TCP
|
||||||
/// provides a single client persistent connection with data error correction and a durable
|
/// provides a single client persistent connection with data error correction and a durable
|
||||||
/// synchronous data connection.
|
/// synchronous data connection.
|
||||||
///
|
///
|
||||||
|
|
||||||
class TCPSocket : public Socket {
|
class TCPSocket : public Socket {
|
||||||
@ -24,20 +24,20 @@ namespace core {
|
|||||||
TCPSocket(EPoll &ePoll);
|
TCPSocket(EPoll &ePoll);
|
||||||
TCPSocket(EPoll &ePoll, std::string text);
|
TCPSocket(EPoll &ePoll, std::string text);
|
||||||
~TCPSocket();
|
~TCPSocket();
|
||||||
|
|
||||||
void connect(IPAddress &address);
|
void connect(IPAddress &address);
|
||||||
|
|
||||||
IPAddress ipAddress;
|
IPAddress ipAddress;
|
||||||
|
|
||||||
///
|
///
|
||||||
/// The output method is called by a socket session (BMASession) 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
|
/// BMATCPSocket or BMASession you can override the method to add attributes
|
||||||
/// to the list.
|
/// to the list.
|
||||||
///
|
///
|
||||||
|
|
||||||
virtual void output(std::stringstream &out);
|
virtual void output(std::stringstream &out);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
BIN
output/main
Executable file
BIN
output/main
Executable file
Binary file not shown.
6
src/main.cpp
Normal file
6
src/main.cpp
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
std::cout << "Hello world!" << std::endl;
|
||||||
|
}
|
38
src/workspace.code-workspace
Normal file
38
src/workspace.code-workspace
Normal 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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user