diff -r f2d8620e2434 -r 3d8af0e778f4 GUI/SharpDisplayClient.cs --- a/GUI/SharpDisplayClient.cs Tue Feb 03 10:14:18 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,291 +0,0 @@ - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; -using SharpDisplay; -using System.ServiceModel; -using System.ServiceModel.Channels; - - -namespace SharpDisplay -{ - /// - /// - /// - [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)] - public class Callback : ICallback, IDisposable - { - private DisplayClient DisplayClient { get; set; } - - public Callback(DisplayClient aClient) - { - DisplayClient = aClient; - } - - public void OnConnected() - { - //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread); - //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId); - - MessageBox.Show("OnConnected()", "Client"); - } - - - public void OnCloseOrder() - { - DisplayClient.Close(); - //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread); - //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId); - - //MessageBox.Show("OnServerClosing()", "Client"); - //MainForm.CloseConnectionThreadSafe(); - //MainForm.CloseThreadSafe(); - } - - //From IDisposable - public void Dispose() - { - - } - } - - - /// - /// - /// - [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)] - public class Client : DuplexClientBase - { - public string SessionId { get { return InnerChannel.SessionId; } } - - public Client(ICallback aCallback) - : base(new InstanceContext(aCallback), new NetTcpBinding(SecurityMode.None, true), new EndpointAddress("net.tcp://localhost:8001/DisplayService")) - { } - - public void SetName(string aClientName) - { - Channel.SetName(aClientName); - } - - public void SetLayout(TableLayout aLayout) - { - Channel.SetLayout(aLayout); - } - - public void SetField(DataField aField) - { - Channel.SetField(aField); - } - - public void SetFields(System.Collections.Generic.IList aFields) - { - Channel.SetFields(aFields); - } - - public int ClientCount() - { - return Channel.ClientCount(); - } - - public bool IsReady() - { - return State == CommunicationState.Opened || State == CommunicationState.Created; - } - } - - - /// - /// Handle connection with our Sharp Display Server. - /// If the connection is faulted it will attempt to restart it. - /// - public class DisplayClient - { - private Client iClient; - private Callback iCallback; - private bool resetingConnection = false; - - //private MainForm MainForm { get; set; } - public string SessionId { get { return iClient.SessionId; } } - public string Name { get; private set; } - private TableLayout Layout { get; set; } - private System.Collections.Generic.IList Fields { get; set; } - - - public DisplayClient(/*MainForm aMainForm*/) - { - //MainForm = aMainForm; - Name = ""; - Fields = new DataField[]{}; - } - - /// - /// Initialize our server connection. - /// - public void Open() - { - iCallback = new Callback(this); - iClient = new Client(iCallback); - } - - /// - /// Terminate our server connection. - /// - public void Close() - { - iClient.Close(); - iClient = null; - iCallback.Dispose(); - iCallback = null; - } - - /// - /// Tells whether a server connection is available. - /// - /// True if a server connection is available. False otherwise. - public bool IsReady() - { - return (iClient != null && iCallback != null && iClient.IsReady()); - } - - /// - /// Check if our server connection is available and attempt reset it if it isn't. - /// This is notably dealing with timed out connections. - /// - public void CheckConnection() - { - if (!IsReady() && !resetingConnection) - { - //Try to reconnect - Open(); - - //Avoid stack overflow in case of persisting failure - resetingConnection = true; - - try - { - //On reconnect there is a bunch of properties we need to reset - if (Name != "") - { - iClient.SetName(Name); - } - - SetLayout(Layout); - iClient.SetFields(Fields); - } - finally - { - //Make sure our this state does not get out of sync - resetingConnection = true; - } - } - } - - public void SetName(string aClientName) - { - Name = aClientName; - CheckConnection(); - iClient.SetName(aClientName); - } - - - public void SetLayout(TableLayout aLayout) - { - Layout = aLayout; - CheckConnection(); - iClient.SetLayout(aLayout); - } - - /// - /// Set the specified field. - /// - /// - /// True if the specified field was set client side. False means you need to redefine all your fields using CreateFields. - public bool SetField(DataField aField) - { - int i = 0; - bool fieldFound = false; - foreach (DataField field in Fields) - { - if (field.Index == aField.Index) - { - //Update our field then - Fields[i] = aField; - fieldFound = true; - break; - } - i++; - } - - if (!fieldFound) - { - //Field not found, make to use SetFields with all your fields at least once after setting your layout. - return false; - } - - CheckConnection(); - iClient.SetField(aField); - return true; - } - - /// - /// Use this function when updating existing fields. - /// - /// - public bool SetFields(System.Collections.Generic.IList aFields) - { - int fieldFoundCount = 0; - foreach (DataField fieldUpdate in aFields) - { - int i = 0; - foreach (DataField existingField in Fields) - { - if (existingField.Index == fieldUpdate.Index) - { - //Update our field then - Fields[i] = fieldUpdate; - fieldFoundCount++; - //Move on to the next field - break; - } - i++; - } - } - - // - if (fieldFoundCount!=aFields.Count) - { - //Field not found, make sure to use SetFields with all your fields at least once after setting your layout. - return false; - } - - CheckConnection(); - iClient.SetFields(aFields); - return true; - } - - /// - /// Use this function when creating your fields. - /// - /// - public void CreateFields(System.Collections.Generic.IList aFields) - { - Fields = aFields; - CheckConnection(); - iClient.SetFields(aFields); - } - - public int ClientCount() - { - CheckConnection(); - return iClient.ClientCount(); - } - - - - } - - -}