Пример #1
0
        void Session_Data(RudpSession session, byte[] data)
        {
            IMStatus status = OpenStatus(session.UserID);


            G2Header root = new G2Header(data);

            if (G2Protocol.ReadPacket(root))
            {
                if (root.Name == IMPacket.Message)
                {
                    MessageData message = MessageData.Decode(root);

                    if (message.TargetID != 0)
                    {
                        Debug.Assert(session.UserID == Core.UserID);
                        if (session.UserID != Core.UserID)
                        {
                            return;
                        }

                        status = OpenStatus(message.TargetID);
                    }

                    ProcessMessage(status, new InstantMessage(Core, session, message));
                }

                if (root.Name == IMPacket.Alive)
                {
                    status.SetTTL(session.ClientID, SessionTimeout * 2);
                }
            }
        }
Пример #2
0
        void SendWhoResponse(ChatRoom room, RudpSession session)
        {
            Debug.Assert(!IsCommandRoom(room.Kind));

            List <ChatWho> whoPackets = new List <ChatWho>();

            ChatWho who = new ChatWho();

            who.RoomID = room.RoomID;
            whoPackets.Add(who);

            room.Members.LockReading(delegate()
            {
                foreach (ulong id in room.Members)
                {
                    if (Network.RudpControl.GetActiveSessions(id).Count > 0) // only send members who are connected
                    {
                        who.Members.Add(id);

                        if (who.Members.Count > 40) // 40 * 8 = 320 bytes
                        {
                            who        = new ChatWho();
                            who.RoomID = room.RoomID;
                            whoPackets.Add(who);
                        }
                    }
                }
            });

            // send who to already connected locations
            foreach (ChatWho packet in whoPackets)
            {
                session.SendData(ServiceID, 0, packet);
            }
        }
Пример #3
0
        public void GetPublicList(ulong user)
        {
            if (Core.InvokeRequired)
            {
                Core.RunInCoreAsync(() => GetPublicList(user));
                return;
            }

            ShareCollection collection;

            if (!Collections.SafeTryGetValue(user, out collection))
            {
                collection = new ShareCollection(user);
                Collections.SafeAdd(user, collection);
            }

            AddTargets(collection.ToRequest, user, 0);

            foreach (DhtClient target in collection.ToRequest)
            {
                RudpSession session = Network.RudpControl.GetActiveSession(target);

                if (session == null)
                {
                    Network.RudpControl.Connect(target);
                    collection.Status = "Connecting to " + Core.GetName(target.UserID);
                }
                else
                {
                    SendPublicRequest(session, collection);
                }
            }
        }
Пример #4
0
        void Session_Data(RudpSession session, byte[] data)
        {
            G2Header root = new G2Header(data);

            if (G2Protocol.ReadPacket(root))
            {
                switch (root.Name)
                {
                case ChatPacket.Data:
                    ReceiveMessage(ChatText.Decode(root), session);
                    break;

                case ChatPacket.Status:
                    ReceiveStatus(ChatStatus.Decode(root), session);
                    break;

                case ChatPacket.Invite:
                    ReceiveInvite(ChatInvite.Decode(root), session);
                    break;

                case ChatPacket.Who:
                    ReceiveWho(ChatWho.Decode(root), session);
                    break;
                }
            }
        }
Пример #5
0
        private void ReceiveMessage(ChatText message, RudpSession session)
        {
            if (Core.Buddies.IgnoreList.SafeContainsKey(session.UserID))
            {
                return;
            }

            // remote's command low, is my command high
            // do here otherwise have to send custom roomID packets to selfs/lowers/highers

            if (Trust != null && session.UserID != Core.UserID)
            {
                // if check fails then it is loop node sending data, keep it unchanged
                if (message.Kind == RoomKind.Command_High && Trust.IsLowerDirect(session.UserID, message.ProjectID))
                {
                    message.Kind = RoomKind.Command_Low;
                }

                else if (message.Kind == RoomKind.Command_Low && Trust.IsHigher(session.UserID, message.ProjectID))
                {
                    message.Kind = RoomKind.Command_High;
                }

                else if (message.Kind == RoomKind.Live_High)
                {
                    message.Kind = RoomKind.Live_Low;
                }

                else if (message.Kind == RoomKind.Live_Low)
                {
                    message.Kind = RoomKind.Live_High;
                }
            }

            ulong id = IsCommandRoom(message.Kind) ? GetRoomID(message.ProjectID, message.Kind) : message.RoomID;

            ChatRoom room = null;

            // if not in room let remote user know
            if (!RoomMap.TryGetValue(id, out room) ||
                !room.Active)
            {
                SendStatus(session);
                return;
            }

            // if sender not in room
            if (!room.Members.SafeContains(session.UserID))
            {
                return;
            }

            if (!ChatNewsUpdate)
            {
                ChatNewsUpdate = true;
                Core.MakeNews(ServiceIDs.Chat, Core.GetName(session.UserID) + " is chatting", session.UserID, 0, false);
            }

            ProcessMessage(room, new ChatMessage(Core, session, message));
        }
Пример #6
0
        private void ReceivePublicDetails(RudpSession session, ShareCollection file)
        {
            ShareCollection collection;

            if (!Collections.SafeTryGetValue(session.UserID, out collection))
            {
                return;
            }

            collection.Key  = file.Key;
            collection.Size = file.Size;
            collection.Hash = file.Hash;

            foreach (DhtClient done in collection.ToRequest.Where(t => t.ClientID == session.ClientID).ToArray())
            {
                collection.ToRequest.Remove(done);
            }


            FileDetails details = new FileDetails(ServiceID, DataTypePublic, file.Hash, file.Size, null);

            object[] args = new object[] { collection, (object)session.ClientID };


            DhtClient  client   = new DhtClient(session.UserID, session.ClientID);
            OpTransfer transfer = Core.Transfers.StartDownload(client.UserID, details, GetPublicPath(collection), new EndDownloadHandler(CollectionDownloadFinished), args);

            transfer.AddPeer(client);
            transfer.DoSearch = false;

            collection.Status = "Starting List Download";
        }
Пример #7
0
        void SendInviteProof(ChatRoom room, RudpSession session)
        {
            if (!room.Invites.ContainsKey(Core.UserID))
            {
                return;
            }

            // if already sent proof to client, return
            Tuple <ChatInvite, List <ushort> > tried;

            if (!room.Invites.TryGetValue(session.UserID, out tried))
            {
                tried = new Tuple <ChatInvite, List <ushort> >(null, new List <ushort>());
                room.Invites[session.UserID] = tried;
            }

            if (tried.Param2.Contains(session.ClientID))
            {
                return;
            }

            tried.Param2.Add(session.ClientID);

            ChatInvite invite = new ChatInvite();

            invite.RoomID       = room.RoomID;
            invite.Title        = room.Title;
            invite.SignedInvite = room.Invites[Core.UserID].Param1.SignedInvite;

            session.SendData(ServiceID, 0, invite);
        }
Пример #8
0
        public void Session_Update(RudpSession session)
        {
            if (!ActiveUsers.Contains(session.UserID))
            {
                return;
            }

            IMStatus status = null;

            if (!IMMap.SafeTryGetValue(session.UserID, out status))
            {
                return;
            }


            if (session.Status == SessionStatus.Active)
            {
                // needs to be set here as well be cause we don't receive a keep alive from remote host on connect
                status.SetTTL(session.ClientID, SessionTimeout * 2);

                session.SendData(ServiceID, 0, new IMKeepAlive());
            }

            Update(status);
        }
Пример #9
0
 public InstantMessage(OpCore core, RudpSession session, MessageData message)
 {
     UserID    = session.UserID;
     ClientID  = session.ClientID;
     TimeStamp = core.TimeNow;
     Text      = message.Text;
     Format    = message.Format;
 }
Пример #10
0
 public ChatMessage(OpCore core, RudpSession session, ChatText text)
 {
     UserID    = session.UserID;
     ClientID  = session.ClientID;
     TimeStamp = core.TimeNow;
     Text      = text.Text;
     Format    = text.Format;
 }
Пример #11
0
        void SendWhoRequest(ChatRoom room, RudpSession session)
        {
            ChatWho whoReq = new ChatWho();

            whoReq.Request = true;
            whoReq.RoomID  = room.RoomID;
            session.SendData(ServiceID, 0, whoReq);
        }
Пример #12
0
        void ReceiveStatus(ChatStatus status, RudpSession session)
        {
            // status is what nodes send to each other to tell what rooms they are active in

            RoomMap.LockReading(delegate()
            {
                foreach (ChatRoom room in RoomMap.Values)
                {
                    bool update = false;

                    // remove from room
                    if (!status.ActiveRooms.Contains(room.RoomID) && room.Members.SafeContains(session.UserID))
                    {
                        if (!IsCommandRoom(room.Kind))
                        {
                            if (room.Members.SafeContains(session.UserID))
                            {
                                ProcessMessage(room, GetNameAndLocation(session) + " left the room");
                            }

                            room.RemoveMember(session.UserID);
                        }

                        update = true;
                    }

                    // add member to room
                    if (IsCommandRoom(room.Kind) && room.Members.SafeContains(session.UserID))
                    {
                        update = true;
                    }

                    else if (status.ActiveRooms.Contains(room.RoomID))
                    {
                        // if room private check that sender is verified
                        if (room.Kind == RoomKind.Secret && !room.Verified.ContainsKey(session.UserID))
                        {
                            continue;
                        }

                        if (!room.Members.SafeContains(session.UserID))
                        {
                            ProcessMessage(room, GetNameAndLocation(session) + " joined the room");
                        }

                        room.AddMember(session.UserID);
                        update = true;
                    }

                    if (update)
                    {
                        Core.RunInGuiThread(room.MembersUpdate);
                    }
                }
            });
        }
Пример #13
0
        public void Session_Update(RudpSession session)
        {
            // send node rooms that we have in common
            if (session.Status == SessionStatus.Active)
            {
                // send invites
                RoomMap.LockReading(delegate()
                {
                    // if we are host of room and connect hasn't been sent invite
                    foreach (ChatRoom room in RoomMap.Values)
                    {
                        if (room.NeedSendInvite(session.UserID, session.ClientID))
                        {
                            // invite not sent
                            if (room.Kind == RoomKind.Private || room.Host == Core.UserID)
                            {
                                session.SendData(ServiceID, 0, room.Invites[session.UserID].Param1);
                                room.Invites[session.UserID].Param2.Add(session.ClientID);
                                ProcessMessage(room, "Invite sent to " + GetNameAndLocation(session));
                                SendWhoResponse(room, session);
                            }
                            // else private room and we are not the host, send proof we belong here
                            else
                            {
                                SendInviteProof(room, session);
                            }
                        }

                        // ask member who else is in room
                        if (!IsCommandRoom(room.Kind) &&
                            room.Members.SafeContains(session.UserID))
                        {
                            SendWhoRequest(room, session);
                        }
                    }
                });


                SendStatus(session);
            }

            // if disconnected
            if (session.Status == SessionStatus.Closed)
            {
                foreach (ChatRoom room in FindRoom(session.UserID))
                {
                    if (room.Active)
                    {
                        // don't remove from members unless explicitly told in status
                        Core.RunInGuiThread(room.MembersUpdate);
                    }
                }
            }
        }
Пример #14
0
        void ReceiveWho(ChatWho who, RudpSession session)
        {
            // if in room
            ChatRoom room;

            // if not in room, send status
            if (!RoomMap.TryGetValue(who.RoomID, out room))
            {
                SendStatus(session);
                return;
            }

            // if room not public, and not from verified private room member or host, igonre
            if (IsCommandRoom(room.Kind) || (room.Kind == RoomKind.Secret && !room.Verified.ContainsKey(session.UserID)))
            {
                return;
            }

            if (!room.Active)
            {
                return;
            }

            // if requset
            if (who.Request)
            {
                SendWhoResponse(room, session);
            }

            // if reply
            else
            {
                // add members to our own list
                foreach (ulong id in who.Members)
                {
                    if (!room.Members.SafeContains(id))
                    {
                        room.AddMember(id);

                        if (Trust != null && Trust.GetTrust(id) == null)
                        {
                            Trust.Research(id, 0, false);
                        }

                        Core.Locations.Research(id);
                    }
                }

                // connect to new members
                ConnectRoom(room);
            }
        }
Пример #15
0
        public bool Contains(RudpSession rudp)
        {
            if (AllSessions.Contains(rudp.UserID))
                return true;

            if (!Sessions.ContainsKey(rudp.UserID))
                return false;

            if (Sessions[rudp.UserID].Contains(rudp.ClientID))
                return true;

            return false;
        }
Пример #16
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);
                    }
                }
            }
        }
Пример #17
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--;
                        }
                    }
                }
            });
        }
Пример #18
0
        void Session_Data(RudpSession session, byte[] data)
        {
            G2Header root = new G2Header(data);

            if (G2Protocol.ReadPacket(root))
            {
                switch (root.Name)
                {
                case SharePacket.PublicRequest:
                    ReceivePublicRequest(session);
                    break;

                case SharePacket.Collection:
                    ReceivePublicDetails(session, ShareCollection.Decode(root, session.UserID));
                    break;
                }
            }
        }
Пример #19
0
        private void ReceivePublicRequest(RudpSession session)
        {
            // if in global im, only allow if on buddies list
            if (Core.User.Settings.GlobalIM)
            {
                if (!Core.Buddies.BuddyList.SafeContainsKey(session.UserID))
                {
                    return;
                }
            }

            if (Core.Buddies.IgnoreList.SafeContainsKey(session.UserID))
            {
                return;
            }

            if (Local.Hash != null)
            {
                session.SendData(ServiceID, DataTypeSession, Local);
            }
        }
Пример #20
0
        private void SendStatus(RudpSession session)
        {
            // send even if empty so they know to remove us

            ChatStatus status = new ChatStatus();

            RoomMap.LockReading(delegate()
            {
                foreach (ChatRoom room in RoomMap.Values)
                {
                    if (room.Active && !IsCommandRoom(room.Kind))
                    {
                        if (room.Kind == RoomKind.Secret && !room.Verified.ContainsKey(session.UserID))
                        {
                            continue;
                        }

                        status.ActiveRooms.Add(room.RoomID);
                    }
                }
            });

            session.SendData(ServiceID, 0, status);
        }
Пример #21
0
 void Session_Data(RudpSession session, byte[] data)
 {
     Comm_ReceiveData(session, data);
 }
Пример #22
0
        void Session_Data(RudpSession session, byte[] data)
        {
            G2Header root = new G2Header(data);

            if (G2Protocol.ReadPacket(root))
            {
                switch (root.Name)
                {
                    case SharingPacket.File:
                        ReceiveFileRequest(session, SharedFile.Decode(root, Core.Network.Local.ClientID));
                        break;

                    case SharingPacket.PublicRequest:
                        ReceivePublicRequest(session);
                        break;

                    case SharingPacket.Collection:
                        ReceivePublicDetails(session, ShareCollection.Decode(root, session.UserID));
                        break;
                }
            }
        }
Пример #23
0
        public void ReceiveCommPacket(G2ReceivedPacket raw, RudpPacket packet)
        {
            try
            {
                if (packet.PacketType == RudpPacketType.Light ||
                    packet.PacketType == RudpPacketType.LightAck)
                {
                    LightComm.ReceivePacket(raw, packet);
                    return;
                }

                // if a socket already set up
                lock (RudpControl.SocketMap)
                    if (RudpControl.SocketMap.ContainsKey(packet.PeerID))
                    {
                        RudpControl.SocketMap[packet.PeerID].RudpReceive(raw, packet, IsLookup);
                        return;
                    }

                // if starting new session
                if (packet.PacketType != RudpPacketType.Syn)
                    return;

                RudpSyn syn = new RudpSyn(packet.Payload);

                // prevent connection from self
                if (syn.SenderID == Local.UserID && syn.ClientID == Local.ClientID)
                    return;

                // find connecting session with same or unknown client id
                ulong id = syn.SenderID ^ syn.ClientID;

                if (RudpControl.SessionMap.ContainsKey(id))
                {
                    RudpSession session = RudpControl.SessionMap[id];

                    // if session id zero or matches forward
                    if ((session.Comm.State == RudpState.Connecting && session.Comm.RemotePeerID == 0) ||
                        (session.Comm.State != RudpState.Closed && session.Comm.RemotePeerID == syn.ConnID)) // duplicate syn
                    {
                        session.Comm.RudpReceive(raw, packet, IsLookup);
                        return;
                    }

                    else if (session.Comm.State == RudpState.Finishing)
                    {
                        RudpControl.RemoveSession(session);
                        // remove session, allow new one to be created
                    }
                    else
                        return;
                }

                // if clientid not in session, create new session
                RudpSession newSession = new RudpSession(RudpControl, syn.SenderID, syn.ClientID, true);

                RudpControl.SessionMap[id] = newSession;

                // send ack before sending our own syn (connect)
                // ack tells remote which address is good so that our syn's ack comes back quickly
                newSession.Comm.RudpReceive(raw, packet, IsLookup);

                newSession.Connect();

                UpdateLog("RUDP", "Inbound session accepted to ClientID " + syn.ClientID.ToString());
            }
            catch (Exception ex)
            {
                UpdateLog("Exception", "DhtNetwork::ReceiveCommPacket: " + ex.Message);
            }
        }
Пример #24
0
 void Session_Data(RudpSession session, byte[] data)
 {
     Comm_ReceiveData(session, data);
 }
Пример #25
0
        private void ReceivePublicRequest(RudpSession session)
        {
            // if in global im, only allow if on buddies list
            if (Core.User.Settings.GlobalIM)
                if (!Core.Buddies.BuddyList.SafeContainsKey(session.UserID))
                    return;

            if (Core.Buddies.IgnoreList.SafeContainsKey(session.UserID))
                return;

            if(Local.Hash != null)
                session.SendData(ServiceID, DataTypeSession, Local);
        }
Пример #26
0
 public InstantMessage(OpCore core, RudpSession session, MessageData message)
 {
     UserID = session.UserID;
     ClientID = session.ClientID;
     TimeStamp = core.TimeNow;
     Text = message.Text;
     Format = message.Format;
 }
Пример #27
0
        void ReceiveInvite(ChatInvite invite, RudpSession session)
        {
            // if in global im, only allow if on buddies list
            if (Core.User.Settings.GlobalIM)
            {
                if (!Core.Buddies.BuddyList.SafeContainsKey(session.UserID))
                {
                    return;
                }
            }

            if (Core.Buddies.IgnoreList.SafeContainsKey(session.UserID))
            {
                return;
            }

            bool showInvite = false;

            ChatRoom room;

            if (!RoomMap.TryGetValue(invite.RoomID, out room))
            {
                RoomKind kind = invite.SignedInvite != null ? RoomKind.Secret : RoomKind.Private;
                room        = new ChatRoom(kind, invite.RoomID, invite.Title);
                room.RoomID = invite.RoomID;
                room.Kind   = kind;
                room.AddMember(session.UserID);

                if (invite.Host != null)
                {
                    room.Host = Utilities.KeytoID(invite.Host);
                    Core.IndexKey(room.Host, ref invite.Host);
                }

                RoomMap.SafeAdd(room.RoomID, room);

                showInvite = true;
            }

            // private room
            if (room.Kind == RoomKind.Secret)
            {
                if (!Core.KeyMap.ContainsKey(room.Host))
                {
                    return;
                }

                byte[] hostKey = Core.KeyMap[room.Host];


                // if this is host sending us our verification
                if (session.UserID == room.Host)
                {
                    // check that host signed our public key with his private
                    if (!Utilities.CheckSignedData(hostKey, Core.KeyMap[Core.UserID], invite.SignedInvite))
                    {
                        return;
                    }

                    if (!room.Invites.ContainsKey(Core.UserID)) // would fail if a node's dupe on network sends invite back to itself
                    {
                        room.Invites.Add(Core.UserID, new Tuple <ChatInvite, List <ushort> >(invite, new List <ushort>()));
                    }
                }

                // else this is node in room sending us proof of being invited
                else
                {
                    if (!Core.KeyMap.ContainsKey(session.UserID))
                    {
                        return; // key should def be in map, it was added when session was made to sender
                    }
                    // check that host signed remote's key with host's private
                    if (!Utilities.CheckSignedData(hostKey, Core.KeyMap[session.UserID], invite.SignedInvite))
                    {
                        return;
                    }
                }

                // if not verified yet, add them and send back our own verification
                if (!room.Verified.ContainsKey(session.UserID))
                {
                    room.Verified[session.UserID] = true;

                    if (room.Active)
                    {
                        SendInviteProof(room, session); // someone sends us their proof, we send it back in return
                        SendStatus(session);            // send status here because now it will include private rooms
                    }
                }
            }

            if (Trust != null && !Trust.TrustMap.SafeContainsKey(session.UserID))
            {
                Trust.Research(session.UserID, 0, false);
            }

            if (showInvite)
            {
                Core.RunInGuiThread(NewInvite, session.UserID, room);
            }
        }
Пример #28
0
        public bool Connect(LocationData location)
        {
            if (location.UserID == Network.Local.UserID && location.Source.ClientID == Network.Local.ClientID)
                return false;

            ulong id = location.UserID ^ location.Source.ClientID;

            if (SessionMap.ContainsKey(id))
                return SessionMap[id].Status != SessionStatus.Closed;

            RudpSession session = new RudpSession(this, location.UserID, location.Source.ClientID, false);
            SessionMap[id] = session;

            session.Comm.AddAddress(new RudpAddress(new DhtAddress(location.IP, location.Source)));

            foreach (DhtAddress address in location.Proxies)
                session.Comm.AddAddress(new RudpAddress(address));

            foreach (DhtAddress server in location.TunnelServers)
                session.Comm.AddAddress(new RudpAddress(new DhtContact(location.Source, location.IP, location.TunnelClient, server)));

            session.Connect();

            return true;
        }
Пример #29
0
        private void SendPublicRequest(RudpSession session, ShareCollection collection)
        {
            collection.Status = "Requesting List";

            session.SendData(ServiceID, DataTypeSession, new PublicShareRequest(), true);
        }
Пример #30
0
        private void SendPublicRequest(RudpSession session, ShareCollection collection)
        {
            collection.Status = "Requesting List";

            session.SendData(ServiceID, DataTypeSession, new PublicShareRequest());
        }
Пример #31
0
        private void SendFileRequest(RudpSession session, SharedFile file)
        {
            foreach (DhtClient taraget in file.ToRequest.Where(c => c.UserID == session.UserID && c.ClientID == session.ClientID).ToArray())
                file.ToRequest.Remove(taraget);

            file.SaveLocal = false;
            session.SendData(ServiceID, DataTypeSession, file, true);

            file.TransferStatus = "Request sent to " + Core.GetName(session.UserID);
        }
Пример #32
0
 private void ReceivePublicRequest(RudpSession session)
 {
     if(Local.Hash != null)
         session.SendData(ServiceID, DataTypeSession, Local, true);
 }
Пример #33
0
        private void ReceivePublicDetails(RudpSession session, ShareCollection file)
        {
            ShareCollection collection;
            if (!Collections.SafeTryGetValue(session.UserID, out collection))
                return;

            collection.Key = file.Key;
            collection.Size = file.Size;
            collection.Hash = file.Hash;

            foreach (DhtClient done in collection.ToRequest.Where(t => t.ClientID == session.ClientID).ToArray())
                collection.ToRequest.Remove(done);

            FileDetails details = new FileDetails(ServiceID, DataTypePublic, file.Hash, file.Size, null);
            object[] args = new object[] { collection, (object) session.ClientID };

            DhtClient client = new DhtClient(session.UserID, session.ClientID);
            OpTransfer transfer = Core.Transfers.StartDownload(client.UserID, details, args, new EndDownloadHandler(CollectionDownloadFinished));
            transfer.AddPeer(client);
            transfer.DoSearch = false;

            collection.Status = "Starting List Download";
        }
Пример #34
0
        public void ReceiveCommPacket(G2ReceivedPacket raw, RudpPacket packet)
        {
            try
            {
                if (packet.PacketType == RudpPacketType.Light ||
                    packet.PacketType == RudpPacketType.LightAck)
                {
                    LightComm.ReceivePacket(raw, packet);
                    return;
                }

                // if a socket already set up
                lock (RudpControl.SocketMap)
                    if (RudpControl.SocketMap.ContainsKey(packet.PeerID))
                    {
                        RudpControl.SocketMap[packet.PeerID].RudpReceive(raw, packet, IsLookup);
                        return;
                    }

                // if starting new session
                if (packet.PacketType != RudpPacketType.Syn)
                {
                    return;
                }

                RudpSyn syn = new RudpSyn(packet.Payload);

                // prevent connection from self
                if (syn.SenderID == Local.UserID && syn.ClientID == Local.ClientID)
                {
                    return;
                }


                // find connecting session with same or unknown client id
                ulong id = syn.SenderID ^ syn.ClientID;

                if (RudpControl.SessionMap.ContainsKey(id))
                {
                    RudpSession session = RudpControl.SessionMap[id];

                    // if session id zero or matches forward
                    if ((session.Comm.State == RudpState.Connecting && session.Comm.RemotePeerID == 0) ||
                        (session.Comm.State != RudpState.Closed && session.Comm.RemotePeerID == syn.ConnID)) // duplicate syn
                    {
                        session.Comm.RudpReceive(raw, packet, IsLookup);
                        return;
                    }

                    else if (session.Comm.State == RudpState.Finishing)
                    {
                        RudpControl.RemoveSession(session);
                        // remove session, allow new one to be created
                    }
                    else
                    {
                        return;
                    }
                }

                // if clientid not in session, create new session
                RudpSession newSession = new RudpSession(RudpControl, syn.SenderID, syn.ClientID, true);

                RudpControl.SessionMap[id] = newSession;

                // send ack before sending our own syn (connect)
                // ack tells remote which address is good so that our syn's ack comes back quickly
                newSession.Comm.RudpReceive(raw, packet, IsLookup);

                newSession.Connect();


                UpdateLog("RUDP", "Inbound session accepted to ClientID " + syn.ClientID.ToString());
            }
            catch (Exception ex)
            {
                UpdateLog("Exception", "DhtNetwork::ReceiveCommPacket: " + ex.Message);
            }
        }
Пример #35
0
 public void Add(RudpSession rudp)
 {
     Add(rudp.UserID, rudp.ClientID);
 }
Пример #36
0
        void Session_Update(RudpSession session)
        {
            DhtClient client = new DhtClient(session.UserID, session.ClientID);

            if (session.Status == SessionStatus.Active)
            {
                Local.Files.LockReading(() =>
                {
                    foreach (SharedFile file in Local.Files.Where(s => s.ToRequest.Any(c => c.UserID == session.UserID && c.ClientID == session.ClientID)))
                        SendFileRequest(session, file);
                });

                ShareCollection collection;
                if (Collections.SafeTryGetValue(session.UserID, out collection))
                    if (collection.ToRequest.Any(t => t.ClientID == session.ClientID))
                        SendPublicRequest(session, collection);
            }
        }
Пример #37
0
        public void Session_Update(RudpSession session)
        {
            if (!ActiveUsers.Contains(session.UserID))
                return;

            IMStatus status = null;
            if (!IMMap.SafeTryGetValue(session.UserID, out status))
                return;

            if (session.Status == SessionStatus.Active)
            {
                // needs to be set here as well be cause we don't receive a keep alive from remote host on connect
                status.SetTTL(session.ClientID, SessionTimeout * 2);

                session.SendData(ServiceID, 0, new IMKeepAlive());
            }

            Update(status);
        }
Пример #38
0
        public bool Connect(DhtClient client)
        {
            if (client.UserID == Network.Local.UserID && client.ClientID == Network.Local.ClientID)
                return false;

            // sessionmap and socketmap both need to have the same # of entries
            // if a session is fin or closed, we need to wait for fins to complete before re-assigning entry
            if(SessionMap.ContainsKey(client.RoutingID))
                return SessionMap[client.RoutingID].Status != SessionStatus.Closed;

            RudpSession session = new RudpSession(this, client.UserID, client.ClientID, false);
            SessionMap[client.RoutingID] = session;

            if (Network.LightComm.Clients.ContainsKey(client.RoutingID))
                foreach (RudpAddress address in Network.LightComm.Clients[client.RoutingID].Addresses)
                    session.Comm.AddAddress(address);

            session.Connect();

            return true; // indicates that we will eventually notify caller with close, so caller can clean up
        }
Пример #39
0
        void Session_Data(RudpSession session, byte[] data)
        {
            IMStatus status = OpenStatus(session.UserID);

            G2Header root = new G2Header(data);

            if (G2Protocol.ReadPacket(root))
            {
                if (root.Name == IMPacket.Message)
                {
                    MessageData message = MessageData.Decode(root);

                    if(message.TargetID != 0)
                    {
                        Debug.Assert(session.UserID == Core.UserID);
                        if(session.UserID != Core.UserID)
                            return;

                        status = OpenStatus(message.TargetID);
                    }

                    ProcessMessage(status, new InstantMessage(Core, session, message));
                }

                if (root.Name == IMPacket.Alive)
                    status.SetTTL(session.ClientID, SessionTimeout * 2);
            }
        }
Пример #40
0
        public void RemoveSession(RudpSession session)
        {
            lock (SocketMap)
                if (SocketMap.ContainsKey(session.Comm.PeerID))
                    SocketMap.Remove(session.Comm.PeerID);

            SessionMap.Remove(session.RoutingID);
        }
Пример #41
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);
            }
        }
Пример #42
0
        private void ReceiveFileRequest(RudpSession session, SharedFile file)
        {
            DhtClient client = new DhtClient(session.UserID, session.ClientID);

            // check if file hash already on sharelist, if it is ignore
            bool alertUser = true;

            Local.Files.LockReading(() =>
            {
                SharedFile existing = Local.Files.Where(s => Utilities.MemCompare(s.Hash, file.Hash)).FirstOrDefault();

                // transfer exists, but this is from another source, or we started up and someone is trying
                // to resend file to us, which this auto adds the new source
                if (existing != null)
                {
                    if (!existing.Ignore)
                        StartTransfer(client, existing);

                    alertUser = false;
                }
            });

            if (!alertUser)
                return;

            file.Ignore = true; // turned off once accepted, allowing this item to be saved to header
            file.FileID = OpTransfer.GetFileID(ServiceID, file.Hash, file.Size);

            Local.Files.SafeAdd(file);
            Core.RunInGuiThread(GuiFileUpdate, file);

             file.TransferStatus =  "Request received from " + Core.GetName(session.UserID);

            Core.RunInGuiThread((System.Windows.Forms.MethodInvoker) delegate()
            {
                new AcceptFileForm(Core, client, file).ShowDialog();
            });
        }
Пример #43
0
        public RudpSocket(RudpSession session, bool listening)
        {
            Session = session;
            Core = session.Core;
            Network = Session.Network;

            Bandwidth = new BandwidthLog(Core.RecordBandwidthSeconds);

            Listening = listening;

            PeerID = (ushort)Core.RndGen.Next(1, ushort.MaxValue);

            lock (Session.RudpControl.SocketMap)
                Session.RudpControl.SocketMap[PeerID] = this;
        }