refactor: ServerProxy::EResult => ServerProxy::ConnectionResult enum class

This commit is contained in:
sithlord48 2025-07-08 08:39:25 -04:00 committed by Chris Rizzitello
parent fe3c41f857
commit 29e9b8ae3b
2 changed files with 28 additions and 23 deletions

View file

@ -96,10 +96,11 @@ void ServerProxy::handleData()
LOG((CLOG_DEBUG2 "msg from server: %c%c%c%c", code[0], code[1], code[2], code[3])); LOG((CLOG_DEBUG2 "msg from server: %c%c%c%c", code[0], code[1], code[2], code[3]));
try { try {
switch ((this->*m_parser)(code)) { switch ((this->*m_parser)(code)) {
case kOkay: using enum ConnectionResult;
case Okay:
break; break;
case kUnknown: case Unknown:
LOG((CLOG_ERR "invalid message from server: %c%c%c%c", code[0], code[1], code[2], code[3])); LOG((CLOG_ERR "invalid message from server: %c%c%c%c", code[0], code[1], code[2], code[3]));
// not possible to determine message boundaries // not possible to determine message boundaries
// read the whole stream to discard unkonwn data // read the whole stream to discard unkonwn data
@ -107,7 +108,7 @@ void ServerProxy::handleData()
; ;
break; break;
case kDisconnect: case Disconnect:
return; return;
} }
} catch (const XBadClient &e) { } catch (const XBadClient &e) {
@ -124,8 +125,10 @@ void ServerProxy::handleData()
flushCompressedMouse(); flushCompressedMouse();
} }
ServerProxy::EResult ServerProxy::parseHandshakeMessage(const uint8_t *code) ServerProxy::ConnectionResult ServerProxy::parseHandshakeMessage(const uint8_t *code)
{ {
using enum ConnectionResult;
if (memcmp(code, kMsgQInfo, 4) == 0) { if (memcmp(code, kMsgQInfo, 4) == 0) {
queryInfo(); queryInfo();
} }
@ -161,7 +164,7 @@ ServerProxy::EResult ServerProxy::parseHandshakeMessage(const uint8_t *code)
// server wants us to hangup // server wants us to hangup
LOG((CLOG_DEBUG1 "recv close")); LOG((CLOG_DEBUG1 "recv close"));
m_client->disconnect(nullptr); m_client->disconnect(nullptr);
return kDisconnect; return Disconnect;
} }
else if (memcmp(code, kMsgEIncompatible, 4) == 0) { else if (memcmp(code, kMsgEIncompatible, 4) == 0) {
@ -170,36 +173,38 @@ ServerProxy::EResult ServerProxy::parseHandshakeMessage(const uint8_t *code)
ProtocolUtil::readf(m_stream, kMsgEIncompatible + 4, &major, &minor); ProtocolUtil::readf(m_stream, kMsgEIncompatible + 4, &major, &minor);
LOG((CLOG_ERR "server has incompatible version %d.%d", major, minor)); LOG((CLOG_ERR "server has incompatible version %d.%d", major, minor));
m_client->refuseConnection("server has incompatible version"); m_client->refuseConnection("server has incompatible version");
return kDisconnect; return Disconnect;
} }
else if (memcmp(code, kMsgEBusy, 4) == 0) { else if (memcmp(code, kMsgEBusy, 4) == 0) {
LOG((CLOG_ERR "server already has a connected client with name \"%s\"", m_client->getName().c_str())); LOG((CLOG_ERR "server already has a connected client with name \"%s\"", m_client->getName().c_str()));
m_client->refuseConnection("server already has a connected client with our name"); m_client->refuseConnection("server already has a connected client with our name");
return kDisconnect; return Disconnect;
} }
else if (memcmp(code, kMsgEUnknown, 4) == 0) { else if (memcmp(code, kMsgEUnknown, 4) == 0) {
LOG((CLOG_ERR "server refused client with name \"%s\"", m_client->getName().c_str())); LOG((CLOG_ERR "server refused client with name \"%s\"", m_client->getName().c_str()));
m_client->refuseConnection("server refused client with our name"); m_client->refuseConnection("server refused client with our name");
return kDisconnect; return Disconnect;
} }
else if (memcmp(code, kMsgEBad, 4) == 0) { else if (memcmp(code, kMsgEBad, 4) == 0) {
LOG((CLOG_ERR "server disconnected due to a protocol error")); LOG((CLOG_ERR "server disconnected due to a protocol error"));
m_client->refuseConnection("server reported a protocol error"); m_client->refuseConnection("server reported a protocol error");
return kDisconnect; return Disconnect;
} else if (memcmp(code, kMsgDLanguageSynchronisation, 4) == 0) { } else if (memcmp(code, kMsgDLanguageSynchronisation, 4) == 0) {
setServerLanguages(); setServerLanguages();
} else { } else {
return kUnknown; return Unknown;
} }
return kOkay; return Okay;
} }
ServerProxy::EResult ServerProxy::parseMessage(const uint8_t *code) ServerProxy::ConnectionResult ServerProxy::parseMessage(const uint8_t *code)
{ {
using enum ConnectionResult;
if (memcmp(code, kMsgDMouseMove, 4) == 0) { if (memcmp(code, kMsgDMouseMove, 4) == 0) {
mouseMove(); mouseMove();
} }
@ -305,13 +310,13 @@ ServerProxy::EResult ServerProxy::parseMessage(const uint8_t *code)
// server wants us to hangup // server wants us to hangup
LOG((CLOG_DEBUG1 "recv close")); LOG((CLOG_DEBUG1 "recv close"));
m_client->disconnect(nullptr); m_client->disconnect(nullptr);
return kDisconnect; return Disconnect;
} else if (memcmp(code, kMsgEBad, 4) == 0) { } else if (memcmp(code, kMsgEBad, 4) == 0) {
LOG((CLOG_ERR "server disconnected due to a protocol error")); LOG((CLOG_ERR "server disconnected due to a protocol error"));
m_client->disconnect("server reported a protocol error"); m_client->disconnect("server reported a protocol error");
return kDisconnect; return Disconnect;
} else { } else {
return kUnknown; return Unknown;
} }
// send a reply. this is intended to work around a delay when // send a reply. this is intended to work around a delay when
@ -323,7 +328,7 @@ ServerProxy::EResult ServerProxy::parseMessage(const uint8_t *code)
// TCP_NODELAY is enabled. // TCP_NODELAY is enabled.
ProtocolUtil::writef(m_stream, kMsgCNoop); ProtocolUtil::writef(m_stream, kMsgCNoop);
return kOkay; return Okay;
} }
void ServerProxy::handleKeepAliveAlarm() void ServerProxy::handleKeepAliveAlarm()

View file

@ -52,14 +52,14 @@ public:
//@} //@}
protected: protected:
enum EResult enum class ConnectionResult
{ {
kOkay, Okay,
kUnknown, Unknown,
kDisconnect Disconnect
}; };
EResult parseHandshakeMessage(const uint8_t *code); ConnectionResult parseHandshakeMessage(const uint8_t *code);
EResult parseMessage(const uint8_t *code); ConnectionResult parseMessage(const uint8_t *code);
private: private:
// if compressing mouse motion then send the last motion now // if compressing mouse motion then send the last motion now
@ -102,7 +102,7 @@ private:
void checkMissedLanguages() const; void checkMissedLanguages() const;
private: private:
using MessageParser = EResult (ServerProxy::*)(const uint8_t *); using MessageParser = ConnectionResult (ServerProxy::*)(const uint8_t *);
Client *m_client = nullptr; Client *m_client = nullptr;
deskflow::IStream *m_stream = nullptr; deskflow::IStream *m_stream = nullptr;