void ForAllUsers(Action <ulong> action) { RoomMap.LockReading(delegate() { foreach (ChatRoom room in RoomMap.Values) { if (room.Active && !IsCommandRoom(room.Kind)) { room.Members.LockReading(delegate() { foreach (ulong id in room.Members) { action(id); } }); if (room.Invites != null) { foreach (ulong id in room.Invites.Keys) { action(id); } } if (room.Verified != null) { foreach (ulong id in room.Verified.Keys) { action(id); } } } } }); }
public void SaveLocal() { // create temp, write buddy list string tempPath = Core.GetTempPath(); byte[] key = Utilities.GenerateKey(Core.StrongRndGen, 256); using (IVCryptoStream crypto = IVCryptoStream.Save(tempPath, key)) { PacketStream stream = new PacketStream(crypto, Network.Protocol, FileAccess.Write); BuddyList.LockReading(() => { foreach (OpBuddy buddy in BuddyList.Values) { stream.WritePacket(buddy); } }); IgnoreList.LockReading(() => // we need to ensure we have name/key for each ignore { foreach (OpBuddy ignore in IgnoreList.Values) { stream.WritePacket(ignore); } }); } byte[] publicEncryptedKey = Core.User.Settings.KeyPair.Encrypt(key, false); //private key required to decrypt buddy list Cache.UpdateLocal(tempPath, publicEncryptedKey, null); }
void Core_SecondTimer() { // need keep alives because someone else might have IM window open while we have it closed // send keep alives every x secs if (Core.TimeNow.Second % SessionTimeout == 0) { foreach (var userID in ActiveUsers) { IMStatus status = null; if (IMMap.SafeTryGetValue(userID, out status)) { foreach (ushort client in status.TTL.Keys) { if (status.TTL[client].Value > 0) { RudpSession session = Network.RudpControl.GetActiveSession(userID, client); if (session != null) { status.TTL[client].Value = SessionTimeout * 2; session.SendData(ServiceID, 0, new IMKeepAlive()); } } } } } } // timeout sessions IMMap.LockReading(delegate() { foreach (IMStatus status in IMMap.Values) { foreach (BoxInt ttl in status.TTL.Values) { if (ttl.Value > 0) { ttl.Value--; } } } }); }
void Core_MinuteTimer() { // prune List <OpVersionedFile> remove = null; if (FileMap.SafeCount > PruneSize) { FileMap.LockReading(delegate() { // local cache region is in KeepData and consists of the closest dht nodes that are part of our // same trust hierarchy (so that we don't cache or flood the network with old data of people who've left) remove = (from file in FileMap.Values where !Core.KeepData.SafeContainsKey(file.UserID) orderby file.UserID ^ Core.UserID descending select file).Take(FileMap.Count - PruneSize).ToList(); }); } if (remove == null) { return; } foreach (OpVersionedFile file in remove) { if (FileRemoved != null) { FileRemoved.Invoke(file); } if (file.Header != null && file.Header.FileHash != null) // local sync doesnt use files { try { File.Delete(GetFilePath(file.Header)); } catch { } } FileMap.SafeRemove(file.UserID); RunSaveHeaders = true; } }
void Network_StatusChange() { if (!Network.Established) { return; } // trigger download of files now in cache range StorageMap.LockReading(delegate() { foreach (OpStorage storage in StorageMap.Values) { if (Network.Routing.InCacheArea(storage.UserID)) { LoadHeaderFile(GetFilePath(storage), storage, true, false); } } }); }
// ensure that key/name associations persist between runs, done so remote people dont change their name and try to play with // us, once we make an association with a key, we change that name on our terms, also prevents key spoofing with dupe // user ids public void SaveKeyIndex(PacketStream stream) { NameMap.LockReading(() => { foreach (ulong user in KeyMap.Keys) { if (NameMap.ContainsKey(user)) { Debug.Assert(NameMap[user] != null); if (NameMap[user] != null) { stream.WritePacket(new UserInfo() { Name = NameMap[user], Key = KeyMap[user] }); } } } }); }
void Location_KnowOnline(List <ulong> users) { // keep aware if our buddies are online or not BuddyList.LockReading(() => users.AddRange(BuddyList.Keys)); }
void Core_SecondTimer() { OpCore global = Core.Context.Lookup; // global publish - so others can find entry to the op if (Core.User.Settings.OpAccess != AccessType.Secret && global != null && global.Network.Responsive && (global.Firewall == FirewallType.Open || Network.UseLookupProxies) && Core.TimeNow > NextGlobalPublish) { PublishGlobal(); } if (!Network.Established) { return; } // run code below every quarter second if (Core.TimeNow.Second % 15 != 0) { return; } // keep local client from being pinged, or removed LocalClient.LastSeen = Core.TimeNow.AddMinutes(1); LocalClient.NextPing = Core.TimeNow.AddMinutes(1); // remove expired clients - either from not notifying us, or we've lost interest and stopped pinging Clients.LockWriting(delegate() { foreach (ClientInfo client in Clients.Values.Where(c => Core.TimeNow > c.Timeout).ToArray()) { Clients.Remove(client.RoutingID); SignalUpdate(client, false); } }); // get form services users that we should keep tabs on LocalPings.Clear(); KnowOnline.Invoke(LocalPings); // ping clients that we are locally caching, or we have interest in Clients.LockReading(delegate() { foreach (ClientInfo client in (from c in Clients.Values where Core.TimeNow > c.NextPing && (Network.Routing.InCacheArea(c.UserID) || LocalPings.Contains(c.UserID)) select c).ToArray()) { Send_Ping(client); } }); // remove users no longer interested in our upates foreach (DhtClient expired in (from id in NotifyUsers.Keys where Core.TimeNow > NotifyUsers[id] select id).ToArray()) { NotifyUsers.Remove(expired); if (PendingNotifications.Contains(expired)) { PendingNotifications.Remove(expired); } } // send 2 per second, if new update, start over again foreach (DhtClient client in PendingNotifications.Take(2).ToArray()) { LocationNotify notify = new LocationNotify(); notify.Timeout = CurrentTimeout; notify.SignedLocation = LocalClient.SignedData; Network.LightComm.SendReliable(client, ServiceID, 0, notify); PendingNotifications.Remove(client); } }
public void SimTest() { if (Core.InvokeRequired) { Core.RunInCoreAsync(delegate() { SimTest(); }); return; } uint project = 0; // schedule PlanBlock block = new PlanBlock(); block.ProjectID = project; block.Title = Core.TextGen.GenerateWords(1)[0]; block.StartTime = Core.TimeNow.AddDays(Core.RndGen.Next(365)); // anytime in next year block.EndTime = block.StartTime.AddDays(Core.RndGen.Next(3, 90)); // half a week to 3 months block.Description = Core.TextGen.GenerateSentences(1)[0]; block.Scope = -1; block.Unique = Core.RndGen.Next(); // add to local plan LocalPlan.AddBlock(block); // goals // get uplinks including self and scan all goals for anything assigned to local List <ulong> uplinks = Core.Trust.GetUplinkIDs(Core.UserID, project); uplinks.Add(Core.UserID); List <PlanGoal> assignedGoals = new List <PlanGoal>(); PlanMap.LockReading(delegate() { foreach (ulong uplink in uplinks) { if (PlanMap.ContainsKey(uplink) && PlanMap[uplink].GoalMap != null) { foreach (List <PlanGoal> list in PlanMap[uplink].GoalMap.Values) { foreach (PlanGoal goal in list) { if (goal.Person == Core.UserID) { assignedGoals.Add(goal); } } } } } }); PlanGoal randGoal = null; if (assignedGoals.Count > 0) { randGoal = assignedGoals[Core.RndGen.Next(assignedGoals.Count)]; } List <ulong> downlinks = Core.Trust.GetDownlinkIDs(Core.UserID, project, 3); if (downlinks.Count > 0) { // create new goal PlanGoal newGoal = new PlanGoal(); newGoal.Project = project; newGoal.Title = GetRandomTitle(); newGoal.End = Core.TimeNow.AddDays(Core.RndGen.Next(30, 300)); newGoal.Description = Core.TextGen.GenerateSentences(1)[0]; int choice = Core.RndGen.Next(100); // create new goal if (randGoal == null || choice < 10) { newGoal.Ident = Core.RndGen.Next(); newGoal.Person = Core.UserID; } // delegate goal to sub else if (randGoal != null && choice < 50) { PlanGoal head = randGoal; // delegate down newGoal.Ident = head.Ident; newGoal.BranchUp = head.BranchDown; newGoal.BranchDown = Core.RndGen.Next(); newGoal.Person = downlinks[Core.RndGen.Next(downlinks.Count)]; } else { newGoal = null; } if (newGoal != null) { LocalPlan.AddGoal(newGoal); } } // add item to random goal if (randGoal != null) { PlanItem item = new PlanItem(); item.Ident = randGoal.Ident; item.Project = randGoal.Project; item.BranchUp = randGoal.BranchDown; item.Title = GetRandomTitle(); item.HoursTotal = Core.RndGen.Next(3, 30); item.HoursCompleted = Core.RndGen.Next(0, item.HoursTotal); item.Description = Core.TextGen.GenerateSentences(1)[0]; LocalPlan.AddItem(item); } SaveLocal(); }