Client/Server duplex is still a mess in C#.
authorsl
Thu, 14 Aug 2014 18:37:23 +0200
changeset 29c4e03315035c
parent 28 2aa3974c485f
child 30 c375286d1a1c
Client/Server duplex is still a mess in C#.
Client/Client.cs
Client/MainForm.Designer.cs
Client/MainForm.cs
Server/MainForm.Designer.cs
Server/MainForm.cs
Server/Servers.cs
     1.1 --- a/Client/Client.cs	Thu Aug 14 10:41:44 2014 +0200
     1.2 +++ b/Client/Client.cs	Thu Aug 14 18:37:23 2014 +0200
     1.3 @@ -32,6 +32,7 @@
     1.4  
     1.5              //MessageBox.Show("OnServerClosing()", "Client");
     1.6              Program.iMainForm.CloseConnection();
     1.7 +            Program.iMainForm.Close();
     1.8          }
     1.9  
    1.10          //From IDisposable
     2.1 --- a/Client/MainForm.Designer.cs	Thu Aug 14 10:41:44 2014 +0200
     2.2 +++ b/Client/MainForm.Designer.cs	Thu Aug 14 18:37:23 2014 +0200
     2.3 @@ -48,7 +48,7 @@
     2.4              this.ClientSize = new System.Drawing.Size(443, 252);
     2.5              this.Controls.Add(this.buttonSetText);
     2.6              this.Name = "MainForm";
     2.7 -            this.Text = "Sharp Display Client";
     2.8 +            this.Text = "Client";
     2.9              this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);
    2.10              this.Load += new System.EventHandler(this.MainForm_Load);
    2.11              this.ResumeLayout(false);
     3.1 --- a/Client/MainForm.cs	Thu Aug 14 10:41:44 2014 +0200
     3.2 +++ b/Client/MainForm.cs	Thu Aug 14 18:37:23 2014 +0200
     3.3 @@ -37,24 +37,38 @@
     3.4              InstanceContext instanceContext = new InstanceContext(iCallback);
     3.5              iClient = new Client(instanceContext);
     3.6  
     3.7 -            iClient.Connect("TestClient");
     3.8 +            //Connect using unique name
     3.9 +            string name = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt");
    3.10 +            iClient.Connect(name);
    3.11 +            Text = Text + ": " + name;
    3.12  
    3.13          }
    3.14  
    3.15          public void CloseConnection()
    3.16          {
    3.17 -            iClient.Close();
    3.18 +            if (IsClientReady())
    3.19 +            {
    3.20 +                //iClient.Disconnect();
    3.21 +                iClient.Close();
    3.22 +            }
    3.23 +
    3.24              iClient = null;
    3.25              iCallback = null;
    3.26          }
    3.27  
    3.28          private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    3.29          {
    3.30 -            if (iClient != null) //Could catch exception instead
    3.31 +            if (IsClientReady()) //Could catch exception instead
    3.32              {
    3.33                  iClient.Disconnect();
    3.34 -                CloseConnection();
    3.35              }
    3.36 +
    3.37 +            CloseConnection();
    3.38 +        }
    3.39 +
    3.40 +        public bool IsClientReady()
    3.41 +        {
    3.42 +            return (iClient != null && iClient.State == CommunicationState.Opened);
    3.43          }
    3.44      }
    3.45  }
     4.1 --- a/Server/MainForm.Designer.cs	Thu Aug 14 10:41:44 2014 +0200
     4.2 +++ b/Server/MainForm.Designer.cs	Thu Aug 14 18:37:23 2014 +0200
     4.3 @@ -31,6 +31,7 @@
     4.4              this.components = new System.ComponentModel.Container();
     4.5              this.tabControl = new System.Windows.Forms.TabControl();
     4.6              this.tabPageDisplay = new System.Windows.Forms.TabPage();
     4.7 +            this.checkBoxFixedPitchFontOnly = new System.Windows.Forms.CheckBox();
     4.8              this.buttonSuspend = new System.Windows.Forms.Button();
     4.9              this.buttonStartClient = new System.Windows.Forms.Button();
    4.10              this.checkBoxReverseScreen = new System.Windows.Forms.CheckBox();
    4.11 @@ -55,7 +56,7 @@
    4.12              this.toolStripStatusLabelSpring = new System.Windows.Forms.ToolStripStatusLabel();
    4.13              this.toolStripStatusLabelPower = new System.Windows.Forms.ToolStripStatusLabel();
    4.14              this.toolStripStatusLabelFps = new System.Windows.Forms.ToolStripStatusLabel();
    4.15 -            this.checkBoxFixedPitchFontOnly = new System.Windows.Forms.CheckBox();
    4.16 +            this.buttonCloseClients = new System.Windows.Forms.Button();
    4.17              this.tabControl.SuspendLayout();
    4.18              this.tabPageDisplay.SuspendLayout();
    4.19              this.panelDisplay.SuspendLayout();
    4.20 @@ -79,6 +80,7 @@
    4.21              // 
    4.22              // tabPageDisplay
    4.23              // 
    4.24 +            this.tabPageDisplay.Controls.Add(this.buttonCloseClients);
    4.25              this.tabPageDisplay.Controls.Add(this.checkBoxFixedPitchFontOnly);
    4.26              this.tabPageDisplay.Controls.Add(this.buttonSuspend);
    4.27              this.tabPageDisplay.Controls.Add(this.buttonStartClient);
    4.28 @@ -101,6 +103,16 @@
    4.29              this.tabPageDisplay.Text = "Display";
    4.30              this.tabPageDisplay.UseVisualStyleBackColor = true;
    4.31              // 
    4.32 +            // checkBoxFixedPitchFontOnly
    4.33 +            // 
    4.34 +            this.checkBoxFixedPitchFontOnly.AutoSize = true;
    4.35 +            this.checkBoxFixedPitchFontOnly.Location = new System.Drawing.Point(113, 275);
    4.36 +            this.checkBoxFixedPitchFontOnly.Name = "checkBoxFixedPitchFontOnly";
    4.37 +            this.checkBoxFixedPitchFontOnly.Size = new System.Drawing.Size(120, 17);
    4.38 +            this.checkBoxFixedPitchFontOnly.TabIndex = 17;
    4.39 +            this.checkBoxFixedPitchFontOnly.Text = "Fixed pitch font only";
    4.40 +            this.checkBoxFixedPitchFontOnly.UseVisualStyleBackColor = true;
    4.41 +            // 
    4.42              // buttonSuspend
    4.43              // 
    4.44              this.buttonSuspend.Location = new System.Drawing.Point(7, 253);
    4.45 @@ -401,15 +413,15 @@
    4.46              this.toolStripStatusLabelFps.Size = new System.Drawing.Size(26, 17);
    4.47              this.toolStripStatusLabelFps.Text = "FPS";
    4.48              // 
    4.49 -            // checkBoxFixedPitchFontOnly
    4.50 +            // buttonCloseClients
    4.51              // 
    4.52 -            this.checkBoxFixedPitchFontOnly.AutoSize = true;
    4.53 -            this.checkBoxFixedPitchFontOnly.Location = new System.Drawing.Point(113, 275);
    4.54 -            this.checkBoxFixedPitchFontOnly.Name = "checkBoxFixedPitchFontOnly";
    4.55 -            this.checkBoxFixedPitchFontOnly.Size = new System.Drawing.Size(120, 17);
    4.56 -            this.checkBoxFixedPitchFontOnly.TabIndex = 17;
    4.57 -            this.checkBoxFixedPitchFontOnly.Text = "Fixed pitch font only";
    4.58 -            this.checkBoxFixedPitchFontOnly.UseVisualStyleBackColor = true;
    4.59 +            this.buttonCloseClients.Location = new System.Drawing.Point(7, 224);
    4.60 +            this.buttonCloseClients.Name = "buttonCloseClients";
    4.61 +            this.buttonCloseClients.Size = new System.Drawing.Size(75, 23);
    4.62 +            this.buttonCloseClients.TabIndex = 18;
    4.63 +            this.buttonCloseClients.Text = "Close Clients";
    4.64 +            this.buttonCloseClients.UseVisualStyleBackColor = true;
    4.65 +            this.buttonCloseClients.Click += new System.EventHandler(this.buttonCloseClients_Click);
    4.66              // 
    4.67              // MainForm
    4.68              // 
    4.69 @@ -466,6 +478,7 @@
    4.70          private System.Windows.Forms.Button buttonStartClient;
    4.71          private System.Windows.Forms.Button buttonSuspend;
    4.72          private System.Windows.Forms.CheckBox checkBoxFixedPitchFontOnly;
    4.73 +        private System.Windows.Forms.Button buttonCloseClients;
    4.74      }
    4.75  }
    4.76  
     5.1 --- a/Server/MainForm.cs	Thu Aug 14 10:41:44 2014 +0200
     5.2 +++ b/Server/MainForm.cs	Thu Aug 14 18:37:23 2014 +0200
     5.3 @@ -30,6 +30,7 @@
     5.4          /// Our collection of clients
     5.5          /// </summary>
     5.6          public Dictionary<string, IDisplayServiceCallback> iClients;
     5.7 +        public bool iClosing;
     5.8  
     5.9          public MainForm()
    5.10          {
    5.11 @@ -372,8 +373,15 @@
    5.12          public void StopServer()
    5.13          {
    5.14              //Tell connected client first? Is that possible?
    5.15 -            BroadcastCloseEvent();
    5.16 -            iServiceHost.Close();
    5.17 +
    5.18 +            if (iClients.Count>0)
    5.19 +            {
    5.20 +                //Tell our clients
    5.21 +                BroadcastCloseEvent();
    5.22 +            }
    5.23 +
    5.24 +            //iServiceHost.Close();
    5.25 +
    5.26          }
    5.27  
    5.28          public void BroadcastCloseEvent()
    5.29 @@ -422,6 +430,11 @@
    5.30              }
    5.31          }
    5.32  
    5.33 +        private void buttonCloseClients_Click(object sender, EventArgs e)
    5.34 +        {
    5.35 +            BroadcastCloseEvent();
    5.36 +        }
    5.37 +
    5.38  
    5.39  
    5.40      }
     6.1 --- a/Server/Servers.cs	Thu Aug 14 10:41:44 2014 +0200
     6.2 +++ b/Server/Servers.cs	Thu Aug 14 18:37:23 2014 +0200
     6.3 @@ -67,6 +67,7 @@
     6.4              {
     6.5                  Program.iMainForm.iClients.Remove(aClientName);
     6.6              }
     6.7 +
     6.8          }
     6.9  
    6.10