Client/Client.cs
author sl
Fri, 15 Aug 2014 11:11:17 +0200
changeset 31 f19b04646b6a
parent 30 c375286d1a1c
child 32 4c416d2878dd
permissions -rw-r--r--
Fixing our client issue with static MainForm overwritten when using multiple clients.
That was basically our issue with broadcast not working the way it should.
sl@20
     1
using System;
sl@20
     2
using System.Collections.Generic;
sl@20
     3
using System.Linq;
sl@20
     4
using System.Text;
sl@20
     5
using System.Threading.Tasks;
sl@20
     6
using System.Windows.Forms;
sl@22
     7
using SharpDisplayInterface;
sl@20
     8
using System.ServiceModel;
sl@20
     9
using System.ServiceModel.Channels;
sl@20
    10
sl@20
    11
sl@20
    12
namespace SharpDisplayClient
sl@20
    13
{
sl@25
    14
    /// <summary>
sl@25
    15
    ///
sl@25
    16
    /// </summary>
sl@31
    17
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
sl@30
    18
    public class Callback : IDisplayServiceCallback, IDisposable
sl@20
    19
    {
sl@31
    20
        private MainForm MainForm { get; set; }
sl@31
    21
sl@31
    22
        public Callback(MainForm aMainForm)
sl@31
    23
        {
sl@31
    24
            MainForm = aMainForm;
sl@31
    25
        }
sl@31
    26
sl@20
    27
        public void OnConnected()
sl@20
    28
        {
sl@20
    29
            //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread);
sl@20
    30
            //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId);
sl@20
    31
sl@20
    32
            MessageBox.Show("OnConnected()", "Client");
sl@20
    33
        }
sl@20
    34
sl@20
    35
sl@20
    36
        public void OnServerClosing()
sl@20
    37
        {
sl@20
    38
            //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread);
sl@20
    39
            //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId);
sl@20
    40
sl@21
    41
            //MessageBox.Show("OnServerClosing()", "Client");
sl@31
    42
            MainForm.CloseConnectionThreadSafe();
sl@31
    43
            MainForm.CloseThreadSafe();
sl@20
    44
        }
sl@25
    45
sl@25
    46
        //From IDisposable
sl@25
    47
        public void Dispose()
sl@25
    48
        {
sl@25
    49
sl@25
    50
        }
sl@20
    51
    }
sl@20
    52
sl@20
    53
sl@25
    54
    /// <summary>
sl@25
    55
    ///
sl@25
    56
    /// </summary>
sl@31
    57
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
sl@30
    58
    public class Client : DuplexClientBase<IDisplayService>
sl@20
    59
    {
sl@30
    60
        public string Name { get; set; }
sl@30
    61
        public string SessionId { get { return InnerChannel.SessionId; } }
sl@26
    62
sl@26
    63
        public Client(InstanceContext callbackInstance)
sl@30
    64
            : base(callbackInstance, new NetTcpBinding(SecurityMode.None, true), new EndpointAddress("net.tcp://localhost:8001/DisplayService"))
sl@20
    65
        { }
sl@20
    66
sl@20
    67
        public void Connect(string aClientName)
sl@20
    68
        {
sl@26
    69
            Name = aClientName;
sl@20
    70
            Channel.Connect(aClientName);
sl@20
    71
        }
sl@20
    72
sl@26
    73
        public void Disconnect()
sl@26
    74
        {
sl@26
    75
            Channel.Disconnect(Name);
sl@26
    76
            Name = "";
sl@26
    77
        }
sl@26
    78
sl@20
    79
        public void SetText(int aLineIndex, string aText)
sl@20
    80
        {
sl@20
    81
            Channel.SetText(aLineIndex, aText);
sl@20
    82
        }
sl@20
    83
sl@20
    84
sl@20
    85
        public void SetTexts(System.Collections.Generic.IList<string> aTexts)
sl@20
    86
        {
sl@20
    87
            Channel.SetTexts(aTexts);
sl@20
    88
        }
sl@20
    89
sl@30
    90
        
sl@30
    91
sl@20
    92
    }
sl@20
    93
}