SharpDisplay: Migrating to new robust client scheme.
3 using System.Collections.Generic;
6 using System.Threading.Tasks;
7 using System.Windows.Forms;
9 using System.ServiceModel;
10 using System.ServiceModel.Channels;
13 namespace SharpDisplay
18 [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
19 public class Callback : ICallback, IDisposable
21 private DisplayClient DisplayClient { get; set; }
23 public Callback(DisplayClient aClient)
25 DisplayClient = aClient;
28 public void OnConnected()
30 //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread);
31 //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId);
33 MessageBox.Show("OnConnected()", "Client");
37 public void OnCloseOrder()
39 DisplayClient.Close();
40 //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread);
41 //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId);
43 //MessageBox.Show("OnServerClosing()", "Client");
44 //MainForm.CloseConnectionThreadSafe();
45 //MainForm.CloseThreadSafe();
59 [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
60 public class Client : DuplexClientBase<IService>
62 public string SessionId { get { return InnerChannel.SessionId; } }
64 public Client(ICallback aCallback)
65 : base(new InstanceContext(aCallback), new NetTcpBinding(SecurityMode.None, true), new EndpointAddress("net.tcp://localhost:8001/DisplayService"))
68 public void SetName(string aClientName)
70 Channel.SetName(aClientName);
73 public void SetLayout(TableLayout aLayout)
75 Channel.SetLayout(aLayout);
78 public void SetField(DataField aField)
80 Channel.SetField(aField);
83 public void SetFields(System.Collections.Generic.IList<DataField> aFields)
85 Channel.SetFields(aFields);
88 public int ClientCount()
90 return Channel.ClientCount();
95 return State == CommunicationState.Opened || State == CommunicationState.Created;
101 /// Handle connection with our Sharp Display Server.
102 /// If the connection is faulted it will attempt to restart it.
104 public class DisplayClient
106 private Client iClient;
107 private Callback iCallback;
108 private bool resetingConnection = false;
110 //private MainForm MainForm { get; set; }
111 public string SessionId { get { return iClient.SessionId; } }
112 public string Name { get; private set; }
113 private TableLayout Layout { get; set; }
114 private System.Collections.Generic.IList<DataField> Fields { get; set; }
117 public DisplayClient(/*MainForm aMainForm*/)
119 //MainForm = aMainForm;
121 Fields = new DataField[]{};
125 /// Initialize our server connection.
129 iCallback = new Callback(this);
130 iClient = new Client(iCallback);
134 /// Terminate our server connection.
145 /// Tells whether a server connection is available.
147 /// <returns>True if a server connection is available. False otherwise.</returns>
148 public bool IsReady()
150 return (iClient != null && iCallback != null && iClient.IsReady());
154 /// Check if our server connection is available and attempt reset it if it isn't.
155 /// This is notably dealing with timed out connections.
157 public void CheckConnection()
159 if (!IsReady() && !resetingConnection)
164 //Avoid stack overflow in case of persisting failure
165 resetingConnection = true;
169 //On reconnect there is a bunch of properties we need to reset
172 iClient.SetName(Name);
176 iClient.SetFields(Fields);
180 //Make sure our this state does not get out of sync
181 resetingConnection = true;
186 public void SetName(string aClientName)
190 iClient.SetName(aClientName);
194 public void SetLayout(TableLayout aLayout)
198 iClient.SetLayout(aLayout);
202 /// Set the specified field.
204 /// <param name="aField"></param>
205 /// <returns>True if the specified field was set client side. False means you need to redefine all your fields using CreateFields.</returns>
206 public bool SetField(DataField aField)
209 bool fieldFound = false;
210 foreach (DataField field in Fields)
212 if (field.Index == aField.Index)
214 //Update our field then
224 //Field not found, make to use SetFields with all your fields at least once after setting your layout.
229 iClient.SetField(aField);
234 /// Use this function when updating existing fields.
236 /// <param name="aFields"></param>
237 public bool SetFields(System.Collections.Generic.IList<DataField> aFields)
239 int fieldFoundCount = 0;
240 foreach (DataField fieldUpdate in aFields)
243 foreach (DataField existingField in Fields)
245 if (existingField.Index == fieldUpdate.Index)
247 //Update our field then
248 Fields[i] = fieldUpdate;
250 //Move on to the next field
258 if (fieldFoundCount!=aFields.Count)
260 //Field not found, make sure to use SetFields with all your fields at least once after setting your layout.
265 iClient.SetFields(aFields);
270 /// Use this function when creating your fields.
272 /// <param name="aFields"></param>
273 public void CreateFields(System.Collections.Generic.IList<DataField> aFields)
277 iClient.SetFields(aFields);
280 public int ClientCount()
283 return iClient.ClientCount();