private void buttonSearchServices_Click(object sender, EventArgs e) { if (!(NetworkingToolkit.ValidateIPAddress(ip) && NetworkingToolkit.ValidatePort(port))) { configurarToolStripMenuItem.PerformClick(); } listViewPCs.Items.Clear(); connection.ServerIP = ip; connection.ServerPort = port; try { foreach (var pc in connection.GetPlayers()) { ListViewItem temp = new ListViewItem(pc.IP.ToString()); temp.SubItems.Add(pc.Name); temp.SubItems.Add((pc.Displays as List <WCFScreenInformation>).Count.ToString()); temp.Tag = pc; listViewPCs.Items.Add(temp); } } catch { } }
private void buttonOk_Click(object sender, EventArgs e) { bool allOk = false; if (!NetworkingToolkit.ValidateIPAddress(textBoxIP.Text)) { Transition.run(textBoxIP, "BackColor", Color.MistyRose, new TransitionType_EaseInEaseOut(500)); } else { allOk = true; Transition.run(textBoxIP, "BackColor", SystemColors.Window, new TransitionType_EaseInEaseOut(500)); } if (!NetworkingToolkit.ValidatePort(textBoxPort.Text)) { allOk = false; Transition.run(textBoxPort, "BackColor", Color.MistyRose, new TransitionType_EaseInEaseOut(500)); } else { allOk = true; Transition.run(textBoxPort, "BackColor", SystemColors.Window, new TransitionType_EaseInEaseOut(500)); } if (allOk) { this.ip = textBoxIP.Text; this.port = textBoxPort.Text; this.DialogResult = System.Windows.Forms.DialogResult.OK; this.Close(); } }
private void UpdatePlayerStatus() { if (connection != null) { SetStatusLine("Geral", "Estado", connection == null ? "Desligado" : connection.PlayerWindowIsOpen() ? "Ligado" : "Desligado"); SetStatusLine("Geral", "Nome do dispositivo", NetworkingToolkit.resolveIP(connection.ServerIP)); SetStatusLine("Geral", "Endereço IP", connection.ServerIP); SetStatusLine("Geral", "Monitores", connection.GetDisplayInformation().Length + ""); UpdatePlayerStatusHelper(); } }
private void StartService(string serverIP, string serverPort, string localPort) { if (!(NetworkingToolkit.ValidateIPAddress(serverIP) && NetworkingToolkit.ValidatePort(serverPort) && NetworkingToolkit.ValidatePort(localPort))) { throw new ArgumentException() { Source = "ListeningForm.StartService(string serverIP, string serverPort, string localPort)" } } ; ////endereço do player Uri baseAddress = new Uri(String.Format("net.tcp://{0}:{1}/PlayerService/{2}", NetworkingToolkit.LocalIPAddress, localPort, Guid.NewGuid())); ////endpoint para onde as mensagens de announcement serão enviadas Uri announcementEndpointAddress = new Uri(String.Format("net.tcp://{0}:{1}/Announcement", serverIP, serverPort)); //criar o host do serviço serviceHost = new ServiceHost(typeof(PlayerService), baseAddress); try { ////Adicionar um endpoint para o serviço //NetTcpBinding tcpBindingService = new NetTcpBinding(); //tcpBindingService.Security.Mode = SecurityMode.None; //Alterar a autenticação para um modelo melhor ////tcpBindingService.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.None; //tcpBindingService.MaxReceivedMessageSize = 10000000; //tcpBindingService.MaxBufferSize = 10000000; //tcpBindingService.MaxBufferPoolSize = 10000000; //ServiceEndpoint netTcpEndpoint = serviceHost.AddServiceEndpoint(typeof(IPlayer), tcpBindingService, string.Empty); //Criar um endpoint para o announcement server, que aponta para o DiscoveryProxy NetTcpBinding tcpBindingAnnouncement = new NetTcpBinding(); tcpBindingAnnouncement.Security.Mode = SecurityMode.None; //Alterar a autenticação para um modelo melhor ////http://nerdwords.blogspot.pt/2008/01/wcf-error-socket-connection-was-aborted.html AnnouncementEndpoint announcementEndpoint = new AnnouncementEndpoint(tcpBindingAnnouncement, new EndpointAddress(announcementEndpointAddress)); //Criar um DiscoveryBehaviour e adicionar o endpoint ServiceDiscoveryBehavior serviceDiscoveryBehavior = new ServiceDiscoveryBehavior(); serviceDiscoveryBehavior.AnnouncementEndpoints.Add(announcementEndpoint); #if EXPOSE_METADATA //Adicionar um endpoint MEX (Metadata EXchange) por TCP System.ServiceModel.Channels.BindingElement bindingElement = new System.ServiceModel.Channels.TcpTransportBindingElement(); System.ServiceModel.Channels.CustomBinding binding = new System.ServiceModel.Channels.CustomBinding(bindingElement); System.ServiceModel.Description.ServiceMetadataBehavior metadataBehavior = serviceHost.Description.Behaviors.Find <System.ServiceModel.Description.ServiceMetadataBehavior>(); if (metadataBehavior == null) { metadataBehavior = new System.ServiceModel.Description.ServiceMetadataBehavior(); serviceHost.Description.Behaviors.Add(metadataBehavior); } serviceHost.AddServiceEndpoint(typeof(System.ServiceModel.Description.IMetadataExchange), binding, "MEX"); #endif //Adicionar o serviceDiscoveryBehavior ao host para poder ser descoberto serviceHost.Description.Behaviors.Add(serviceDiscoveryBehavior); serviceHost.Open(); Clipboard.SetText(baseAddress.ToString()); } catch (CommunicationException e) { Log(e); } catch (TimeoutException e) { Log(e); } catch (Exception ex) { Log(ex); } } void serviceHost_UnknownMessageReceived(object sender, UnknownMessageReceivedEventArgs e) { Log("Mensagem desconhecida recebida"); Log(e.Message.ToString()); } void serviceHost_RefreshState(object sender, EventArgs e) { this.RefreshState(); }
private void buttonRandomize_Click(object sender, EventArgs e) { textBoxLocalPort.Text = NetworkingToolkit.RandomPort().ToString(); }
private void buttonConnect_Click(object sender, EventArgs e) { this.BeginInvoke((MethodInvoker) delegate { (sender as Button).Enabled = false; if ((sender as Button).Text == "Ligar") { try { #region Checks string serverIP = textBoxServerIP.Text, serverPort = textBoxServerPort.Text, localPort = textBoxLocalPort.Text; bool ready = true; if (!NetworkingToolkit.ValidateIPAddress(serverIP)) { ready = false; Transition.run(textBoxServerIP, "BackColor", Color.MistyRose, new TransitionType_EaseInEaseOut(300)); } else { Transition.run(textBoxServerIP, "BackColor", SystemColors.Window, new TransitionType_EaseInEaseOut(300)); } if (!NetworkingToolkit.ValidatePort(serverPort)) { ready = false; Transition.run(textBoxServerPort, "BackColor", Color.MistyRose, new TransitionType_EaseInEaseOut(300)); } else { Transition.run(textBoxServerPort, "BackColor", SystemColors.Window, new TransitionType_EaseInEaseOut(300)); } if (!NetworkingToolkit.ValidatePort(localPort)) { ready = false; Transition.run(textBoxLocalPort, "BackColor", Color.MistyRose, new TransitionType_EaseInEaseOut(300)); } else { Transition.run(textBoxLocalPort, "BackColor", SystemColors.Window, new TransitionType_EaseInEaseOut(300)); } #endregion if (ready) { this.Log("A iniciar o serviço"); StartService(serverIP, serverPort, localPort); } if (serviceHost.State == CommunicationState.Opened || serviceHost.State == CommunicationState.Opening) { this.Log(string.Format("Ligado ao servidor {1}:{2}{0}Endpoint: {3}", Environment.NewLine, serverIP, serverPort, serviceHost.Description.Endpoints[0].Address)); (sender as Button).Text = "Desligar"; } else { this.Log(string.Format("Problema a ligar ao servidor {0}:{1}", serverIP, serverPort)); } } catch (ArgumentException ex) { #if DEBUG this.Log(ex); #endif } catch (Exception ex) { #if DEBUG this.Log(ex); #endif } } else { try { this.Log("A fechar o serviço"); serviceHost.Close(); this.Log("Serviço fechado com sucesso"); } catch (Exception ex) { this.Log(ex); this.Log("A abortar o serviço"); serviceHost.Abort(); this.Log("Serviço abortado"); } if (serviceHost.State == CommunicationState.Closed || serviceHost.State == CommunicationState.Closing) { (sender as Button).Text = "Ligar"; } } (sender as Button).Enabled = true; }); }
static void Main(string[] args) { string port = ""; bool validOption = false; #region Escolher a porta para os probes do { Console.WriteLine("Port for probe (where clients will connect to find servers) " + Environment.NewLine + "[ENTER: 8001 | r: random | value: 1 - 65535]:"); port = Console.ReadLine(); switch (port.ToLower()) { case "r": port = NetworkingToolkit.RandomPort().ToString(); validOption = true; break; case "": port = "8001"; validOption = true; break; default: int i; if (int.TryParse(port, out i)) { validOption = i < 65535; } else { validOption = false; } break; } Console.WriteLine((validOption ? "Listening for probes on port " : "Invalid option: ") + port + Environment.NewLine); }while (!validOption); Uri probeEndpointAddress = new Uri("net.tcp://" + NetworkingToolkit.LocalIPAddress + ":" + port + "/Probe"); #endregion #region Escolher a porta para os announcements do { Console.WriteLine("Port for announcement (where servers will announce themselves) " + Environment.NewLine + "[ENTER: 9021 | r: random | value: 1 - 65535]:"); port = Console.ReadLine(); switch (port.ToLower()) { case "r": port = NetworkingToolkit.RandomPort().ToString(); validOption = true; break; case "": port = "9021"; validOption = true; break; default: int i; if (int.TryParse(port, out i)) { validOption = i < 65535; } else { validOption = false; } break; } Console.WriteLine(validOption ? "Listening for announcements on port " + port : "Invalid option"); }while (!validOption); Uri announcementEndpointAddress = new Uri("net.tcp://" + NetworkingToolkit.LocalIPAddress + ":" + port + "/Announcement"); #endregion //Host the DiscoveryProxy service ServiceDiscoveryProxy proxyServiceInstance = new ServiceDiscoveryProxy(); ServiceHost proxyServiceHost = new ServiceHost(proxyServiceInstance); try { //Add DiscoveryEndpoint to receive Probe and Resolve messages NetTcpBinding bindingDiscovery = new NetTcpBinding(); bindingDiscovery.Security.Mode = SecurityMode.None; bindingDiscovery.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.None; DiscoveryEndpoint discoveryEndpoint = new DiscoveryEndpoint(bindingDiscovery, new EndpointAddress(probeEndpointAddress)); discoveryEndpoint.IsSystemEndpoint = false; //Add AnnouncementEndpoint to receive HAI and KTHXBYE announcement messages NetTcpBinding bindingAnnouncement = new NetTcpBinding(); bindingAnnouncement.Security.Mode = SecurityMode.None; //bindingAnnouncement.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.None; AnnouncementEndpoint announcementEndpoint = new AnnouncementEndpoint(bindingAnnouncement, new EndpointAddress(announcementEndpointAddress)); proxyServiceHost.AddServiceEndpoint(discoveryEndpoint); proxyServiceHost.AddServiceEndpoint(announcementEndpoint); proxyServiceInstance.PrintDiscoveryMetadata += ServiceDiscoveryProxy_PrintDiscoveryMetadata; proxyServiceHost.Open(); Console.WriteLine("Proxy Service started."); Console.WriteLine("Probe endpoint: " + discoveryEndpoint.Address); Console.WriteLine("Announcement endpoint: " + announcementEndpoint.Address); Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate the service."); Console.WriteLine(); Console.ReadLine(); proxyServiceHost.Close(); } catch (CommunicationException e) { Console.WriteLine(e.Message); } catch (TimeoutException e) { Console.WriteLine(e.Message); } if (proxyServiceHost.State != CommunicationState.Closed) { Console.WriteLine("Aborting the service..."); proxyServiceHost.Abort(); } }