62 lines
1.6 KiB
C++
62 lines
1.6 KiB
C++
#include "TCPSession.h"
|
|
#include "TCPServer.h"
|
|
#include "Log.h"
|
|
#include "PString.h"
|
|
|
|
namespace core {
|
|
|
|
TCPSession::TCPSession(EPoll &ePoll, TCPServer &server) : TCPSocket(ePoll), server(server) {}
|
|
|
|
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::stringstream &out, std::string data = "") {
|
|
if(data.length() > 0) {
|
|
if(!server.commands.processRequest(data, this, out))
|
|
server.sessionErrorHandler("Invalid data received.", out);
|
|
}
|
|
else {
|
|
onConnected(out);
|
|
}
|
|
}
|
|
|
|
void TCPSession::onRegister() {
|
|
std::stringstream out;
|
|
protocol(out);
|
|
send(out.str());
|
|
}
|
|
|
|
void TCPSession::onConnected(std::stringstream &out) {}
|
|
|
|
void TCPSession::onDataReceived(std::string data) {
|
|
std::stringstream out;
|
|
protocol(out, data);
|
|
send(out.str());
|
|
}
|
|
|
|
void TCPSession::sendToAll(std::string data) {
|
|
for(auto session : server.sessions)
|
|
if(session != this)
|
|
session->write(data);
|
|
}
|
|
|
|
void TCPSession::sendToAll(SessionFilter filter, std::string data) {
|
|
for(auto session : server.sessions)
|
|
if(filter.test(*session))
|
|
if(session != this)
|
|
session->write(data);
|
|
}
|
|
|
|
void TCPSession::send(std::string data) {
|
|
write(data);
|
|
}
|
|
|
|
}
|