⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.cpp

📁 WYD Server 753 (Development)
💻 CPP
字号:
/* by My Destiny Team */

#include "server.h"
#include "client.h"
#include "packets.h"
// Informacoes do servidorcServer Server;
cServer::cServer() : cSocket()
{
    currUser = 0;
    maxUser = 0;    memset(language, 0, sizeof(language));
}

cServer::~cServer()
{    for(u32 x = 0; x < MAX_LANGUAGE; x++)        if(language[x] != NULL)            delete language[x];
}

// Inicia o servidor
bool cServer::Init()
{
    if(/* init server */
        !cServer::ReadConfig()      ||
        !cSocket::ReadConfig()      ||
        !cSocket::StartDatabase()   ||
        !cSocket::StartServer()
      )
    { /* falha ao iniciar o serv */
        return false;
    }

    Log(NORMAL, GetMessage(SERVERINIT), GetServerIP(), GetServerPort());
    Log(NORMAL, GetMessage(DATABASEINIT), GetDatabaseIP(), GetDatabasePort());
    return true;
}
// Le o arquivo de configuracao do servidor//// Retorna false se ocorreu um erro// Retorna true se leu com sucessobool cServer::ReadConfig(){    const char *def = "server.ini";    FILE *fp = fopen(def, "r");    if(fp == NULL)    { /* not found */        Log(WARN, GetMessage(FILENOTFOUND), def);        return false;    }    while(fgets(buf, sizeof(buf), fp))    {        if(buf[0] == '#' || buf[0] == '\n')            continue;        sscanf(buf, "%s = %s", cmd, val);        if(strcmp(cmd, "MaxUser") == 0)            maxUser = atoi(val);    }    fclose(fp);    Log(NORMAL, GetMessage(FILELOADED), def);    return true;}// Le o arquivo de idiomas//// Retorna false se ocorreu um erro// Retorna true se leu com sucessobool cServer::ReadLanguage(){    FILE *fp = fopen("language.txt", "r");    if(fp == NULL)    { /* not found */        wxMessageBox("language.txt not found.",            "Error: fopen", wxOK | wxICON_ERROR);        return false;    }    u32 index = 0;    char buflang[96];    while(fgets(buf, sizeof(buf), fp))    {        if(buf[0] == '#' || buf[0] == '\n')            continue;        if(sscanf(buf, "%03d\t\"%095[^\"]", &index, buflang) != 2)            continue;        if(index < MAX_LANGUAGE)        { /* index valido */            int leng = strlen(buflang);            char *str = new char[leng + 1];            strcpy(str, buflang);            language[index] = str;        }    }    fclose(fp);    return true;}
// Recebe o primeiro pacote do client
//
// Retorna false se o client nao enviou o hello packet
// Retorna true se o client foi logado com sucesso
bool cServer::Login(sSocket &sSock)
{    if(!sSock.Info.Flag.sHello)    { /* n enviou o pacote de hello */        if(4 == recv(sSock.Sock, buf, 4, 0))        { /* tamanho valido */            if(*(u32*)buf == 0x1F11F311)            { /* pacote valido */                sSock.Info.Flag.sHello = true;                return true;            }        }        DeleteSocket(sSock);    }
    else if(cSocket::Login(sSock))
    { /* new client */
        cClient *Client = (cClient*)List.Insert();
        if(Client == NULL)
        { /* lista cheia */
            Log(WARN, GetMessage(CLIENTLIST_FULL));            Packet.SendServerMessage(sSock, GetMessage(CLIENTLIST_FULL));
        }
        else
        { /* client adicionado */
            sSock.clientID = Client->clientID;            Client->Socket = &sSock;

            Log(NORMAL, GetMessage(CLIENTCONNECTED), sSock.clientID,
                inet_ntoa(sSock.socketInfo.sin_addr), ntohs(sSock.socketInfo.sin_port));
            currUser++;            RefStatusBar();
            Packet.RequestLogin(Client, sSock, (pCL_20Dh*)GetPointerPacket());
            return true;
        }        DeleteSocket(sSock);
    }

    return false;
}

// Trata o pacote recebido//// Retorna false se o pacote eh invalido// Retorna true se o pacote eh valido
void cServer::ReceivedPacket(sSocket &sSock, pHeader *pServer)
{    if(sSock.clientID == DATABASE_ID)        Packet.DBControl(sSock, pServer);    else
        Packet.ClientControl(sSock, pServer);
}
// Deleta o socket da listavoid cServer::DeleteSocket(s32 id){    DeleteSocket(cSocket::Get(id));}
// Deleta o socket da lista
bool cServer::DeleteSocket(sSocket &sSock)
{    cClient *Client = (cClient*)List.Get(sSock.clientID);    if(Client == NULL)    { /* id invalida */        Log(WARN, "check INVALID ID: id = %d", sSock.clientID);        return false;    }    if(Client->isLogged)    { // Client conectado
        if(Client->Character != NULL)
        { // Salva a conta e o personagem
            Client->Save(SAVE_ACCOUNT | SAVE_CHARACTER);
        }
        else
        { // Salva somente a conta
            Client->Save(SAVE_ACCOUNT);
        }    }
    if(!cSocket::DeleteSocket(sSock))        return false;

    if(sSock.clientID != INVALID_ID)
    { /* deleta o client do servidor */
        currUser--;        RefStatusBar();

        Log(NORMAL, GetMessage(CLIENTDISCONNECTED), sSock.clientID);
        List.Remove(sSock.clientID);
    }    return true;
}

// Retorna a mensagemconst char *cServer::GetMessage(u32 id) const { return language[id]; }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -