SFSConnection.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Sfs2X;
  5. using Sfs2X.Core;
  6. using Sfs2X.Entities;
  7. using Sfs2X.Entities.Data;
  8. using Sfs2X.Logging;
  9. using Sfs2X.Requests;
  10. public class SFSConnection : IConnection
  11. {
  12. public enum ERROR_CODE
  13. {
  14. ConnectFailed = 1,
  15. ConnectLost = 2,
  16. LoginFailed = 3,
  17. JoinRoomError = 4,
  18. NoUserId = 5,
  19. }
  20. private SmartFox smartFox;
  21. private MessageManager.MessageCallBackDelegate callBack;
  22. private bool isFirstTimeJoinLobby;
  23. private List<Message> jamMessageList;
  24. private int reloginCount = 0;
  25. public SFSConnection ()
  26. {
  27. if (Application.isWebPlayer) {
  28. if (!Security.PrefetchSocketPolicy(Config.SERVER_ADDRESS, Config.SERVER_PORT, 500)) {
  29. Debuger.LogError("Security Exception. Policy file loading failed!");
  30. }
  31. }
  32. jamMessageList = new List<Message>();
  33. Reset();
  34. }
  35. public void Reset()
  36. {
  37. if(smartFox != null)
  38. {
  39. smartFox.RemoveAllEventListeners();
  40. try
  41. {
  42. smartFox.KillConnection();
  43. }
  44. catch(System.Exception e)
  45. {
  46. Debuger.LogException(e);
  47. }
  48. reloginCount = 0;
  49. }
  50. // Register for basic callbacks
  51. smartFox = new SmartFox(false);
  52. smartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection);
  53. smartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
  54. smartFox.AddEventListener(SFSEvent.LOGIN, OnLogin);
  55. smartFox.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
  56. smartFox.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom);
  57. smartFox.AddEventListener(SFSEvent.ROOM_JOIN_ERROR, OnJoinRoomError);
  58. smartFox.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtension);
  59. }
  60. public void ProcessEvents() {
  61. smartFox.ProcessEvents();
  62. CheckReconnect ();
  63. }
  64. public int myUserId
  65. {
  66. set
  67. {
  68. }
  69. get
  70. {
  71. if(smartFox.MySelf != null)
  72. return smartFox.MySelf.Id;
  73. return -1;
  74. }
  75. }
  76. public bool IsConnected
  77. {
  78. get{
  79. return smartFox.IsConnected;
  80. }
  81. }
  82. public void SetResponseMessageCallBack(MessageManager.MessageCallBackDelegate callBack)
  83. {
  84. this.callBack = callBack;
  85. }
  86. public void SendMessage(Message msg)
  87. {
  88. if(!smartFox.IsConnected)
  89. {
  90. jamMessageList.Add(msg);
  91. Connect();
  92. return;
  93. }
  94. else if(smartFox.IsConnecting || smartFox.MySelf == null)
  95. {
  96. jamMessageList.Add(msg);
  97. Debuger.LogError("Send message["+msg.cmd+"] while smartFox is connecting");
  98. return;
  99. }
  100. if(msg.cmd == Command.MessageArray)
  101. {
  102. if(msg.isRoomMsg)
  103. {
  104. smartFox.Send(new ExtensionRequest(msg.cmd, msg.data, smartFox.LastJoinedRoom));
  105. }
  106. else
  107. {
  108. smartFox.Send(new ExtensionRequest(msg.cmd, msg.data));
  109. }
  110. }
  111. else if(Command.IsRoomExtensionCommand(msg.cmd))
  112. {
  113. smartFox.Send(new ExtensionRequest(msg.cmd, msg.data, smartFox.LastJoinedRoom));
  114. }
  115. else
  116. {
  117. smartFox.Send(new ExtensionRequest(msg.cmd, msg.data));
  118. }
  119. }
  120. //********** connection start ************
  121. public void Connect()
  122. {
  123. isFirstTimeJoinLobby = true;
  124. if(smartFox.IsConnected)
  125. {
  126. Login();
  127. }
  128. else
  129. {
  130. if(Config.USE_LOCAL_SERVER)
  131. {
  132. Debuger.Log("connect to "+Config.LOCAL_SERVER_ADDRESS+":"+Config.SERVER_PORT);
  133. smartFox.Connect(Config.LOCAL_SERVER_ADDRESS, Config.SERVER_PORT);
  134. }
  135. else
  136. {
  137. Debuger.Log("connect to "+Config.SERVER_ADDRESS+":"+Config.SERVER_PORT);
  138. smartFox.Connect(Config.SERVER_ADDRESS, Config.SERVER_PORT);
  139. }
  140. }
  141. }
  142. private void OnConnection(BaseEvent evt)
  143. {
  144. bool success = (bool)evt.Params["success"];
  145. if (success) {
  146. Debuger.Log("Connection successful!");
  147. Login();
  148. } else {
  149. Debuger.LogError("Can't connect to server!");
  150. loginResult(false, ERROR_CODE.ConnectFailed.GetHashCode());
  151. Reset();
  152. }
  153. }
  154. private float lastLostTime;
  155. private void CheckReconnect()
  156. {
  157. if (!Session.GetInstance ().myUserData.isLogin)
  158. return;
  159. if(lastLostTime > 0)
  160. {
  161. if(GameTime.time - lastLostTime > 15f)
  162. {
  163. lastLostTime = GameTime.time;
  164. Connect ();
  165. }
  166. }
  167. }
  168. public void Disconnect()
  169. {
  170. smartFox.Disconnect();
  171. }
  172. public void OnConnectionLost(BaseEvent evt)
  173. {
  174. lastLostTime = GameTime.time;
  175. loginResult(false, ERROR_CODE.ConnectLost.GetHashCode());
  176. Debuger.LogError("Connection lost; reason: " + (string)evt.Params["reason"]);
  177. Reset();
  178. }
  179. //********** connection end ************
  180. //********** login start ************
  181. private void Login()
  182. {
  183. int id = Session.GetInstance ().myUserData.id;
  184. ISFSObject param = new SFSObject ();
  185. param.PutUtfString ("n", WWW.EscapeURL(Session.GetInstance ().myUserData.nick));
  186. param.PutInt ("c", Session.GetInstance().myUserData.clanId);
  187. smartFox.Send(new LoginRequest(id.ToString(), Config.VERSION_LABEL, Config.ZONE, param));
  188. }
  189. private void OnLogin(BaseEvent evt)
  190. {
  191. traceParams(evt);
  192. // JoinLobby();
  193. }
  194. private void OnLoginError(BaseEvent evt)
  195. {
  196. traceParams(evt);
  197. if(StringUtil.ToInt(evt.Params["errorCode"].ToString()) == 28 && reloginCount < 5)
  198. {
  199. Debuger.LogWarning("SQL Error Relogin");
  200. reloginCount++;
  201. Login();
  202. }
  203. else
  204. {
  205. loginResult(false, ERROR_CODE.LoginFailed.GetHashCode());
  206. Reset();
  207. }
  208. }
  209. private void loginResult(bool success, int errorCode=0)
  210. {
  211. ISFSObject data = new SFSObject();
  212. data.PutBool("success", success);
  213. if(!success)
  214. {
  215. lastLostTime = GameTime.time;
  216. data.PutInt("error", errorCode);
  217. Message msg = new Message(Command.ConnectionError, data);
  218. if(callBack != null)
  219. callBack(msg.GetMessagePackage());
  220. }
  221. else
  222. {
  223. lastLostTime = 0;
  224. for(int i=0; i<jamMessageList.Count; i++)
  225. {
  226. SendMessage(jamMessageList[i]);
  227. }
  228. }
  229. jamMessageList.Clear();
  230. }
  231. //********** login end ************
  232. //********** join room start ************
  233. private void JoinLobby()
  234. {
  235. smartFox.Send(new JoinRoomRequest(Config.LOBBY_ROOM_NAME));
  236. }
  237. private void OnJoinRoom(BaseEvent evt)
  238. {
  239. traceParams(evt);
  240. Room room = (SFSRoom)evt.Params["room"];
  241. if(room.Name == Config.LOBBY_ROOM_NAME || room.GroupId == Config.CLAN_ROOM_PREFIX)
  242. {
  243. if(isFirstTimeJoinLobby)
  244. {
  245. isFirstTimeJoinLobby = false;
  246. loginResult(true);
  247. }
  248. }
  249. }
  250. private void OnJoinRoomError(BaseEvent evt)
  251. {
  252. traceParams(evt);
  253. // searchBattleResult(false, ERROR_CODE.JoinRoomError.GetHashCode());
  254. }
  255. //********** join room start ************
  256. private void OnExtension(BaseEvent evt)
  257. {
  258. string cmd = (string)evt.Params["cmd"];
  259. ISFSObject paramsObj = (ISFSObject)evt.Params["params"];
  260. Debuger.Log ("SFS Extension Recieved : "+cmd+" "+paramsObj.GetDump());
  261. Message msg = new Message(cmd, paramsObj);
  262. if(callBack != null)
  263. callBack(msg.GetMessagePackage());
  264. }
  265. public void traceParams(BaseEvent evt)
  266. {
  267. Debuger.LogWarning("+++++ "+evt.Type+" +++++");
  268. foreach(DictionaryEntry de in evt.Params)
  269. {
  270. Debuger.Log(de.Key+" : "+de.Value);
  271. }
  272. Debuger.LogWarning("-----------------");
  273. }
  274. }