PomeloClientFactory.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using CommonLang;
  2. using CommonLang.Property;
  3. using SimpleJson;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net.Sockets;
  9. using System.Text;
  10. namespace Pomelo.DotNetClient
  11. {
  12. public class PomeloClientFactory
  13. {
  14. public static PomeloClientFactory Instance
  15. {
  16. get; private set;
  17. }
  18. static PomeloClientFactory()
  19. {
  20. new PomeloClientFactory();
  21. }
  22. public PomeloClientFactory()
  23. {
  24. Instance = this;
  25. }
  26. public virtual PomeloClientAdapter CreateAdapter(IPomeloClientAdapterListener client)
  27. {
  28. return new PomeloTCP(client);
  29. }
  30. }
  31. public interface PomeloClientAdapter
  32. {
  33. Socket Session { get; }
  34. NetWorkState NetWorkState { get; }
  35. long TotalRecvBytes { get; }
  36. long TotalSentBytes { get; }
  37. void connect(string host, int port, int timeout, JsonObject user);
  38. void disconnect();
  39. void send(SendMessage msg);
  40. void disposing();
  41. void update(float deltime);
  42. bool resolve_route(RecvMessage msg, byte flag, out string route);
  43. void DoSendUpdate();
  44. long GetPing();
  45. }
  46. public interface IPomeloClientAdapterListener
  47. {
  48. void OnReceivedMessage(RecvMessage msg);
  49. void OnNetWorkStateChanged(NetWorkState st);
  50. void OnError(Exception err);
  51. void OnDisconnected(Socket session, string reason);
  52. void OnConnected(Socket session, JsonObject user);
  53. void ClearQueue();
  54. }
  55. public interface ISerializer
  56. {
  57. void Serialize(Stream stream, object msg);
  58. object Deserialize(Stream source, object value, Type type);
  59. }
  60. #region _Types_
  61. /// <summary>
  62. /// 网络状态
  63. /// </summary>
  64. public enum NetWorkState
  65. {
  66. [Desc("主动断开")]
  67. CLOSED,
  68. [Desc("正在连接")]
  69. CONNECTING,
  70. [Desc("连接成功")]
  71. CONNECTED,
  72. [Desc("被动断开")]
  73. DISCONNECTED,
  74. [Desc("连接超时")]
  75. TIMEOUT,
  76. [Desc("网络错误")]
  77. ERROR,
  78. [Desc("服务器踢人")]
  79. KICK,
  80. [Desc("初始状态")]
  81. NONE
  82. }
  83. public class PomeloException : Exception
  84. {
  85. private readonly int code;
  86. public int Code
  87. {
  88. get { return code; }
  89. }
  90. public string Route
  91. {
  92. get; internal set;
  93. }
  94. public bool Timeout
  95. {
  96. get; internal set;
  97. }
  98. public PomeloException(int code, string message)
  99. : base(message)
  100. {
  101. this.code = code;
  102. }
  103. public PomeloException(int code, string message, Exception inner)
  104. : base(message, inner)
  105. {
  106. this.code = code;
  107. }
  108. }
  109. public delegate object MessageDecoder(string route, Stream input);
  110. public delegate void MessageEncoder(string route, object msg, Stream output);
  111. public delegate bool ProcessMessageImmediately(string route, Stream data);
  112. public delegate bool ResponseValidater(object response, out int s2c_code, out string s2c_msg);
  113. public class PushHandler
  114. {
  115. public string Route { get { return route; } }
  116. public bool IsBinary { get { return callback_bin != null; } }
  117. internal readonly PomeloClient client;
  118. internal readonly string route;
  119. private readonly Action<object> callback;
  120. private readonly Action<byte[]> callback_bin;
  121. internal readonly MessageDecoder decoder;
  122. internal PushHandler(PomeloClient client, string route, Action<object> cb, MessageDecoder dc)
  123. {
  124. this.client = client;
  125. this.route = route;
  126. this.decoder = dc;
  127. this.callback = cb;
  128. this.callback_bin = null;
  129. }
  130. internal PushHandler(PomeloClient client, string route, Action<byte[]> cb)
  131. {
  132. this.client = client;
  133. this.route = route;
  134. this.decoder = null;
  135. this.callback = null;
  136. this.callback_bin = cb;
  137. }
  138. internal void Invoke(object data)
  139. {
  140. try
  141. {
  142. callback(data);
  143. }
  144. catch (Exception err) { client.onError(err); }
  145. }
  146. internal void InvokeBin(byte[] data)
  147. {
  148. try
  149. {
  150. callback_bin(data);
  151. }
  152. catch (Exception err) { client.onError(err); }
  153. }
  154. public void Clear()
  155. {
  156. client.remove_push(this);
  157. }
  158. }
  159. public struct RequestHandler
  160. {
  161. public PomeloClient Client { get { return client; } }
  162. public string Route { get { return route; } }
  163. public MessageDecoder Decoder { get { return decoder; } }
  164. public bool IsBinary { get { return callback_bin != null; } }
  165. private PomeloClient client;
  166. private string route;
  167. private Action<PomeloException, object> callback;
  168. private Action<PomeloException, byte[]> callback_bin;
  169. private MessageDecoder decoder;
  170. private long start_time_ms;
  171. internal RequestHandler(PomeloClient client, string route, Action<PomeloException, object> callback, Action<PomeloException, byte[]> callback_bin, MessageDecoder decoder)
  172. {
  173. this.client = client;
  174. this.route = route;
  175. this.decoder = decoder;
  176. this.callback = callback;
  177. this.callback_bin = callback_bin;
  178. this.start_time_ms = CUtils.CurrentTimeMS;
  179. }
  180. internal void Invoke(object data)
  181. {
  182. try
  183. {
  184. callback(null, data);
  185. }
  186. catch (Exception err) { client.onError(err); }
  187. }
  188. internal void InvokeBin(byte[] data)
  189. {
  190. try
  191. {
  192. callback_bin(null, data);
  193. }
  194. catch (Exception err) { client.onError(err); }
  195. }
  196. internal void Invoke(PomeloException err)
  197. {
  198. try
  199. {
  200. if (callback != null) callback(err, null);
  201. else if (callback_bin != null) callback_bin(err, null);
  202. }
  203. catch (Exception err2) { client.onError(err2); }
  204. }
  205. internal bool CheckTimeout(int timeout_ms, long current_time_ms)
  206. {
  207. if (start_time_ms + timeout_ms < current_time_ms)
  208. {
  209. return true;
  210. }
  211. return false;
  212. }
  213. }
  214. #endregion
  215. }