InterThread.cs
上传用户:szltgg
上传日期:2019-05-16
资源大小:604k
文件大小:7k
源码类别:

Telnet服务器

开发平台:

C#

  1. /*
  2. * Copyright (c) 2005 Poderosa Project, All Rights Reserved.
  3. * $Id: InterThread.cs,v 1.2 2005/04/20 08:45:46 okajima Exp $
  4. */
  5. using System;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8. using Poderosa.Connection;
  9. using Poderosa.Communication;
  10. using Poderosa.ConnectionParam;
  11. using Poderosa.Text;
  12. using Poderosa.Config;
  13. using Poderosa.Forms;
  14. namespace Poderosa
  15. {
  16. //庴怣僗儗僢僪偑捈愙儊僢僙乕僕儃僢僋僗側偳傪弌偟偵偄偔偺偼壗偐偲僩儔僽儖偺壏彴側偺偱丄
  17. //儊僀儞偺僗儗僢僪偲庴偗搉偟傪偟側偑傜張棟偡傞丅
  18. //偨偲偊偽丄僆僾僔儑儞僟僀傾儘僌傪弌偟偰偄傞娫偵僒乕僶偑愗抐傪愗偭偰偒偨偲偒側偳丄GFrame傪恊偵MessageBox傪屇傫偱偟傑偄丄偦偺屻偺摦嶌偑偪傚偭偲晄怰偵側偭偨丅
  19. public class InterThreadUIService {
  20. private string _msg;
  21. private Exception _exception;
  22. private DialogResult _dialogResult;
  23. private TerminalConnection _connection;
  24. protected IntPtr _mainFrameHandle;
  25. public enum Service {
  26. Warning,
  27. Information,
  28. AskUserYesNo,
  29. DisconnectedFromServer,
  30. ToggleSelectionMode,
  31. IndicateBell,
  32. CriticalError,
  33. InvalidCharDetected,
  34. UnsupportedCharSetDetected,
  35. UnsupportedEscapeSequence,
  36. InvalidDocumentOperation,
  37. RefreshConnection,
  38. End
  39. }
  40. public IntPtr MainFrameHandle {
  41. get {
  42. return _mainFrameHandle;
  43. }
  44. set {
  45. _mainFrameHandle = value;
  46. }
  47. }
  48. //偙傟傜偺奺儊僜僢僪偼偦傟偧傟儘僢僋偟側偑傜幚峴偝傟傞
  49. internal void Warning(TerminalDocument doc, string msg) {
  50. Monitor.Exit(doc);
  51. lock(this) {
  52. _msg = msg;
  53. SendMessageCore(Service.Warning);
  54. }
  55. Monitor.Enter(doc);
  56. }
  57. public void Warning(string msg) {
  58. lock(this) {
  59. _msg = msg;
  60. SendMessageCore(Service.Warning);
  61. }
  62. }
  63. public void Information(string msg) {
  64. lock(this) {
  65. _msg = msg;
  66. SendMessageCore(Service.Information);
  67. }
  68. }
  69. public DialogResult AskUserYesNo(string msg) {
  70. lock(this) {
  71. _msg = msg;
  72. SendMessageCore(Service.AskUserYesNo);
  73. }
  74. return _dialogResult;
  75. }
  76. internal void ReportCriticalError(TerminalDocument doc, Exception ex) {
  77. Monitor.Exit(doc);
  78. lock(this) {
  79. _exception = ex;
  80. SendMessageCore(Service.CriticalError);
  81. }
  82. Monitor.Enter(doc);
  83. }
  84. public void ReportCriticalError(Exception ex) {
  85. lock(this) {
  86. _exception = ex;
  87. SendMessageCore(Service.CriticalError);
  88. }
  89. }
  90. public void DisconnectedFromServer(TerminalConnection con) {
  91. lock(this) {
  92. _connection = con;
  93. SendMessageCore(Service.DisconnectedFromServer);
  94. }
  95. }
  96. internal void IndicateBell(TerminalDocument doc) {
  97. Monitor.Exit(doc);
  98. lock(this) {
  99. SendMessageCore(Service.IndicateBell);
  100. }
  101. Monitor.Enter(doc);
  102. }
  103. internal void InvalidCharDetected(TerminalDocument doc, string msg) {
  104. Monitor.Exit(doc);
  105. lock(this) {
  106. _msg = msg;
  107. SendMessageCore(Service.InvalidCharDetected);
  108. }
  109. Monitor.Enter(doc);
  110. }
  111. internal void UnsupportedCharSetDetected(TerminalDocument doc, string msg) {
  112. Monitor.Exit(doc);
  113. lock(this) {
  114. _msg = msg;
  115. SendMessageCore(Service.UnsupportedCharSetDetected);
  116. }
  117. Monitor.Enter(doc);
  118. }
  119. internal void UnsupportedEscapeSequence(TerminalDocument doc, string msg) {
  120. Monitor.Exit(doc);
  121. lock(this) {
  122. _msg = msg;
  123. SendMessageCore(Service.UnsupportedEscapeSequence);
  124. }
  125. Monitor.Enter(doc);
  126. }
  127. internal void InvalidDocumentOperation(TerminalDocument doc, string msg) {
  128. Monitor.Exit(doc);
  129. lock(this) {
  130. _msg = msg;
  131. SendMessageCore(Service.InvalidDocumentOperation);
  132. }
  133. Monitor.Enter(doc);
  134. }
  135. public void RefreshConnection(ConnectionTag tag) {
  136. TerminalDocument doc = tag.Document;
  137. Monitor.Exit(doc);
  138. lock(this) {
  139. _connection = tag.Connection;
  140. SendMessageCore(Service.RefreshConnection);
  141. }
  142. Monitor.Enter(doc);
  143. }
  144. protected void SendMessageCore(Service svc) {
  145. Win32.SendMessage(_mainFrameHandle, GConst.WMG_UIMESSAGE, new IntPtr((int)svc), IntPtr.Zero);
  146. }
  147. internal void AdjustIMEComposition(IntPtr hWnd, TerminalDocument doc) {
  148. Monitor.Exit(doc);
  149. lock(this) {
  150. Win32.SendMessage(hWnd, GConst.WMG_MAINTHREADTASK, new IntPtr(GConst.WPG_ADJUSTIMECOMPOSITION), IntPtr.Zero);
  151. }
  152. Monitor.Enter(doc);
  153. }
  154. internal void ToggleTextSelectionMode(IntPtr hWnd, TerminalDocument doc) {
  155. Monitor.Exit(doc);
  156. lock(this) {
  157. Win32.SendMessage(hWnd, GConst.WMG_MAINTHREADTASK, new IntPtr(GConst.WPG_TOGGLESELECTIONMODE), IntPtr.Zero);
  158. }
  159. Monitor.Enter(doc);
  160. }
  161. //儊僀儞僗儗僢僪偐傜幚峴
  162. public virtual void ProcessMessage(IntPtr wParam, IntPtr lParam) {
  163. switch((Service)(int)wParam) {
  164. case Service.Warning:
  165. GUtil.Warning(GEnv.Frame, _msg);
  166. break;
  167. case Service.Information:
  168. GUtil.Warning(GEnv.Frame, _msg, MessageBoxIcon.Information);
  169. break;
  170. case Service.AskUserYesNo:
  171. _dialogResult = GUtil.AskUserYesNo(GEnv.Frame, _msg);
  172. break;
  173. case Service.DisconnectedFromServer:
  174. DisconnectedFromServer();
  175. break;
  176. case Service.IndicateBell:
  177. GEnv.Frame.IndicateBell();
  178. break;
  179. case Service.CriticalError:
  180. GUtil.ReportCriticalError(_exception);
  181. break;
  182. case Service.InvalidCharDetected: //偄傑偼偙偺俁偮偼儊僢僙乕僕僥僉僗僩偱偟偐嬫暿偟側偄
  183. case Service.UnsupportedCharSetDetected:
  184. case Service.UnsupportedEscapeSequence:
  185. case Service.InvalidDocumentOperation:
  186. BadCharDetected();
  187. break;
  188. case Service.RefreshConnection:
  189. GEnv.Frame.RefreshConnection(GEnv.Connections.FindTag(_connection));
  190. break;
  191. }
  192. }
  193. private void DisconnectedFromServer() {
  194. /*
  195.             if(!_connection.IsClosed && (_connection.Param is TCPTerminalParam)) {
  196. string host = ((TCPTerminalParam)_connection.Param).Host;
  197. string msg = String.Format(GEnv.Strings.GetString("Message.InterThread.Disconnected"), host);
  198. if(GEnv.Options.DisconnectNotification==DisconnectNotification.MessageBox)
  199. GUtil.Warning(GEnv.Frame, msg);
  200. else if(GEnv.Options.DisconnectNotification==DisconnectNotification.StatusBar)
  201. GEnv.Frame.SetStatusBarText(msg);
  202. }
  203.              * */
  204. _connection.Close();
  205.             _connection.WriteChars("disconnected".ToCharArray());
  206.             /*
  207.  * GEnv.Connections.FindTag(_connection).IsTerminated = true;
  208. if(GEnv.Options.CloseOnDisconnect)
  209. GEnv.GetConnectionCommandTarget(_connection).Close();
  210. else
  211. GEnv.Frame.RefreshConnection(GEnv.Connections.FindTag(_connection));
  212. if(GEnv.Options.QuitAppWithLastPane && GEnv.Connections.Count==0)
  213. GEnv.Frame.AsForm().Close();
  214.              */
  215.             
  216. }
  217. private void BadCharDetected() {
  218. switch(GEnv.Options.WarningOption) {
  219. case WarningOption.StatusBar:
  220. GEnv.Frame.SetStatusBarText(_msg);
  221. break;
  222. case WarningOption.MessageBox: {
  223. WarningWithDisableOption dlg = new WarningWithDisableOption(_msg);
  224. if(GUtil.ShowModalDialog(GEnv.Frame, dlg)==DialogResult.OK && dlg.CheckedDisableOption)
  225. GEnv.Options.WarningOption = WarningOption.Ignore;
  226. break;
  227. }
  228. }
  229. }
  230. }
  231. }