Got it compiling and somewhat working, except post.

This commit is contained in:
Brad Arant committed 2020-11-29 13:57:06 -08:00
1 parent 7ee13e87d2
commit bcea21cf96
12 files changed
+373 -133

No files matched your search

+26 -28
View File
@@ -6,35 +6,33 @@
namespace core {
TCPServer::TCPServer(EPoll &ePoll, IPAddress address, std::string text)
: TCPSocket(ePoll, text) {
TCPServer::TCPServer(EPoll &ePoll, IPAddress address, std::string text)
: TCPSocket(ePoll, text) {
setDescriptor(socket(AF_INET, SOCK_STREAM, 0));
int yes = 1;
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));
if(listen(getDescriptor(), 20) < 0)
if(listen(getDescriptor(), 20) < 0)
throw coreutils::Exception("Error on listen to socket");
}
TCPServer::~TCPServer() {
coreutils::Log(coreutils::LOG_DEBUG_2) << "Closing server socket " << getDescriptor() << ".";
coreutils::Log(coreutils::LOG_DEBUG_2) << "Closing server socket " << getDescriptor() << ".";
close(getDescriptor());
}
void TCPServer::onDataReceived(std::string data) {
coreutils::Log(coreutils::LOG_DEBUG_2) << "entering TCPServer::onDataReceived socket " << getDescriptor() << ".";
lock.lock();
TCPSession *session = accept();
if(session)
sessions.push_back(session);
if(session)
sessions.push_back(session);
lock.unlock();
coreutils::Log(coreutils::LOG_DEBUG_2) << "Leaving TCPServer::onDataReceived socket " << getDescriptor() << ".";
}
TCPSession * TCPServer::accept() {
TCPSession *session = getSocketAccept(ePoll);
session->setDescriptor(::accept(getDescriptor(), (struct sockaddr *)&session->ipAddress.addr, &session->ipAddress.addressLength));
TCPSession * TCPServer::accept() {
TCPSession *session = getSocketAccept(ePoll);
session->setDescriptor(::accept(getDescriptor(), (struct sockaddr *)&session->ipAddress.addr, &session->ipAddress.addressLength));
// if(blackList && blackList->contains(session->ipAddress.getClientAddress())) {
// session->shutdown();
// 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() << ".";
return session;
}
void TCPServer::removeFromSessionList(TCPSession *session) {
void TCPServer::removeFromSessionList(TCPSession *session) {
std::vector<TCPSession *>::iterator cursor;
for(cursor = sessions.begin(); cursor < sessions.end(); ++cursor)
if(*cursor == session)
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);
}
}
TCPSession * TCPServer::getSocketAccept(EPoll &ePoll) {
return new TCPSession(ePoll, *this);
}
void TCPServer::output(TCPSession *session) {
void TCPServer::output(TCPSession *session) {
std::stringstream out;
out << "|" << session->ipAddress.getClientAddressAndPort();
out << "|" << session->ipAddress.getClientAddressAndPort();
session->send();
}
int TCPServer::processCommand(std::string command, TCPSession *session, std::stringstream &data) {
int sequence = 0;
int TCPServer::processCommand(std::string command, TCPSession *session, std::stringstream &data) {
int sequence = 0;
for(auto *sessionx : sessions) {
data << "|" << ++sequence;
sessionx->output(data);
sessionx->output(data);
data << "|" << std::endl;
}
return 1;
}
}