#include "TCPSocket.h" #include "EPoll.h" #include "Log.h" #include "Exception.h" #include "errno.h" namespace core { TCPSocket::TCPSocket(EPoll &ePoll) : Socket(ePoll) {} TCPSocket::TCPSocket(EPoll &ePoll, std::string text) : Socket(ePoll, text) {} TCPSocket::TCPSocket(EPoll &ePoll, TLSInfo *tlsInfo, std::string text) : Socket(ePoll, text), TLS(tlsInfo), tlsInfo(tlsInfo) {} TCPSocket::TCPSocket(EPoll &ePoll, SSL_CTX *ctx, std::string text) : Socket(ePoll, text), TLS(ctx) {} TCPSocket::~TCPSocket() {} void TCPSocket::connect(IPAddress &address) { setDescriptor(socket(AF_INET, SOCK_STREAM, 0)); if(::connect(getDescriptor(), (struct sockaddr *)&address.addr, address.addressLength) == -1) throw coreutils::Exception("Error on connect to TCP socket." + errno); coreutils::Log(coreutils::LOG_DEBUG_3) << "Connected to IP..." << address.getClientAddressAndPort(); } void TCPSocket::output(std::stringstream &out) { out << "|" << ipAddress.getClientAddressAndPort(); } void TCPSocket::onDataReceived(coreutils::ZString &data) { if(tlsInfo) { } if (data.getLength() > 0) { lineBuffer = (char *)realloc(lineBuffer, lineBufferSize + data.getLength()); memcpy(lineBuffer + lineBufferSize, data.getData(), data.getLength()); lineBufferSize += data.getLength(); while (lineBufferSize > 0) { if (blockSize == 0) { lineLength = strcspn(lineBuffer, "\r\n"); if (lineLength == lineBufferSize) break; coreutils::ZString zLine(lineBuffer, lineLength); onLineReceived(zLine); if (lineBuffer[lineLength] == '\r') ++lineLength; if (lineBuffer[lineLength] == '\n') ++lineLength; lineBufferSize -= lineLength; if (lineBufferSize > 0) memmove(lineBuffer, lineBuffer + lineLength, lineBufferSize); lineBuffer = (char *)realloc(lineBuffer, lineBufferSize); } else if (lineBufferSize >= blockLength) { coreutils::ZString zBlock(lineBuffer, blockLength); onBlockReceived(zBlock); lineBufferSize -= blockLength; if (lineBufferSize > 0) memmove(lineBuffer, lineBuffer + blockLength, lineBufferSize); lineBuffer = (char *)realloc(lineBuffer, lineBufferSize); } } } } void TCPSocket::onLineReceived(coreutils::ZString &line) { if (term) shutdown("termination requested"); } void TCPSocket::onBlockReceived(coreutils::ZString &block) {} void TCPSocket::setBlockSize(int blockSize) { this->blockSize = blockSize; } }