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 Session_Update(RudpSession session)
        {
            DhtClient client = new DhtClient(session.UserID, session.ClientID);

            if (session.Status == SessionStatus.Active)
            {
                ShareCollection collection;
                if (Collections.SafeTryGetValue(session.UserID, out collection))
                {
                    if (collection.ToRequest.Any(t => t.ClientID == session.ClientID))
                    {
                        SendPublicRequest(session, collection);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public OpStorage GetStorage(ulong key)
        {
            OpStorage storage = null;

            StorageMap.SafeTryGetValue(key, out storage);

            return(storage);
        }
Exemplo n.º 4
0
        public OpVersionedFile GetFile(ulong id)
        {
            OpVersionedFile vfile = null;

            FileMap.SafeTryGetValue(id, out vfile);

            return(vfile);
        }
Exemplo n.º 5
0
        public OpProfile GetProfile(ulong id)
        {
            OpProfile profile = null;

            ProfileMap.SafeTryGetValue(id, out profile);

            return(profile);
        }
Exemplo n.º 6
0
        public void JoinCommand(uint project, RoomKind kind)
        {
            uint id = GetRoomID(project, kind);

            // create if doesnt exist
            ChatRoom room = null;

            if (!RoomMap.SafeTryGetValue(id, out room))
            {
                room = new ChatRoom(kind, project);
            }

            room.Active = true;
            room.AddMember(Core.UserID);

            RoomMap.SafeAdd(id, room);

            RefreshCommand(room);
            SendStatus(room);
            ConnectRoom(room);
        }
Exemplo n.º 7
0
        public string GetName(ulong user)
        {
            string name;

            if (NameMap.SafeTryGetValue(user, out name))
            {
                return(name);
            }

            name = user.ToString();

            return((name.Length > 5) ? name.Substring(0, 5) : name);
        }
Exemplo n.º 8
0
        public BuddyService(OpCore core)
        {
            Core         = core;
            Network      = Core.Network;
            Core.Buddies = this;

            Network.CoreStatusChange  += new StatusChange(Network_StatusChange);
            Core.KeepDataCore         += new KeepDataHandler(Core_KeepData);
            Core.Locations.KnowOnline += new KnowOnlineHandler(Location_KnowOnline);
            Core.MinuteTimerEvent     += Core_MinuteTimer;
            Cache = new VersionedCache(Network, ServiceID, 0, false);

            Cache.FileAquired += new FileAquiredHandler(Cache_FileAquired);
            Cache.Load();

            if (!BuddyList.SafeContainsKey(Core.UserID))
            {
                AddBuddy(Core.User.Settings.UserName, Core.User.Settings.KeyPublic);
            }

            BuddyList.SafeTryGetValue(Core.UserID, out LocalBuddy);
        }
Exemplo n.º 9
0
        public OpPlan GetPlan(ulong id, bool tryLoad)
        {
            OpPlan plan = null;

            PlanMap.SafeTryGetValue(id, out plan);

            if (plan == null)
            {
                return(null);
            }

            if (tryLoad && !plan.Loaded)
            {
                LoadPlan(id);
            }

            return((!tryLoad || (tryLoad && plan.Loaded)) ? plan : null);
        }
Exemplo n.º 10
0
        bool Transfers_DataFileSearch(ulong key, FileDetails details)
        {
            ulong hashID = BitConverter.ToUInt64(details.Hash, 0);

            OpFile file = null;

            if (FileMap.SafeTryGetValue(hashID, out file))
            {
                if (details.Size == file.Size && Utilities.MemCompare(details.Hash, file.Hash))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 11
0
        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--;
                        }
                    }
                }
            });
        }
Exemplo n.º 12
0
        void HashFile(HashPack pack)
        {
            // three steps, hash file, encrypt file, hash encrypted file
            try
            {
                OpFile      file = null;
                StorageFile info = pack.File.Info.Clone();

                // remove old references from local file
                OpFile commonFile = null;
                if (FileMap.SafeTryGetValue(pack.File.Info.HashID, out commonFile))
                {
                    commonFile.DeRef(); //crit test
                }
                if (!File.Exists(pack.Path))
                {
                    return;
                }

                // do public hash
                Utilities.ShaHashFile(pack.Path, ref info.InternalHash, ref info.InternalSize);
                info.InternalHashID = BitConverter.ToUInt64(info.InternalHash, 0);

                // if file exists in public map, use key for that file
                OpFile internalFile = null;
                InternalFileMap.SafeTryGetValue(info.InternalHashID, out internalFile);

                if (internalFile != null)
                {
                    file = internalFile;
                    file.References++;

                    // if file already encrypted in our system, continue
                    if (File.Exists(GetFilePath(info.HashID)))
                    {
                        info.Size    = file.Size;
                        info.FileKey = file.Key;

                        info.Hash   = file.Hash;
                        info.HashID = file.HashID;

                        if (!Utilities.MemCompare(file.Hash, pack.File.Info.Hash))
                        {
                            ReviseFile(pack, info);
                        }

                        return;
                    }
                }

                // file key is opID and public hash xor'd so that files won't be duplicated on the network
                // apply special compartment key here as well, xor again
                RijndaelManaged crypt = Utilities.CommonFileKey(Core.User.Settings.OpKey, info.InternalHash);
                info.FileKey = crypt.Key;

                // encrypt file to temp dir
                string tempPath = Core.GetTempPath();
                Utilities.EncryptTagFile(pack.Path, tempPath, crypt, Core.Network.Protocol, ref info.Hash, ref info.Size);
                info.HashID = BitConverter.ToUInt64(info.Hash, 0);

                // move to official path
                string path = GetFilePath(info.HashID);
                if (!File.Exists(path))
                {
                    File.Move(tempPath, path);
                }

                // if we dont have record of file make one
                if (file == null)
                {
                    file = new OpFile(info);
                    file.References++;
                    FileMap.SafeAdd(info.HashID, file);
                    InternalFileMap.SafeAdd(info.InternalHashID, file);
                }
                // else, record already made, just needed to put the actual file in the system
                else
                {
                    Debug.Assert(info.HashID == file.HashID);
                }


                // if hash is different than previous mark as modified
                if (!Utilities.MemCompare(file.Hash, pack.File.Info.Hash))
                {
                    ReviseFile(pack, info);
                }
            }
            catch (Exception ex)
            {
                /*rotate file to back of queue
                 * lock (HashQueue)
                 *  if (HashQueue.Count > 1)
                 *      HashQueue.Enqueue(HashQueue.Dequeue());*/

                Core.Network.UpdateLog("Storage", "Hash thread: " + ex.Message);
            }
        }
Exemplo n.º 13
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();
            }
        }