GUI/SharpDisplayClient.cs
branchMiniDisplay
changeset 445 fe4c711fd7f8
child 446 0cb7b9f6a6f8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/GUI/SharpDisplayClient.cs	Sun Sep 21 21:55:36 2014 +0200
     1.3 @@ -0,0 +1,159 @@
     1.4 +/*
     1.5 + 
     1.6 +  This Source Code Form is subject to the terms of the Mozilla Public
     1.7 +  License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 +  file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.9 + 
    1.10 +  Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
    1.11 +	
    1.12 +*/
    1.13 +
    1.14 +using System;
    1.15 +using System.Collections.Generic;
    1.16 +using System.Drawing;
    1.17 +using System.Text;
    1.18 +using System.Diagnostics;
    1.19 +using System.Windows.Forms;
    1.20 +using System.Windows;
    1.21 +using OpenHardwareMonitor.Hardware;
    1.22 +using OpenHardwareMonitor.Utilities;
    1.23 +using System.Runtime.InteropServices;
    1.24 +using UacHelpers;
    1.25 +//
    1.26 +using System.ServiceModel;
    1.27 +using System.Runtime.Serialization;
    1.28 +using SharpDisplay;
    1.29 +
    1.30 +namespace SharpDisplay
    1.31 +{
    1.32 +    //That contract need to be in the same namespace than the original assembly
    1.33 +    //otherwise our parameter won't make to the server.
    1.34 +    //See: http://stackoverflow.com/questions/14956377/passing-an-object-using-datacontract-in-wcf/25455292#25455292
    1.35 +    [DataContract]
    1.36 +    public class TextField
    1.37 +    {
    1.38 +        public TextField()
    1.39 +        {
    1.40 +            Index = 0;
    1.41 +            Text = "";
    1.42 +            Alignment = ContentAlignment.MiddleLeft;
    1.43 +        }
    1.44 +
    1.45 +        public TextField(int aIndex, string aText = "", ContentAlignment aAlignment = ContentAlignment.MiddleLeft)
    1.46 +        {
    1.47 +            Index = aIndex;
    1.48 +            Text = aText;
    1.49 +            Alignment = aAlignment;
    1.50 +        }
    1.51 +
    1.52 +        [DataMember]
    1.53 +        public int Index { get; set; }
    1.54 +
    1.55 +        [DataMember]
    1.56 +        public string Text { get; set; }
    1.57 +
    1.58 +        [DataMember]
    1.59 +        public ContentAlignment Alignment { get; set; }
    1.60 +    }
    1.61 +
    1.62 +
    1.63 +    [ServiceContract(CallbackContract = typeof(ICallback), SessionMode = SessionMode.Required)]
    1.64 +    public interface IService
    1.65 +    {
    1.66 +        /// <summary>
    1.67 +        /// Set the name of this client.
    1.68 +        /// Name is a convenient way to recognize your client.
    1.69 +        /// Naming you client is not mandatory.
    1.70 +        /// In the absence of a name the session ID is often used instead.
    1.71 +        /// </summary>
    1.72 +        /// <param name="aClientName"></param>
    1.73 +        [OperationContract(IsOneWay = true)]
    1.74 +        void SetName(string aClientName);
    1.75 +
    1.76 +        /// <summary>
    1.77 +        /// Put the given text in the given field on your display.
    1.78 +        /// Fields are often just lines of text.
    1.79 +        /// </summary>
    1.80 +        /// <param name="aTextFieldIndex"></param>
    1.81 +        [OperationContract(IsOneWay = true)]
    1.82 +        void SetText(TextField aTextField);
    1.83 +
    1.84 +        /// <summary>
    1.85 +        /// Allows a client to set multiple text fields at once.
    1.86 +        /// </summary>
    1.87 +        /// <param name="aTexts"></param>
    1.88 +        [OperationContract(IsOneWay = true)]
    1.89 +        void SetTexts(System.Collections.Generic.IList<TextField> aTextFields);
    1.90 +
    1.91 +        /// <summary>
    1.92 +        /// Provides the number of clients currently connected
    1.93 +        /// </summary>
    1.94 +        /// <returns></returns>
    1.95 +        [OperationContract()]
    1.96 +        int ClientCount();
    1.97 +
    1.98 +    }
    1.99 +
   1.100 +
   1.101 +    public interface ICallback
   1.102 +    {
   1.103 +        [OperationContract(IsOneWay = true)]
   1.104 +        void OnConnected();
   1.105 +
   1.106 +        /// <summary>
   1.107 +        /// Tell our client to close its connection.
   1.108 +        /// Notably sent when the server is shutting down.
   1.109 +        /// </summary>
   1.110 +        [OperationContract(IsOneWay = true)]
   1.111 +        void OnCloseOrder();
   1.112 +    }
   1.113 +}
   1.114 +
   1.115 +
   1.116 +
   1.117 +namespace SharpDisplay
   1.118 +{
   1.119 +
   1.120 +    /// <summary>
   1.121 +    ///
   1.122 +    /// </summary>
   1.123 +    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
   1.124 +    public class Client : DuplexClientBase<IService>
   1.125 +    {
   1.126 +        public string Name { get; set; }
   1.127 +        public string SessionId { get { return InnerChannel.SessionId; } }
   1.128 +
   1.129 +        public Client(ICallback aCallback)
   1.130 +            : base(new InstanceContext(aCallback), new NetTcpBinding(SecurityMode.None, true), new EndpointAddress("net.tcp://localhost:8001/DisplayService"))
   1.131 +        { }
   1.132 +
   1.133 +        public void SetName(string aClientName)
   1.134 +        {
   1.135 +            Name = aClientName;
   1.136 +            Channel.SetName(aClientName);
   1.137 +        }
   1.138 +
   1.139 +        public void SetText(TextField aTextField)
   1.140 +        {
   1.141 +            Channel.SetText(aTextField);
   1.142 +        }
   1.143 +
   1.144 +        public void SetTexts(System.Collections.Generic.IList<TextField> aTextFields)
   1.145 +        {
   1.146 +            Channel.SetTexts(aTextFields);
   1.147 +        }
   1.148 +
   1.149 +        public int ClientCount()
   1.150 +        {
   1.151 +            return Channel.ClientCount();
   1.152 +        }
   1.153 +
   1.154 +        public bool IsReady()
   1.155 +        {
   1.156 +            return State == CommunicationState.Opened;
   1.157 +        }
   1.158 +
   1.159 +    }
   1.160 +
   1.161 +
   1.162 +}
   1.163 \ No newline at end of file