ServerCore/TCPSession.cpp
2020-04-13 09:40:58 -07:00

59 lines
1.3 KiB
C++

#include "TCPSession.h"
#include "TCPServer.h"
#include "Log.h"
#include "PString.h"
namespace core {
TCPSession::TCPSession(EPoll &ePoll, TCPServer &server, std::string text) : TCPSocket(ePoll, text), server(server) {}
TCPSession::~TCPSession() {
server.removeFromSessionList(this);
}
void TCPSession::output(std::stringstream &data) {
data << "|" << ipAddress.getClientAddressAndPort();
}
void TCPSession::protocol(std::string data = "") {
if(data.length() > 0) {
if(!server.commands.processRequest(data, this, out))
server.sessionErrorHandler("Invalid data received.", out);
}
}
void TCPSession::onRegistered() {
onConnected();
protocol();
send();
}
void TCPSession::onConnected() {}
void TCPSession::onDataReceived(std::string data) {
protocol(data);
send();
}
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("");
}
void TCPSession::send() {
write(out.str());
out.str("");
}
}