Ejemplo n.º 1
0
        private void buttonConnect_Click(object sender, EventArgs e)
        {
            try
            {
                if (buttonConnect.Text == "Connect")
                {
                    // Creates a client object that 'lives' here on the client.
                    client = new RemotingClient();

                    listBoxLog.Items.Add("Created client with ID " + client.ClientID);

                    // Hook into the event exposed on the Sink object so we can transfer a server
                    // message through to this class.
                    client.clientCallback += OnMessageReceived;

                    // Hook into the event to allow the server to send (other) notifications
                    // (Currently only asserted when clients connect or disconnect from server by
                    //  means of registering respective event handlers associated with their client
                    //  references)
                    client.serverNotificationCallback += OnServerNotification;

                    if (channel == null)
                    {
                        int clientChannel = FindUnusedPort(IPAddress.Loopback);

                        listBoxLog.Items.Add("Using (unused) client port " + clientChannel);

                        if (clientChannel != 0)
                        {
                            BinaryServerFormatterSinkProvider serverProv = new BinaryServerFormatterSinkProvider();
                            serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
                            BinaryClientFormatterSinkProvider clientProv = new BinaryClientFormatterSinkProvider();

                            IDictionary props = new Hashtable();
                            props["port"] = clientChannel;

                            // Register a client channel so the server an communicate back - it needs a channel
                            // opened for the callback to the CallbackSink object that is anchored on the client!
                            // channel = new HttpChannel(clientChannel++);
                            channel = new HttpChannel(props, clientProv, serverProv);
                        }
                        else
                        {
                            throw new Exception("Couldn't find unused client port!");
                        }
                    }

                    // Registers a channel with the channel services
                    ChannelServices.RegisterChannel(channel, false);

                    // Now create a transparent proxy to the server component
                    MarshalByRefObject obj = (MarshalByRefObject)RemotingServices.Connect(typeof(RemotingServer), "http://" + textBoxServerIPAddress.Text + ":" + textBoxServerPortConnect.Text + "/Server");
                    server = obj as RemotingServer;

                    // Create messaging callback objects
                    clientCallback = new RemotingClient.ClientCallback(client.SendMessage);

                    // Register callback
                    if (clientCallback != null)
                    {
                        // Simple messaging callback for broadcasting of messages from one client
                        server.RegisterMessageCallback(clientCallback);
                        // Messaging callback associated with the client to allow for sending messages to a particular client
                        server.RegisterMessageCallback(clientCallback, client);
                    }

                    buttonConnect.Text               = "Disconnect";
                    textBoxServerIPAddress.Enabled   = false;
                    textBoxServerPortConnect.Enabled = false;

                    foreach (Control control in tabPageServer.Controls)
                    {
                        control.Enabled = false;
                    }
                }
                else
                {
                    if (server != null)
                    {
                        if (clientCallback != null)
                        {
                            server.UnregisterMessageCallback(clientCallback);
                        }

                        if (client != null)
                        {
                            server.UnregisterMessageCallback(client);
                        }
                    }

                    if (client != null)
                    {
                        client.clientCallback             -= OnMessageReceived;
                        client.serverNotificationCallback -= OnServerNotification;
                    }

                    if (channel != null)
                    {
                        ChannelDataStore cds = (ChannelDataStore)channel.ChannelData;

                        foreach (string s in cds.ChannelUris)
                        {
                            channel.StopListening(s);
                        }

                        ChannelServices.UnregisterChannel(channel);
                    }

                    clientCallback = null;
                    server         = null;
                    client         = null;
                    channel        = null;

                    OnServerNotification();

                    buttonConnect.Text               = "Connect";
                    textBoxServerIPAddress.Enabled   = true;
                    textBoxServerPortConnect.Enabled = true;

                    foreach (Control control in tabPageServer.Controls)
                    {
                        control.Enabled = true;
                    }
                }
            }
            catch (Exception ex)
            {
                listBoxLog.Items.Add(ex.Message);
            }
        }