Пример #1
0
 public static void SaveState(SyncState state)
 {
     try
     {
         var json = JsonConvert.SerializeObject(state);
         var path = GetStateFilepath(state.SubsessionId);
         File.WriteAllText(path, json);
     }
     catch (Exception ex)
     {
     }
 }
Пример #2
0
        private void HandleStateUpdate(SyncResponse response)
        {
            _state = (SyncState)JsonConvert.DeserializeObject <SyncState>(response.Data.State.ToString());
            Debug.WriteLine(">> Received state update: " + _state);

            // Save state
            SaveState(_state);

            // Copy settings that come from the server
            OfftrackHistory.OfftrackLimit = _state.OfftrackLimit;

            this.OnStateUpdated();
        }
Пример #3
0
        private static void SyncLiveStatus(SyncState state)
        {
            foreach (var kvp in state.LiveStatus.ToList())
            {
                var address = kvp.Key;
                var live = kvp.Value;

                var user = SyncManager.Instance.FindUser(address);
                if (user != null)
                {
                    user.IsLive = live;
                }
            }
        }
        private static void SyncLiveStatus(SyncState state)
        {
            foreach (var kvp in state.LiveStatus.ToList())
            {
                var address = kvp.Key;
                var live    = kvp.Value;

                var user = SyncManager.Instance.FindUser(address);
                if (user != null)
                {
                    user.IsLive = live;
                }
            }
        }
Пример #5
0
        public static string SendUpdate(SyncState state)
        {
            try
            {
                var settings = Properties.Settings.Default;

                var url = settings.LiveAdminUrl;
                var key = settings.LiveAdminKey;

                var request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";

                var data = new LiveAdminData();
                data.Admins = state.Users.Select(LiveAdminData.LiveAdminUser.FromUser).ToList();
                data.Penalties = state.Penalties.Select(LiveAdminData.LiveAdminPenalty.FromPenalty)
                    .OrderByDescending(p => p.TimeGMT).ToList();

                var json = JsonConvert.SerializeObject(data);

                var post = string.Format("key={0}&ssid={1}&data={2}",
                    key,
                    state.SubsessionId,
                    System.Net.WebUtility.UrlEncode(json));
                var bytes = Encoding.UTF8.GetBytes(post);

                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = bytes.Length;

                using (var dataStream = request.GetRequestStream())
                {
                    dataStream.Write(bytes, 0, bytes.Length);
                }

                var response = request.GetResponse();
                using (var dataStream = response.GetResponseStream())
                {
                    using (var reader = new StreamReader(dataStream))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
Пример #6
0
        private SyncManager(Dispatcher dispatcher)
        {
            _dispatcher = dispatcher;

            _offlineUser = new User();
            _offlineUser.IsRegistered = true;
            _offlineUser.CustId       = 0;
            _offlineUser.Name         = "Local";
            _offlineUser.ShortName    = "L";
            _offlineUser.UserColor    = UserColor.Offline;
            _offlineUser.IsHost       = true;

            _status = ConnectionStatus.Disconnected;
            _state  = new SyncState(0);
            _users  = new UserContainerCollection();

            _users.Add(new UserContainer(_offlineUser));
        }
Пример #7
0
        private SyncManager(Dispatcher dispatcher)
        {
            _dispatcher = dispatcher;

            _offlineUser = new User();
            _offlineUser.IsRegistered = true;
            _offlineUser.CustId = 0;
            _offlineUser.Name = "Local";
            _offlineUser.ShortName = "L";
            _offlineUser.UserColor = UserColor.Offline;
            _offlineUser.IsHost = true;

            _status = ConnectionStatus.Disconnected;
            _state = new SyncState(0);
            _users = new UserContainerCollection();

            _users.Add(new UserContainer(_offlineUser));
        }
Пример #8
0
        public Task <ConnectionResult> Connect(string ip, int port, int custid, string username, string shortname, string password)
        {
            _name         = username;
            _shortname    = shortname;
            _password     = password;
            _custid       = custid;
            _subSessionId = Connection.Instance.SubSessionId.GetValueOrDefault();

            _state = new SyncState(_subSessionId);

            _address = string.Format("{0}:{1}", ip, port);
            string path = string.Format("ws://{0}/channel", _address);

            var tcs = new TaskCompletionSource <ConnectionResult>();

            try
            {
                // Create new websocket
                _client = new WebSocketClient(path);
                _client.ConnectTimeout = new TimeSpan(0, 10, 0);

                // Handle connection events
                _client.OnConnected  += OnConnected;
                _client.OnDisconnect += context => OnServerDisconnected();

                // Handle received event
                _client.OnReceive += context => OnReceive(context, tcs);

                // Connect and wait for connected event
                _client.Connect();
            }
            catch (Exception ex)
            {
                this.ResetStatus();

                tcs.SetResult(new ConnectionResult
                {
                    Success = false,
                    Message = "Unknown error during connection: " + ex.Message
                });
            }

            return(tcs.Task);
        }
Пример #9
0
        private static void SyncUserCameras(SyncState state)
        {
            foreach (var driver in Simulator.Instance.Drivers.ToList())
            {
                driver.UserCameras.Clear();
            }

            foreach (var kvp in state.WatchedDrivers.ToList())
            {
                var address = kvp.Key;
                var carId = kvp.Value;

                var user = SyncManager.Instance.FindUser(address);
                if (user != null)
                {
                    var driver = Simulator.Instance.Drivers.FromId(carId);
                    if (driver != null)
                    {
                        driver.UserCameras.Add(user);
                    }
                }
            }
        }
        private static void SyncUserCameras(SyncState state)
        {
            foreach (var driver in Simulator.Instance.Drivers.ToList())
            {
                driver.UserCameras.Clear();
            }

            foreach (var kvp in state.WatchedDrivers.ToList())
            {
                var address = kvp.Key;
                var carId   = kvp.Value;

                var user = SyncManager.Instance.FindUser(address);
                if (user != null)
                {
                    var driver = Simulator.Instance.Drivers.FromId(carId);
                    if (driver != null)
                    {
                        driver.UserCameras.Add(user);
                    }
                }
            }
        }
Пример #11
0
        public void Initialize(int port, string password, int adminId, long ssid)
        {
            this.Stop();

            // Find existing state
            var existing = LoadState(ssid);
            
            _state = existing ?? new SyncState(ssid);

            _password = password;
            _adminId = adminId;
            _subSessionId = ssid;

            // Start new server
            _server = new WebSocketServer(port, IPAddress.Any)
            {
                OnReceive = OnReceive,
                OnConnected = OnConnected,
                OnDisconnect = OnDisconnect,
                TimeOut = new TimeSpan(0, 5, 0)
            };

            _server.Start();
            this.Status = ServerStatus.Active;

            _localServerInfo = ServerInfo.Local(port, password);

            // Start ping loop
            _pingThread = new Thread(PingLoop);
            _pingThread.IsBackground = true;
            _pingThread.Start();

            // Start web-update thread
            _webThread = new Thread(WebUpdateLoop);
            _webThread.IsBackground = true;
            _webThread.Start();
        }
 public static void Update(SyncState state)
 {
     SyncUserCameras(state);
     SyncLiveStatus(state);
     SyncPenalties(state);
 }
 private static void SyncPenalties(SyncState state)
 {
 }
Пример #14
0
        public Task<ConnectionResult> Connect(string ip, int port, int custid, string username, string shortname, string password)
        {
            _name = username;
            _shortname = shortname;
            _password = password;
            _custid = custid;
            _subSessionId = Connection.Instance.SubSessionId.GetValueOrDefault();

            _state = new SyncState(_subSessionId);

            _address = string.Format("{0}:{1}", ip, port);
            string path = string.Format("ws://{0}/channel", _address);

            var tcs = new TaskCompletionSource<ConnectionResult>();

            try
            {
                // Create new websocket
                _client = new WebSocketClient(path);
                _client.ConnectTimeout = new TimeSpan(0, 10, 0);

                // Handle connection events
                _client.OnConnected += OnConnected;
                _client.OnDisconnect += context => OnServerDisconnected();

                // Handle received event
                _client.OnReceive += context => OnReceive(context, tcs);
                
                // Connect and wait for connected event
                _client.Connect();

            }
            catch (Exception ex)
            {
                this.ResetStatus();

                tcs.SetResult(new ConnectionResult
                    {
                        Success = false,
                        Message = "Unknown error during connection: " + ex.Message
                    });
            }

            return tcs.Task;
        }
Пример #15
0
 public static void SaveState(SyncState state)
 {
     try
     {
         var json = JsonConvert.SerializeObject(state);
         var path = GetStateFilepath(state.SubsessionId);
         File.WriteAllText(path, json);
     }
     catch (Exception ex)
     {
     }
 }
Пример #16
0
        private static void SyncPenalties(SyncState state)
        {

        }
Пример #17
0
 public static void Update(SyncState state)
 {
     SyncUserCameras(state);
     SyncLiveStatus(state);
     SyncPenalties(state);
 }
Пример #18
0
        private void HandleStateUpdate(SyncResponse response)
        {
            _state = (SyncState) JsonConvert.DeserializeObject<SyncState>(response.Data.State.ToString());
            Debug.WriteLine(">> Received state update: " + _state);

            // Save state
            SaveState(_state);

            // Copy settings that come from the server
            OfftrackHistory.OfftrackLimit = _state.OfftrackLimit;

            this.OnStateUpdated();
        }