GUI/SharpDisplayClient.cs
author StephaneLenclud
Sun, 21 Sep 2014 21:55:36 +0200
branchMiniDisplay
changeset 445 fe4c711fd7f8
child 446 0cb7b9f6a6f8
permissions -rw-r--r--
Support for SharpDisplayManager.
Switching to .NET v4.0 to allow System.ServiceModel.
OxyPlot fix and recompile for .NET v4.0.
FrontView fix to start-up without SoundGraphAccess.exe.
StephaneLenclud@445
     1
/*
StephaneLenclud@445
     2
 
StephaneLenclud@445
     3
  This Source Code Form is subject to the terms of the Mozilla Public
StephaneLenclud@445
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
StephaneLenclud@445
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
StephaneLenclud@445
     6
 
StephaneLenclud@445
     7
  Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
StephaneLenclud@445
     8
	
StephaneLenclud@445
     9
*/
StephaneLenclud@445
    10
StephaneLenclud@445
    11
using System;
StephaneLenclud@445
    12
using System.Collections.Generic;
StephaneLenclud@445
    13
using System.Drawing;
StephaneLenclud@445
    14
using System.Text;
StephaneLenclud@445
    15
using System.Diagnostics;
StephaneLenclud@445
    16
using System.Windows.Forms;
StephaneLenclud@445
    17
using System.Windows;
StephaneLenclud@445
    18
using OpenHardwareMonitor.Hardware;
StephaneLenclud@445
    19
using OpenHardwareMonitor.Utilities;
StephaneLenclud@445
    20
using System.Runtime.InteropServices;
StephaneLenclud@445
    21
using UacHelpers;
StephaneLenclud@445
    22
//
StephaneLenclud@445
    23
using System.ServiceModel;
StephaneLenclud@445
    24
using System.Runtime.Serialization;
StephaneLenclud@445
    25
using SharpDisplay;
StephaneLenclud@445
    26
StephaneLenclud@445
    27
namespace SharpDisplay
StephaneLenclud@445
    28
{
StephaneLenclud@445
    29
    //That contract need to be in the same namespace than the original assembly
StephaneLenclud@445
    30
    //otherwise our parameter won't make to the server.
StephaneLenclud@445
    31
    //See: http://stackoverflow.com/questions/14956377/passing-an-object-using-datacontract-in-wcf/25455292#25455292
StephaneLenclud@445
    32
    [DataContract]
StephaneLenclud@445
    33
    public class TextField
StephaneLenclud@445
    34
    {
StephaneLenclud@445
    35
        public TextField()
StephaneLenclud@445
    36
        {
StephaneLenclud@445
    37
            Index = 0;
StephaneLenclud@445
    38
            Text = "";
StephaneLenclud@445
    39
            Alignment = ContentAlignment.MiddleLeft;
StephaneLenclud@445
    40
        }
StephaneLenclud@445
    41
StephaneLenclud@445
    42
        public TextField(int aIndex, string aText = "", ContentAlignment aAlignment = ContentAlignment.MiddleLeft)
StephaneLenclud@445
    43
        {
StephaneLenclud@445
    44
            Index = aIndex;
StephaneLenclud@445
    45
            Text = aText;
StephaneLenclud@445
    46
            Alignment = aAlignment;
StephaneLenclud@445
    47
        }
StephaneLenclud@445
    48
StephaneLenclud@445
    49
        [DataMember]
StephaneLenclud@445
    50
        public int Index { get; set; }
StephaneLenclud@445
    51
StephaneLenclud@445
    52
        [DataMember]
StephaneLenclud@445
    53
        public string Text { get; set; }
StephaneLenclud@445
    54
StephaneLenclud@445
    55
        [DataMember]
StephaneLenclud@445
    56
        public ContentAlignment Alignment { get; set; }
StephaneLenclud@445
    57
    }
StephaneLenclud@445
    58
StephaneLenclud@445
    59
StephaneLenclud@445
    60
    [ServiceContract(CallbackContract = typeof(ICallback), SessionMode = SessionMode.Required)]
StephaneLenclud@445
    61
    public interface IService
StephaneLenclud@445
    62
    {
StephaneLenclud@445
    63
        /// <summary>
StephaneLenclud@445
    64
        /// Set the name of this client.
StephaneLenclud@445
    65
        /// Name is a convenient way to recognize your client.
StephaneLenclud@445
    66
        /// Naming you client is not mandatory.
StephaneLenclud@445
    67
        /// In the absence of a name the session ID is often used instead.
StephaneLenclud@445
    68
        /// </summary>
StephaneLenclud@445
    69
        /// <param name="aClientName"></param>
StephaneLenclud@445
    70
        [OperationContract(IsOneWay = true)]
StephaneLenclud@445
    71
        void SetName(string aClientName);
StephaneLenclud@445
    72
StephaneLenclud@445
    73
        /// <summary>
StephaneLenclud@445
    74
        /// Put the given text in the given field on your display.
StephaneLenclud@445
    75
        /// Fields are often just lines of text.
StephaneLenclud@445
    76
        /// </summary>
StephaneLenclud@445
    77
        /// <param name="aTextFieldIndex"></param>
StephaneLenclud@445
    78
        [OperationContract(IsOneWay = true)]
StephaneLenclud@445
    79
        void SetText(TextField aTextField);
StephaneLenclud@445
    80
StephaneLenclud@445
    81
        /// <summary>
StephaneLenclud@445
    82
        /// Allows a client to set multiple text fields at once.
StephaneLenclud@445
    83
        /// </summary>
StephaneLenclud@445
    84
        /// <param name="aTexts"></param>
StephaneLenclud@445
    85
        [OperationContract(IsOneWay = true)]
StephaneLenclud@445
    86
        void SetTexts(System.Collections.Generic.IList<TextField> aTextFields);
StephaneLenclud@445
    87
StephaneLenclud@445
    88
        /// <summary>
StephaneLenclud@445
    89
        /// Provides the number of clients currently connected
StephaneLenclud@445
    90
        /// </summary>
StephaneLenclud@445
    91
        /// <returns></returns>
StephaneLenclud@445
    92
        [OperationContract()]
StephaneLenclud@445
    93
        int ClientCount();
StephaneLenclud@445
    94
StephaneLenclud@445
    95
    }
StephaneLenclud@445
    96
StephaneLenclud@445
    97
StephaneLenclud@445
    98
    public interface ICallback
StephaneLenclud@445
    99
    {
StephaneLenclud@445
   100
        [OperationContract(IsOneWay = true)]
StephaneLenclud@445
   101
        void OnConnected();
StephaneLenclud@445
   102
StephaneLenclud@445
   103
        /// <summary>
StephaneLenclud@445
   104
        /// Tell our client to close its connection.
StephaneLenclud@445
   105
        /// Notably sent when the server is shutting down.
StephaneLenclud@445
   106
        /// </summary>
StephaneLenclud@445
   107
        [OperationContract(IsOneWay = true)]
StephaneLenclud@445
   108
        void OnCloseOrder();
StephaneLenclud@445
   109
    }
StephaneLenclud@445
   110
}
StephaneLenclud@445
   111
StephaneLenclud@445
   112
StephaneLenclud@445
   113
StephaneLenclud@445
   114
namespace SharpDisplay
StephaneLenclud@445
   115
{
StephaneLenclud@445
   116
StephaneLenclud@445
   117
    /// <summary>
StephaneLenclud@445
   118
    ///
StephaneLenclud@445
   119
    /// </summary>
StephaneLenclud@445
   120
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
StephaneLenclud@445
   121
    public class Client : DuplexClientBase<IService>
StephaneLenclud@445
   122
    {
StephaneLenclud@445
   123
        public string Name { get; set; }
StephaneLenclud@445
   124
        public string SessionId { get { return InnerChannel.SessionId; } }
StephaneLenclud@445
   125
StephaneLenclud@445
   126
        public Client(ICallback aCallback)
StephaneLenclud@445
   127
            : base(new InstanceContext(aCallback), new NetTcpBinding(SecurityMode.None, true), new EndpointAddress("net.tcp://localhost:8001/DisplayService"))
StephaneLenclud@445
   128
        { }
StephaneLenclud@445
   129
StephaneLenclud@445
   130
        public void SetName(string aClientName)
StephaneLenclud@445
   131
        {
StephaneLenclud@445
   132
            Name = aClientName;
StephaneLenclud@445
   133
            Channel.SetName(aClientName);
StephaneLenclud@445
   134
        }
StephaneLenclud@445
   135
StephaneLenclud@445
   136
        public void SetText(TextField aTextField)
StephaneLenclud@445
   137
        {
StephaneLenclud@445
   138
            Channel.SetText(aTextField);
StephaneLenclud@445
   139
        }
StephaneLenclud@445
   140
StephaneLenclud@445
   141
        public void SetTexts(System.Collections.Generic.IList<TextField> aTextFields)
StephaneLenclud@445
   142
        {
StephaneLenclud@445
   143
            Channel.SetTexts(aTextFields);
StephaneLenclud@445
   144
        }
StephaneLenclud@445
   145
StephaneLenclud@445
   146
        public int ClientCount()
StephaneLenclud@445
   147
        {
StephaneLenclud@445
   148
            return Channel.ClientCount();
StephaneLenclud@445
   149
        }
StephaneLenclud@445
   150
StephaneLenclud@445
   151
        public bool IsReady()
StephaneLenclud@445
   152
        {
StephaneLenclud@445
   153
            return State == CommunicationState.Opened;
StephaneLenclud@445
   154
        }
StephaneLenclud@445
   155
StephaneLenclud@445
   156
    }
StephaneLenclud@445
   157
StephaneLenclud@445
   158
StephaneLenclud@445
   159
}