177 lines
6.0 KiB
C++
177 lines
6.0 KiB
C++
#include <a8/a8.h>
|
|
|
|
#include <a8/stringlist.h>
|
|
#include <a8/csvreader.h>
|
|
#include <a8/mysql.h>
|
|
#include <a8/openssl.h>
|
|
#include <a8/strutils.h>
|
|
#include <a8/mutable_xobject.h>
|
|
|
|
#include <f8/f8.h>
|
|
#include <f8/udplog.h>
|
|
|
|
#include <google/protobuf/message.h>
|
|
#include <google/protobuf/stubs/common.h>
|
|
#include <google/protobuf/util/json_util.h>
|
|
|
|
#include "f8/utils.h"
|
|
|
|
namespace f8
|
|
{
|
|
|
|
std::string PbToJson(const google::protobuf::Message* msg)
|
|
{
|
|
std::string json;
|
|
google::protobuf::util::JsonPrintOptions options;
|
|
options.add_whitespace = true;
|
|
options.always_print_primitive_fields = true;
|
|
options.preserve_proto_field_names = true;
|
|
MessageToJsonString(*msg, &json, options).ok();
|
|
return json;
|
|
}
|
|
|
|
void InitMysqlConnection(a8::mysql::Query* query)
|
|
{
|
|
f8::UdpLog::Instance()->Info("show variables like 'character%';", {});
|
|
f8::UdpLog::Instance()->Info("Variable_name\tValue", {});
|
|
#if 1
|
|
query->ExecScript("SET character_set_server=utf8;", {});
|
|
query->ExecScript("SET NAMES utf8;", {});
|
|
#endif
|
|
query->ExecQuery("show variables like 'character%';", {});
|
|
while (!query->Eof()) {
|
|
std::string line;
|
|
line = query->GetValue(0).GetString() + "\t" + query->GetValue(1).GetString();
|
|
f8::UdpLog::Instance()->Info(line.c_str(), {});
|
|
assert(!(query->GetValue(0).GetString() == "character_set_client" &&
|
|
query->GetValue(1).GetString() != "utf8"));
|
|
assert(!(query->GetValue(0).GetString() == "character_set_connection" &&
|
|
query->GetValue(1).GetString() != "utf8"));
|
|
assert(!(query->GetValue(0).GetString() == "character_set_database" &&
|
|
query->GetValue(1).GetString() != "utf8"));
|
|
assert(!(query->GetValue(0).GetString() == "character_set_results" &&
|
|
query->GetValue(1).GetString() != "utf8"));
|
|
assert(!(query->GetValue(0).GetString() == "character_set_server" &&
|
|
query->GetValue(1).GetString() != "utf8"));
|
|
assert(!(query->GetValue(0).GetString() == "character_set_system" &&
|
|
query->GetValue(1).GetString() != "utf8"));
|
|
query->Next();
|
|
}
|
|
|
|
f8::UdpLog::Instance()->Info("show variables like '%collation%';", {});
|
|
f8::UdpLog::Instance()->Info("Variable_name\tValue", {});
|
|
query->ExecQuery("show variables like '%collation%';", {});
|
|
while (!query->Eof()) {
|
|
std::string line;
|
|
line = query->GetValue(0).GetString() + "\t" + query->GetValue(1).GetString();
|
|
f8::UdpLog::Instance()->Info(line.c_str(), {});
|
|
assert(query->GetValue(1).GetString() == "utf8_general_ci");
|
|
query->Next();
|
|
}
|
|
}
|
|
|
|
void CheckMysqlConnection(a8::mysql::Connection* conn, a8::mysql::Query* query,
|
|
std::string dbhost, int port, std::string dbuser, std::string dbpasswd, std::string gamedb)
|
|
{
|
|
if (query->ExecQuery("SELECT 1;", {}) <= 0) {
|
|
f8::UdpLog::Instance()->Warning("mysql disconnect", {});
|
|
if (conn->Connect(dbhost, 3306, dbuser, dbpasswd, gamedb)) {
|
|
f8::InitMysqlConnection(query);
|
|
f8::UdpLog::Instance()->Info("mysql reconnect successed", {});
|
|
} else {
|
|
f8::UdpLog::Instance()->Info("mysql reconnect failed", {});
|
|
}
|
|
}
|
|
}
|
|
|
|
bool CheckRegisterTimeInSessionId(const std::string& accountid, const std::string& sessionid)
|
|
{
|
|
std::vector<std::string> strings;
|
|
a8::Split(sessionid, strings, '_');
|
|
if (strings.size() < 4) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
time_t ExtractRegisterTimeFromSessionId(const std::string& sessionid)
|
|
{
|
|
std::vector<std::string> strings;
|
|
a8::Split(sessionid, strings, '_');
|
|
if (strings.size() < 4) {
|
|
return 0;
|
|
}
|
|
std::string session_createtime = strings[0];
|
|
std::string account_registertime = strings[1];
|
|
std::string md51 = strings[2];
|
|
std::string md52 = strings[3];
|
|
return a8::XValue(account_registertime);
|
|
}
|
|
|
|
bool IsValidSessionId(const std::string& accountid, const std::string& sessionid)
|
|
{
|
|
std::vector<std::string> strings;
|
|
a8::Split(sessionid, strings, '_');
|
|
if (strings.size() < 4) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
int ExtractGameIdFromAccountId(const std::string& accountid)
|
|
{
|
|
std::vector<std::string> strings;
|
|
a8::Split(accountid, strings, '_');
|
|
if (strings.size() < 2) {
|
|
return false;
|
|
}
|
|
std::string channelid = strings[0];
|
|
std::string gameid = strings[1];
|
|
return a8::XValue(gameid);
|
|
}
|
|
|
|
int ExtractChannelIdFromAccountId(const std::string& accountid)
|
|
{
|
|
std::vector<std::string> strings;
|
|
a8::Split(accountid, strings, '_');
|
|
if (strings.size() < 2) {
|
|
return false;
|
|
}
|
|
std::string channelid = strings[0];
|
|
std::string gameid = strings[1];
|
|
return a8::XValue(channelid);
|
|
}
|
|
|
|
bool IsOnlineEnv()
|
|
{
|
|
static bool is_online = !getenv("SERVER_ENV");
|
|
return is_online;
|
|
}
|
|
|
|
bool IsTestEnv()
|
|
{
|
|
static bool is_test = a8::SafeStrCmp(getenv("SERVER_ENV"), "TEST") == 0;
|
|
return is_test;
|
|
}
|
|
|
|
bool IsValidNormalConfig(a8::XObject& conf, std::vector<std::string> fields)
|
|
{
|
|
if (conf.GetType() != a8::XOT_ARRAY) {
|
|
abort();
|
|
}
|
|
for (int i = 0; i < conf.Size(); ++i) {
|
|
std::shared_ptr<a8::XObject> node = conf.At(i);
|
|
if (node->At("instance_id")->AsXValue().GetInt() != i + 1) {
|
|
abort();
|
|
}
|
|
for (std::string& field_name : fields) {
|
|
if (!node->HasKey(field_name)) {
|
|
abort();
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
}
|