초기커밋
This commit is contained in:
186
ServerCommon/ProudNet/ProudNetListener.cs
Normal file
186
ServerCommon/ProudNet/ProudNetListener.cs
Normal file
@@ -0,0 +1,186 @@
|
||||
|
||||
using Nettention.Proud;
|
||||
|
||||
|
||||
using ServerCore;
|
||||
using ServerBase;
|
||||
|
||||
|
||||
using MODULE_ID = System.UInt32;
|
||||
using ServerControlCenter;
|
||||
using System.Diagnostics;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
|
||||
namespace ServerCommon;
|
||||
|
||||
|
||||
public abstract partial class ProudNetListener : ListenSessionBase, IModule
|
||||
{
|
||||
private readonly ModuleContext m_module_context;
|
||||
|
||||
private readonly ServerLogicBase m_server_logic_base;
|
||||
|
||||
private int m_max_connection_count;
|
||||
|
||||
public ProudNetListener( ServerLogicBase serverLogicBase
|
||||
, ModuleContext moduleContext)
|
||||
: base(new NetServer(), serverLogicBase.getServerType())
|
||||
{
|
||||
m_module_context = moduleContext;
|
||||
|
||||
var config_param = moduleContext.getConfigParam() as ConfigParam;
|
||||
NullReferenceCheckHelper.throwIfNull(config_param, () => $"config_param is null !!! - {toBasicString()}");
|
||||
|
||||
m_server_logic_base = serverLogicBase;
|
||||
m_max_connection_count = config_param.MaxConnectionCount;
|
||||
}
|
||||
|
||||
public async Task<Result> startModule()
|
||||
{
|
||||
var err_msg = string.Empty;
|
||||
var result = new Result();
|
||||
|
||||
return await Task.FromResult(result);
|
||||
}
|
||||
|
||||
public async Task<Result> stopModule()
|
||||
{
|
||||
var err_msg = string.Empty;
|
||||
var result = new Result();
|
||||
|
||||
return await Task.FromResult(result);
|
||||
}
|
||||
|
||||
public async Task<Result> startListen()
|
||||
{
|
||||
var err_msg = string.Empty;
|
||||
var result = new Result();
|
||||
|
||||
try
|
||||
{
|
||||
var module_context = getModuleContext();
|
||||
var config_param = module_context.getConfigParam() as ConfigParam;
|
||||
NullReferenceCheckHelper.throwIfNull(config_param, () => $"config_param is null !!! - {toBasicString()}");
|
||||
|
||||
var listen_ip = config_param.ListenIp;
|
||||
var listen_port = config_param.ListenPort;
|
||||
|
||||
if (true == Process.GetCurrentProcess().isRunningServerWithListenPort(listen_port))
|
||||
{
|
||||
err_msg = $"Already running server with Listen Port !!! : ListenPort:{listen_port} - {toBasicString()}";
|
||||
result.setFail(ServerErrorCode.AlreadyRunningServerWithListenPort, err_msg);
|
||||
Log.getLogger().error(result.toBasicString());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
if (false == onAttachProxyAll())
|
||||
{
|
||||
err_msg = $"Failed to attach Proxy All !!! - {toBasicString()}";
|
||||
result.setFail(ServerErrorCode.ProxyAttachFailed, err_msg);
|
||||
Log.getLogger().error(result.toBasicString());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
if (false == setMessageMaxLength())
|
||||
{
|
||||
err_msg = $"Failed to set message max length!!! - {toBasicString()}";
|
||||
result.setFail(ServerErrorCode.SetMessageMaxLengthFailed, err_msg);
|
||||
Log.getLogger().error(result.toBasicString());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
if (false == onBindStubHandler())
|
||||
{
|
||||
err_msg = $"Failed to bind Stub Handler !!! - {toBasicString()}";
|
||||
result.setFail(ServerErrorCode.SubHandlerBindFailed, err_msg);
|
||||
Log.getLogger().error(result.toBasicString());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
var start_param = config_param.StartServerParam;
|
||||
start_param.protocolVersion = new Nettention.Proud.Guid(Constant.Version);
|
||||
start_param.tcpPorts.Add(listen_port);
|
||||
start_param.enableNagleAlgorithm = false;
|
||||
start_param.clientEmergencyLogMaxLineCount = 10;
|
||||
start_param.hostIDGenerationPolicySimplePacketMode = HostIDGenerationPolicy.HostIDGenerationPolicy_NoRecycle;
|
||||
start_param.m_enableAutoConnectionRecoveryOnServer = false;
|
||||
|
||||
Log.getLogger().info($"Success, starModule(), start Listen ProudNet : {config_param.toBasicString()} - {toBasicString()}");
|
||||
|
||||
var net_server = getNetServer();
|
||||
|
||||
net_server.SetDefaultTimeoutTimeMs(config_param.KeepAliveMSec);
|
||||
net_server.Start(config_param.StartServerParam);
|
||||
|
||||
getLargePacketHandler().onInit(this);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var error_code = ServerErrorCode.ProudNetException;
|
||||
err_msg = $"Exception !!!, Failed to perform !!!, in startListen() : errorCode:{error_code}, exception:{e} - {toBasicString()}";
|
||||
result.setFail(error_code, err_msg);
|
||||
Log.getLogger().error(result.toBasicString());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return await Task.FromResult(result);
|
||||
}
|
||||
|
||||
public async Task<Result> stopListen()
|
||||
{
|
||||
var result = new Result();
|
||||
|
||||
try
|
||||
{
|
||||
var net_server = getNetServer();
|
||||
net_server.Stop();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var error_code = ServerErrorCode.ProudNetException;
|
||||
var err_msg = $"Exception !!!, Failed to perform !!!, in stopListen() : errorCode:{error_code}, exception:{e} - {toBasicString()}";
|
||||
result.setFail(error_code, err_msg);
|
||||
Log.getLogger().error(result.toBasicString());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return await Task.FromResult(result);
|
||||
}
|
||||
|
||||
public virtual async Task onLogoutUserAllByKick()
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
|
||||
var sessions = getEntityWithSessions().Values.ToList();
|
||||
|
||||
foreach (var client_session in sessions)
|
||||
{
|
||||
if (false == disconnectClient(client_session.getSessionId()))
|
||||
{
|
||||
Log.getLogger().error($"Failed to disconnect Client by Kick !!! : {client_session.toBasicString()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void changeMaxConnectionCount(int maxConnectionCount)
|
||||
{
|
||||
Log.getLogger().debug($"Changed MaxConnectionCount of ProudNetListener : old:{m_max_connection_count} => new:{maxConnectionCount} - {toBasicString()}");
|
||||
m_max_connection_count = maxConnectionCount;
|
||||
}
|
||||
|
||||
public override int getMaxConnectionCount()
|
||||
{
|
||||
return m_max_connection_count;
|
||||
}
|
||||
|
||||
protected abstract bool onAttachProxyAll();
|
||||
|
||||
protected abstract bool setMessageMaxLength();
|
||||
}
|
||||
61
ServerCommon/ProudNet/ProudNetListenerConfig.cs
Normal file
61
ServerCommon/ProudNet/ProudNetListenerConfig.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Nettention.Proud;
|
||||
|
||||
|
||||
using ServerCore;
|
||||
using ServerBase;
|
||||
|
||||
|
||||
namespace ServerCommon;
|
||||
|
||||
public abstract partial class ProudNetListener : ListenSessionBase, IModule
|
||||
{
|
||||
public class ConfigParam : IConfigParam
|
||||
{
|
||||
public string ListenIp { get; set; } = string.Empty;
|
||||
public ushort ListenPort { get; set; } = 0;
|
||||
|
||||
public int MaxConnectionCount { get; set; } = 0;
|
||||
public StartServerParameter StartServerParam { get; set; } = new();
|
||||
|
||||
public int KeepAliveMSec { get; set; } = 30 * ConstValue.default_1000_millisec;
|
||||
|
||||
public Result tryReadFromJsonOrDefault(JObject jObject)
|
||||
{
|
||||
var result = new Result();
|
||||
var err_msg = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
var client_listen = jObject["ClientListen"];
|
||||
NullReferenceCheckHelper.throwIfNull(client_listen, () => $"client_listen is null !!!");
|
||||
ListenIp = client_listen["Ip"]?.Value<string>() ?? AwsHelper.getAwsPublicIPv4OrEthernetIPv4();
|
||||
ListenPort = client_listen["Port"]?.Value<ushort>() ?? ListenPort;
|
||||
MaxConnectionCount = jObject["DefaultMaxUser"]?.Value<int>() ?? MaxConnectionCount;
|
||||
KeepAliveMSec = jObject["SessionKeepAliveTimeSec"]?.Value<UInt16>() * ConstValue.default_1000_millisec ?? KeepAliveMSec;
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var error_code = ServerErrorCode.TryCatchException;
|
||||
err_msg = $"Exception !!!, Failed to perform in tryReadFromJsonOrDefault() !!! : errorCode:{error_code}, exception:{e} - {this.getTypeName()}";
|
||||
result.setFail(error_code, err_msg);
|
||||
Log.getLogger().error(err_msg);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public string toBasicString()
|
||||
{
|
||||
return $"ConfigParam: ListenIp/Port:{ListenIp}:{ListenPort}, KeepAliveMSec:{KeepAliveMSec}, {StartServerParam.ToString()}";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user