using CommonLang.Log;
using CommonLang.Protocol;
using CommonNetwork_ICE.Util;
using CommonServer.Protocol;
using CommonServer.Server;
using CommonServer_ICE.handler;
using CommonServer_ICE.Msg;
using CommonServer_ICE.Session;
using Slice;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CommonServer_ICE.Server
{
///
/// ICE的TCP消息委托处理实现类
///
public sealed class IceTcpServer : IceConnectServer
{
private static Logger log = LoggerFactory.GetLogger("IceTcpServer");
private TcpServerRouter routerAdapter;
public IceTcpServer(IPackageCodec codec)
: base(codec)
{
}
///
/// 启动ICE服务器
///
///
internal override void StartIceServer()
{
routerAdapter = new TcpServerRouter(this);
try
{
int result = routerAdapter.main(new string[0], getInitData());
if (result != 0)
{
throw new Exception("启动失败。");
}
}
catch (Exception e)
{
throw e;
}
}
///
/// 获取传输类型
///
///
internal override int getCommType()
{
return Constants.COMM_TYPE_TCP;
}
///
/// 初始化ICE TCP参数
///
///
private Ice.InitializationData getInitData()
{
Ice.InitializationData initData = new Ice.InitializationData();
Ice.Properties props = Ice.Util.createProperties();
props.setProperty(Constants.TCP_SERVER_INSTANCE_NAME + ".Endpoints", "tcp -h " + getServerConfig().Ip + " -p " + getServerConfig().Port);
initData.properties = props;
return initData;
}
///
/// 返回会话总数量
///
public override int SessionCount
{
get { return TcpSessionManager.getSessionCount(); }
}
///
/// 会话管理器
///
internal TcpSessionManager TcpSessionManager
{
private get;
set;
}
//广播消息
public override void Broadcast(IMessage message)
{
List sessionList = TcpSessionManager.getAllSession();
foreach (ISession session in sessionList)
{
session.Send(message);
}
}
///
/// 判断某个Session是否存在
///
///
///
public override bool HasSession(ISession session)
{
return TcpSessionManager.hasSession(session);
}
///
/// 获取单个Session
///
///
///
public override ISession GetSessionByID(string sessionID)
{
return TcpSessionManager.getSession(sessionID);
}
///
/// 获取所有Session
///
///
public override IEnumerable GetSessions()
{
if (TcpSessionManager == null)
{
return new List();
}
return TcpSessionManager.getAllSession();
}
///
/// 释放服务器资源
///
public override void Dispose()
{
log.Info("关闭服务器,释放各种资源");
// 关闭所有Session消息接收通道
if (TcpSessionManager != null)
{
List sessionList = TcpSessionManager.getAllSession();
foreach (ISession session in sessionList)
{
session.Disconnect(true);
}
}
if (routerAdapter != null)
{
routerAdapter.Dispose();
}
this.serverListener.OnDestory();
}
}
}