Example #1
0
        public void ReceiveDisconnectMessage(string guestId)
        {
            _log.LogInformation($"-> SignalRHost.{nameof(ReceiveDisconnectMessage)}");
            _log.LogDebug($"{nameof(guestId)} {guestId}");
            _log.LogDebug($"Local UserId: {PeerInfo.Message.PeerId}");

            var success = Guid.TryParse(guestId, out Guid guestGuid);

            if (!success ||
                guestGuid == Guid.Empty)
            {
                _log.LogWarning($"GuestId is not a GUID");
                return;
            }

            var existingGuest = ConnectedPeers.FirstOrDefault(g => g.PeerId == guestId);

            if (existingGuest == null)
            {
                _log.LogWarning("No existing guest found");
                return;
            }

            ConnectedPeers.Remove(existingGuest);
            UpdateConnectedPeers(null);
            RaiseUpdateEvent();
            _log.LogInformation($"SignalRHost.{nameof(ReceiveDisconnectMessage)} ->");
        }
Example #2
0
        internal void OperationSend(IDataPacket data, Guid peerGuid, Guid operationGuid)
        {
            var peer = ConnectedPeers.FirstOrDefault(p => p.Identity.Guid == peerGuid);

            if (peer == null)
            {
                throw new PeerNotFoundException();
            }
            peer.SendData(data, this, new SocketOperationContext(operationGuid));
        }
Example #3
0
        public void Send(IDataPacket data, Predicate <Peer.Peer> pred)
        {
            var peer = ConnectedPeers.FirstOrDefault(new Func <Peer.Peer, bool>(pred));

            if (peer == null)
            {
                throw new PeerNotFoundException();
            }
            peer.SendData(data, this, null);
        }
Example #4
0
        public void Send(IDataPacket data, int peerId)
        {
            var peer = ConnectedPeers.FirstOrDefault(p => p.Identity.Id == peerId);

            if (peer == null)
            {
                throw new PeerNotFoundException();
            }
            peer.SendData(data, this, null);
        }
Example #5
0
        /// <summary>
        /// Sends a packet containing data to a specific peer
        /// </summary>
        /// <param name="packet">The packet to be sent</param>
        /// <param name="targetPeer">A peer will be filtered from connected peers based on this predicate</param>
        public void SendPacket(IPacket packet, Predicate <Peer> targetPeer)
        {
            var target = ConnectedPeers.FirstOrDefault(x => targetPeer(x));

            if (target == null)
            {
                throw new Exception("No peer match for current predicate");
            }

            lock (_sendLock) target.Processor.SendPacket(packet, target.NetStream).Wait();
        }
Example #6
0
 private void OnToPeerMessage(PacketHeader packetHeader, Connection connection, ToPeerMessage incomingObject)
 {
     NetworkComms.Logger.Warn("Message: " + incomingObject.Message.Type.ToString());
     if (Address.Equals(incomingObject.SenderAddress))
     {
         Peer reciever = ConnectedPeers.FirstOrDefault(peer => peer.Address.Equals(incomingObject.RecieverAddress));
         try
         {
             if (reciever != null && reciever.ConnectedPeers.Contains(this))
             {
                 reciever.Connection.SendObject(incomingObject.GetType().Name, incomingObject);
             }
         }
         catch
         {
             reciever.OnError();
         }
     }
 }
Example #7
0
        public async Task ReceiveGuestMessage(string json)
        {
            _log.LogInformation($"-> SignalRHost.{nameof(ReceiveGuestMessage)}");
            //_log.LogDebug(json);

            var messagePeer = JsonConvert.DeserializeObject <PeerMessage>(json);

            if (messagePeer == null ||
                string.IsNullOrEmpty(messagePeer.PeerId))
            {
                _log.LogWarning($"No PeerId found");
                return;
            }

            _log.LogDebug($"PeerId: {messagePeer.PeerId}");

            var success = Guid.TryParse(messagePeer.PeerId, out Guid peerId);

            if (!success ||
                peerId == Guid.Empty)
            {
                _log.LogWarning($"PeerId is not a GUID");
                return;
            }

            if (messagePeer.PeerId == PeerInfo.Message.PeerId)
            {
                _log.LogTrace($"Self announce received");
                return;
            }

            var existingPeer = ConnectedPeers.FirstOrDefault(g => g.PeerId == messagePeer.PeerId);

            if (existingPeer == null)
            {
                _log.LogWarning("No existing guest found");
                UpdateConnectedPeers(messagePeer);
            }
            else
            {
                _log.LogDebug($"Existing peer found: Old name {existingPeer.DisplayName}");
                existingPeer.CustomName = messagePeer.CustomName;
                existingPeer.IsHost     = messagePeer.IsHost;

                UpdateConnectedPeers(null);
                _log.LogDebug($"Existing guest found: New name {existingPeer.DisplayName}");
            }

            // Refresh the clocks and the message (only when a guest registers)

            _log.LogDebug($"messagePeer.IsHost {messagePeer.IsHost}");

            if (!messagePeer.IsHost)
            {
                if (IsAnyClockRunning)
                {
                    await StartAllClocks(false);
                }

                await SendMessage(CurrentMessage.Value);
            }

            RaiseUpdateEvent();
            _log.LogInformation($"SignalRHost.{nameof(ReceiveGuestMessage)} ->");
        }