Exemplo n.º 1
0
        private void OnConnectToPeerWithTrackerMessage(PacketHeader packetHeader, Connection connection, ConnectToPeerWithTrackerMessage incomingObject)
        {
            Peer peerToConnect = allPeers.FirstOrDefault(peer => peer.Address.Equals(incomingObject.RecieverAddress));

            if (peerToConnect != null)
            {
                if (!ConnectedPeers.Contains(peerToConnect))
                {
                    ConnectedPeers.Add(peerToConnect);
                }
                if (!peerToConnect.ConnectedPeers.Contains(this))
                {
                    peerToConnect.ConnectedPeers.Add(this);

                    try
                    {
                        peerToConnect.Connection.SendObject(incomingObject.GetType().Name, incomingObject);
                    }
                    catch
                    {
                        peerToConnect.OnError();
                    }
                }
            }
            //если трекер не нашел пир адресат то отправляем отправителю дисконнект адресата
            else
            {
                var message = new PeerDisconnectMessage(incomingObject.SenderAddress);

                Connection.SendObject(message.GetType().Name, message);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Handle incoming packets related to connection setup
        /// </summary>
        /// <param name="message">The message to handle</param>
        /// <returns>A PacketResponse indicating if the message was consumed</returns>
        public PacketResponse HandlePacket(Message message)
        {
            switch (message.MessageType)
            {
            case MessageType.HeartbeatRequest:
                var msg = new Message(MessageType.Heartbeat, message.Sender)
                {
                    DataAsString = Dns.GetHostName()
                };
                Outbox.SendUnencryptedMessage(msg);
                if (!ConnectedPeers.ContainsKey(message.Sender))
                {
                    ConnectedPeers.Add(message.Sender, message.DataAsString);
                }
                return(PacketResponse.Accept);

            case MessageType.Heartbeat:
                if (!ConnectedPeers.ContainsKey(message.Sender))
                {
                    ConnectedPeers.Add(message.Sender, message.DataAsString);
                }
                return(PacketResponse.Accept);

            case MessageType.EncryptionPacket:
                SecurityManager.GetInstance().SetKeysFromPacket(message.Data);
                Inbox.GetInstance().UseEncryption = true;
                ConnectionDone = true;

                return(PacketResponse.AcceptAndFinish);

            default:
                return(PacketResponse.Decline);
            }
        }
Exemplo n.º 3
0
        private void UpdateConnectedPeers(PeerMessage message)
        {
            if (message != null)
            {
                ConnectedPeers.Add(message);
            }

            NamedGuests = ConnectedPeers
                          .Where(g => !string.IsNullOrEmpty(g.CustomName) &&
                                 g.CustomName != PeerMessage.AnonymousName &&
                                 !g.IsHost).ToList();

            AnonymousGuests = ConnectedPeers
                              .Count(g => (string.IsNullOrEmpty(g.CustomName) ||
                                           g.CustomName == PeerMessage.AnonymousName) &&
                                     !g.IsHost);

            NamedHosts = ConnectedPeers
                         .Where(g => !string.IsNullOrEmpty(g.CustomName) &&
                                g.CustomName != PeerMessage.AnonymousName &&
                                g.IsHost).ToList();

            AnonymousHosts = ConnectedPeers
                             .Count(g => (string.IsNullOrEmpty(g.CustomName) ||
                                          g.CustomName == PeerMessage.AnonymousName) &&
                                    g.IsHost);

            RaiseUpdateEvent();

            foreach (var guest in ConnectedPeers)
            {
                _log.LogDebug($"{guest.CustomName}");
                _log.LogDebug($"{guest.DisplayName}");
            }
        }
Exemplo n.º 4
0
        private async void AcceptAsync()
        {
            while (true)
            {
                var client = await _uNetSock.AcceptTcpClientAsync();

                var peer = new Peer(client, this, Settings);

                //Subscribe to peer events
                peer.OnPeerDisconnected += PeerDisconnect;
                peer.OnPacketReceived   += (o, e) => { if (OnPacketReceived != null)
                                                       {
                                                           OnPacketReceived(o, e);
                                                       }
                };
                peer.InternalOnPacketReceived += InternalOnPacketReceived;
                peer.Processor.OnPacketSent   += (o, e) => { if (OnPacketSent != null)
                                                             {
                                                                 OnPacketSent(null, new PacketEventArgs(null, e.Packet, e.RawPacketSize));
                                                             }
                };

                ConnectedPeers.Add(peer);

                if (_debug)
                {
                    Debug.Print("Peer connected from: " + peer.RemoteEndPoint);
                }

                if (OnPeerConnected != null)
                {
                    OnPeerConnected(null, new PeerConnectedEventArgs(peer));
                }
            }
        }
Exemplo n.º 5
0
 private void AcceptPeer(Peer.Peer newPeer)
 {
     newPeer.OnPeerSynchronized +=
         (sender, e) => OnPeerConnected.Raise(this, new ChannelEventArgs(this, e.Peer));
     newPeer.OnPeerDisconnected +=
         (sender, e) =>
     {
         e.Peer.Dispose();
         lock (_lockObj)
             ConnectedPeers.Remove(e.Peer);
     };
     newPeer.OnRawPacketReceived        += RawPacketReceived;
     newPeer.OnSequenceFragmentReceived += (sender, e) => OnSequenceFragmentReceived.Raise(sender, e);
     lock (_lockObj)
         ConnectedPeers.Add(newPeer);
 }