123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- 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<Message> 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<Message>();
-
- 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<jamMessageList.Count; i++)
- {
- SendMessage(jamMessageList[i]);
- }
- }
- jamMessageList.Clear();
- }
- //********** login end ************
- //********** join room start ************
- private void JoinLobby()
- {
- smartFox.Send(new JoinRoomRequest(Config.LOBBY_ROOM_NAME));
- }
- private void OnJoinRoom(BaseEvent evt)
- {
- traceParams(evt);
- Room room = (SFSRoom)evt.Params["room"];
- if(room.Name == Config.LOBBY_ROOM_NAME || room.GroupId == Config.CLAN_ROOM_PREFIX)
- {
- if(isFirstTimeJoinLobby)
- {
- isFirstTimeJoinLobby = false;
- loginResult(true);
- }
- }
- }
- private void OnJoinRoomError(BaseEvent evt)
- {
- traceParams(evt);
- // searchBattleResult(false, ERROR_CODE.JoinRoomError.GetHashCode());
- }
- //********** join room start ************
- private void OnExtension(BaseEvent evt)
- {
- string cmd = (string)evt.Params["cmd"];
- ISFSObject paramsObj = (ISFSObject)evt.Params["params"];
- Debuger.Log ("SFS Extension Recieved : "+cmd+" "+paramsObj.GetDump());
- Message msg = new Message(cmd, paramsObj);
- if(callBack != null)
- callBack(msg.GetMessagePackage());
- }
- public void traceParams(BaseEvent evt)
- {
- Debuger.LogWarning("+++++ "+evt.Type+" +++++");
- foreach(DictionaryEntry de in evt.Params)
- {
- Debuger.Log(de.Key+" : "+de.Value);
- }
- Debuger.LogWarning("-----------------");
- }
- }
|