refactored giant switch to smaller functions

This commit is contained in:
Kamil 2020-11-17 01:00:10 +01:00 committed by Gent S
parent db98af9775
commit 4760d91ccd
2 changed files with 370 additions and 308 deletions

View File

@ -21,6 +21,64 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
switch (data->type) {
case P_CL2LS_REQ_LOGIN: {
login(sock, data);
break;
}
case P_CL2LS_REP_LIVE_CHECK: {
loginSessions[sock].lastHeartbeat = getTime();
break;
}
case P_CL2LS_REQ_CHECK_CHAR_NAME: {
nameCheck(sock, data);
break;
}
case P_CL2LS_REQ_SAVE_CHAR_NAME: {
nameSave(sock, data);
break;
}
case P_CL2LS_REQ_CHAR_CREATE: {
characterCreate(sock, data);
break;
}
case P_CL2LS_REQ_CHAR_DELETE: {
characterDelete(sock, data);
break;
}
case P_CL2LS_REQ_CHAR_SELECT: {
characterSelect(sock, data);
break;
}
case P_CL2LS_REQ_SHARD_SELECT: {
shardSelect(sock, data);
break;
}
case P_CL2LS_REQ_SAVE_CHAR_TUTOR: {
finishTutorial(sock, data);
break;
}
case P_CL2LS_REQ_CHANGE_CHAR_NAME: {
changeName(sock, data);
break;
}
case P_CL2LS_REQ_PC_EXIT_DUPLICATE:{
duplicateExit(sock, data);
break;
}
default:
if (settings::VERBOSITY)
std::cerr << "OpenFusion: LOGIN UNIMPLM ERR. PacketType: " << Defines::p2str(CL2LS, data->type) << " (" << data->type << ")" << std::endl;
break;
/*
* Unimplemented CL2LS packets:
* P_CL2LS_CHECK_NAME_LIST - unused by the client
* P_CL2LS_REQ_SERVER_SELECT
* P_CL2LS_REQ_SHARD_LIST_INFO - dev commands, useless as we only run 1 server
*/
}
}
#pragma region packets
void CNLoginServer::login(CNSocket* sock, CNPacketData* data) {
if (data->size != sizeof(sP_CL2LS_REQ_LOGIN))
return; // ignore the malformed packet
@ -48,7 +106,8 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
// checking regex
if (!CNLoginServer::isLoginDataGood(userLogin, userPassword)) {
errorCode = (int)LoginError::LOGIN_ERROR;
} else {
}
else {
std::unique_ptr<Database::Account> findUser = Database::findAccount(userLogin);
// if account not found, make new one
if (findUser == nullptr) {
@ -59,21 +118,24 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
success = true;
// if user exists, check if password is correct
} else if (CNLoginServer::isPasswordCorrect(findUser->Password, userPassword)) {
}
else if (CNLoginServer::isPasswordCorrect(findUser->Password, userPassword)) {
/*calling this here to timestamp login attempt,
* in order to make duplicate exit sanity check work*/
Database::updateSelected(findUser->AccountID, findUser->Selected);
// check if account isn't currently in use
if (CNLoginServer::isAccountInUse(findUser->AccountID)) {
errorCode = (int)LoginError::ID_ALREADY_IN_USE;
} else { // if not, login success
}
else { // if not, login success
loginSessions[sock] = CNLoginData();
loginSessions[sock].userID = findUser->AccountID;
loginSessions[sock].slot = findUser->Selected;
loginSessions[sock].lastHeartbeat = getTime();
success = true;
}
} else {
}
else {
errorCode = (int)LoginError::ID_AND_PASSWORD_DO_NOT_MATCH;
}
}
@ -131,20 +193,16 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
sock->sendPacket((void*)&charInfo, P_LS2CL_REP_CHAR_INFO, sizeof(sP_LS2CL_REP_CHAR_INFO));
}
} else {
}
else {
INITSTRUCT(sP_LS2CL_REP_LOGIN_FAIL, resp);
U8toU16(userLogin, resp.szID, sizeof(resp.szID));
resp.iErrorCode = errorCode;
sock->sendPacket((void*)&resp, P_LS2CL_REP_LOGIN_FAIL, sizeof(sP_LS2CL_REP_LOGIN_FAIL));
}
}
break;
}
case P_CL2LS_REP_LIVE_CHECK: {
loginSessions[sock].lastHeartbeat = getTime();
break;
}
case P_CL2LS_REQ_CHECK_CHAR_NAME: {
void CNLoginServer::nameCheck(CNSocket* sock, CNPacketData* data) {
if (data->size != sizeof(sP_CL2LS_REQ_CHECK_CHAR_NAME))
return;
@ -156,7 +214,8 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
if (!CNLoginServer::isCharacterNameGood(U16toU8(nameCheck->szFirstName), U16toU8(nameCheck->szLastName))) {
success = false;
errorcode = 4;
} else if (!Database::isNameFree(nameCheck)){ // check if name isn't already occupied
}
else if (!Database::isNameFree(nameCheck)) { // check if name isn't already occupied
success = false;
errorcode = 1;
}
@ -175,14 +234,15 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
memcpy(resp.szLastName, nameCheck->szLastName, sizeof(char16_t) * 17);
sock->sendPacket((void*)&resp, P_LS2CL_REP_CHECK_CHAR_NAME_SUCC, sizeof(sP_LS2CL_REP_CHECK_CHAR_NAME_SUCC));
} else {
}
else {
INITSTRUCT(sP_LS2CL_REP_CHECK_CHAR_NAME_FAIL, resp);
resp.iErrorCode = errorcode;
sock->sendPacket((void*)&resp, P_LS2CL_REP_CHECK_CHAR_NAME_FAIL, sizeof(sP_LS2CL_REP_CHECK_CHAR_NAME_FAIL));
}
break;
}
case P_CL2LS_REQ_SAVE_CHAR_NAME: {
}
void CNLoginServer::nameSave(CNSocket* sock, CNPacketData* data) {
if (data->size != sizeof(sP_CL2LS_REQ_SAVE_CHAR_NAME))
return;
@ -206,9 +266,9 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
sock->sendPacket((void*)&resp, P_LS2CL_REP_SAVE_CHAR_NAME_SUCC, sizeof(sP_LS2CL_REP_SAVE_CHAR_NAME_SUCC));
Database::updateSelected(loginSessions[sock].userID, save->iSlotNum);
break;
}
case P_CL2LS_REQ_CHAR_CREATE: {
}
void CNLoginServer::characterCreate(CNSocket* sock, CNPacketData* data) {
if (data->size != sizeof(sP_CL2LS_REQ_CHAR_CREATE))
return;
@ -251,9 +311,9 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
sock->sendPacket((void*)&resp, P_LS2CL_REP_CHAR_CREATE_SUCC, sizeof(sP_LS2CL_REP_CHAR_CREATE_SUCC));
Database::updateSelected(loginSessions[sock].userID, player.slot);
break;
}
case P_CL2LS_REQ_CHAR_DELETE: {
}
void CNLoginServer::characterDelete(CNSocket* sock, CNPacketData* data) {
if (data->size != sizeof(sP_CL2LS_REQ_CHAR_DELETE))
return;
@ -264,9 +324,9 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
resp.iSlotNum = operationResult;
sock->sendPacket((void*)&resp, P_LS2CL_REP_CHAR_DELETE_SUCC, sizeof(sP_LS2CL_REP_CHAR_DELETE_SUCC));
loginSessions[sock].lastHeartbeat = getTime();
break;
}
case P_CL2LS_REQ_CHAR_SELECT: {
}
void CNLoginServer::characterSelect(CNSocket* sock, CNPacketData* data) {
if (data->size != sizeof(sP_CL2LS_REQ_CHAR_SELECT))
return;
@ -284,9 +344,9 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
loginSessions[sock].selectedChar = chararacter->iPC_UID;
Database::updateSelected(loginSessions[sock].userID, loginSessions[sock].characters[chararacter->iPC_UID].slot);
sock->sendPacket((void*)&resp, P_LS2CL_REP_CHAR_SELECT_SUCC, sizeof(sP_LS2CL_REP_CHAR_SELECT_SUCC));
break;
}
case P_CL2LS_REQ_SHARD_SELECT: {
}
void CNLoginServer::shardSelect(CNSocket* sock, CNPacketData* data) {
if (data->size != sizeof(sP_CL2LS_REQ_SHARD_SELECT))
return;
@ -311,9 +371,9 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
CNSharedData::setPlayer(resp.iEnterSerialKey, loginSessions[sock].characters[loginSessions[sock].selectedChar]);
sock->sendPacket((void*)&resp, P_LS2CL_REP_SHARD_SELECT_SUCC, sizeof(sP_LS2CL_REP_SHARD_SELECT_SUCC));
break;
}
case P_CL2LS_REQ_SAVE_CHAR_TUTOR: {
}
void CNLoginServer::finishTutorial(CNSocket* sock, CNPacketData* data) {
if (data->size != sizeof(sP_CL2LS_REQ_SAVE_CHAR_TUTOR))
return;
sP_CL2LS_REQ_SAVE_CHAR_TUTOR* save = (sP_CL2LS_REQ_SAVE_CHAR_TUTOR*)data->buf;
@ -325,9 +385,9 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
loginSessions[sock].lastHeartbeat = getTime();
// no response here
break;
}
case P_CL2LS_REQ_CHANGE_CHAR_NAME: {
}
void CNLoginServer::changeName(CNSocket* sock, CNPacketData* data) {
if (data->size != sizeof(sP_CL2LS_REQ_CHANGE_CHAR_NAME))
return;
@ -336,16 +396,16 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
INITSTRUCT(sP_LS2CL_REP_CHANGE_CHAR_NAME_SUCC, resp);
resp.iPC_UID = save->iPCUID;
memcpy(resp.szFirstName, save->szFirstName, sizeof(char16_t)*9);
memcpy(resp.szFirstName, save->szFirstName, sizeof(char16_t) * 9);
memcpy(resp.szLastName, save->szLastName, sizeof(char16_t) * 17);
resp.iSlotNum = save->iSlotNum;
loginSessions[sock].lastHeartbeat = getTime();
sock->sendPacket((void*)&resp, P_LS2CL_REP_CHANGE_CHAR_NAME_SUCC, sizeof(sP_LS2CL_REP_CHANGE_CHAR_NAME_SUCC));
break;
}
case P_CL2LS_REQ_PC_EXIT_DUPLICATE:{
}
void CNLoginServer::duplicateExit(CNSocket* sock, CNPacketData* data) {
if (data->size != sizeof(sP_CL2LS_REQ_PC_EXIT_DUPLICATE))
return;
@ -355,25 +415,14 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
// sanity check
if (account == nullptr) {
std::cout << "[WARN] P_CL2LS_REQ_PC_EXIT_DUPLICATE submitted unknown username: " << exit->szID << std::endl;
break;
return;
}
exitDuplicate(account->AccountID);
break;
}
default:
if (settings::VERBOSITY)
std::cerr << "OpenFusion: LOGIN UNIMPLM ERR. PacketType: " << Defines::p2str(CL2LS, data->type) << " (" << data->type << ")" << std::endl;
break;
/*
* Unimplemented CL2LS packets:
* P_CL2LS_CHECK_NAME_LIST - unused by the client
* P_CL2LS_REQ_SERVER_SELECT
* P_CL2LS_REQ_SHARD_LIST_INFO - dev commands, useless as we only run 1 server
*/
}
}
#pragma endregion
#pragma region connections
void CNLoginServer::newConnection(CNSocket* cns) {
cns->setActiveKey(SOCKETKEY_E); // by default they accept keys encrypted with the default key
}
@ -401,6 +450,8 @@ void CNLoginServer::onStep() {
}
}
#pragma endregion
#pragma region helperMethods
bool CNLoginServer::isAccountInUse(int accountId) {
std::map<CNSocket*, CNLoginData>::iterator it;
@ -443,4 +494,4 @@ bool CNLoginServer::isCharacterNameGood(std::string Firstname, std::string Lastn
std::regex lastnamecheck(R"(((?! )(?!\.)[a-zA-Z0-9]*\.{0,1}(?!\.+ +)[a-zA-Z0-9]* {0,1}(?! +))*$)");
return (std::regex_match(Firstname, firstnamecheck) && std::regex_match(Lastname, lastnamecheck));
}
#pragma endregion helperMethods
#pragma endregion

View File

@ -31,6 +31,17 @@ private:
static void handlePacket(CNSocket* sock, CNPacketData* data);
static std::map<CNSocket*, CNLoginData> loginSessions;
static void login(CNSocket* sock, CNPacketData* data);
static void nameCheck(CNSocket* sock, CNPacketData* data);
static void nameSave(CNSocket* sock, CNPacketData* data);
static void characterCreate(CNSocket* sock, CNPacketData* data);
static void characterDelete(CNSocket* sock, CNPacketData* data);
static void characterSelect(CNSocket* sock, CNPacketData* data);
static void shardSelect(CNSocket* sock, CNPacketData* data);
static void finishTutorial(CNSocket* sock, CNPacketData* data);
static void changeName(CNSocket* sock, CNPacketData* data);
static void duplicateExit(CNSocket* sock, CNPacketData* data);
static bool isLoginDataGood(std::string login, std::string password);
static bool isPasswordCorrect(std::string actualPassword, std::string tryPassword);
static bool isAccountInUse(int accountId);