Skip to content

Commit

Permalink
Split login message if too long
Browse files Browse the repository at this point in the history
  • Loading branch information
callaa committed Apr 11, 2017
1 parent a0e318a commit b68d62e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
36 changes: 31 additions & 5 deletions src/shared/server/loginhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ void LoginHandler::announceServerInfo()
greeting.type = protocol::ServerReply::LOGIN;
greeting.message = "Welcome";
greeting.reply["title"] = m_server->config()->getConfigString(config::ServerTitle);
// TODO if message length exceeds maximum, split session list into multiple messages
QJsonArray sessions = m_server->sessionDescriptions();
if(m_server->templateLoader()) {
// Add session templates to list, if not shadowed by live sessions
Expand All @@ -99,7 +98,27 @@ void LoginHandler::announceServerInfo()
}
greeting.reply["sessions"] = sessions;

send(greeting);
if(!send(greeting)) {
// Reply was too long to fit in the message envelope!
// Split the reply into separte announcements and send it in pieces
protocol::ServerReply piece;
piece.type = greeting.type;
piece.message = greeting.message;
piece.reply["title"] = greeting.reply["title"];

if(!greeting.reply["title"].toString().isEmpty()) {
send(piece);
}

for(const QJsonValue &session : sessions) {
QJsonArray a;
a << session;
piece.reply["sessions"] = a;
send(piece);
// Include the title as part of the first message, but not the later ones
piece.reply.remove("title");
}
}
}

void LoginHandler::announceSession(const QJsonObject &session)
Expand Down Expand Up @@ -460,10 +479,17 @@ void LoginHandler::handleStarttls()
m_state = WAIT_FOR_IDENT;
}

void LoginHandler::send(const protocol::ServerReply &cmd)
bool LoginHandler::send(const protocol::ServerReply &cmd)
{
if(!m_complete)
m_client->sendDirectMessage(protocol::MessagePtr(new protocol::Command(0, cmd)));
if(!m_complete) {
protocol::MessagePtr msg(new protocol::Command(0, cmd));
if(msg.cast<protocol::Command>().isOversize()) {
qWarning("Oversize login message %s", qPrintable(cmd.message));
return false;
}
m_client->sendDirectMessage(msg);
}
return true;
}

void LoginHandler::sendError(const QString &code, const QString &message)
Expand Down
2 changes: 1 addition & 1 deletion src/shared/server/loginhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private slots:
void handleJoinMessage(const protocol::ServerCommand &cmd);
void handleStarttls();
void guestLogin(const QString &username);
void send(const protocol::ServerReply &cmd);
bool send(const protocol::ServerReply &cmd);
void sendError(const QString &code, const QString &message);

Client *m_client;
Expand Down

0 comments on commit b68d62e

Please sign in to comment.