DeltaCompressionWrite









How do I use Delta Compression Write
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: NetworkingPeer.cs Copy
3271     // calls OnPhotonSerializeView (through ExecuteOnSerialize)
3273     private Hashtable OnSerializeWrite(PhotonView view)
3274     {
3275         PhotonStream pStream = new PhotonStream( true, null );
3276         PhotonMessageInfo info = new PhotonMessageInfo( this.mLocalActor, this.ServerTimeInMilliSeconds, view );
3277
3278         // each view creates a list of values that should be sent
3279         view.SerializeView( pStream, info );
3280
3281         if( pStream.Count == 0 )
3282         {
3283             return null;
3284         }
3285
3286         object[] dataArray = pStream.data.ToArray();
3287
3288         if (view.synchronization == ViewSynchronization.UnreliableOnChange)
3289         {
3290             if (AlmostEquals(dataArray, view.lastOnSerializeDataSent))
3291             {
3292                 if (view.mixedModeIsReliable)
3293                 {
3294                     return null;
3295                 }
3296
3297                 view.mixedModeIsReliable = true;
3298                 view.lastOnSerializeDataSent = dataArray;
3299             }
3300             else
3301             {
3302                 view.mixedModeIsReliable = false;
3303                 view.lastOnSerializeDataSent = dataArray;
3304             }
3305         }
3306
3307         // EVDATA:
3308         // 0=View ID (an int, never compressed cause it's not in the data)
3309         // 1=data of observed type (different per type of observed object)
3310         // 2=compressed data (in this case, key 1 is empty)
3311         // 3=list of values that are actually null (if something was changed but actually IS null)
3312         Hashtable evData = new Hashtable();
3313         evData[(byte)0] = (int)view.viewID;
3314         evData[(byte)1] = dataArray; // this is the actual data (script or observed object)
3315
3316
3317         if (view.synchronization == ViewSynchronization.ReliableDeltaCompressed)
3318         {
3319             // compress content of data set (by comparing to view.lastOnSerializeDataSent)
3320             // the "original" dataArray is NOT modified by DeltaCompressionWrite
3321             // if something was compressed, the evData key 2 and 3 are used (see above)
3322             bool somethingLeftToSend = this.DeltaCompressionWrite(view, evData);
3323
3324             // buffer the full data set (for next compression)
3325             view.lastOnSerializeDataSent = dataArray;
3326
3327             if (!somethingLeftToSend)
3328             {
3329                 return null;
3330             }
3331         }
3332
3333         return evData;
3334     }
File name: NetworkingPeer.cs Copy
3425     private bool DeltaCompressionWrite(PhotonView view, Hashtable data)
3426     {
3427         if (view.lastOnSerializeDataSent == null)
3428         {
3429             return true; // all has to be sent
3430         }
3431
3432         // We can compress as we sent a full update previously (readers can re-use previous values)
3433         object[] lastData = view.lastOnSerializeDataSent;
3434         object[] currentContent = data[(byte)1] as object[];
3435
3436         if (currentContent == null)
3437         {
3438             // no data to be sent
3439             return false;
3440         }
3441
3442         if (lastData.Length != currentContent.Length)
3443         {
3444             // if new data isn't same length as before, we send the complete data-set uncompressed
3445             return true;
3446         }
3447
3448         object[] compressedContent = new object[currentContent.Length];
3449         int compressedValues = 0;
3450
3451         List valuesThatAreChangedToNull = new List();
3452         for (int index = 0; index < compressedContent.Length; index++)
3453         {
3454             object newObj = currentContent[index];
3455             object oldObj = lastData[index];
3456             if (this.ObjectIsSameWithInprecision(newObj, oldObj))
3457             {
3458                 // compress (by using null, instead of value, which is same as before)
3459                 compressedValues++;
3460                 // compressedContent[index] is already null (initialized)
3461             }
3462             else
3463             {
3464                 compressedContent[index] = currentContent[index];
3465
3466                 // value changed, we don't replace it with null
3467                 // new value is null (like a compressed value): we have to mark it so it STAYS null instead of being replaced with previous value
3468                 if (newObj == null)
3469                 {
3470                     valuesThatAreChangedToNull.Add(index);
3471                 }
3472             }
3473         }
3474
3475         // Only send the list of compressed fields if we actually compressed 1 or more fields.
3476         if (compressedValues > 0)
3477         {
3478             data.Remove((byte)1); // remove the original data (we only send compressed data)
3479
3480             if (compressedValues == currentContent.Length)
3481             {
3482                 // all values are compressed to null, we have nothing to send
3483                 return false;
3484             }
3485
3486             data[(byte)2] = compressedContent; // current, compressted data is moved to key 2 to mark it as compressed
3487             if (valuesThatAreChangedToNull.Count > 0)
3488             {
3489                 data[(byte)3] = valuesThatAreChangedToNull.ToArray(); // data that is actually null (not just cause we didn't want to send it)
3490             }
3491         }
3492
3493         return true; // some data was compressed but we need to send something
3494     }

DeltaCompressionWrite 87 lượt xem

Gõ tìm kiếm nhanh...