Client/Client.cs
changeset 78 f0dda362f77e
parent 77 042c1fa136b9
child 80 408ef0501cb7
     1.1 --- a/Client/Client.cs	Tue Dec 16 11:08:18 2014 +0100
     1.2 +++ b/Client/Client.cs	Tue Dec 16 13:24:12 2014 +0100
     1.3 @@ -96,14 +96,16 @@
     1.4  
     1.5  
     1.6      /// <summary>
     1.7 -    ///
     1.8 +    /// Handle connection with our Sharp Display Server.
     1.9 +    /// If the connection is faulted it will attempt to restart it.
    1.10      /// </summary>
    1.11      public class DisplayClient
    1.12      {
    1.13 -        Client iClient;
    1.14 -        Callback iCallback;
    1.15 +        private Client iClient;
    1.16 +        private Callback iCallback;
    1.17 +        private bool resetingConnection = false;
    1.18 +
    1.19          private MainForm MainForm { get; set; }
    1.20 -
    1.21          public string SessionId { get { return iClient.SessionId; } }
    1.22          public string Name { get; private set; }
    1.23          private TableLayout Layout { get; set; }
    1.24 @@ -117,12 +119,18 @@
    1.25              Fields = new DataField[]{};
    1.26          }
    1.27  
    1.28 +        /// <summary>
    1.29 +        /// Initialize our server connection.
    1.30 +        /// </summary>
    1.31          public void Open()
    1.32          {
    1.33              iCallback = new Callback(MainForm);
    1.34              iClient = new Client(iCallback);
    1.35          }
    1.36  
    1.37 +        /// <summary>
    1.38 +        /// Terminate our server connection.
    1.39 +        /// </summary>
    1.40          public void Close()
    1.41          {
    1.42              iClient.Close();
    1.43 @@ -131,26 +139,45 @@
    1.44              iCallback = null;
    1.45          }
    1.46  
    1.47 +        /// <summary>
    1.48 +        /// Tells whether a server connection is available.
    1.49 +        /// </summary>
    1.50 +        /// <returns>True if a server connection is available. False otherwise.</returns>
    1.51          public bool IsReady()
    1.52          {
    1.53              return (iClient != null && iCallback != null && iClient.IsReady());
    1.54          }
    1.55  
    1.56 +        /// <summary>
    1.57 +        /// Check if our server connection is available and attempt reset it if it isn't.
    1.58 +        /// This is notably dealing with timed out connections.
    1.59 +        /// </summary>
    1.60          public void CheckConnection()
    1.61          {
    1.62 -            if (!IsReady())
    1.63 +            if (!IsReady() && !resetingConnection)
    1.64              {
    1.65                  //Try to reconnect
    1.66                  Open();
    1.67  
    1.68 -                //On reconnect there is a bunch of properties we need to set
    1.69 -                if (Name != "")
    1.70 +                //Avoid stack overflow in case of persisting failure
    1.71 +                resetingConnection = true;
    1.72 +
    1.73 +                try
    1.74                  {
    1.75 -                    iClient.SetName(Name);
    1.76 +                    //On reconnect there is a bunch of properties we need to reset
    1.77 +                    if (Name != "")
    1.78 +                    {
    1.79 +                        iClient.SetName(Name);
    1.80 +                    }
    1.81 +
    1.82 +                    SetLayout(Layout);
    1.83 +                    SetFields(Fields);
    1.84                  }
    1.85 -
    1.86 -                SetLayout(Layout);
    1.87 -                SetFields(Fields);
    1.88 +                finally
    1.89 +                {
    1.90 +                    //Make sure our this state does not get out of sync
    1.91 +                    resetingConnection = true;
    1.92 +                }
    1.93              }
    1.94          }
    1.95