Adding disconnect function to our interface.
authorsl
Thu, 14 Aug 2014 09:57:44 +0200
changeset 26a6fb2b2f73b0
parent 25 6f10207a89a8
child 27 9c49c04fc620
Adding disconnect function to our interface.
Client/Client.cs
Client/MainForm.Designer.cs
Client/MainForm.cs
Interface/Interface.cs
Server/Servers.cs
     1.1 --- a/Client/Client.cs	Thu Aug 14 09:30:14 2014 +0200
     1.2 +++ b/Client/Client.cs	Thu Aug 14 09:57:44 2014 +0200
     1.3 @@ -14,7 +14,7 @@
     1.4      /// <summary>
     1.5      ///
     1.6      /// </summary>
     1.7 -    public partial class ClientInput : IDisplayServiceCallback, IDisposable
     1.8 +    public partial class Callback : IDisplayServiceCallback, IDisposable
     1.9      {
    1.10          public void OnConnected()
    1.11          {
    1.12 @@ -45,17 +45,26 @@
    1.13      /// <summary>
    1.14      ///
    1.15      /// </summary>
    1.16 -    public partial class ClientOutput : DuplexClientBase<IDisplayService>, IDisplayService
    1.17 +    public partial class Client : DuplexClientBase<IDisplayService>
    1.18      {
    1.19 -        public ClientOutput(InstanceContext callbackInstance)
    1.20 +        private string Name { get; set; }
    1.21 +
    1.22 +        public Client(InstanceContext callbackInstance)
    1.23              : base(callbackInstance, new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:8001/DisplayService"))
    1.24          { }
    1.25  
    1.26          public void Connect(string aClientName)
    1.27          {
    1.28 +            Name = aClientName;
    1.29              Channel.Connect(aClientName);
    1.30          }
    1.31  
    1.32 +        public void Disconnect()
    1.33 +        {
    1.34 +            Channel.Disconnect(Name);
    1.35 +            Name = "";
    1.36 +        }
    1.37 +
    1.38          public void SetText(int aLineIndex, string aText)
    1.39          {
    1.40              Channel.SetText(aLineIndex, aText);
    1.41 @@ -67,6 +76,5 @@
    1.42              Channel.SetTexts(aTexts);
    1.43          }
    1.44  
    1.45 -
    1.46      }
    1.47  }
     2.1 --- a/Client/MainForm.Designer.cs	Thu Aug 14 09:30:14 2014 +0200
     2.2 +++ b/Client/MainForm.Designer.cs	Thu Aug 14 09:57:44 2014 +0200
     2.3 @@ -49,6 +49,7 @@
     2.4              this.Controls.Add(this.buttonSetText);
     2.5              this.Name = "MainForm";
     2.6              this.Text = "Sharp Display Client";
     2.7 +            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);
     2.8              this.Load += new System.EventHandler(this.MainForm_Load);
     2.9              this.ResumeLayout(false);
    2.10  
     3.1 --- a/Client/MainForm.cs	Thu Aug 14 09:30:14 2014 +0200
     3.2 +++ b/Client/MainForm.cs	Thu Aug 14 09:57:44 2014 +0200
     3.3 @@ -15,8 +15,8 @@
     3.4  {
     3.5      public partial class MainForm : Form
     3.6      {
     3.7 -        ClientOutput iClientOutput;
     3.8 -        ClientInput iClientInput;
     3.9 +        Client iClient;
    3.10 +        Callback iCallback;
    3.11  
    3.12          public MainForm()
    3.13          {
    3.14 @@ -27,25 +27,34 @@
    3.15          {
    3.16              //iClient.SetText(0,"Top");
    3.17              //iClient.SetText(1, "Bottom");
    3.18 -            iClientOutput.SetTexts(new string[] { "Top", "Bottom" });
    3.19 +            iClient.SetTexts(new string[] { "Top", "Bottom" });
    3.20          }
    3.21  
    3.22          private void MainForm_Load(object sender, EventArgs e)
    3.23          {
    3.24 -            iClientInput = new ClientInput();
    3.25 +            iCallback = new Callback();
    3.26              //Instance context is then managed by our client class
    3.27 -            InstanceContext instanceContext = new InstanceContext(iClientInput);
    3.28 -            iClientOutput = new ClientOutput(instanceContext);
    3.29 +            InstanceContext instanceContext = new InstanceContext(iCallback);
    3.30 +            iClient = new Client(instanceContext);
    3.31  
    3.32 -            iClientOutput.Connect("TestClient");
    3.33 +            iClient.Connect("TestClient");
    3.34  
    3.35          }
    3.36  
    3.37          public void CloseConnection()
    3.38          {
    3.39 -            iClientOutput.Close();
    3.40 -            iClientOutput = null;
    3.41 -            iClientInput = null;
    3.42 +            iClient.Close();
    3.43 +            iClient = null;
    3.44 +            iCallback = null;
    3.45 +        }
    3.46 +
    3.47 +        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    3.48 +        {
    3.49 +            if (iClient != null) //Could catch exception instead
    3.50 +            {
    3.51 +                iClient.Disconnect();
    3.52 +                CloseConnection();
    3.53 +            }
    3.54          }
    3.55      }
    3.56  }
     4.1 --- a/Interface/Interface.cs	Thu Aug 14 09:30:14 2014 +0200
     4.2 +++ b/Interface/Interface.cs	Thu Aug 14 09:57:44 2014 +0200
     4.3 @@ -17,6 +17,9 @@
     4.4          void Connect(string aClientName);
     4.5  
     4.6          [OperationContract(IsOneWay = true)]
     4.7 +        void Disconnect(string aClientName);
     4.8 +
     4.9 +        [OperationContract(IsOneWay = true)]
    4.10          void SetText(int aLineIndex, string aText);
    4.11  
    4.12          [OperationContract(IsOneWay = true)]
     5.1 --- a/Server/Servers.cs	Thu Aug 14 09:30:14 2014 +0200
     5.2 +++ b/Server/Servers.cs	Thu Aug 14 09:57:44 2014 +0200
     5.3 @@ -49,19 +49,26 @@
     5.4          //
     5.5          public void Connect(string aClientName)
     5.6          {
     5.7 +            Disconnect(aClientName);
     5.8 +
     5.9 +            //Register our client and its callback interface
    5.10              IDisplayServiceCallback callback = OperationContext.Current.GetCallbackChannel<IDisplayServiceCallback>();
    5.11 -            //remove the old client if any
    5.12 -            if (Program.iMainForm.iClients.Keys.Contains(aClientName))
    5.13 -            {
    5.14 -                Program.iMainForm.iClients.Remove(aClientName);
    5.15 -            }
    5.16 -            //Register our client
    5.17              Program.iMainForm.iClients.Add(aClientName, callback);
    5.18  
    5.19              //For some reason MP still hangs on that one
    5.20              //callback.OnConnected();
    5.21          }
    5.22  
    5.23 +        ///
    5.24 +        public void Disconnect(string aClientName)
    5.25 +        {
    5.26 +            //remove the old client if any
    5.27 +            if (Program.iMainForm.iClients.Keys.Contains(aClientName))
    5.28 +            {
    5.29 +                Program.iMainForm.iClients.Remove(aClientName);
    5.30 +            }
    5.31 +        }
    5.32 +
    5.33  
    5.34      }
    5.35