/[AnywhereTS-MSSQL]/branches/TerminalServices/TSAdminTool/TerminalServices.cs
ViewVC logotype

Contents of /branches/TerminalServices/TSAdminTool/TerminalServices.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 271 - (show annotations) (download)
Sun Aug 26 00:04:01 2012 UTC (8 years, 1 month ago) by william
File size: 9147 byte(s)
+ switch TerminalServices backend to Cassia 2.0.0.60

1 #define USE_CASSIA_WTS_INFO_CLASS // when define will use Cassia.Impl.WTS_INFO_CLASS instead of TSManager.WTSInfoClass
2 #region TSMANAGER VERBOSE MESSAGE LOGGING
3 #define ENABLE_TSMANAGER_VERBOSE_MESSAGES // when define will allow the below
4 #define ENABLE_TSMANAGER_VERBOSE_DEBUG_MESSAGES // when defined will allow calls to: LogDebugVerboseMessage(string format, params object[] args)
5 #define ENABLE_TSMANAGER_VERBOSE_ERROR_MESSAGES // when defined will allow calls to: LogErrorVerboseMessage(string format, params object[] args)
6 //#define ENABLE_TSMANAGER_VERBOSE_FATAL_MESSAGES // when defined will allow calls to: LogFatalVerboseMessage(string format, params object[] args)
7 //#define ENABLE_TSMANAGER_VERBOSE_INFO_MESSAGES // when defined will allow calls to: LogInfoVerboseMessage(string format, params object[] args)
8 //#define ENABLE_TSMANAGER_VERBOSE_WARN_MESSAGES // when defined will allow calls to: LogWarnVerboseMessage(string format, params object[] args)
9 #endregion
10 using System;
11 using System.Collections.Generic;
12 using System.Text;
13 using System.Runtime.InteropServices;
14 using System.Net;
15 using System.Windows.Forms;
16 using System.Data;
17 using log4net;
18 using System.Diagnostics;
19 using System.ComponentModel;
20 using Cassia.Impl;
21 using Cassia;
22 using AnywhereTS.Extensions;
23 namespace AnywhereTS
24 {
25 public class TSManager
26 {
27 #region TSMANAGER VERBOSE MESSAGE LOGGING
28 [Conditional("ENABLE_TSMANAGER_VERBOSE_DEBUG_MESSAGES")]
29 private static void LogDebugVerboseMessage(string format, params object[] args)
30 {
31 #if ENABLE_TSMANAGER_VERBOSE_MESSAGES
32 Logging.DefaultLog.DebugFormat(format, args);
33 #endif
34 }
35 [Conditional("ENABLE_TSMANAGER_VERBOSE_ERROR_MESSAGES")]
36 private static void LogErrorVerboseMessage(string format, params object[] args)
37 {
38 #if ENABLE_TSMANAGER_VERBOSE_MESSAGES
39 Logging.DefaultLog.ErrorFormat(format, args);
40 #endif
41 }
42 [Conditional("ENABLE_TSMANAGER_VERBOSE_FATAL_MESSAGES")]
43 private static void LogFatalVerboseMessage(string format, params object[] args)
44 {
45 #if ENABLE_TSMANAGER_VERBOSE_MESSAGES
46 Logging.DefaultLog.FatalFormat(format, args);
47 #endif
48 }
49 [Conditional("ENABLE_TSMANAGER_VERBOSE_INFO_MESSAGES")]
50 private static void LogInfoVerboseMessage(string format, params object[] args)
51 {
52 #if ENABLE_TSMANAGER_VERBOSE_MESSAGES
53 Logging.DefaultLog.InfoFormat(format, args);
54 #endif
55 }
56 [Conditional("ENABLE_TSMANAGER_VERBOSE_WARN_MESSAGES")]
57 private static void LogWarnVerboseMessage(string format, params object[] args)
58 {
59 #if ENABLE_TSMANAGER_VERBOSE_MESSAGES
60 Logging.DefaultLog.WarnFormat(format, args);
61 #endif
62 }
63 #endregion
64
65
66 // Return address for a terminal server session.
67 // WTSClientAddress gives a pointer to a WTS_CLIENT_ADDRESS structure containing
68 // the network type and network address of the client. If the function is called
69 // from the Terminal Services console, ppBuffer returns a NULL pointer.
70 // Note that the first byte of the IP address returned in the ppBuffer
71 // parameter will be located at an offset of two bytes from the first location
72 // of the buffer.
73 private static string GetTSClientAddress(int sessionID, ITerminalServerHandle server)
74 {
75 TerminalServer ts = new TerminalServer(server);
76 ts.Open();
77 var session = ts.GetSession(sessionID);
78 var ClientIPAddress = session.ClientIPAddress;
79 ts.Close();
80 return ClientIPAddress.ToString();
81 }
82
83 // Get display parameters for a user session on a terminal server
84 // In: SessionID = identifier for the session
85 // In: server = server handle for the server that the session resides on.
86 private static void GetTSClientDisplay(int sessionID, ITerminalServerHandle server, out int horizontalResolution, out int verticalResolution, out int colorDepth)
87 {
88 TerminalServer ts = new TerminalServer(server);
89 ts.Open();
90 var session = ts.GetSession(sessionID);
91 var ClientDisplay = session.ClientDisplay;
92 horizontalResolution = ClientDisplay.HorizontalResolution;
93 verticalResolution = ClientDisplay.VerticalResolution;
94 colorDepth = ClientDisplay.BitsPerPixel;
95 ts.Close();
96 }
97
98
99 private static string GetTSUserName(int sessionID, ITerminalServerHandle server)
100 {
101 TerminalServer ts = new TerminalServer(server);
102 ts.Open();
103 var session = ts.GetSession(sessionID);
104 var currentUserName = session.UserName;
105 ts.Close();
106 return currentUserName;
107 }
108
109 public static string GetTSClientName(int sessionID, ITerminalServerHandle server)
110 {
111 TerminalServer ts = new TerminalServer(server);
112 ts.Open();
113 var session = ts.GetSession(sessionID);
114 var ClientName = session.ClientName;
115 ts.Close();
116 return ClientName;
117 }
118
119 // List all sessions on all registered terminal servers and return in a list with selected data for each session.
120 public static List<ITerminalServicesSession> ListSessions()
121 {
122 return ListSessions(GetMyName());
123 }
124
125 // List all sessions on the terminal server and return in a list with selected data for each session.
126 public static List<ITerminalServicesSession> ListSessions(String serverName)
127 {
128 TerminalServer ts = new TerminalServer(new RemoteServerHandle(serverName));
129 List<ITerminalServicesSession> sessions = new List<ITerminalServicesSession>();
130 var ISessions = ts.GetSessions();
131 foreach (var session in ISessions) { sessions.Add(session); }
132 return sessions;
133 }
134 // Return matching session for a macAddress, or null, if the mac address is not used in any session.
135 // Called when a child node selected in the tree view
136 public static ITerminalServicesSession GetSession(string macAddress)
137 {
138 LogDebugVerboseMessage("GetSession(string macAddress)::called");
139 foreach (AtsTerminalServer ts in AtsEnvironment.TerminalServers)
140 {
141 TerminalServer real_ts = new TerminalServer(new RemoteServerHandle(ts.networkPath));
142 foreach (var t in real_ts.GetSessions())
143 {
144 int session_index = real_ts.GetSessions().IndexOf(t);
145 string mac = t.ClientIPAddress.ToMacAddress();
146 if (mac == macAddress)
147 {
148 LogDebugVerboseMessage("GetSession(string macAddress)::session[{0}] found for macAddress={1}", session_index, macAddress);
149 return t;
150 }
151 }
152 }
153 // Session not found, return null.
154 return null;
155 }
156
157 // Convert session state to string
158 public static string StateToString(Cassia.ConnectionState state)
159 {
160 switch (state)
161 {
162 case Cassia.ConnectionState.Active:
163 return "User logged on";
164 case Cassia.ConnectionState.Connected:
165 return "Connected to client";
166 case Cassia.ConnectionState.ConnectQuery:
167 return "Connecting to client";
168 case Cassia.ConnectionState.Shadowing:
169 return "Shadowing";
170 case Cassia.ConnectionState.Disconnected:
171 return "Logged on without client";
172 case Cassia.ConnectionState.Idle:
173 return "Waiting for client to connect";
174 case Cassia.ConnectionState.Listening:
175 return "Listening for connection";
176 case Cassia.ConnectionState.Reset:
177 return "Being reset";
178 case Cassia.ConnectionState.Down:
179 return "Down due to error";
180 case Cassia.ConnectionState.Initializing:
181 return "In initialization";
182 default:
183 LogDebugVerboseMessage("StateToString(WTS_CONNECTSTATE_CLASS state)::Unknown Session State={0}", state.ToString());
184 return "Unknown state (error!)";
185 }
186 }
187 public static string GetMyName()
188 {
189 TerminalServer ts = new TerminalServer(new LocalServerHandle());
190 ts.Open();
191 var ClientName = ts.ServerName == null ? Environment.MachineName : ts.ServerName;
192 ts.Close();
193 return ClientName;
194 }
195
196 } // Class TSManager
197 } // Namespace TerminalServices

  ViewVC Help
Powered by ViewVC 1.1.22