Exemplo n.º 1
0
        void Receive_Notify(DhtClient client, LocationNotify notify)
        {
            if (notify.SignedLocation != null)
            {
                Store_Local(new DataReq(null, client.UserID, ServiceID, 0, notify.SignedLocation));
            }


            ClientInfo info;

            if (!Clients.SafeTryGetValue(client.RoutingID, out info))
            {
                return;
            }

            if (notify.GoingOffline)
            {
                Clients.SafeRemove(client.RoutingID);
                SignalUpdate(info, false);
                return;
            }

            info.LastSeen    = Core.TimeNow;
            info.PingTimeout = notify.Timeout;
            info.NextPing    = Core.TimeNow.AddSeconds(notify.Timeout);
        }
Exemplo n.º 2
0
        void Cache_FileRemoved(OpVersionedFile file)
        {
            OpProfile profile = GetProfile(file.UserID);

            if (profile != null)
            {
                ProfileMap.SafeRemove(file.UserID);
            }
        }
Exemplo n.º 3
0
        void Cache_FileRemoved(OpVersionedFile file)
        {
            OpStorage storage = GetStorage(file.UserID);

            if (storage != null)
            {
                UnloadHeaderFile(GetFilePath(storage), storage.File.Header.FileKey);
            }

            StorageMap.SafeRemove(file.UserID);
        }
Exemplo n.º 4
0
        void Cache_FileRemoved(OpVersionedFile file)
        {
            OpPlan plan = GetPlan(file.UserID, false);

            if (plan == null)
            {
                return;
            }

            PlanMap.SafeRemove(file.UserID);
        }
Exemplo n.º 5
0
        public void RemoveBuddy(ulong user)
        {
            if (!BuddyList.SafeContainsKey(user))
            {
                return;
            }

            BuddyList.SafeRemove(user);

            SaveList = true;
            Core.RunInGuiThread(GuiUpdate);
        }
Exemplo n.º 6
0
        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;
            }
        }
Exemplo n.º 7
0
        public void Ignore(ulong user, bool ignore)
        {
            if (ignore)
            {
                OpBuddy buddy = new OpBuddy()
                {
                    ID      = user,
                    Name    = Core.GetName(user),
                    Key     = Core.KeyMap[user], //crit - not thread safe, change key map?
                    Ignored = true
                };

                IgnoreList.SafeAdd(user, buddy);
            }
            else
            {
                IgnoreList.SafeRemove(user);
            }


            SaveList = true;

            Core.RunInGuiThread(GuiUpdate);
        }
Exemplo n.º 8
0
        public void SecondTimer()
        {
            if (InvokeRequired)
            {
                RunTimer = true;
                ProcessEvent.Set();
                return;
            }

            try
            {
                // networks
                Network.SecondTimer();

                if (SecondTimerEvent != null)
                {
                    SecondTimerEvent.Invoke();
                }

                // service bandwidth
                foreach (BandwidthLog buffer in ServiceBandwidth.Values)
                {
                    buffer.NextSecond();
                }

                // before minute timer give gui 2 secs to tell us of nodes it doesnt want removed
                if (KeepDataCore != null && MinuteCounter == 58)
                {
                    KeepData.SafeClear();

                    KeepDataCore.Invoke();
                    RunInGuiThread(KeepDataGui);
                }


                MinuteCounter++;

                if (MinuteCounter == 60)
                {
                    MinuteCounter = 0;

                    if (MinuteTimerEvent != null)
                    {
                        MinuteTimerEvent.Invoke();
                    }

                    // prune keys from keymap - dont remove focused, remove furthest first
                    if (KeyMap.Count > KeyMax)
                    {
                        foreach (ulong user in (from id in KeyMap.Keys
                                                where !KeepData.SafeContainsKey(id) &&
                                                !Network.RudpControl.SessionMap.Values.Any(socket => socket.UserID == id) &&
                                                !Network.TcpControl.SocketList.Any(socket => socket.UserID == id)
                                                orderby Network.Local.UserID ^ id descending
                                                select id).Take(KeyMap.Count - KeyMax).ToArray())
                        {
                            KeyMap.Remove(user);
                            if (NameMap.SafeContainsKey(user))
                            {
                                NameMap.SafeRemove(user);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ConsoleLog("Exception OpCore::SecondTimer_Tick: " + ex.Message);
            }
        }
Exemplo n.º 9
0
        void PumpThread(int index)
        {
            while (true)
            {
                PumpStart[index].WaitOne();

                if (Shutdown)
                {
                    return;
                }


                // send packets
                foreach (SimPacket packet in InPackets[index])
                {
                    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.SafeAdd(packet.Tcp, socket);
                            TcpSourcetoDest.SafeAdd(socket, packet.Tcp);

                            packet.Tcp.OnConnect();
                        }

                        break;

                    case SimPacketType.Tcp:
                        TcpConnect dest;
                        if (TcpSourcetoDest.SafeTryGetValue(packet.Tcp, out dest))
                        {
                            dest.Core.Sim.BytesRecvd += (ulong)packet.Packet.Length;

                            packet.Packet.CopyTo(dest.RecvBuffer, dest.RecvBuffSize);
                            dest.OnReceive(packet.Packet.Length);
                        }
                        break;

                    case SimPacketType.TcpClose:
                        TcpConnect destClose;
                        if (TcpSourcetoDest.SafeTryGetValue(packet.Tcp, out destClose))
                        {
                            destClose.OnReceive(0);

                            TcpSourcetoDest.SafeRemove(packet.Tcp);
                            TcpSourcetoDest.SafeRemove(destClose);
                        }
                        break;
                    }
                }

                // send messages from gui
                if (!TestCoreThread)
                {
                    Instances.SafeForEach(instance =>
                    {
                        if (instance.ThreadIndex != index)
                        {
                            return;
                        }

                        Action <OpCore> runMessages = core =>
                        {
                            // process invoked functions, dequeue quickly to continue processing
                            while (core.CoreMessages.Count > 0)
                            {
                                AsyncCoreFunction function = null;

                                lock (core.CoreMessages)
                                    function = core.CoreMessages.Dequeue();

                                if (function != null)
                                {
                                    function.Result    = function.Method.DynamicInvoke(function.Args);
                                    function.Completed = true;
                                    function.Processed.Set();
                                }
                            }
                        };

                        if (instance.Context.Lookup != null)
                        {
                            runMessages(instance.Context.Lookup);
                        }

                        instance.Context.Cores.SafeForEach(c => runMessages(c));
                    });
                }

                // instance timer
                if (CurrentPump == 0) // stepping would cause second timer to run every 250ms without this
                {
                    Instances.SafeForEach(instance =>
                    {
                        if (instance.ThreadIndex == index)
                        {
                            instance.Context.SecondTimer_Tick(null);
                        }
                    });
                }

                PumpEnd[index].Set();
            }
        }