Exemplo n.º 1
0
        /// <summary>
        ///     Collets changes in the profile, and sends them to client after delay
        /// </summary>
        /// <param name="profile"></param>
        /// <param name="delay"></param>
        /// <returns></returns>
        private IEnumerator SendUpdatesToClient(ObservableServerProfile profile, float delay)
        {
            // Wait for the delay
            if (delay > 0.01f)
            {
                yield return(new WaitForSecondsRealtime(delay));
            }
            else
            {
                yield return(null);
            }

            // Remove value from debounced updates
            _debouncedClientUpdates.Remove(profile.Username);

            if (profile.ClientPeer == null || !profile.ClientPeer.IsConnected)
            {
                // If client is not connected, and we don't need to send him profile updates
                profile.ClearUpdates();
                yield break;
            }

            using (var ms = new MemoryStream()) {
                using (var writer = new EndianBinaryWriter(EndianBitConverter.Big, ms)) {
                    profile.GetUpdates(writer);
                    profile.ClearUpdates();
                }

                profile.ClientPeer.SendMessage(
                    MessageHelper.Create((short)MsfOpCodes.UpdateClientProfile, ms.ToArray()),
                    DeliveryMethod.ReliableSequenced);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Sends a request to server, retrieves all profile values, and applies them to a provided
        ///     profile
        /// </summary>
        public void FillProfileValues(ObservableServerProfile profile, SuccessCallback callback,
                                      IClientSocket connection)
        {
            if (!connection.IsConnected)
            {
                callback.Invoke(false, "Not connected");
                return;
            }

            connection.SendMessage((short)MsfOpCodes.ServerProfileRequest, profile.Username, (status, response) => {
                if (status != ResponseStatus.Success)
                {
                    callback.Invoke(false, response.AsString("Unknown error"));
                    return;
                }

                // Use the bytes received, to replicate the profile
                profile.FromBytes(response.AsBytes());

                profile.ClearUpdates();

                _profiles[profile.Username] = profile;

                profile.ModifiedInServer += serverProfile => { OnProfileModified(profile, connection); };

                profile.Disposed += OnProfileDisposed;

                callback.Invoke(true, null);
            });
        }