IceConnectServer.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using CommonServer.Protocol;
  2. using CommonServer.Server;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using CommonNetwork_ICE.Common;
  8. using CommonServer_ICE.Session;
  9. using CommonLang.Protocol;
  10. using CommonNetwork_ICE.Util;
  11. using CommonServer_ICE.handler;
  12. using CommonServer_ICE.Msg;
  13. using System.Threading;
  14. using CommonLang.Log;
  15. using Slice;
  16. using System.IO;
  17. using CommonLang.IO;
  18. using System.Collections.Specialized;
  19. namespace CommonServer_ICE.Server
  20. {
  21. /// <summary>
  22. /// ICE服务器:ISever实现类,实现服务器端ICE网络监听
  23. /// </summary>
  24. public abstract class IceConnectServer : IServer
  25. {
  26. private static Logger log = LoggerFactory.GetLogger("IceConnectServer");
  27. // 编码转换器
  28. private IPackageCodec codec = null;
  29. // 服务器配置
  30. private IceServerConfig config = null;
  31. // 服务器监听器
  32. protected IServerListener serverListener = null;
  33. // 服务器ICE监听对象
  34. protected IceConnector serverConnector = null;
  35. // 客户端连接地址
  36. private string clientConnectString;
  37. public IceConnectServer(IPackageCodec codec)
  38. {
  39. this.codec = codec;
  40. this.config = new IceServerConfig();
  41. }
  42. public string ClientConnectString
  43. {
  44. get { return clientConnectString; }
  45. }
  46. public IPackageCodec PackageCodec
  47. {
  48. get { return this.codec; }
  49. }
  50. /// <summary>
  51. /// 返回会话总数量
  52. /// </summary>
  53. public abstract int SessionCount { get; }
  54. /// <summary>
  55. /// 开启服务器
  56. /// </summary>
  57. /// <param name="ip"></param>
  58. /// <param name="port"></param>
  59. /// <param name="listener"></param>
  60. public void Open(NameValueCollection config, IServerListener listener)
  61. {
  62. string localHost = config.Get("ICE_RECV_LOCAL_IP");
  63. string localPort = config.Get("ICE_RECV_LOCAL_PORT");
  64. this.clientConnectString = config.Get("CLIENT_CONNECT_STRING");
  65. if (localHost == null ||
  66. localPort == null ||
  67. clientConnectString == null)
  68. {
  69. throw new Exception(string.Format(
  70. @"Config Must Be Contains '{0}' and '{1}' and '{2}' fields !",
  71. "ICE_RECV_LOCAL_IP",
  72. "ICE_RECV_LOCAL_PORT",
  73. "CLIENT_CONNECT_STRING"));
  74. }
  75. int l_port = 0;
  76. if (!int.TryParse(localPort, out l_port))
  77. {
  78. throw new Exception(string.Format(
  79. @"Config '{0}' must be a digit value !",
  80. "ICE_RECV_LOCAL_PORT"));
  81. }
  82. // 初始化服务器端设置
  83. this.config.Ip = localHost;
  84. this.config.Port = l_port;
  85. this.serverListener = listener;
  86. this.serverListener.OnInit(this);
  87. this.serverConnector = new IceConnector(
  88. Constants.SEND_INTERFACE_NAME,
  89. Constants.SERVER_SENDER_RECV_ADAPTER,
  90. "", 0,
  91. this.config.Ip,
  92. this.config.Port);
  93. this.serverConnector.GetConnectorConfig().LocalIp = localHost;
  94. this.serverConnector.GetConnectorConfig().LocalPort = l_port;
  95. Thread iceServerThread = new Thread(startIceServer);
  96. iceServerThread.Start();
  97. }
  98. /// <summary>
  99. /// 启动ICE服务器
  100. /// </summary>
  101. /// <returns></returns>
  102. internal abstract void StartIceServer();
  103. /// <summary>
  104. /// 获取传输类型
  105. /// </summary>
  106. /// <returns></returns>
  107. internal abstract int getCommType();
  108. /// <summary>
  109. /// 不阻塞主线程,启用新线程启动服务器
  110. /// </summary>
  111. private void startIceServer()
  112. {
  113. try
  114. {
  115. StartIceServer();
  116. }
  117. catch (Exception e)
  118. {
  119. Exception innerEx = e.InnerException;
  120. string msg = "";
  121. if (innerEx != null)
  122. {
  123. msg = innerEx.Message;
  124. }
  125. else
  126. {
  127. msg = e.Message;
  128. }
  129. log.Error("ICE服务器启动异常:" + msg);
  130. throw e;
  131. }
  132. finally
  133. {
  134. if (serverConnector.RecvConnector != null)
  135. {
  136. serverConnector.Destroy();
  137. }
  138. }
  139. }
  140. /// <summary>
  141. /// 增加了一个新会话
  142. /// </summary>
  143. /// <param name="session"></param>
  144. internal void OnNewSessionConnected(IceServerIoSession session)
  145. {
  146. log.Trace("服务器新增加了一个会话,来自地址IP【" + session.GetRemoteAddress() + "】");
  147. ISessionListener sessionListener = serverListener.OnSessionConnected(session);
  148. session.bindSessionListener(sessionListener);
  149. }
  150. /// <summary>
  151. /// 释放服务器资源
  152. /// </summary>
  153. public abstract void Dispose();
  154. public IceServerConfig getServerConfig()
  155. {
  156. return this.config;
  157. }
  158. public abstract void Broadcast(IMessage message);
  159. public abstract bool HasSession(ISession session);
  160. public abstract ISession GetSessionByID(string sessionID);
  161. public abstract IEnumerable<ISession> GetSessions();
  162. }
  163. }