123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using Sfs2X.Entities.Data;
- public class BAServer
- {
- public enum ConnectionType
- {
- Local,
- Remote,
- }
- private MessageManager.MessageCallBackDelegate msgManCallBack;
- private BARoom currentRoom;
- private List<Message> messageStack;
- private ResponseWaiter resWaiter;
- private IConnection connection;
- private IConnection localConnection;
- private IConnection remoteConnection;
- public BAServer ()
- {
- remoteConnection = new SFSConnection();
- localConnection = new LocalConnection();
- messageStack = new List<Message>();
- resWaiter = new ResponseWaiter();
- SetConnection (ConnectionType.Remote);
- }
- public void SetMsgManCallBack(MessageManager.MessageCallBackDelegate callBack)
- {
- this.msgManCallBack = callBack;
- }
- public void SetConnection(ConnectionType type)
- {
- IConnection newConnection = null;
- switch(type)
- {
- case ConnectionType.Local:
- newConnection = localConnection;
- break;
- case ConnectionType.Remote:
- newConnection = remoteConnection;
- break;
- }
- if (connection != null) {
- connection.SetResponseMessageCallBack (null);
- // if(connection.IsConnected && connection != newConnection)
- // {
- // connection.Disconnect();
- // }
- }
- connection = newConnection;
- connection.SetResponseMessageCallBack (OnRemoteCallBack);
- }
- public void Connect()
- {
- if(!connection.IsConnected)
- connection.Connect ();
- }
- public bool IsConnected()
- {
- return connection.IsConnected;
- }
- public void EnterBase()
- {
- resWaiter.StartWait();
- // SetConnection (ConnectionType.Local);
- Message msg = new Message(Command.EnterBase, new SFSObject());
- Debuger.Log ("Send : ["+msg.cmd+"]"+msg.data.GetDump());
- // connection.SendMessage(msg);
- DealEnterBase(msg);
- }
- private void DealEnterBase(Message msg)
- {
- string mapName = MapData.MapID.Base.ToString ();
- ISFSArray arr = new SFSArray ();
- ISFSObject myObj = new SFSObject ();
- myObj.PutUtfString (UserSFSObjectLabel.ID, Session.myUserId.ToString());
- myObj.PutInt (UserSFSObjectLabel.TEAM_ID, TeamUtil.Team.Blue.GetHashCode());
- myObj.PutUtfString (UserSFSObjectLabel.NICK, WWW.EscapeURL(Session.GetInstance().myUserData.nick));
- arr.AddSFSObject(myObj);
- // ISFSObject oppObj = new SFSObject ();
- // oppObj.PutInt ("u", -1);
- // oppObj.PutInt ("t", TeamUtil.Team.Red.GetHashCode());
- // oppObj.PutUtfString ("i", "-1");
- // oppObj.PutUtfString ("n", RandomNPCNick());
- // arr.AddSFSObject (oppObj);
- ISFSObject data = new SFSObject();
- data.PutUtfString("m", mapName);
- data.PutUtfString ("c", Session.myUserId.ToString());
- data.PutBool ("s", true);
- data.PutSFSArray("a", arr);
- Message callBackMsg = new Message(Command.SearchRoom, data);
- OnRemoteCallBack(callBackMsg.GetMessagePackage());
- }
- public void SearchRoom()
- {
- resWaiter.StartWait();
- // SetConnection (ConnectionType.Local);
- ISFSObject data = new SFSObject();
- Message msg = new Message(Command.SearchRoom, data);
- Debuger.Log ("Send : ["+msg.cmd+"]"+msg.data.GetDump());
- connection.SendMessage(msg);
- }
- private void HandleReplay()
- {
- ISFSObject msg = ReplayManager.GetInstance ().GetRecord ();
- if (msg != null)
- RoomHandleResponse (msg);
- }
- private void SetRoom(MapData.MapID mapId, ISFSArray arr, int myUserId)
- {
- if (currentRoom != null)
- currentRoom.Dispose ();
- switch(mapId)
- {
- case MapData.MapID.Base:
- currentRoom = new BaseRoom(this);
- break;
- case MapData.MapID.Challenge:
- currentRoom = new ChallengeRoom(this);
- break;
- case MapData.MapID.Defense:
- currentRoom = new DefenceRoom (this);
- break;
- case MapData.MapID.Flag:
- currentRoom = new FlagRoom (this);
- break;
- }
- if(currentRoom == null)
- throw new System.Exception("Illegal Map id["+mapId.ToString()+"]");
- currentRoom.AddUser(arr);
- currentRoom.handlerCallBack = RoomHandleResponse;
- if (currentRoom.isRecord)
- ReplayManager.GetInstance ().StartRecord ();
- }
- private void UpdateRoom()
- {
- if(currentRoom != null)
- currentRoom.Update();
- }
- public void RoomHandleResponse(ISFSObject msg)
- {
- if (currentRoom != null && currentRoom.isRecord) {
- //Did not ignore zone message yet
- ReplayManager.GetInstance ().CheckRecord (msg);
- }
- Debuger.Log ("Recieve : "+msg.GetDump());
- if(msgManCallBack != null)
- msgManCallBack(msg);
- }
- public void Send(ISFSObject msg)
- {
- Message message = new Message (msg);
- Send (message);
- }
- public void Send(Message msg)
- {
- if(currentRoom == null)
- return;
- //TODO add to message trace
- Debuger.Log ("Send : ["+msg.cmd+"]"+msg.data.GetDump());
- for(int i=0; i<messageStack.Count; i++)
- {
- Message existMsg = messageStack[i];
- if(existMsg.cmd == msg.cmd && existMsg.AttempOverride(msg))
- {
- return;
- }
- }
- if(messageStack.Count < 50)
- messageStack.Add(msg);
- }
- private void CheckMessageStack()
- {
- if(resWaiter.isWaiting || messageStack.Count == 0)
- return;
- resWaiter.StartWait();
- int unknowMsg = 0;
- int zoneMsg = 1;
- int roomMsg = 2;
- int msgType = unknowMsg;
- ISFSArray arr = new SFSArray();
- for(int i=0; i<messageStack.Count; i++)
- {
- Message m = messageStack[i];
- int tempMsgType = Command.IsRoomExtensionCommand (m.cmd) ? roomMsg : zoneMsg;
- if (msgType == unknowMsg)
- {
- msgType = tempMsgType;
- }
- else if(msgType != tempMsgType)
- {
- continue;
- }
- ISFSObject obj = new SFSObject();
- obj.PutUtfString(Message.LABEL_COMMAND, m.cmd);
- obj.PutSFSObject(Message.LABEL_DATA, m.data);
- arr.AddSFSObject (obj);
- messageStack.RemoveAt (i);
- i--;
- }
- ISFSObject data = new SFSObject();
- data.PutSFSArray(Message.LABEL_DATA_ARRAY, arr);
- Message msg = new Message(Command.MessageArray, data);
- msg.isRoomMsg = (msgType == roomMsg);
- connection.SendMessage(msg);
- }
- private void OnRemoteCallBack(ISFSObject msg)
- {
- string cmd = msg.GetUtfString(Message.LABEL_COMMAND);
- ISFSObject data = msg.GetSFSObject(Message.LABEL_DATA);
- if(cmd == Command.MessageArray)
- {
- int sender = StringUtil.ToInt(data.GetUtfString(Message.LABEL_SENDER));
- int timeStamp = data.GetInt (Message.LABEL_TIMESTAMP);
- if(currentRoom.IsMyUserId(sender))
- resWaiter.StopWait();
- if (sender == currentRoom.aiTaker)
- currentRoom.AITakerMessageGot ();
- ISFSArray arr = data.GetSFSArray(Message.LABEL_DATA_ARRAY);
- for(int i=0; i<arr.Count; i++)
- {
- ISFSObject m = arr.GetSFSObject(i);
- currentRoom.HandleMassage(m, sender, timeStamp);
- }
- }
- else if(cmd == Command.SearchRoom)
- {
- resWaiter.StopWait();
- string mapId = data.GetUtfString("m");
- int aiTaker = StringUtil.ToInt(data.GetUtfString ("c"));
- bool isStarted = data.GetBool ("s");
- ISFSArray arr = data.GetSFSArray("a");
- SetRoom(MapData.GetMapIdByName(mapId), arr, connection.myUserId);
- currentRoom.aiTaker = aiTaker;
- RoomHandleResponse(msg);
- }
- }
- public void Update()
- {
- connection.ProcessEvents();
- if(ReplayManager.GetInstance().IsReplaying())
- {
- HandleReplay ();
- }
- else
- {
- UpdateRoom();
- CheckMessageStack();
- }
- }
- public void Disconnect()
- {
- if(connection != null)
- connection.Disconnect();
- }
- public bool IsWaiting()
- {
- return resWaiter.isWaiting;
- }
- public class ResponseWaiter
- {
- private bool _isWaiting;
- private float startTime;
- public void StartWait()
- {
- _isWaiting = true;
- startTime = GameTime.realtimeSinceStartup;
- }
- public void StopWait()
- {
- _isWaiting = false;
- }
- public bool isWaiting
- {
- get{
- return _isWaiting;
- }
- }
- public float timeWaited
- {
- get{
- return _isWaiting ? GameTime.realtimeSinceStartup - startTime : 0;
- }
- }
- }
- }
|