Beispiel #1
0
        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
            {
            }
        }
Beispiel #2
0
        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 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 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;
            });
        }