# HG changeset patch # User sl # Date 1407968598 -7200 # Node ID 274a6b27c3f9331f124eab6a6f0f00b892e94ffb # Parent e3d394dd0388fded1f680c379d3cb68bdfbdd61c Adding server closing notification to clients. diff -r e3d394dd0388 -r 274a6b27c3f9 Client/Client.cs --- a/Client/Client.cs Wed Aug 13 23:02:40 2014 +0200 +++ b/Client/Client.cs Thu Aug 14 00:23:18 2014 +0200 @@ -27,7 +27,8 @@ //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread); //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId); - MessageBox.Show("OnServerClosing()", "Client"); + //MessageBox.Show("OnServerClosing()", "Client"); + Program.iMainForm.CloseConnection(); } } diff -r e3d394dd0388 -r 274a6b27c3f9 Client/MainForm.cs --- a/Client/MainForm.cs Wed Aug 13 23:02:40 2014 +0200 +++ b/Client/MainForm.cs Thu Aug 14 00:23:18 2014 +0200 @@ -16,6 +16,7 @@ public partial class MainForm : Form { ClientOutput iClientOutput; + InstanceContext iInstanceContext; ClientInput iClientInput; public MainForm() @@ -33,11 +34,21 @@ private void MainForm_Load(object sender, EventArgs e) { iClientInput = new ClientInput(); - InstanceContext context = new InstanceContext(iClientInput); - iClientOutput = new ClientOutput(context); + iInstanceContext = new InstanceContext(iClientInput); + iClientOutput = new ClientOutput(iInstanceContext); iClientOutput.Connect("TestClient"); } + + public void CloseConnection() + { + //If we close the instance context after the client output it hangs + iInstanceContext.Close(); + iInstanceContext = null; + iClientOutput.Close(); + iClientOutput = null; + iClientInput = null; + } } } diff -r e3d394dd0388 -r 274a6b27c3f9 Client/Program.cs --- a/Client/Program.cs Wed Aug 13 23:02:40 2014 +0200 +++ b/Client/Program.cs Thu Aug 14 00:23:18 2014 +0200 @@ -8,6 +8,8 @@ { static class Program { + public static MainForm iMainForm; + /// /// The main entry point for the application. /// @@ -16,7 +18,8 @@ { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new MainForm()); + iMainForm = new MainForm(); + Application.Run(iMainForm); } } } diff -r e3d394dd0388 -r 274a6b27c3f9 Server/MainForm.cs --- a/Server/MainForm.cs Wed Aug 13 23:02:40 2014 +0200 +++ b/Server/MainForm.cs Thu Aug 14 00:23:18 2014 +0200 @@ -22,11 +22,16 @@ System.Drawing.Bitmap iBmp; bool iCreateBitmap; //Workaround render to bitmap issues when minimized ServiceHost iServiceHost; + /// + /// Our collection of clients + /// + public Dictionary iClients; public MainForm() { LastTickTime = DateTime.Now; iDisplay = new Display(); + iClients = new Dictionary(); InitializeComponent(); UpdateStatus(); @@ -360,9 +365,38 @@ public void StopServer() { //Tell connected client first? Is that possible? + BroadcastCloseEvent(); iServiceHost.Close(); } + public void BroadcastCloseEvent() + { + var inactiveClients = new List(); + foreach (var client in iClients) + { + //if (client.Key != eventData.ClientName) + { + try + { + client.Value.OnServerClosing(/*eventData*/); + } + catch (Exception ex) + { + inactiveClients.Add(client.Key); + } + } + } + + if (inactiveClients.Count > 0) + { + foreach (var client in inactiveClients) + { + iClients.Remove(client); + } + } + } + + } } diff -r e3d394dd0388 -r 274a6b27c3f9 Server/Servers.cs --- a/Server/Servers.cs Wed Aug 13 23:02:40 2014 +0200 +++ b/Server/Servers.cs Thu Aug 14 00:23:18 2014 +0200 @@ -2,6 +2,8 @@ using System.Windows.Forms; using System.Collections; using System.ServiceModel; +using System.Collections.Generic; +using System.Linq; namespace SharpDisplayManager { @@ -47,9 +49,19 @@ public void Connect(string aClientName) { IDisplayServiceCallback callback = OperationContext.Current.GetCallbackChannel(); - callback.OnConnected(); + //remove the old client if any + if (Program.iMainForm.iClients.Keys.Contains(aClientName)) + { + Program.iMainForm.iClients.Remove(aClientName); + } + //Register our client + Program.iMainForm.iClients.Add(aClientName, callback); + + //For some reason MP still hangs on that one + //callback.OnConnected(); } + } } diff -r e3d394dd0388 -r 274a6b27c3f9 Server/Services.cs --- a/Server/Services.cs Wed Aug 13 23:02:40 2014 +0200 +++ b/Server/Services.cs Thu Aug 14 00:23:18 2014 +0200 @@ -24,7 +24,7 @@ [OperationContract(IsOneWay = true)] void OnConnected(); - [OperationContract] + [OperationContract(IsOneWay = true)] void OnServerClosing(); } }