Many changes to ServerCore

This commit is contained in:
barant committed 2021-07-23 08:57:27 -07:00
1 parent 6969ef0fbb
commit d445834af6
83 files changed
+666 -756

No files matched your search

+25 -3
View File
@@ -1,4 +1,5 @@
#include "TCPServer.h"
#include "TCPService.h"
#include "EPoll.h"
#include "TCPSession.h"
#include "Exception.h"
@@ -12,9 +13,10 @@ namespace core {
int yes = 1;
setsockopt(getDescriptor(), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
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)
throw coreutils::Exception("Error on listen to socket");
throw coreutils::Exception("Error on listen to socket");
service = getService(ePoll);
}
TCPServer::~TCPServer() {
@@ -58,6 +60,10 @@ namespace core {
throw coreutils::Exception(errorString);
}
TCPService * TCPServer::getService(EPoll &epoll) {
return new TCPService(epoll);
}
TCPSession * TCPServer::getSocketAccept(EPoll &ePoll) {
return new TCPSession(ePoll, *this);
}
@@ -68,7 +74,7 @@ namespace core {
session->send();
}
int TCPServer::processCommand(std::string command, TCPSession *session, std::stringstream &data) {
int TCPServer::processCommand(coreutils::ZString command, TCPSession *session, std::stringstream &data) {
int sequence = 0;
for(auto *sessionx : sessions) {
data << "|" << ++sequence;
@@ -77,5 +83,21 @@ namespace core {
}
return 1;
}
void TCPSession::sendToAll() {
for(auto session : server.sessions)
if(session != this)
session->write(out.str());
out.str("");
}
void TCPSession::sendToAll(SessionFilter filter) {
for(auto session : server.sessions)
if(filter.test(*session))
if(session != this)
session->write(out.str());
out.str("");
}
}