Example #1
0
        //==================================================================
        private void DispatchProtocol(uint pid, uint index, object ptlObj)
        {
            PTLListenerHelper helper = null;

            if (index > 0)
            {
                int i = 0;
                for (i = 0; i < m_lstRspListener.Count; i++)
                {
                    helper = m_lstRspListener[i];
                    if (helper.index == index)
                    {
                        m_lstRspListener.RemoveAt(i);
                        helper.Dispatch(ptlObj);
                        break;
                    }
                }

                if (i == m_lstRspListener.Count)
                {
                    MyLogger.LogWarning(TAG, "DispatchProtocol() pid:{0}, index:{1} duplicated msg!", pid.ToString(), index);
                }
            }

            helper = m_mapPID2Listener[pid];
            if (helper != null)
            {
                helper.Dispatch(ptlObj);
            }
            else
            {
                MyLogger.LogWarning(TAG, "DispatchProtocol() pid:{0} no active listner!", pid.ToString());
            }
        }
Example #2
0
 /// <summary>
 /// Whether a gameobject is spawned by this pool
 /// </summary>
 /// <param name="item">an item</param>
 /// <returns>true if the item is spawned by this pool</returns>
 public bool IsSpawned(GameObject item)
 {
     if (!item)
     {
         if (DebugLog)
         {
             MyLogger.LogWarning("SmartPool (" + PoolName + ").IsSpawned: item is null!");
         }
         return(false);
     }
     return(mSpawned.Contains(item));
 }
Example #3
0
 public void OnPositionUpdatedFromNetwork(PositionUpdate update)
 {
     if (IsLocalPlayer)
     {
         MyLogger.LogWarning("Received position update from server for local player: " + update.Id);
     }
     else
     {
         transform.position = new Vector3(update.X, update.Y, update.Z);
         transform.rotation = Quaternion.Euler(new Vector3(update.RotX, update.RotY, update.RotZ));
     }
 }
Example #4
0
 /// <summary>
 /// Despawn a gameobject and add it to the stock
 /// </summary>
 /// <param name="item"></param>
 public void DespawnItem(GameObject item)
 {
     if (!item)
     {
         if (DebugLog)
         {
             MyLogger.LogWarning("SmartPool (" + PoolName + ").DespawnItem: item is null!");
         }
         return;
     }
     if (IsSpawned(item))
     {
                     #if UNITY_3
         item.active = false;
                     #else
         item.SetActive(false);
                     #endif
         //item.name = Prefab.name + "_stock";
         mSpawned.Remove(item);
         mPreStock.Push(item);
         mStock.Push(item);
         //从mDealyDespawn中移除
         mDealyDespawn.Find(dealyItem =>
         {
             if (dealyItem._obj.GetInstanceID() == item.GetInstanceID())
             {
                 mDealyDespawn.Remove(dealyItem);
                 return(true);
             }
             return(false);
         });
         if (DebugLog)
         {
             MyLogger.Log("SmartPool (" + PoolName + "): Despawning '" + item.name);
         }
     }
     else
     {
         if (!mStock.Contains(item) && !mPreStock.Contains(item))
         {
             GameObject.Destroy(item);
             if (DebugLog)
             {
                 MyLogger.LogWarning("SmartPool (" + PoolName + "): Cant Despawn" + item.name + "' because it's not managed by this pool! However, SmartPool destroyed it!");
             }
         }
         else
         {
             item.SetActive(false);
         }
     }
 }
Example #5
0
 /// <summary>
 /// Destroys a spawned item instead of despawning it
 /// </summary>
 /// <param name="item">the spawned item to destroy</param>
 public void KillItem(GameObject item)
 {
     if (!item)
     {
         if (DebugLog)
         {
             MyLogger.LogWarning("SmartPool (" + PoolName + ").KillItem: item is null!");
         }
         return;
     }
     mSpawned.Remove(item);
     Destroy(item);
 }
Example #6
0
    /// <summary>
    /// Spawn an item from a specific pool
    /// </summary>
    /// <param name="poolName">the pool's name</param>
    /// <returns>a gameobject or null if spawning failed</returns>
    public static GameObject Spawn(string poolName, Vector3 position, Quaternion rotation)
    {
        SmartPool P;

        if (_Pools.TryGetValue(poolName, out P))
        {
            return(P.SpawnItem(position, rotation));
        }
        else
        {
            MyLogger.LogWarning("SmartPool: No pool with name '" + poolName + "' found!");
            return(null);
        }
    }
Example #7
0
        //------------------------------------------------------------

        #region start server
        public bool Start(int port)
        {
            if (isRunning)
            {
                MyLogger.LogWarning(LOG_TAG_MAIN, "Start()", "cannot start duplicated Server!");
                return(false);
            }
            MyLogger.Log(LOG_TAG_MAIN, "Start()  port = {0}", port.ToString());

            DelAllSession();

            try
            {
                mLogicLastTicks   = DateTime.Now.Ticks;
                mRealTicksAtStart = mLogicLastTicks;

                //create Game Socket
                mGameSocket = new KCPSocket(0, 1);
                mGameSocket.AddReceiveListener(OnReceive);
                isRunning = true;

                //create game room
                mRoom = new FSPRoom();
                mRoom.Create();
                mRoomRPC = mRoom;

                //create  thread
                MyLogger.Log(LOG_TAG_MAIN, "Start()  create server thead");
                mThreadMain = new Thread(Thread_Main)
                {
                    IsBackground = true
                };
                mThreadMain.Start();
            }
            catch (Exception e)
            {
                MyLogger.LogError(LOG_TAG_MAIN, "Start() ", e.Message);
                Close();
                return(false);
            }

            //when user exit the game using stop button in UnityEditor, cannot release all resource in time
            //add listener here
#if UNITY_EDITOR
            UnityEditor.EditorApplication.playmodeStateChanged -= OnEditorPlayModeChanged;
            UnityEditor.EditorApplication.playmodeStateChanged += OnEditorPlayModeChanged;
#endif
            return(true);
        }
Example #8
0
        //------------------------------------------------------------

        private void OnReceive(byte[] buffer, int size, IPEndPoint remotePoint)
        {
            FSPDataC2S data = PBSerializer.NDeserialize <FSPDataC2S>(buffer);

            FSPSession session = GetSession(data.sid);

            if (session == null)
            {
                MyLogger.LogWarning(LOG_TAG_RECV, "DoReceive()", "unknown SID = " + data.sid);
                //player does not exist,reply nothing
                return;
            }
            this.Log("DoReceive() Receive Buffer, SID={0}, IP={1}, Size={2}", session.Id, remotePoint, buffer.Length);

            session.EndPoint = remotePoint;
            session.Receive(data);
        }
Example #9
0
        internal FSPSession AddSession(uint sid)
        {
            FSPSession s = GetSession(sid);

            if (s != null)
            {
                MyLogger.LogWarning(LOG_TAG_MAIN, "AddSession()", " SID used = " + sid);
                return(s);
            }
            MyLogger.Log(LOG_TAG_MAIN, "AddSession() SID = " + sid);

            s = new FSPSession(sid, mGameSocket);

            lock (m_ListSession)
            {
                m_ListSession.Add(s);
            }
            return(s);
        }
Example #10
0
 /// <summary>
 /// Whether a gameobject is managed by this pool
 /// </summary>
 /// <param name="item">an item</param>
 /// <returns>true if item is managed by this pool</returns>
 public bool IsManagedObject(GameObject item)
 {
     if (!item)
     {
         if (DebugLog)
         {
             MyLogger.LogWarning("SmartPool (" + PoolName + ").IsManagedObject: item is null!");
         }
         return(false);
     }
     if (mSpawned.Contains(item) || mStock.Contains(item) || mPreStock.Contains(item))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Example #11
0
        public void Close()
        {
            if (mSystemSocket != null)
            {
                try
                {
                    mSystemSocket.Shutdown(SocketShutdown.Both);
                }
                catch (Exception e)
                {
                    MyLogger.LogWarning(LOG_TAG, "Close() ", e.Message + e.StackTrace);
                }

                mSystemSocket.Close();
                mSystemSocket = null;
            }

            isActive = false;
            GC.SuppressFinalize(this);
        }
Example #12
0
        //------------------------------------------------------------
        #region ReceiveFrom,SendTo Function
        public int ReceiveFrom(byte[] buffer, int maxsize, ref IPEndPoint remoteEP)
        {
            int cnt = 0;

            EndPoint ip = null;

            if (!mEnableBlockOnRecv)
            {
                if (mSystemSocket.Available <= 0)
                {
                    return(0);
                }
            }


            if (mAddrFamily == AddressFamily.InterNetwork)
            {
                //In IPv4 ,the same as android
                ip  = IPUtils.GetIPEndPointAny(AddressFamily.InterNetwork, 0);
                cnt = mSystemSocket.ReceiveFrom(buffer, maxsize, SocketFlags.None, ref ip);

                if (cnt > 0 && remoteEP != null && !remoteEP.Equals(ip))
                {
                    MyLogger.LogWarning(LOG_TAG, "ReceiveFrom()", " receive msg from stranger IP:Port(" + ip + ")!");
                    return(0);
                }
            }
            else
            {
                //In IPv6
                ip  = remoteEP;
                cnt = mSystemSocket.ReceiveFrom(buffer, maxsize, SocketFlags.None, ref ip);
            }

            remoteEP = ip as IPEndPoint;



            return(cnt);
        }
Example #13
0
        //---------------------------------------------------------
        public bool AddPlayer(uint playerId, uint sid)
        {
            MyLogger.Log(LOG_TAG, "AddPlayer() playerId:{0}, sid:{1}", playerId.ToString(), sid);

            if (mState != FSPGameState.Create)
            {
                MyLogger.LogError(LOG_TAG, "AddPlayer() cannot create player in current state! State = {0}", mState.ToString());
                return(false);
            }

            FSPPlayer player = null;

            for (int i = 0; i < mPlayerList.Count; i++)
            {
                player = mPlayerList[i];
                if (player.Id == playerId)
                {
                    MyLogger.LogWarning(LOG_TAG, "AddPlayer()", " PlayerId used!replace with new id! PlayerId = " + playerId);
                    mPlayerList.RemoveAt(i);
                    FSPServer.Instance.DelSession(player.Sid);
                    player.Dispose();
                    break;
                }
            }

            if (mPlayerList.Count >= MaxPlayerNum)
            {
                MyLogger.LogError(LOG_TAG, "AddPlayer() maximum player reached! MaxPlayerNum = {0}", MaxPlayerNum.ToString());
                return(false);
            }

            FSPSession session = FSPServer.Instance.AddSession(sid);

            player = new FSPPlayer(playerId, mFSPParam.serverTimeout, session, OnPlayerReceive);
            mPlayerList.Add(player);

            return(true);
        }
Example #14
0
        protected async Task <JObject> GetResponse(string url)
        {
            url = url
                  .Replace("{from}", _configuration["FROM_CURRENCY"])
                  .Replace("{to}", _configuration["TO_CURRENCY"]);

            var client = new HttpClient();

            client.DefaultRequestHeaders.Add("User-Agent", "CustomAgent");

            var response = await client.GetAsync(url);

            if (response.IsSuccessStatusCode)
            {
                return(JObject.Parse(await response.Content.ReadAsStringAsync()));
            }
            else
            {
                var message = $"Het ophalen van de prijs geeft een ongeldige statuscode({response.StatusCode}). Message: { await response.Content.ReadAsStringAsync()}";
                _logger.LogWarning(message);
                throw new Exception(message);
            }
        }