using UnityEngine; using System.Collections; using System.Collections.Generic; using Sfs2X; using Sfs2X.Core; using Sfs2X.Entities; using Sfs2X.Entities.Data; using Sfs2X.Logging; using Sfs2X.Requests; public class SFSConnection : IConnection { public enum ERROR_CODE { ConnectFailed = 1, ConnectLost = 2, LoginFailed = 3, JoinRoomError = 4, NoUserId = 5, } private SmartFox smartFox; private MessageManager.MessageCallBackDelegate callBack; private bool isFirstTimeJoinLobby; private List jamMessageList; private int reloginCount = 0; public SFSConnection () { if (Application.isWebPlayer) { if (!Security.PrefetchSocketPolicy(Config.SERVER_ADDRESS, Config.SERVER_PORT, 500)) { Debuger.LogError("Security Exception. Policy file loading failed!"); } } jamMessageList = new List(); Reset(); } public void Reset() { if(smartFox != null) { smartFox.RemoveAllEventListeners(); try { smartFox.KillConnection(); } catch(System.Exception e) { Debuger.LogException(e); } reloginCount = 0; } // Register for basic callbacks smartFox = new SmartFox(false); smartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection); smartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost); smartFox.AddEventListener(SFSEvent.LOGIN, OnLogin); smartFox.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError); smartFox.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom); smartFox.AddEventListener(SFSEvent.ROOM_JOIN_ERROR, OnJoinRoomError); smartFox.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtension); } public void ProcessEvents() { smartFox.ProcessEvents(); CheckReconnect (); } public int myUserId { set { } get { if(smartFox.MySelf != null) return smartFox.MySelf.Id; return -1; } } public bool IsConnected { get{ return smartFox.IsConnected; } } public void SetResponseMessageCallBack(MessageManager.MessageCallBackDelegate callBack) { this.callBack = callBack; } public void SendMessage(Message msg) { if(!smartFox.IsConnected) { jamMessageList.Add(msg); Connect(); return; } else if(smartFox.IsConnecting || smartFox.MySelf == null) { jamMessageList.Add(msg); Debuger.LogError("Send message["+msg.cmd+"] while smartFox is connecting"); return; } if(msg.cmd == Command.MessageArray) { if(msg.isRoomMsg) { smartFox.Send(new ExtensionRequest(msg.cmd, msg.data, smartFox.LastJoinedRoom)); } else { smartFox.Send(new ExtensionRequest(msg.cmd, msg.data)); } } else if(Command.IsRoomExtensionCommand(msg.cmd)) { smartFox.Send(new ExtensionRequest(msg.cmd, msg.data, smartFox.LastJoinedRoom)); } else { smartFox.Send(new ExtensionRequest(msg.cmd, msg.data)); } } //********** connection start ************ public void Connect() { isFirstTimeJoinLobby = true; if(smartFox.IsConnected) { Login(); } else { if(Config.USE_LOCAL_SERVER) { Debuger.Log("connect to "+Config.LOCAL_SERVER_ADDRESS+":"+Config.SERVER_PORT); smartFox.Connect(Config.LOCAL_SERVER_ADDRESS, Config.SERVER_PORT); } else { Debuger.Log("connect to "+Config.SERVER_ADDRESS+":"+Config.SERVER_PORT); smartFox.Connect(Config.SERVER_ADDRESS, Config.SERVER_PORT); } } } private void OnConnection(BaseEvent evt) { bool success = (bool)evt.Params["success"]; if (success) { Debuger.Log("Connection successful!"); Login(); } else { Debuger.LogError("Can't connect to server!"); loginResult(false, ERROR_CODE.ConnectFailed.GetHashCode()); Reset(); } } private float lastLostTime; private void CheckReconnect() { if (!Session.GetInstance ().myUserData.isLogin) return; if(lastLostTime > 0) { if(GameTime.time - lastLostTime > 15f) { lastLostTime = GameTime.time; Connect (); } } } public void Disconnect() { smartFox.Disconnect(); } public void OnConnectionLost(BaseEvent evt) { lastLostTime = GameTime.time; loginResult(false, ERROR_CODE.ConnectLost.GetHashCode()); Debuger.LogError("Connection lost; reason: " + (string)evt.Params["reason"]); Reset(); } //********** connection end ************ //********** login start ************ private void Login() { int id = Session.GetInstance ().myUserData.id; ISFSObject param = new SFSObject (); param.PutUtfString ("n", WWW.EscapeURL(Session.GetInstance ().myUserData.nick)); param.PutInt ("c", Session.GetInstance().myUserData.clanId); smartFox.Send(new LoginRequest(id.ToString(), Config.VERSION_LABEL, Config.ZONE, param)); } private void OnLogin(BaseEvent evt) { traceParams(evt); // JoinLobby(); } private void OnLoginError(BaseEvent evt) { traceParams(evt); if(StringUtil.ToInt(evt.Params["errorCode"].ToString()) == 28 && reloginCount < 5) { Debuger.LogWarning("SQL Error Relogin"); reloginCount++; Login(); } else { loginResult(false, ERROR_CODE.LoginFailed.GetHashCode()); Reset(); } } private void loginResult(bool success, int errorCode=0) { ISFSObject data = new SFSObject(); data.PutBool("success", success); if(!success) { lastLostTime = GameTime.time; data.PutInt("error", errorCode); Message msg = new Message(Command.ConnectionError, data); if(callBack != null) callBack(msg.GetMessagePackage()); } else { lastLostTime = 0; for(int i=0; i