Got it compiling and somewhat working, except post.

This commit is contained in:
Brad Arant 2020-11-29 13:57:06 -08:00
parent 7ee13e87d2
commit bcea21cf96
12 changed files with 373 additions and 133 deletions

31
.vscode/launch.json vendored Normal file
View 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
View 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
View 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!

View File

@ -23,13 +23,11 @@ namespace core {
} }
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() {

View File

@ -37,8 +37,7 @@ namespace core {
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;
@ -51,8 +50,7 @@ namespace core {
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);
break; } else {
case BLOCK:
if(lineBufferSize >= blockLength) { if(lineBufferSize >= blockLength) {
onBlockReceived(std::string(lineBuffer, blockLength)); onBlockReceived(std::string(lineBuffer, blockLength));
lineBufferSize -= blockLength; lineBufferSize -= blockLength;
@ -60,19 +58,16 @@ namespace core {
memmove(lineBuffer, lineBuffer + blockLength, lineBufferSize); memmove(lineBuffer, lineBuffer + blockLength, lineBufferSize);
lineBuffer = (char *)realloc(lineBuffer, 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)

View File

@ -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;
}; };

View File

@ -30,7 +30,7 @@ namespace core {
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.

BIN
output/main Executable file

Binary file not shown.

6
src/main.cpp Normal file
View File

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

View File

@ -0,0 +1,38 @@
{
"folders": [
{
"path": "../../CoreUtils"
},
{
"path": ".."
},
{
"path": "../../HTTPServer"
}
],
"settings": {},
"launch": {
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "/home/barant/Development/HTTPServer/HTTPServer",
"args": [],
"stopAtEntry": false,
"cwd": "/home/barant/Development/HTTPServer/",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
}