|
|
using WebSocketSharp.Server;
|
|
|
using MySql.Data.MySqlClient;
|
|
|
using System.Text;
|
|
|
using System.Security.Cryptography;
|
|
|
using OMS.NET.Instructs;
|
|
|
using System.Dynamic;
|
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
|
|
|
namespace OMS.NET
|
|
|
{
|
|
|
class Program
|
|
|
{
|
|
|
static void Main(string[] args)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
GlobalArea.LoadServerConfig();//加载服务器配置 默认server.yaml
|
|
|
GlobalArea.LoadRsaPkey();//加载rsa私钥 默认.pkey
|
|
|
DbCheck();
|
|
|
//预加载一些数据以方便后面的处理
|
|
|
GlobalArea.BuildLayerData();
|
|
|
|
|
|
//开启ws监听
|
|
|
var wssv = new WebSocketServer(Convert.ToInt16(GlobalArea.ServerConfig.ListenPort), false);
|
|
|
//wssv.SslConfiguration.ServerCertificate = new X509Certificate2(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "server.pfx"));
|
|
|
wssv.AddWebSocketService<MapServer>("/");
|
|
|
wssv.Start();
|
|
|
Console.WriteLine("已开启ws监听, 端口: " + GlobalArea.ServerConfig.ListenPort);
|
|
|
Console.WriteLine("输入exit退出程序");
|
|
|
bool loopFlag = true;
|
|
|
while (loopFlag)
|
|
|
{
|
|
|
string input = Console.ReadLine() ?? "";
|
|
|
switch (input.Trim())
|
|
|
{
|
|
|
case "exit":
|
|
|
loopFlag = false;
|
|
|
break;
|
|
|
case "logins":
|
|
|
Console.WriteLine("当前登录用户 " + GlobalArea.LoginUserCount);
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
wssv.Stop();
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
GlobalArea.Log.Error(e.Message);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 检查数据库,如果不满足预设条件则初始化
|
|
|
/// </summary>
|
|
|
static void DbCheck()
|
|
|
{
|
|
|
//.dblock存在,则默认数据库结构无问题
|
|
|
string dblockPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @".dblock");
|
|
|
string initsqlPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"init.sql");
|
|
|
if (!File.Exists(dblockPath))
|
|
|
{
|
|
|
using MySqlConnection connection = new(GlobalArea.ServerConfig.ConnectionString);
|
|
|
connection.Open();
|
|
|
GlobalArea.Log.Info("dblock不存在,连接到数据库服务进行检查");
|
|
|
//检查数据库是否存在,检查数据表是否
|
|
|
// 检查数据库是否存在
|
|
|
string dbName = GlobalArea.ServerConfig.DataBaseName;
|
|
|
string checkDatabaseQuery = $"SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '{dbName}'";
|
|
|
MySqlCommand checkDatabaseCmd = new(checkDatabaseQuery, connection);
|
|
|
object result = checkDatabaseCmd.ExecuteScalar();
|
|
|
|
|
|
if (result == null)
|
|
|
{
|
|
|
// 指定数据库不存在,选择创建数据库及相关表结构
|
|
|
string createDatabaseQuery = $"CREATE DATABASE {dbName} CHARACTER SET utf8mb4";
|
|
|
MySqlCommand createDatabaseCmd = new(createDatabaseQuery, connection);
|
|
|
createDatabaseCmd.ExecuteNonQuery();
|
|
|
GlobalArea.Log.Info($"已创建数据库 {dbName}");
|
|
|
}
|
|
|
|
|
|
//创建默认数据表,实现通用逻辑,从init.sql读取创建相关表的sql语句,默认以分号结尾,忽略空行和换行
|
|
|
//如果表已存在则sql应当不会执行任何操作,需要在sql添加 IF NOT EXISTS
|
|
|
if (File.Exists(initsqlPath))
|
|
|
{
|
|
|
string useDb = $"USE {dbName}";
|
|
|
MySqlCommand createDatabaseCmd = new(useDb, connection);
|
|
|
createDatabaseCmd.ExecuteNonQuery();
|
|
|
File.ReadAllText(initsqlPath)
|
|
|
.Split(new[] { ';' })
|
|
|
.Select(s => s.Trim())
|
|
|
.Where(s => !string.IsNullOrWhiteSpace(s))
|
|
|
.ToList().ForEach(sql =>
|
|
|
{
|
|
|
MySqlCommand createDatabaseCmd = new(sql, connection);
|
|
|
createDatabaseCmd.ExecuteNonQuery();
|
|
|
GlobalArea.Log.Info($"执行SQL成功 {sql}");
|
|
|
});
|
|
|
}
|
|
|
File.WriteAllText(dblockPath, "");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
GlobalArea.Log.Info("满足预设条件,跳过数据库检查");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|