private void ProcessSession() { var now = DateTime.Now; // very firsts Session if (ActivityState == null) { // create fresh activity state ActivityState = new ActivityState(); ActivityState.SessionCount = 1; // first session TransferSessionPackage(); ActivityState.ResetSessionAttributes(now); ActivityState.Enabled = Enabled; WriteActivityStateInternal(); return; } var lastInterval = now - ActivityState.LastActivity.Value; if (lastInterval.Ticks < 0) { Logger.Error("Time Travel!"); ActivityState.LastActivity = now; WriteActivityStateInternal(); return; } // new session if (lastInterval > SessionInterval) { ActivityState.SessionCount++; ActivityState.LastInterval = lastInterval; TransferSessionPackage(); ActivityState.ResetSessionAttributes(now); WriteActivityStateInternal(); return; } // new subsession if (lastInterval > SubsessionInterval) { ActivityState.SubSessionCount++; ActivityState.SessionLenght += lastInterval; ActivityState.LastActivity = now; WriteActivityStateInternal(); Logger.Info("Started subsession {0} of session {1}", ActivityState.SubSessionCount, ActivityState.SessionCount); return; } }
// does not close stream received. Caller is responsible to close if it wants it internal static void SerializeToStream(Stream stream, ActivityState activity) { var writer = new BinaryWriter(stream); writer.Write(activity.EventCount); writer.Write(activity.SessionCount); writer.Write(activity.SubSessionCount); writer.Write(Util.SerializeTimeSpanToLong(activity.SessionLenght)); writer.Write(Util.SerializeTimeSpanToLong(activity.TimeSpent)); writer.Write(Util.SerializeDatetimeToLong(activity.LastActivity)); writer.Write(Util.SerializeDatetimeToLong(activity.CreatedAt)); writer.Write(Util.SerializeTimeSpanToLong(activity.LastInterval)); writer.Write(activity.Uuid.ToString()); writer.Write(activity.Enabled); writer.Write(activity.AskingAttribution); }
internal PackageBuilder(AdjustConfig adjustConfig, DeviceInfo deviceInfo, ActivityState activityState, DateTime createdAt) : this(adjustConfig, deviceInfo, createdAt) { ActivityState = activityState.Clone(); }
// does not close stream received. Caller is responsible to close if it wants it internal static ActivityState DeserializeFromStream(Stream stream) { ActivityState activity = null; var reader = new BinaryReader(stream); activity = new ActivityState(); activity.EventCount = reader.ReadInt32(); activity.SessionCount = reader.ReadInt32(); activity.SubSessionCount = reader.ReadInt32(); activity.SessionLenght = Util.DeserializeTimeSpanFromLong(reader.ReadInt64()); activity.TimeSpent = Util.DeserializeTimeSpanFromLong(reader.ReadInt64()); activity.LastActivity = Util.DeserializeDateTimeFromLong(reader.ReadInt64()); activity.CreatedAt = Util.DeserializeDateTimeFromLong(reader.ReadInt64()); activity.LastInterval = Util.DeserializeTimeSpanFromLong(reader.ReadInt64()); // create Uuid for migrating devices activity.Uuid = Util.TryRead(() => Guid.Parse(reader.ReadString()), () => Guid.NewGuid()); // default value of IsEnabled for migrating devices activity.Enabled = Util.TryRead(() => reader.ReadBoolean(), () => true); // default value for AskingAttribution for migrating devices activity.AskingAttribution = Util.TryRead(() => reader.ReadBoolean(), () => false); return activity; }