Skip to content

Commit

Permalink
Renamed session classes in server. Implemented client base.
Browse files Browse the repository at this point in the history
  • Loading branch information
drclaws committed Nov 1, 2020
1 parent ea4ba19 commit e4643e6
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 108 deletions.
9 changes: 1 addition & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
# Temporary ignored while not complited
client/
CMakeList.txt
makefile
simple_lib/include/simple_lib/message.cpp

# C++ build files and directories
## C++ build files and directories
build/
*.o
21 changes: 12 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,28 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
set(INCLUDE_PATH simple_lib/include)

# Common static lib
project(simple_lib)
add_library(${PROJECT_NAME} STATIC simple_lib/src/common.cpp)
add_library(simple_lib STATIC simple_lib/src/common.cpp)

# Server
project(simple_server)
add_executable(${PROJECT_NAME} server/main.cpp)

target_sources(${PROJECT_NAME} PRIVATE server/ClientSession.cpp server/ClientSession.hpp)
target_sources(${PROJECT_NAME} PRIVATE server/Session.cpp server/Session.hpp)
target_sources(${PROJECT_NAME} PRIVATE server/Server.cpp server/Server.hpp)
target_sources(${PROJECT_NAME} PRIVATE server/TcpSession.cpp server/TcpSession.hpp)
target_sources(${PROJECT_NAME} PRIVATE server/UdpSession.cpp server/UdpSession.hpp)
target_sources(${PROJECT_NAME} PRIVATE server/SessionTcp.cpp server/SessionTcp.hpp)
target_sources(${PROJECT_NAME} PRIVATE server/SessionUdp.cpp server/SessionUdp.hpp)

target_sources(${PROJECT_NAME} PRIVATE server/message.cpp server/message.h)
target_sources(${PROJECT_NAME} PRIVATE server/session_result.h)

target_include_directories(${PROJECT_NAME} PUBLIC ${INCLUDE_PATH})
target_link_libraries(${PROJECT_NAME} simple_lib)

include(CTest)
enable_testing()
# client
project(simple_client)
add_executable(${PROJECT_NAME} client/main.cpp)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
include(CPack)
target_sources(${PROJECT_NAME} PRIVATE client/MainConsole.cpp client/MainConsole.hpp)

target_include_directories(${PROJECT_NAME} PUBLIC ${INCLUDE_PATH})
target_link_libraries(${PROJECT_NAME} simple_lib)
46 changes: 46 additions & 0 deletions client/MainConsole.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "MainConsole.hpp"

#include <sys/eventfd.h>

#include <errno.h>
#include <unistd.h>

namespace simpleApp
{
MainConsole::MainConsole()
{

}

MainConsole::~MainConsole()
{
if (this->breakEventFd != -1)
close(this->breakEventFd);
}

int MainConsole::initBreak()
{
if (this->breakEventFd != -1)
close(this->breakEventFd);

this->breakEventFd = eventfd(0, EFD_NONBLOCK);

return this->breakEventFd == -1 ? errno : 0;
}

int MainConsole::raiseBreak()
{
if (this->breakEventFd != -1)
{
if (eventfd_write(this->breakEventFd, static_cast<eventfd_t>(1)) == -1)
return errno;
}

return 0;
}

int MainConsole::consoleLoop()
{
return 0;
}
}
17 changes: 17 additions & 0 deletions client/MainConsole.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

namespace simpleApp
{
class MainConsole
{
public:
MainConsole();
~MainConsole();

int initBreak();
int raiseBreak();
int consoleLoop();
private:
int breakEventFd = -1;
};
}
41 changes: 41 additions & 0 deletions client/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>

#include <signal.h>

#include "MainConsole.hpp"

simpleApp::MainConsole console = simpleApp::MainConsole();

void onBreak(int s)
{
int err = console.raiseBreak();

if(err != 0)
std::cout << "Break event trigger failed with code " << err << std::endl;
}

int main(int argc, char** argv)
{
std::cout << "Welcome to the Simple Client! For exit press Ctrl+C" << std::endl;

int err = console.initBreak();
if (err != 0)
{
std::cout << "Break event initialization failed with code " << err << std::endl;
return -1;
}

struct sigaction sigIntHandler;

sigIntHandler.sa_handler = onBreak;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;

if(sigaction(SIGINT, &sigIntHandler, NULL) == -1)
{
std::cout << "Break event configuration failed with code " << errno << std::endl;
return -1;
}

return console.consoleLoop();
}
52 changes: 23 additions & 29 deletions server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,21 @@
#include <set>
#include <vector>

#if defined(__linux__)
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <strings.h>

#else
#error System is not supported

#endif

#include <unistd.h>

#include <simple_lib/common.h>

#include "Server.hpp"
#include "ClientSession.hpp"
#include "UdpSession.hpp"
#include "TcpSession.hpp"
#include "Session.hpp"
#include "SessionUdp.hpp"
#include "SessionTcp.hpp"

namespace simpleApp
{
Expand Down Expand Up @@ -148,13 +142,13 @@ namespace simpleApp

Server::~Server()
{
if (this->stopObject != -1)
close(this->stopObject);
if (this->stopEventFd != -1)
close(this->stopEventFd);
}

int Server::serverLoop(uint16_t port)
{
if(this->stopObject == -1)
if(this->stopEventFd == -1)
{
std::cout << "Stop event object is not initialized" << std::endl << std::flush;
return -1;
Expand Down Expand Up @@ -187,7 +181,7 @@ namespace simpleApp
std::cout << "EPoll creation failed with code " << errno << std::endl << std::flush;
isFailed = true;
}
else if(addToEPoll(epollfd, this->stopObject, &this->stopObject, EPOLLIN) == -1)
else if(addToEPoll(epollfd, this->stopEventFd, &this->stopEventFd, EPOLLIN) == -1)
{
std::cout << "EPOLL_CTL_ADD of stop event object failed with code " << errno << std::endl << std::flush;
isFailed = true;
Expand Down Expand Up @@ -230,23 +224,23 @@ namespace simpleApp
const size_t MAX_EVENTS_BUFFER = 10000;
epoll_event * events = static_cast<epoll_event *>(calloc(MAX_EVENTS_BUFFER, sizeof(epoll_event)));

std::set<ClientSession*> slaveSocketsMap = std::set<ClientSession *>();
std::set<Session*> slaveSocketsMap = std::set<Session *>();

bool stopEventHappened = false;

auto initSession = [&slaveSocketsMap, &masterTcpSocket, &masterUdpSocket, &epollfd, port](epoll_event& event)
{
ClientSession* clientSession;
Session* clientSession;
session_result result;

if (event.data.ptr == &masterTcpSocket)
{
clientSession = new TcpSession(epollfd);
clientSession = new SessionTcp(epollfd);
result = clientSession->init(masterTcpSocket, port);
}
else if (event.data.ptr == &masterUdpSocket)
{
clientSession = new UdpSession(epollfd);
clientSession = new SessionUdp(epollfd);
result = clientSession->init(masterUdpSocket, port);
}
else
Expand Down Expand Up @@ -316,17 +310,17 @@ namespace simpleApp
};
while (!stopEventHappened)
{
std::set<ClientSession*> slavesForRemove = std::set<ClientSession*>();
std::set<Session*> slavesForRemove = std::set<Session*>();
int N = epoll_wait(epollfd, events, MAX_EVENTS_BUFFER, -1);
for (size_t i = 0; static_cast<int>(i) < N; i++)
{
if (events[i].data.ptr == &this->stopObject)
if (events[i].data.ptr == &this->stopEventFd)
{
eventfd_t decrement = 1;

std::string msg = "Stop event happened";

if (eventfd_read(this->stopObject, static_cast<eventfd_t *>(&decrement)) == -1)
if (eventfd_read(this->stopEventFd, static_cast<eventfd_t *>(&decrement)) == -1)
{
msg += std::string("\nStop event decrementation failed with code ") +
std::to_string(errno) + ". Server will stop anyway";
Expand All @@ -350,7 +344,7 @@ namespace simpleApp
}
else
{
auto clientSession = reinterpret_cast<ClientSession*>(events[i].data.ptr);
auto clientSession = reinterpret_cast<Session*>(events[i].data.ptr);
if(slaveSocketsMap.find(clientSession) == slaveSocketsMap.end())
{
std::cout << "Unknown event happened" << std::endl;
Expand Down Expand Up @@ -464,7 +458,7 @@ namespace simpleApp
delete val;
}

removeFromEPoll(epollfd, this->stopObject);
removeFromEPoll(epollfd, this->stopEventFd);
}

if(epollfd != -1)
Expand All @@ -485,19 +479,19 @@ namespace simpleApp

int Server::initStop()
{
if (this->stopObject != -1)
close(this->stopObject);
if (this->stopEventFd != -1)
close(this->stopEventFd);

this->stopObject = eventfd(0, EFD_NONBLOCK);
this->stopEventFd = eventfd(0, EFD_NONBLOCK);

return this->stopObject == -1 ? errno : 0;
return this->stopEventFd == -1 ? errno : 0;
}

int Server::stop()
int Server::raiseStop()
{
if (this->stopObject != -1)
if (this->stopEventFd != -1)
{
if (eventfd_write(this->stopObject, static_cast<eventfd_t>(1)) == -1)
if (eventfd_write(this->stopEventFd, static_cast<eventfd_t>(1)) == -1)
return errno;
}

Expand Down
5 changes: 2 additions & 3 deletions server/Server.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <stdint.h>
#include <stdlib.h>

namespace simpleApp
{
Expand All @@ -15,9 +14,9 @@ namespace simpleApp

int initStop();

int stop();
int raiseStop();

private:
int stopObject = -1;
int stopEventFd = -1;
};
} // namespace simpleApp
16 changes: 5 additions & 11 deletions server/ClientSession.cpp → server/Session.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
#include "ClientSession.hpp"
#include "Session.hpp"

#if defined(__linux__)
#include <sys/socket.h>
#include <sys/epoll.h>

#else
#error System is not supported

#endif

#include <unistd.h>

namespace simpleApp
{
ClientSession::ClientSession(int epollfd, std::string name) : epollfd(epollfd), _name(name)
Session::Session(int epollfd, std::string name) : epollfd(epollfd), _name(name)
{

}

ClientSession::~ClientSession()
Session::~Session()
{
this->sessionClose();
}

std::string ClientSession::getName()
std::string Session::getName()
{
return this->_name;
}

void ClientSession::sessionClose()
void Session::sessionClose()
{
if (this->_socket != -1)
{
Expand Down
Loading

0 comments on commit e4643e6

Please sign in to comment.