Beispiel #1
0
        private void DownloadMapDataById(Guid mapId, ServerEventHandler <MapData> callback)
        {
            MapData mapData = null;
            Error   error   = new Error();

            string dataPath = m_persistentDataPath + "/Maps/";
            string filePath = dataPath + mapId + ".data";

            if (!File.Exists(filePath))
            {
                error.Code = StatusCode.NotFound;

                if (m_lag == 0)
                {
                    callback(error, mapData);
                }
                else
                {
                    Job.Submit(() => { Thread.Sleep(m_lag); return(null); }, result => callback(error, mapData));
                }
            }
            else
            {
                Job.Submit(() =>
                {
                    error.Code = StatusCode.OK;
                    ProtobufSerializer serializer = null;
                    var pool = Dependencies.Serializer;
                    try
                    {
                        if (pool != null)
                        {
                            serializer = pool.Acquire();
                        }

                        byte[] mapDataBytes = File.ReadAllBytes(filePath);
                        mapData             = serializer.Deserialize <MapData>(mapDataBytes);
                    }
                    catch (Exception e)
                    {
                        error.Code    = StatusCode.UnhandledException;
                        error.Message = e.Message;
                    }
                    finally
                    {
                        if (serializer != null && pool != null)
                        {
                            pool.Release(serializer);
                        }
                    }

                    return(null);
                },
                           result =>
                {
                    callback(error, mapData);
                });
            }
        }
Beispiel #2
0
        public void GetMaps(Guid clientId, ServerEventHandler <MapInfo[]> callback)
        {
            MapInfo[] mapsInfo = null;
            Error     error    = new Error();

            error.Code = StatusCode.OK;

            try
            {
                string dataPath = m_persistentDataPath + "/Maps/";

                if (Directory.Exists(dataPath))
                {
                    string[] filePath = Directory.GetFiles(dataPath, "*.info", SearchOption.TopDirectoryOnly);
                    mapsInfo = new MapInfo[filePath.Length];
                    for (int i = 0; i < filePath.Length; ++i)
                    {
                        byte[] mapInfoBytes = File.ReadAllBytes(filePath[i]);
                        mapsInfo[i] = m_serializer.Deserialize <MapInfo>(mapInfoBytes);
                    }
                }
                else
                {
                    mapsInfo = new MapInfo[0];
                }
            }
            catch (Exception e)
            {
                error.Code    = StatusCode.UnhandledException;
                error.Message = e.Message;
            }

            if (m_lag == 0)
            {
                callback(error, mapsInfo);
            }
            else
            {
                Job.Submit(() => { Thread.Sleep(m_lag); return(null); }, result => callback(error, mapsInfo));
            }
        }
Beispiel #3
0
        private void DownloadMapData(ServerEventHandler <MapData> callback)
        {
            DownloadMapDataById(m_room.MapInfo.Id, (error, mapDataBytes) =>
            {
                MapData mapData = null;
                try
                {
                    if (!HasError(error))
                    {
                        mapData = m_serializer.Deserialize <MapData>(mapDataBytes);
                    }
                }
                catch (Exception e)
                {
                    error = new Error(StatusCode.UnhandledException)
                    {
                        Message = e.ToString()
                    };
                }

                callback(error, mapData);
            });
        }
        private void Call(RemoteCall rpc, Action <Error, RemoteResult> callback)
        {
            byte[] rpcSerialized = m_serializer.Serialize(rpc);
            m_protocol.BeginRequest(rpcSerialized, null, (requestError, response, userState) =>
            {
                RemoteResult result;
                Error error;
                if (requestError == LowProtocol <ClientSocket> .ErrorOK)
                {
                    try
                    {
                        result = m_serializer.Deserialize <RemoteResult>(response);
                        error  = result.Error;
                    }
                    catch (Exception e)
                    {
                        result = new RemoteResult();
                        error  = new Error(StatusCode.UnhandledException)
                        {
                            Message = e.ToString()
                        };
                    }
                }
                else
                {
                    result = new RemoteResult();
                    if (requestError == LowProtocol <ClientSocket> .ErrorTimeout)
                    {
                        error = new Error(StatusCode.RequestTimeout);
                    }
                    else if (requestError == LowProtocol <ClientSocket> .ErrorClosed)
                    {
                        error = new Error(StatusCode.ConnectionClosed);
                    }
                    else
                    {
                        throw new NotSupportedException("unknow requestError");
                    }
                }

                callback(error, result);
            },
                                    requestSent => { });
        }
        protected virtual void OnMessage(ILowProtocol sender, byte[] args)
        {
            RemoteEvent evt = m_serializer.Deserialize <RemoteEvent>(args);

            OnRemoteEvent(evt);
        }
Beispiel #6
0
        public void Load(byte[] bytes, Action done)
        {
            if (m_isBusy)
            {
                throw new InvalidOperationException("m_isBusy");
            }

            m_isBusy = true;
            m_progressIndicator.IsVisible = true;
            m_progressIndicator.SetText("LOADING...");

            for (int i = 0; i < m_mapCameras.Count; ++i)
            {
                m_mapCameras[i].IsOn = false;
            }

            m_job.Submit(
                () =>
            {
                try
                {
                    ProtobufSerializer serializer = null;
                    try
                    {
                        var pool = Dependencies.Serializer;
                        if (pool != null)
                        {
                            serializer = pool.Acquire();
                            m_map      = serializer.Deserialize <MapRoot>(bytes);
                        }
                    }
                    finally
                    {
                        if (serializer != null)
                        {
                            var pool = Dependencies.Serializer;
                            if (pool != null)
                            {
                                pool.Release(serializer);
                            }
                        }
                    }

                    CalculateBounds();

                    //how to make sure than no one accessing cameras during background thread job ?
                    for (int i = 0; i < m_mapCameras.Count; ++i)
                    {
                        m_mapCameras[i].Map = m_map;
                    }
                }
                catch (Exception e)
                {
                    return(e);
                }

                return(null);
            },
                result =>
            {
                if (result is Exception)
                {
                    Debug.LogError(result.ToString());
                }

                m_isBusy = false;
                m_progressIndicator.IsVisible = false;

                if (Loaded != null)
                {
                    Loaded(this, EventArgs.Empty);
                }

                if (done != null)
                {
                    done();
                }

                for (int i = 0; i < m_mapCameras.Count; ++i)
                {
                    m_mapCameras[i].IsOn = m_isOn;
                }
            });
        }
        private void InitEngine(Guid clientId, ServerEventHandler callback)
        {
            if (m_engine != null)
            {
                MatchFactory.DestroyMatchEngine(m_engine);
            }

            m_engine = null;

            DownloadMapData(m_room.MapInfo.Id, (error, mapData) =>
            {
                if (HasError(error))
                {
                    if (callback != null)
                    {
                        callback(error);
                    }
                }
                else
                {
                    m_job.Submit(() =>
                    {
                        ProtobufSerializer serializer = null;
                        MapRoot mapRoot = null;
                        try
                        {
                            var pool = Dependencies.Serializer;
                            if (pool != null)
                            {
                                serializer = pool.Acquire();
                                mapRoot    = serializer.Deserialize <MapRoot>(mapData.Bytes);
                            }
                        }
                        finally
                        {
                            if (serializer != null)
                            {
                                var pool = Dependencies.Serializer;
                                if (pool != null)
                                {
                                    pool.Release(serializer);
                                }
                            }
                        }

                        return(mapRoot);
                    },
                                 result =>
                    {
                        MapRoot mapRoot = (MapRoot)result;

                        IMatchEngine engine = MatchFactory.CreateMatchEngine(mapRoot, m_room.Players.Count);

                        Dictionary <int, VoxelAbilities>[] allAbilities = new Dictionary <int, VoxelAbilities> [m_room.Players.Count];
                        for (int i = 0; i < m_room.Players.Count; ++i)
                        {
                            allAbilities[i] = m_abilities[m_room.Players[i]].ToDictionary(a => a.Type);
                        }

                        if (m_replay == null)
                        {
                            m_replay = MatchFactory.CreateReplayRecorder();
                        }

                        //Zero is neutral
                        for (int i = 0; i < m_room.Players.Count; ++i)
                        {
                            Guid playerGuid = m_room.Players[i];
                            engine.RegisterPlayer(m_room.Players[i], i, allAbilities);
                        }
                        engine.CompletePlayerRegistration();

                        m_prevTickTime = Time.realtimeSinceStartup;
                        m_engine       = engine;

                        if (callback != null)
                        {
                            callback(error);
                        }

                        m_pingTimer.Ping(clientId);

                        if (Ping != null)
                        {
                            Ping(new Error(StatusCode.OK), new RTTInfo());
                        }
                    });
                }
            });
        }