# HG changeset patch # User sl # Date 1408101998 -7200 # Node ID 4c416d2878dd7db3f14846d352f8a783171ebe6f # Parent f19b04646b6ab7aae9359a6560fca275d7477270 Reworking our protocols. Client name now displayed in our clients tab. diff -r f19b04646b6a -r 4c416d2878dd Client/Client.cs --- a/Client/Client.cs Fri Aug 15 11:11:17 2014 +0200 +++ b/Client/Client.cs Fri Aug 15 13:26:38 2014 +0200 @@ -33,7 +33,7 @@ } - public void OnServerClosing() + public void OnCloseOrder() { //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread); //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId); @@ -64,16 +64,10 @@ : base(callbackInstance, new NetTcpBinding(SecurityMode.None, true), new EndpointAddress("net.tcp://localhost:8001/DisplayService")) { } - public void Connect(string aClientName) + public void SetName(string aClientName) { Name = aClientName; - Channel.Connect(aClientName); - } - - public void Disconnect() - { - Channel.Disconnect(Name); - Name = ""; + Channel.SetName(aClientName); } public void SetText(int aLineIndex, string aText) @@ -81,13 +75,14 @@ Channel.SetText(aLineIndex, aText); } - public void SetTexts(System.Collections.Generic.IList aTexts) { Channel.SetTexts(aTexts); } - - + public int ClientCount() + { + return Channel.ClientCount(); + } } } diff -r f19b04646b6a -r 4c416d2878dd Client/MainForm.cs --- a/Client/MainForm.cs Fri Aug 15 11:11:17 2014 +0200 +++ b/Client/MainForm.cs Fri Aug 15 13:26:38 2014 +0200 @@ -39,10 +39,11 @@ iClient = new Client(instanceContext); //Connect using unique name - string name = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt"); - iClient.Connect(name); + //string name = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt"); + string name = "Client-" + (iClient.ClientCount() - 1); + iClient.SetName(name); //Text = Text + ": " + name; - Text = Text + ": " + iClient.SessionId; + Text = "[[" + name + "]] " + iClient.SessionId; } @@ -67,7 +68,6 @@ //We are in the proper thread if (IsClientReady()) { - //iClient.Disconnect(); Trace.TraceInformation("Closing client: " + iClient.SessionId); iClient.Close(); Trace.TraceInformation("Closed client: " + iClient.SessionId); @@ -99,11 +99,6 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { - if (IsClientReady()) //Could catch exception instead - { - iClient.Disconnect(); - } - CloseConnectionThreadSafe(); } diff -r f19b04646b6a -r 4c416d2878dd Interface/Interface.cs --- a/Interface/Interface.cs Fri Aug 15 11:11:17 2014 +0200 +++ b/Interface/Interface.cs Fri Aug 15 13:26:38 2014 +0200 @@ -14,17 +14,41 @@ SessionMode = SessionMode.Required)] public interface IDisplayService { + /// + /// Set the name of this client. + /// Name is a convenient way to recognize your client. + /// Naming you client is not mandatory. + /// In the absence of a name the session ID is often used instead. + /// + /// [OperationContract(IsOneWay = true)] - void Connect(string aClientName); + void SetName(string aClientName); + /// + /// Put the given text in the given field on your display. + /// Fields are often just lines of text. + /// + /// + /// [OperationContract(IsOneWay = true)] - void Disconnect(string aClientName); + void SetText(int aFieldIndex, string aText); - [OperationContract(IsOneWay = true)] - void SetText(int aLineIndex, string aText); - + /// + /// Allows a client to set multiple text fields at once. + /// First text in the list is set into field index 0. + /// Last text in the list is set into field index N-1. + /// + /// [OperationContract(IsOneWay = true)] void SetTexts(System.Collections.Generic.IList aTexts); + + /// + /// Provides the number of clients currently connected + /// + /// + [OperationContract()] + int ClientCount(); + } @@ -33,8 +57,12 @@ [OperationContract(IsOneWay = true)] void OnConnected(); + /// + /// Tell our client to close its connection. + /// Notably sent when the server is shutting down. + /// [OperationContract(IsOneWay = true)] - void OnServerClosing(); + void OnCloseOrder(); } diff -r f19b04646b6a -r 4c416d2878dd Server/MainForm.cs --- a/Server/MainForm.cs Fri Aug 15 11:11:17 2014 +0200 +++ b/Server/MainForm.cs Fri Aug 15 13:26:38 2014 +0200 @@ -357,6 +357,7 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { StopServer(); + e.Cancel = iClosing; } public void StartServer() @@ -373,16 +374,24 @@ public void StopServer() { - //Tell connected client first? Is that possible? - - if (iClients.Count>0) + if (iClients.Count > 0 && !iClosing) { //Tell our clients + iClosing = true; BroadcastCloseEvent(); } - - //iServiceHost.Close(); - + else if (iClosing) + { + if (MessageBox.Show("Force exit?", "Waiting for clients...", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) + { + iClosing = false; //We make sure we force close if asked twice + } + } + else + { + //We removed that as it often lags for some reason + //iServiceHost.Close(); + } } public void BroadcastCloseEvent() @@ -397,7 +406,7 @@ try { Trace.TraceInformation("BroadcastCloseEvent - " + client.Key); - client.Value.OnServerClosing(/*eventData*/); + client.Value.OnCloseOrder(/*eventData*/); } catch (Exception ex) { @@ -449,6 +458,7 @@ public delegate void RemoveClientDelegate(string aSessionId); public delegate void SetTextDelegate(int aLineIndex, string aText); public delegate void SetTextsDelegate(System.Collections.Generic.IList aTexts); + public delegate void SetClientNameDelegate(string aSessionId, string aName); /// @@ -494,7 +504,16 @@ { Program.iMainForm.iClients.Remove(aSessionId); Program.iMainForm.treeViewClients.Nodes.Remove(Program.iMainForm.treeViewClients.Nodes.Find(aSessionId, false)[0]); - } + } + + if (iClosing && iClients.Count == 0) + { + //We were closing our form + //All clients are now closed + //Just resume our close operation + iClosing = false; + Close(); + } } } @@ -554,7 +573,38 @@ } } } + } + + /// + /// + /// + /// + /// + public void SetClientNameThreadSafe(string aSessionId, string aName) + { + if (this.InvokeRequired) + { + //Not in the proper thread, invoke ourselves + SetClientNameDelegate d = new SetClientNameDelegate(SetClientNameThreadSafe); + this.Invoke(d, new object[] { aSessionId, aName }); + } + else + { + //We are in the proper thread + //Remove this session from both client collection and UI tree view + if (Program.iMainForm.iClients.Keys.Contains(aSessionId)) + { + //Change our session node text + TreeNode node = Program.iMainForm.treeViewClients.Nodes.Find(aSessionId, false)[0]; + node.Text = aName; + //Add a child with SessionId + node.Nodes.Add(new TreeNode(aSessionId)); + + //Program.iMainForm.iClients.Remove(aSessionId); + //Program.iMainForm.treeViewClients.Nodes.Remove(Program.iMainForm.treeViewClients.Nodes.Find(aSessionId, false)[0]); + } + } } } diff -r f19b04646b6a -r 4c416d2878dd Server/Servers.cs --- a/Server/Servers.cs Fri Aug 15 11:11:17 2014 +0200 +++ b/Server/Servers.cs Fri Aug 15 13:26:38 2014 +0200 @@ -20,6 +20,7 @@ class DisplayServer : IDisplayService, IDisposable { public string SessionId { get; set; } + public string Name { get; set; } DisplayServer() { @@ -51,8 +52,10 @@ } // - public void Connect(string aClientName) + public void SetName(string aClientName) { + Name = aClientName; + Program.iMainForm.SetClientNameThreadSafe(SessionId, Name); //Disconnect(aClientName); //Register our client and its callback interface @@ -64,17 +67,9 @@ } /// - public void Disconnect(string aClientName) + public int ClientCount() { - //remove the old client if any - /* - if (Program.iMainForm.iClients.Keys.Contains(aClientName)) - { - Program.iMainForm.iClients.Remove(aClientName); - Program.iMainForm.treeViewClients.Nodes.Remove(Program.iMainForm.treeViewClients.Nodes.Find(aClientName,false)[0]); - } - */ - + return Program.iMainForm.iClients.Count; }