Client/Client.cs
author sl
Mon, 15 Dec 2014 18:52:42 +0100
changeset 73 926cdf23b485
parent 72 fd0bb39a7818
child 74 60d584bad780
permissions -rw-r--r--
Client now recovers from faulty states and timeout.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.Windows.Forms;
     7 using SharpDisplay;
     8 using System.ServiceModel;
     9 using System.ServiceModel.Channels;
    10 
    11 
    12 namespace SharpDisplayClient
    13 {
    14     /// <summary>
    15     ///
    16     /// </summary>
    17     [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
    18     public class Callback : ICallback, IDisposable
    19     {
    20         private MainForm MainForm { get; set; }
    21 
    22         public Callback(MainForm aMainForm)
    23         {
    24             MainForm = aMainForm;
    25         }
    26 
    27         public void OnConnected()
    28         {
    29             //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread);
    30             //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId);
    31 
    32             MessageBox.Show("OnConnected()", "Client");
    33         }
    34 
    35 
    36         public void OnCloseOrder()
    37         {
    38             //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread);
    39             //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId);
    40 
    41             //MessageBox.Show("OnServerClosing()", "Client");
    42             MainForm.CloseConnectionThreadSafe();
    43             MainForm.CloseThreadSafe();
    44         }
    45 
    46         //From IDisposable
    47         public void Dispose()
    48         {
    49 
    50         }
    51     }
    52 
    53 
    54     /// <summary>
    55     ///
    56     /// </summary>
    57     [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
    58     public class Client : DuplexClientBase<IService>
    59     {
    60         public string Name { get; set; }
    61         public string SessionId { get { return InnerChannel.SessionId; } }
    62 
    63         public Client(ICallback aCallback)
    64             : base(new InstanceContext(aCallback), new NetTcpBinding(SecurityMode.None, true), new EndpointAddress("net.tcp://localhost:8001/DisplayService"))
    65         { }
    66 
    67         public void SetName(string aClientName)
    68         {
    69             Name = aClientName;
    70             Channel.SetName(aClientName);
    71         }
    72 
    73 
    74         public void SetLayout(TableLayout aLayout)
    75         {
    76             Channel.SetLayout(aLayout);
    77         }
    78 
    79         public void SetText(DataField aField)
    80         {
    81             Channel.SetText(aField);
    82         }
    83 
    84         public void SetTexts(System.Collections.Generic.IList<DataField> aFields)
    85         {
    86             Channel.SetTexts(aFields);
    87         }
    88 
    89         public void SetBitmap(DataField aField)
    90         {
    91             Channel.SetBitmap(aField);
    92         }
    93 
    94         public int ClientCount()
    95         {
    96             return Channel.ClientCount();
    97         }
    98 
    99         public bool IsReady()
   100         {
   101             return State == CommunicationState.Opened;
   102         }
   103     }
   104 
   105 
   106     /// <summary>
   107     ///
   108     /// </summary>
   109     public class DisplayClient
   110     {
   111         Client iClient;
   112         Callback iCallback;
   113         private MainForm MainForm { get; set; }
   114 
   115         public string Name { get; set; }
   116         public string SessionId { get { return iClient.SessionId; } }
   117 
   118         public DisplayClient(MainForm aMainForm)
   119         {
   120             MainForm = aMainForm;
   121             Name = "";
   122         }
   123 
   124         public void Open()
   125         {
   126             iCallback = new Callback(MainForm);
   127             iClient = new Client(iCallback);
   128             if (Name != "")
   129             {
   130                 iClient.SetName(Name);
   131             }
   132         }
   133 
   134         public void Close()
   135         {
   136             iClient.Close();
   137             iClient = null;
   138             iCallback.Dispose();
   139             iCallback = null;
   140         }
   141 
   142         public bool IsReady()
   143         {
   144             return (iClient != null && iCallback != null && iClient.IsReady());
   145         }
   146 
   147         public void CheckConnection()
   148         {
   149             if (!IsReady())
   150             {
   151                 Open();
   152             }
   153         }
   154 
   155         public void SetName(string aClientName)
   156         {
   157             Name = aClientName;
   158             CheckConnection();
   159             iClient.SetName(aClientName);
   160         }
   161 
   162 
   163         public void SetLayout(TableLayout aLayout)
   164         {
   165             CheckConnection();
   166             iClient.SetLayout(aLayout);
   167         }
   168 
   169         public void SetText(DataField aField)
   170         {
   171             CheckConnection();
   172             iClient.SetText(aField);
   173         }
   174 
   175         public void SetTexts(System.Collections.Generic.IList<DataField> aFields)
   176         {
   177             CheckConnection();
   178             iClient.SetTexts(aFields);
   179         }
   180 
   181         public void SetBitmap(DataField aField)
   182         {
   183             CheckConnection();
   184             iClient.SetBitmap(aField);
   185         }
   186 
   187         public int ClientCount()
   188         {
   189             CheckConnection();
   190             return iClient.ClientCount();
   191         }
   192 
   193 
   194 
   195     }
   196 
   197 
   198 }