IceClientNetSession.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using CommonNetwork_ICE.Util;
  7. using CommonLang.Protocol;
  8. using CommonNetwork_ICE.Common;
  9. using Slice;
  10. using CommonNetwork_ICE.Client;
  11. using CommonNetwork.Net;
  12. using System.Collections;
  13. using CommonLang.IO;
  14. using CommonLang.Log;
  15. using System.Net;
  16. namespace CommonNetwork_ICE.Session
  17. {
  18. /// <summary>
  19. /// 网络会话对象实现,通过会话对象可以发送消息,存储会话级变量
  20. /// </summary>
  21. public abstract class IceClientNetSession : INetSession
  22. {
  23. protected string mHost = "127.0.0.1";
  24. protected int mPort = 80;
  25. protected IceClientConnector iceConnector = null;
  26. protected INetPackageCodec mCodec;
  27. protected INetSessionListener mListener;
  28. private IExternalizableFactory sl;
  29. protected IPEndPoint mRemoteAddress;
  30. private Hashtable mAttributes = new Hashtable();
  31. public long mSendBytes = 0;
  32. public long mRecvBytes = 0;
  33. public long mSendPacks = 0;
  34. public long mRecvPacks = 0;
  35. private object closeLock = new object();
  36. public IceClientNetSession()
  37. {
  38. }
  39. public void SetBattleMessageFactory(IExternalizableFactory externalizableFactory)
  40. {
  41. sl = externalizableFactory;
  42. }
  43. public long TotalSentBytes { get { return mSendBytes; } }
  44. public long TotalRecvBytes { get { return mRecvBytes; } }
  45. public long TotalSentPackages { get { return mSendPacks; } }
  46. public long TotalRecvPackages { get { return mRecvPacks; } }
  47. public string URL { get { return (mHost + ":" + mPort); } }
  48. public string Host { get { return mHost; } }
  49. public int Port { get { return mPort; } }
  50. public IPEndPoint RemoteAddress { get { return mRemoteAddress; } }
  51. /// <summary>
  52. /// 判断当前网络是否已经连接
  53. /// </summary>
  54. /// <returns></returns>
  55. public bool IsConnected
  56. {
  57. get {
  58. if(iceConnector == null)
  59. {
  60. return false;
  61. }
  62. return iceConnector.IsConnected;
  63. }
  64. }
  65. public abstract bool Open(string url, INetPackageCodec codec, INetSessionListener listener);
  66. public bool Close()
  67. {
  68. bool ret = false;
  69. lock (closeLock)
  70. {
  71. if(iceConnector == null)
  72. {
  73. return true;
  74. }
  75. }
  76. iceConnector.Close();
  77. iceConnector = null;
  78. return ret;
  79. }
  80. public void Dispose()
  81. {
  82. Close();
  83. }
  84. //-------------------------------------------------------------------------------------
  85. /// <summary>
  86. /// 发送一个消息,该方法将立即返回。
  87. /// </summary>
  88. /// <param name="data"></param>
  89. public void Send(Object data)
  90. {
  91. if (data == null)
  92. {
  93. return;
  94. }
  95. if (IsConnected)
  96. {
  97. iceConnector.SendMessage(data as IMessage);
  98. }
  99. }
  100. public void SendResponse(IMessage rsponse, int requestMessageID)
  101. {
  102. rsponse.MessageID = requestMessageID;
  103. Send(rsponse);
  104. }
  105. //-------------------------------------------------------------------------------------
  106. public void PutAttribute(Object key, Object value)
  107. {
  108. mAttributes[key] = value;
  109. }
  110. public Object GetAttribute(Object key)
  111. {
  112. return mAttributes[key];
  113. }
  114. override public string ToString()
  115. {
  116. return "Session[" + URL + "](" + GetHashCode() + ")";
  117. }
  118. //-------------------------------------------------------------------------------------
  119. public void onClose()
  120. {
  121. if (mListener != null)
  122. {
  123. mListener.sessionClosed(this);
  124. }
  125. if (mOnSessionClosed != null)
  126. {
  127. mOnSessionClosed.Invoke(this);
  128. }
  129. }
  130. public void onOpen()
  131. {
  132. mListener.sessionOpened(this);
  133. if (mOnSessionOpened != null)
  134. {
  135. mOnSessionOpened.Invoke(this);
  136. }
  137. }
  138. public void onReceive(Object message, int recvBytes)
  139. {
  140. mRecvPacks++;
  141. mRecvBytes += recvBytes;
  142. mListener.messageReceived(this, message);
  143. if (mOnMessageReceived != null)
  144. {
  145. mOnMessageReceived.Invoke(this, message);
  146. }
  147. }
  148. public void onSent(Object message, int sentBytes)
  149. {
  150. mSendPacks++;
  151. mSendBytes += sentBytes;
  152. mListener.messageSent(this, message);
  153. if (mOnMessageSent != null)
  154. {
  155. mOnMessageSent.Invoke(this, message);
  156. }
  157. }
  158. public void onException(Exception err)
  159. {
  160. mListener.onError(this, err);
  161. if (mOnError != null)
  162. {
  163. mOnError.Invoke(this, err);
  164. }
  165. }
  166. //-------------------------------------------------------------------------------------
  167. private OnSessionOpenedHandler mOnSessionOpened;
  168. private OnSessionClosedHandler mOnSessionClosed;
  169. private OnMessageReceivedHandler mOnMessageReceived;
  170. private OnMessageSentHandler mOnMessageSent;
  171. private OnErrorHandler mOnError;
  172. private OnUpdateHandler mOnUpdate;
  173. public event OnSessionOpenedHandler OnSessionOpened { add { mOnSessionOpened += value; } remove { mOnSessionOpened -= value; } }
  174. public event OnSessionClosedHandler OnSessionClosed { add { mOnSessionClosed += value; } remove { mOnSessionClosed -= value; } }
  175. public event OnMessageReceivedHandler OnMessageReceived { add { mOnMessageReceived += value; } remove { mOnMessageReceived -= value; } }
  176. public event OnMessageSentHandler OnMessageSent { add { mOnMessageSent += value; } remove { mOnMessageSent -= value; } }
  177. public event OnErrorHandler OnError { add { mOnError += value; } remove { mOnError -= value; } }
  178. public event OnUpdateHandler OnUpdate { add { mOnUpdate += value; } remove { mOnUpdate -= value; } }
  179. /// <summary>
  180. /// Ice传输解码器
  181. /// </summary>
  182. internal class IceClientMessageCodecImpl : IceMessageCodec
  183. {
  184. public static int DEFAULT_BUFFER_SIZE = 1024;
  185. private INetPackageCodec codec = null;
  186. private IceClientNetSession client;
  187. public IceClientMessageCodecImpl(INetPackageCodec codec, IceClientNetSession client)
  188. {
  189. this.codec = codec;
  190. this.client = client;
  191. }
  192. public bool doDecode(TransMessage transMessage, out CommonLang.Protocol.IMessage message)
  193. {
  194. int length = transMessage.length;
  195. using (MemoryStream ms = new MemoryStream(transMessage.data))
  196. {
  197. object msg;
  198. if (codec.doDecode(client, ms, out msg))
  199. {
  200. message = msg as IMessage;
  201. return true;
  202. }
  203. }
  204. message = null;
  205. return false;
  206. }
  207. public void doEncode(CommonLang.Protocol.IMessage message, out TransMessage transMessage)
  208. {
  209. transMessage = new TransMessage();
  210. IMessage nm = (IMessage)message;
  211. using (MemoryStream ms = new MemoryStream(DEFAULT_BUFFER_SIZE))
  212. {
  213. if (codec.doEncode(client, ms, message))
  214. {
  215. int length = (int)ms.Position;
  216. transMessage.data = new byte[length];
  217. Array.Copy(ms.GetBuffer(), transMessage.data, length);
  218. transMessage.length = length;
  219. transMessage.type = 1;
  220. }
  221. }
  222. }
  223. }
  224. }
  225. }