/* object TimerLock = new object(); * object[] PacketsLock = new object[4] { new object(), new object(), new object(), new object() }; * * ManualResetEvent[] WaitHandles = new ManualResetEvent[5] { new ManualResetEvent(true), new ManualResetEvent(true), * new ManualResetEvent(true), new ManualResetEvent(true), * new ManualResetEvent(true) }; * * * void OldRun() * { * int pumps = 4; * List<SimPacket> tempList = new List<SimPacket>(); * * Thread timerThread = new Thread(RunTimer); * timerThread.Start(); * * Thread[] packetsThread = new Thread[4]; * for (int i = 0; i < 4; i++) * { * packetsThread[i] = new Thread(RunPackets); * packetsThread[i].Start(i); * } * * Thread.Sleep(1000); // lets new threads reach wait( * * * while (true && !Shutdown) * { * if (Paused && !Step) * { * Thread.Sleep(250); * continue; * } * * // load users * if (Flux) * DoFlux(); * * * // instance timer * lock (TimerLock) * { * WaitHandles[0].Reset(); * Monitor.Pulse(TimerLock); * } * * // pump packets, 4 times (250ms latency * for (int i = 0; i < pumps; i++) * { * // clear out buffer by switching with in buffer * lock (PacketHandle) * { * tempList = InPackets; * InPackets = OutPackets; * OutPackets = tempList; * } * * for (int index = 0; index < 4; index++) * lock (PacketsLock[index]) * { * WaitHandles[1 + index].Reset(); * Monitor.Pulse(PacketsLock[index]); * } * * * AutoResetEvent.WaitAll(WaitHandles); * * InPackets.Clear(); * * TimeNow = TimeNow.AddMilliseconds(1000 / pumps); * * if(UpdateView != null) * UpdateView(); * * if (Step || Shutdown) * { * Step = false; * break; * } * } * * // if run sim slow * if (SleepTime > 0) * Thread.Sleep(SleepTime); * } * * * lock(TimerLock) * Monitor.Pulse(TimerLock); * * for(int i = 0; i < 4; i++) * lock(PacketsLock[i]) * Monitor.Pulse(PacketsLock[i]); * } * * void RunTimer() * { * while (true && !Shutdown) * { * lock (TimerLock) * { * Monitor.Wait(TimerLock); * * lock (Instances) * foreach (SimInstance instance in Instances) * instance.Context.SecondTimer_Tick(null, null); * * WaitHandles[0].Set(); * } * } * } * * void RunPackets(object val) * { * int index = (int)val; * * while (true && !Shutdown) * { * lock (PacketsLock[index]) * { * Monitor.Wait(PacketsLock[index]); * * // send packets * foreach (SimPacket packet in InPackets) * { * // 0 - global udp * // 1 - global tcp * // 2 - op udp * // 3 - op tcp * * if ((index == 0 && packet.Type == SimPacketType.Udp && packet.Dest.IsGlobal) || * (index == 1 && packet.Type != SimPacketType.Udp && packet.Dest.IsGlobal) || * (index == 2 && packet.Type == SimPacketType.Udp && !packet.Dest.IsGlobal) || * (index == 3 && packet.Type != SimPacketType.Udp && !packet.Dest.IsGlobal)) * { * * switch (packet.Type) * { * case SimPacketType.Udp: * packet.Dest.Core.Sim.BytesRecvd += (ulong)packet.Packet.Length; * packet.Dest.UdpControl.OnReceive(packet.Packet, packet.Packet.Length, packet.Source); * break; * case SimPacketType.TcpConnect: * TcpConnect socket = packet.Dest.TcpControl.OnAccept(null, packet.Source); * * if (socket != null) * { * TcpSourcetoDest[packet.Tcp] = socket; * TcpSourcetoDest[socket] = packet.Tcp; * * packet.Tcp.OnConnect(); * } * * break; * case SimPacketType.Tcp: * if (TcpSourcetoDest.ContainsKey(packet.Tcp)) * { * TcpConnect dest = TcpSourcetoDest[packet.Tcp]; * * dest.Core.Sim.BytesRecvd += (ulong)packet.Packet.Length; * * packet.Packet.CopyTo(dest.RecvBuffer, dest.RecvBuffSize); * dest.OnReceive(packet.Packet.Length); * } * break; * case SimPacketType.TcpClose: * if (TcpSourcetoDest.ContainsKey(packet.Tcp)) * { * TcpConnect dest = TcpSourcetoDest[packet.Tcp]; * dest.OnReceive(0); * * TcpSourcetoDest.Remove(packet.Tcp); * TcpSourcetoDest.Remove(dest); * } * break; * } * * } * } * * WaitHandles[1 + index].Set(); * } * } * } */ public void DownloadCache(DhtNetwork network) { WebCacheHits++; List <DhtNetwork> open = new List <DhtNetwork>(); // find matching networks that are potential cache entries Instances.LockReading(() => { foreach (DeOpsContext context in from i in Instances where i.RealFirewall == FirewallType.Open && i != network.Core.Sim select i.Context) { if (context.Lookup != null && context.Lookup.Network.OpID == network.OpID) { open.Add(context.Lookup.Network); } context.Cores.LockReading(() => open.AddRange(from c in context.Cores where c.Network.OpID == network.OpID select c.Network)); } }); // give back 3 random cache entries foreach (DhtNetwork net in open.OrderBy(n => RndGen.Next()).Take(3)) { DhtContact contact = net.GetLocalContact(); contact.IP = net.Core.Sim.RealIP; contact.LastSeen = net.Core.TimeNow; network.Cache.AddContact(contact); } }
void RunAudioThread() { while (ThreadRunning) { AudioEvent.WaitOne(); if (Recorder != null && RecordingActive) { lock (Recorder) Recorder.ProcessBuffers(); } Players.LockReading(() => Players.ForEach(p => p.ProcessBuffers())); } }
public int GetActiveMembers(ChatService chat) { int count = 0; Members.LockReading(delegate() { foreach (ulong user in Members) { if (chat.Network.RudpControl.GetActiveSessions(user).Count > 0) { count++; } } }); return(count); }
private void RefreshStructure() { PlanStructure.BeginUpdate(); // save selected PlanNode selected = GetSelected(); // save visible while unloading List <ulong> visible = new List <ulong>(); foreach (TreeListNode node in PlanStructure.Nodes) { if (node.GetType() == typeof(PlanNode)) { UnloadNode((PlanNode)node, visible); } } NodeMap.Clear(); PlanStructure.Nodes.Clear(); // nodes ThreadedList <OpLink> roots = null; if (Trust.ProjectRoots.SafeTryGetValue(ProjectID, out roots)) { roots.LockReading(delegate() { foreach (OpLink root in roots) { if (Uplinks.Contains(root.UserID)) { PlanNode node = CreateNode(root); Plans.Research(root.UserID); LoadNode(node); GuiUtils.InsertSubNode(PlanStructure.virtualParent, node); ExpandPath(node, Uplinks); } if (root.IsLoopRoot && root.Downlinks.Count > 0 && Uplinks.Contains(root.Downlinks[0].UserID)) { foreach (OpLink downlink in root.Downlinks) { if (!root.IsLoopedTo(downlink)) { PlanNode node = CreateNode(downlink); Plans.Research(downlink.UserID); LoadNode(node); GuiUtils.InsertSubNode(PlanStructure.virtualParent, node); ExpandPath(node, Uplinks); } } } } }); } // restore visible foreach (ulong id in visible) { foreach (TreeListNode node in PlanStructure.Nodes) { if (node.GetType() == typeof(PlanNode)) { List <ulong> uplinks = Trust.GetUnconfirmedUplinkIDs(id, ProjectID); uplinks.Add(id); VisiblePath((PlanNode)node, uplinks); } } } // restore selected if (selected != null) { if (NodeMap.ContainsKey(selected.Link.UserID)) { PlanStructure.Select(NodeMap[selected.Link.UserID]); } } PlanStructure.EndUpdate(); }
private void RefreshOperationTree() { BeginUpdate(); // save selected LinkNode selected = GetSelected(); // save visible while unloading List <ulong> visible = new List <ulong>(); foreach (TreeListNode node in Nodes) { if (node.GetType() == typeof(LinkNode)) { UnloadNode((LinkNode)node, visible); } } NodeMap.Clear(); Nodes.Clear(); // white space if (FirstLineBlank) { Nodes.Add(new LabelNode("")); } if (!Trust.ProjectRoots.SafeContainsKey(Project)) { EndUpdate(); return; } string rootname = Core.User.Settings.Operation; if (Project != 0) { rootname = Trust.GetProjectName(Project); } // operation ProjectNode = new ProjectNode(rootname, Project); ProjectNode.Font = TrustedFont; Nodes.Add(ProjectNode); // white space Nodes.Add(new LabelNode("")); // unlinked UnlinkedNode = new ProjectNode("Untrusted", 0); UnlinkedNode.Font = UntrustedFont; Nodes.Add(UnlinkedNode); // if forced, load specific node as root if (ForceRootID != 0) { OpLink root = Trust.GetLink(ForceRootID, Project); if (root != null) { SetupRoot(root); } } // get roots for specific project else { ThreadedList <OpLink> roots = null; if (Trust.ProjectRoots.SafeTryGetValue(Project, out roots)) { roots.LockReading(delegate() { foreach (OpLink root in roots) { SetupRoot(root); } }); } } // show unlinked if there's something to show if (Nodes.IndexOf(UnlinkedNode) + 1 == Nodes.Count) { UnlinkedNode.Text = ""; } else { UnlinkedNode.Text = "Untrusted"; } // restore visible foreach (ulong id in visible) { foreach (TreeListNode node in Nodes) { if (node.GetType() == typeof(LinkNode)) { List <ulong> uplinks = Trust.GetUnconfirmedUplinkIDs(id, Project); uplinks.Add(id); VisiblePath((LinkNode)node, uplinks); } } } // restore selected if (selected != null) { if (NodeMap.ContainsKey(selected.Link.UserID)) { Select(NodeMap[selected.Link.UserID]); } } EndUpdate(); }