f8/cpp/utils.cc
2019-01-12 15:22:11 +08:00

201 lines
7.5 KiB
C++

#include <a8/a8.h>
#include <a8/stringlist.h>
#include <a8/csvreader.h>
#include <a8/udplog.h>
#include <a8/mysql.h>
#include <google/protobuf/message.h>
#include "framework/cpp/utils.h"
namespace f8
{
bool ReadCsvMetaFile(const std::string& filename,
google::protobuf::Message* prototype,
std::function<void (google::protobuf::Message*)> push_back_func)
{
const google::protobuf::Descriptor* descriptor = prototype->GetDescriptor();
const google::protobuf::Reflection* reflection = prototype->GetReflection();
a8::CsvReader reader;
reader.Load(filename);
while (reader.NextLine()) {
google::protobuf::Message* msg = prototype->New();
for (int i = 0; i < descriptor->field_count(); ++i) {
const google::protobuf::FieldDescriptor* field_desc = descriptor->field(i);
const std::string& field_name = field_desc->name();
if (field_name.empty() || field_name[0] == '_') {
continue;
}
switch (field_desc->cpp_type()) {
case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
{
reflection->SetString(msg, field_desc, reader.GetValue(field_name));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_INT32:
{
reflection->SetInt32(msg, field_desc, reader.GetValue(field_name));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_UINT32:
{
reflection->SetUInt32(msg, field_desc, reader.GetValue(field_name));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
{
reflection->SetInt64(msg, field_desc, reader.GetValue(field_name));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
{
reflection->SetUInt64(msg, field_desc, reader.GetValue(field_name));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
{
reflection->SetFloat(msg, field_desc, (double)reader.GetValue(field_name));
}
break;
case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
{
reflection->SetDouble(msg, field_desc, reader.GetValue(field_name));
}
break;
default:
{
abort();
}
break;
}//end switch
}//end for
push_back_func(msg);
delete msg;
}
return true;
}
void InitMysqlConnection(a8::mysql::Query* query)
{
a8::UdpLog::Instance()->Info("show variables like 'character%';", {});
a8::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();
a8::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();
}
a8::UdpLog::Instance()->Info("show variables like '%collation%';", {});
a8::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();
a8::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) {
a8::UdpLog::Instance()->Warning("mysql disconnect", {});
if (conn->Connect(dbhost, 3306, dbuser, dbpasswd, gamedb)) {
f8::InitMysqlConnection(query);
a8::UdpLog::Instance()->Info("mysql reconnect successed", {});
} else {
a8::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 false;
}
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()
{
return !getenv("SERVER_ENV");
}
}