Now support duplex mode with new client.
authorsl
Wed, 13 Aug 2014 23:02:40 +0200
changeset 20e3d394dd0388
parent 19 1a85ec255882
child 21 274a6b27c3f9
Now support duplex mode with new client.
Client/Client.cs
Client/MainForm.cs
Client/SharpDisplayClient.csproj
Server/MainForm.cs
Server/Servers.cs
Server/Services.cs
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Client/Client.cs	Wed Aug 13 23:02:40 2014 +0200
     1.3 @@ -0,0 +1,60 @@
     1.4 +using System;
     1.5 +using System.Collections.Generic;
     1.6 +using System.Linq;
     1.7 +using System.Text;
     1.8 +using System.Threading.Tasks;
     1.9 +using System.Windows.Forms;
    1.10 +using SharpDisplayManager;
    1.11 +using System.ServiceModel;
    1.12 +using System.ServiceModel.Channels;
    1.13 +
    1.14 +
    1.15 +namespace SharpDisplayClient
    1.16 +{
    1.17 +    public partial class ClientInput : SharpDisplayManager.IDisplayServiceCallback
    1.18 +    {
    1.19 +        public void OnConnected()
    1.20 +        {
    1.21 +            //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread);
    1.22 +            //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId);
    1.23 +
    1.24 +            MessageBox.Show("OnConnected()", "Client");
    1.25 +        }
    1.26 +
    1.27 +
    1.28 +        public void OnServerClosing()
    1.29 +        {
    1.30 +            //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread);
    1.31 +            //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId);
    1.32 +
    1.33 +            MessageBox.Show("OnServerClosing()", "Client");
    1.34 +        }
    1.35 +    }
    1.36 +
    1.37 +
    1.38 +
    1.39 +    public partial class ClientOutput : DuplexClientBase<IDisplayService>, IDisplayService
    1.40 +    {
    1.41 +        public ClientOutput(InstanceContext callbackInstance)
    1.42 +            : base(callbackInstance, new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:8001/DisplayService"))
    1.43 +        { }
    1.44 +
    1.45 +        public void Connect(string aClientName)
    1.46 +        {
    1.47 +            Channel.Connect(aClientName);
    1.48 +        }
    1.49 +
    1.50 +        public void SetText(int aLineIndex, string aText)
    1.51 +        {
    1.52 +            Channel.SetText(aLineIndex, aText);
    1.53 +        }
    1.54 +
    1.55 +
    1.56 +        public void SetTexts(System.Collections.Generic.IList<string> aTexts)
    1.57 +        {
    1.58 +            Channel.SetTexts(aTexts);
    1.59 +        }
    1.60 +
    1.61 +
    1.62 +    }
    1.63 +}
     2.1 --- a/Client/MainForm.cs	Tue Aug 12 21:27:35 2014 +0200
     2.2 +++ b/Client/MainForm.cs	Wed Aug 13 23:02:40 2014 +0200
     2.3 @@ -9,14 +9,14 @@
     2.4  using System.Windows.Forms;
     2.5  using System.ServiceModel;
     2.6  using System.ServiceModel.Channels;
     2.7 -using SharpDisplayManager;
     2.8 +
     2.9  
    2.10  namespace SharpDisplayClient
    2.11  {
    2.12      public partial class MainForm : Form
    2.13      {
    2.14 -        ChannelFactory<SharpDisplayManager.IDisplayService> iChannelFactory;
    2.15 -        SharpDisplayManager.IDisplayService iClient;
    2.16 +        ClientOutput iClientOutput;
    2.17 +        ClientInput iClientInput;
    2.18  
    2.19          public MainForm()
    2.20          {
    2.21 @@ -27,18 +27,17 @@
    2.22          {
    2.23              //iClient.SetText(0,"Top");
    2.24              //iClient.SetText(1, "Bottom");
    2.25 -            iClient.SetTexts(new string[] { "Top", "Bottom" });
    2.26 +            iClientOutput.SetTexts(new string[] { "Top", "Bottom" });
    2.27          }
    2.28  
    2.29          private void MainForm_Load(object sender, EventArgs e)
    2.30          {
    2.31 +            iClientInput = new ClientInput();
    2.32 +            InstanceContext context = new InstanceContext(iClientInput);
    2.33 +            iClientOutput = new ClientOutput(context);
    2.34  
    2.35 -            iChannelFactory = new ChannelFactory<SharpDisplayManager.IDisplayService>(
    2.36 -                new NetNamedPipeBinding(),
    2.37 -                new EndpointAddress(
    2.38 -                "net.pipe://localhost/DisplayService"));
    2.39 +            iClientOutput.Connect("TestClient");
    2.40  
    2.41 -            iClient = iChannelFactory.CreateChannel();
    2.42          }
    2.43      }
    2.44  }
     3.1 --- a/Client/SharpDisplayClient.csproj	Tue Aug 12 21:27:35 2014 +0200
     3.2 +++ b/Client/SharpDisplayClient.csproj	Wed Aug 13 23:02:40 2014 +0200
     3.3 @@ -45,6 +45,7 @@
     3.4      <Reference Include="System.Xml" />
     3.5    </ItemGroup>
     3.6    <ItemGroup>
     3.7 +    <Compile Include="Client.cs" />
     3.8      <Compile Include="MainForm.cs">
     3.9        <SubType>Form</SubType>
    3.10      </Compile>
     4.1 --- a/Server/MainForm.cs	Tue Aug 12 21:27:35 2014 +0200
     4.2 +++ b/Server/MainForm.cs	Wed Aug 13 23:02:40 2014 +0200
     4.3 @@ -350,10 +350,10 @@
     4.4              iServiceHost = new ServiceHost
     4.5                  (
     4.6                      typeof(DisplayServer),
     4.7 -                    new Uri[] { new Uri("net.pipe://localhost") }
     4.8 +                    new Uri[] { new Uri("net.tcp://localhost:8001/") }
     4.9                  );
    4.10  
    4.11 -            iServiceHost.AddServiceEndpoint(typeof(IDisplayService), new NetNamedPipeBinding(), "DisplayService");
    4.12 +            iServiceHost.AddServiceEndpoint(typeof(IDisplayService), new NetTcpBinding(), "DisplayService");
    4.13              iServiceHost.Open();
    4.14          }
    4.15  
     5.1 --- a/Server/Servers.cs	Tue Aug 12 21:27:35 2014 +0200
     5.2 +++ b/Server/Servers.cs	Wed Aug 13 23:02:40 2014 +0200
     5.3 @@ -1,6 +1,7 @@
     5.4  using System;
     5.5  using System.Windows.Forms;
     5.6  using System.Collections;
     5.7 +using System.ServiceModel;
     5.8  
     5.9  namespace SharpDisplayManager
    5.10  {
    5.11 @@ -8,6 +9,7 @@
    5.12      /// Implement our display service.
    5.13      /// This class is instantiated anew whenever a client send a request.
    5.14      /// </summary>
    5.15 +    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
    5.16      class DisplayServer : IDisplayService
    5.17      {
    5.18          //From IDisplayService
    5.19 @@ -41,6 +43,13 @@
    5.20                  }
    5.21          }
    5.22  
    5.23 +        //
    5.24 +        public void Connect(string aClientName)
    5.25 +        {
    5.26 +            IDisplayServiceCallback callback = OperationContext.Current.GetCallbackChannel<IDisplayServiceCallback>();
    5.27 +            callback.OnConnected();
    5.28 +        }
    5.29 +
    5.30      }
    5.31  
    5.32  }
     6.1 --- a/Server/Services.cs	Tue Aug 12 21:27:35 2014 +0200
     6.2 +++ b/Server/Services.cs	Wed Aug 13 23:02:40 2014 +0200
     6.3 @@ -2,15 +2,29 @@
     6.4  using System.ServiceModel;
     6.5  using System.Collections;
     6.6  
     6.7 +
     6.8  namespace SharpDisplayManager
     6.9  {
    6.10 -    [ServiceContract]
    6.11 +    [ServiceContract(CallbackContract = typeof(IDisplayServiceCallback))]
    6.12      public interface IDisplayService
    6.13      {
    6.14 -        [OperationContract]
    6.15 +        [OperationContract(IsOneWay = true)]
    6.16 +        void Connect(string aClientName);
    6.17 +
    6.18 +        [OperationContract(IsOneWay = true)]
    6.19          void SetText(int aLineIndex, string aText);
    6.20  
    6.21 -        [OperationContract]
    6.22 +        [OperationContract(IsOneWay = true)]
    6.23          void SetTexts(System.Collections.Generic.IList<string> aTexts);
    6.24      }
    6.25 +
    6.26 +
    6.27 +    public interface IDisplayServiceCallback
    6.28 +    {
    6.29 +        [OperationContract(IsOneWay = true)]
    6.30 +        void OnConnected();
    6.31 +
    6.32 +        [OperationContract]
    6.33 +        void OnServerClosing();
    6.34 +    }
    6.35  }