Many changes to ServerCore
This commit is contained in:
1 parent
6969ef0fbb
commit
d445834af6
83 files changed
+666
-756
No files matched your search
+40
-56
@@ -1,7 +1,6 @@
|
||||
#include "TCPSession.h"
|
||||
#include "TCPServer.h"
|
||||
#include "Log.h"
|
||||
#include "PString.h"
|
||||
|
||||
namespace core {
|
||||
|
||||
@@ -15,15 +14,15 @@ namespace core {
|
||||
data << "|" << ipAddress.getClientAddressAndPort();
|
||||
}
|
||||
|
||||
void TCPSession::protocol(std::string data = "") {
|
||||
void TCPSession::protocol(coreutils::ZString data) {
|
||||
if(!server.commands.processRequest(data, this, out))
|
||||
if(data != "")
|
||||
server.sessionErrorHandler("Invalid data received.", out);
|
||||
if(data.getLength() != 0)
|
||||
server.sessionErrorHandler("Invalid data received.", out);
|
||||
}
|
||||
|
||||
void TCPSession::onRegistered() {
|
||||
onConnected();
|
||||
protocol();
|
||||
protocol((char *)"");
|
||||
send();
|
||||
if(term)
|
||||
shutdown("termination requested");
|
||||
@@ -31,73 +30,58 @@ namespace core {
|
||||
|
||||
void TCPSession::onConnected() {}
|
||||
|
||||
void TCPSession::onDataReceived(char *data, int len) {
|
||||
if(len > 0) {
|
||||
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize + len);
|
||||
memcpy(lineBuffer + lineBufferSize, data, len);
|
||||
lineBufferSize += len;
|
||||
while(lineBufferSize > 0) {
|
||||
if(blockSize == 0) {
|
||||
lineLength = strcspn(lineBuffer, "\r\n");
|
||||
if(lineLength == lineBufferSize)
|
||||
break;
|
||||
onLineReceived(std::string(lineBuffer, lineLength));
|
||||
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);
|
||||
void TCPSession::onDataReceived(coreutils::ZString data) {
|
||||
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;
|
||||
onLineReceived(coreutils::ZString(lineBuffer, lineLength));
|
||||
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) {
|
||||
onBlockReceived(std::string(lineBuffer, blockLength));
|
||||
lineBufferSize -= blockLength;
|
||||
if(lineBufferSize > 0)
|
||||
memmove(lineBuffer, lineBuffer + blockLength, lineBufferSize);
|
||||
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(lineBufferSize >= blockLength) {
|
||||
onBlockReceived(coreutils::ZString(lineBuffer, blockLength));
|
||||
lineBufferSize -= blockLength;
|
||||
if(lineBufferSize > 0)
|
||||
memmove(lineBuffer, lineBuffer + blockLength, lineBufferSize);
|
||||
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void TCPSession::setBlockSize(int blockSize) {
|
||||
this->blockSize = blockSize;
|
||||
}
|
||||
|
||||
void TCPSession::onLineReceived(std::string line) {
|
||||
|
||||
void TCPSession::onLineReceived(coreutils::ZString line) {
|
||||
protocol(line);
|
||||
send();
|
||||
if(term)
|
||||
shutdown("termination requested");
|
||||
}
|
||||
|
||||
void TCPSession::onBlockReceived(std::string block) {
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "[" << block.length() << "]";
|
||||
|
||||
void TCPSession::onBlockReceived(coreutils::ZString block) {
|
||||
coreutils::Log(coreutils::LOG_DEBUG_3) << "[" << block.getLength() << "]";
|
||||
if(term)
|
||||
shutdown("termination requested");
|
||||
}
|
||||
|
||||
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() {
|
||||
if(out.tellp() > 0)
|
||||
write(out.str());
|
||||
write(out.str());
|
||||
out.str("");
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user