public void ExcludePeer(string displayedName) { if (CurrentPeer.PeerEntry.DisplayedName == displayedName) { var nextPeer = AvailablePeers.FirstOrDefault(x => x.PeerEntry.DisplayedName != displayedName); if (nextPeer == null) { MessagesHistoryTextBlock.Text = string.Empty; } else { OuputPeerMessages(nextPeer); CurrentPeer = nextPeer; nextPeer.PeerButton.Background = PEER_BUTTON_CURRENT_COLOR; } } _peersByButtons = _peersByButtons.Where(x => x.Value.PeerEntry.DisplayedName != displayedName) .ToDictionary(x => x.Key, x => x.Value); AvailablePeers = _peersByButtons.Values.ToList(); RefreshPeerPanel(); }
private void OnPeerResolverFound(object sender, ResolveProgressChangedEventArgs e) { var peer = e.PeerNameRecord; foreach (var ep in peer.EndPointCollection) { if (ep.AddressFamily == AddressFamily.InterNetwork) { var remoteUrl = string.Format("net.tcp://{0}:{1}/P2PService", ep.Address, ep.Port); var binding = new NetTcpBinding(); binding.Security.Mode = SecurityMode.None; try { var peerContext = AvailablePeers.FirstOrDefault(x => x.PeerEntry.Address == remoteUrl); if (peerContext == null) { IP2PService remoteService = ChannelFactory <IP2PService> .CreateChannel( binding, new EndpointAddress(remoteUrl)); var peerEntry = new PeerEntry(remoteService, remoteUrl); _foundPeers.Add(peerEntry); _logger.Debug($"New peer found: {peerEntry}"); } else { _foundPeers.Add(peerContext.PeerEntry); } } catch (Exception ex) when(ex is InvalidOperationException || ex is CommunicationException) { _logger.Error(ex, "Cannot process new peer: {0}", ex.Message); } } } }
private void SendMessage() { var messageToSend = MessageTextBox.Text; if (string.IsNullOrEmpty(messageToSend)) { return; } if (CurrentPeer != null) { try { var internalMessage = new Message(_user, messageToSend); var msgOverNet = new MessageOverNet(_user, messageToSend); var msgStr = JsonConvert.SerializeObject(msgOverNet); CurrentPeer.PeerEntry.ServiceProxy.SendMessage(msgStr); CurrentPeer.Messages.Add(internalMessage); MessagesHistoryTextBlock.Text += _user.DisplayedName + SENDER_SEPARATOR + messageToSend + Environment.NewLine; } catch (CommunicationException ex) { _logger.Error($"Connection aborted. Host: {CurrentPeer.PeerEntry.Address}"); MessageBox.Show($"User {CurrentPeer.PeerEntry.DisplayedName} disconected"); _peersByButtons = _peersByButtons.Where(x => x.Key != CurrentPeer.PeerButton).ToDictionary(x => x.Key, x => x.Value); AvailablePeers = _peersByButtons.Select(x => x.Value).ToList(); CurrentPeer = AvailablePeers.FirstOrDefault(); RefreshPeerPanel(); } } MessageTextBox.Text = string.Empty; }