Session.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using CommonLang;
  2. using CommonLang.ByteOrder;
  3. using CommonLang.Concurrent;
  4. using CommonLang.IO;
  5. using CommonLang.Log;
  6. using CommonLang.Net;
  7. using CommonLang.Protocol;
  8. using CommonServer.Server;
  9. using SuperSocket.SocketBase;
  10. using SuperSocket.SocketBase.Protocol;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. namespace CommonServer.SSocket.SuperSocket
  19. {
  20. public class Session : AppSession<Session, BinaryRequestInfo>, ISession
  21. {
  22. private Logger log = LoggerFactory.GetLogger("SSSession");
  23. private HashMap<string, object> mAttributes = new HashMap<string, object>();
  24. private INetPackageCodec mCodec;
  25. private bool mClosing = false;
  26. private SocketAcceptor mOwner;
  27. public Session() { }
  28. public string ID { get { return base.SessionID; } }
  29. public bool IsConnected { get { return base.Connected; } }
  30. public long TotalSentBytes { get; private set; }
  31. public long TotalRecvBytes { get; private set; }
  32. public ISessionListener Listener { get; private set; }
  33. internal void NewSessionConnected(SocketAcceptor owner, ISessionListener listener, INetPackageCodec codec)
  34. {
  35. this.Listener = listener;
  36. this.mCodec = codec;
  37. this.mOwner = owner;
  38. }
  39. internal void NewRequestReceived(int bytes, object obj)
  40. {
  41. this.TotalRecvBytes += bytes;
  42. this.Listener.OnReceivedMessage(this, obj);
  43. }
  44. public bool Send(IMessage message)
  45. {
  46. lock (this)
  47. {
  48. if (mClosing) return false;
  49. }
  50. ArraySegment<byte> segment;
  51. if (mOwner.DoEncodeInternal(this, message, out segment))
  52. {
  53. try
  54. {
  55. send_internal(segment.Array, segment.Offset, segment.Count);
  56. }
  57. catch (Exception err)
  58. {
  59. log.Error(err.Message, err);
  60. Disconnect(false);
  61. return false;
  62. }
  63. this.TotalSentBytes += segment.Count;
  64. mOwner.mTotalSentBytes += segment.Count;
  65. Listener.OnSentMessage(this, message);
  66. return true;
  67. }
  68. return false;
  69. }
  70. private void send_internal(byte[] buff, int offset, int length)
  71. {
  72. #if DEBUG
  73. //模拟网络卡//
  74. if (mOwner.EmulateLag)
  75. {
  76. mOwner.SendDelay(this, buff, offset, length);
  77. }
  78. else
  79. {
  80. base.Send(buff, offset, length);
  81. }
  82. #else
  83. base.Send(buff, offset, length);
  84. #endif
  85. }
  86. public bool SendResponse(CommonLang.Protocol.IMessage request, CommonLang.Protocol.IMessage response)
  87. {
  88. response.MessageID = request.MessageID;
  89. return Send(response);
  90. }
  91. public bool Disconnect(bool force)
  92. {
  93. lock (this)
  94. {
  95. if (mClosing) return false;
  96. mClosing = true;
  97. }
  98. base.Close(CloseReason.ClientClosing);
  99. return true;
  100. }
  101. protected override void OnSessionStarted()
  102. {
  103. base.OnSessionStarted();
  104. Listener.OnConnected(this);
  105. }
  106. protected override void OnSessionClosed(CloseReason reason)
  107. {
  108. base.OnSessionClosed(reason);
  109. Listener.OnDisconnected(this, false, reason.ToString());
  110. }
  111. protected override void HandleException(Exception e)
  112. {
  113. base.HandleException(e);
  114. if (Listener != null)
  115. {
  116. Listener.OnError(this, e);
  117. }
  118. }
  119. public string GetRemoteAddress()
  120. {
  121. return base.RemoteEndPoint.ToString();
  122. }
  123. #region ________Attributes________
  124. public object GetAttribute(string key)
  125. {
  126. return mAttributes.Get(key);
  127. }
  128. public void SetAttribute(string key, object value)
  129. {
  130. mAttributes.Put(key, value);
  131. }
  132. public void RemoveAttribute(string key)
  133. {
  134. mAttributes.RemoveByKey(key);
  135. }
  136. public bool ContainsAttribute(string key)
  137. {
  138. return mAttributes.ContainsKey(key);
  139. }
  140. public ICollection<string> GetAttributeKeys()
  141. {
  142. return mAttributes.Keys;
  143. }
  144. #endregion
  145. }
  146. }