Adding server closing notification to clients.
1.1 --- a/Client/Client.cs Wed Aug 13 23:02:40 2014 +0200
1.2 +++ b/Client/Client.cs Thu Aug 14 00:23:18 2014 +0200
1.3 @@ -27,7 +27,8 @@
1.4 //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread);
1.5 //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId);
1.6
1.7 - MessageBox.Show("OnServerClosing()", "Client");
1.8 + //MessageBox.Show("OnServerClosing()", "Client");
1.9 + Program.iMainForm.CloseConnection();
1.10 }
1.11 }
1.12
2.1 --- a/Client/MainForm.cs Wed Aug 13 23:02:40 2014 +0200
2.2 +++ b/Client/MainForm.cs Thu Aug 14 00:23:18 2014 +0200
2.3 @@ -16,6 +16,7 @@
2.4 public partial class MainForm : Form
2.5 {
2.6 ClientOutput iClientOutput;
2.7 + InstanceContext iInstanceContext;
2.8 ClientInput iClientInput;
2.9
2.10 public MainForm()
2.11 @@ -33,11 +34,21 @@
2.12 private void MainForm_Load(object sender, EventArgs e)
2.13 {
2.14 iClientInput = new ClientInput();
2.15 - InstanceContext context = new InstanceContext(iClientInput);
2.16 - iClientOutput = new ClientOutput(context);
2.17 + iInstanceContext = new InstanceContext(iClientInput);
2.18 + iClientOutput = new ClientOutput(iInstanceContext);
2.19
2.20 iClientOutput.Connect("TestClient");
2.21
2.22 }
2.23 +
2.24 + public void CloseConnection()
2.25 + {
2.26 + //If we close the instance context after the client output it hangs
2.27 + iInstanceContext.Close();
2.28 + iInstanceContext = null;
2.29 + iClientOutput.Close();
2.30 + iClientOutput = null;
2.31 + iClientInput = null;
2.32 + }
2.33 }
2.34 }
3.1 --- a/Client/Program.cs Wed Aug 13 23:02:40 2014 +0200
3.2 +++ b/Client/Program.cs Thu Aug 14 00:23:18 2014 +0200
3.3 @@ -8,6 +8,8 @@
3.4 {
3.5 static class Program
3.6 {
3.7 + public static MainForm iMainForm;
3.8 +
3.9 /// <summary>
3.10 /// The main entry point for the application.
3.11 /// </summary>
3.12 @@ -16,7 +18,8 @@
3.13 {
3.14 Application.EnableVisualStyles();
3.15 Application.SetCompatibleTextRenderingDefault(false);
3.16 - Application.Run(new MainForm());
3.17 + iMainForm = new MainForm();
3.18 + Application.Run(iMainForm);
3.19 }
3.20 }
3.21 }
4.1 --- a/Server/MainForm.cs Wed Aug 13 23:02:40 2014 +0200
4.2 +++ b/Server/MainForm.cs Thu Aug 14 00:23:18 2014 +0200
4.3 @@ -22,11 +22,16 @@
4.4 System.Drawing.Bitmap iBmp;
4.5 bool iCreateBitmap; //Workaround render to bitmap issues when minimized
4.6 ServiceHost iServiceHost;
4.7 + /// <summary>
4.8 + /// Our collection of clients
4.9 + /// </summary>
4.10 + public Dictionary<string, IDisplayServiceCallback> iClients;
4.11
4.12 public MainForm()
4.13 {
4.14 LastTickTime = DateTime.Now;
4.15 iDisplay = new Display();
4.16 + iClients = new Dictionary<string, IDisplayServiceCallback>();
4.17
4.18 InitializeComponent();
4.19 UpdateStatus();
4.20 @@ -360,9 +365,38 @@
4.21 public void StopServer()
4.22 {
4.23 //Tell connected client first? Is that possible?
4.24 + BroadcastCloseEvent();
4.25 iServiceHost.Close();
4.26 }
4.27
4.28 + public void BroadcastCloseEvent()
4.29 + {
4.30 + var inactiveClients = new List<string>();
4.31 + foreach (var client in iClients)
4.32 + {
4.33 + //if (client.Key != eventData.ClientName)
4.34 + {
4.35 + try
4.36 + {
4.37 + client.Value.OnServerClosing(/*eventData*/);
4.38 + }
4.39 + catch (Exception ex)
4.40 + {
4.41 + inactiveClients.Add(client.Key);
4.42 + }
4.43 + }
4.44 + }
4.45 +
4.46 + if (inactiveClients.Count > 0)
4.47 + {
4.48 + foreach (var client in inactiveClients)
4.49 + {
4.50 + iClients.Remove(client);
4.51 + }
4.52 + }
4.53 + }
4.54 +
4.55 +
4.56
4.57 }
4.58 }
5.1 --- a/Server/Servers.cs Wed Aug 13 23:02:40 2014 +0200
5.2 +++ b/Server/Servers.cs Thu Aug 14 00:23:18 2014 +0200
5.3 @@ -2,6 +2,8 @@
5.4 using System.Windows.Forms;
5.5 using System.Collections;
5.6 using System.ServiceModel;
5.7 +using System.Collections.Generic;
5.8 +using System.Linq;
5.9
5.10 namespace SharpDisplayManager
5.11 {
5.12 @@ -47,9 +49,19 @@
5.13 public void Connect(string aClientName)
5.14 {
5.15 IDisplayServiceCallback callback = OperationContext.Current.GetCallbackChannel<IDisplayServiceCallback>();
5.16 - callback.OnConnected();
5.17 + //remove the old client if any
5.18 + if (Program.iMainForm.iClients.Keys.Contains(aClientName))
5.19 + {
5.20 + Program.iMainForm.iClients.Remove(aClientName);
5.21 + }
5.22 + //Register our client
5.23 + Program.iMainForm.iClients.Add(aClientName, callback);
5.24 +
5.25 + //For some reason MP still hangs on that one
5.26 + //callback.OnConnected();
5.27 }
5.28
5.29 +
5.30 }
5.31
5.32 }
6.1 --- a/Server/Services.cs Wed Aug 13 23:02:40 2014 +0200
6.2 +++ b/Server/Services.cs Thu Aug 14 00:23:18 2014 +0200
6.3 @@ -24,7 +24,7 @@
6.4 [OperationContract(IsOneWay = true)]
6.5 void OnConnected();
6.6
6.7 - [OperationContract]
6.8 + [OperationContract(IsOneWay = true)]
6.9 void OnServerClosing();
6.10 }
6.11 }