Connections.cs
上传用户:szltgg
上传日期:2019-05-16
资源大小:604k
文件大小:12k
- /*
- * Copyright (c) 2005 Poderosa Project, All Rights Reserved.
- * $Id: Connections.cs,v 1.2 2005/04/20 08:45:46 okajima Exp $
- */
- using System;
- using ThTimer = System.Threading.Timer;
- using System.Windows.Forms;
- using System.Collections;
- using System.Diagnostics;
- using Poderosa;
- using Poderosa.Text;
- using Poderosa.Terminal;
- using Poderosa.Communication;
- namespace Poderosa.Connection
- {
- /// <summary>
- /// 傾僾儕働乕僔儑儞慡懱偱偺僐僱僋僔儑儞偺儕僗僩偲丄偦傟偑偳偺儁僀儞偵娭楢晅偗傜傟偰偄傞偐傪娗棟偡傞
- /// </summary>
- public class Connections : IEnumerable {
- //愙懕傪奐偄偨弴偵奿擺偝傟傞ConnectionTag攝楍
- private ArrayList _connections;
- private int _activeIndex;
- //Active側媡弴偵奿擺偝傟傞攝楍
- private ArrayList _activatedOrder;
- private KeepAlive _keepAlive;
- internal Connections() {
- _connections = new ArrayList();
- _activeIndex = -1;
- _keepAlive = new KeepAlive();
- _activatedOrder = new ArrayList();
- }
- internal KeepAlive KeepAlive {
- get {
- return _keepAlive;
- }
- }
- public void Add(ConnectionTag t) {
- Debug.Assert(t!=null);
- t.PositionIndex = GEnv.Frame.PositionForNextConnection;
- t.PreservedPositionIndex = t.PositionIndex;
- _connections.Add(t);
- _activatedOrder.Add(t);
- }
- internal void Remove(TerminalConnection con) {
- int i = IndexOf(con);
- if(i==-1) return; //杮摉偼偙偆偄偆偺偼傛傠偟偔側偄偑
- ConnectionTag ct = this.TagAt(i);
- _connections.RemoveAt(i);
- _activatedOrder.Remove(ct);
- _activeIndex = Math.Min(_activeIndex, _connections.Count-1);
- }
- public void Replace(ConnectionTag old, ConnectionTag ct) {
- int i = IndexOf(old);
- Debug.Assert(i!=-1);
- _connections[i] = ct;
- i = _activatedOrder.IndexOf(old);
- Debug.Assert(i!=-1);
- _activatedOrder[i] = ct;
- }
- public void Clear() {
- _connections.Clear();
- _activatedOrder.Clear();
- _activeIndex = -1;
- }
- public void CloseAllConnections() {
- ArrayList target = new ArrayList(_connections); //儘乕僇儖僐僺乕傪嶌傜側偄偲旕摨婜偵僐儗僋僔儑儞偑曄峏偝傟偐偹側偄
- foreach(ConnectionTag ct in target) {
- ct.Connection.Close();
- ct.IsTerminated = true;
- }
- }
- public ArrayList GetSnapshot() {
- return new ArrayList(_connections);
- }
- public int Count {
- get {
- return _connections.Count;
- }
- }
- public bool LiveConnectionsExist {
- get {
- foreach(ConnectionTag ct in _connections)
- if(!ct.Connection.IsClosed) return true;
- return false;
- }
- }
- public IEnumerator GetEnumerator() {
- return _connections.GetEnumerator();
- }
- public IEnumerable OrderedConnections {
- get {
- ArrayList t = new ArrayList(_activatedOrder);
- t.Reverse();
- return t;
- }
- }
- public int ActiveIndex {
- get {
- return _activeIndex;
- }
- }
- public void BringToActivationOrderTop(ConnectionTag ct) {
- _activeIndex = _connections.IndexOf(ct);
- _activatedOrder.Remove(ct);
- _activatedOrder.Add(ct);
- }
- public TerminalConnection ActiveConnection {
- get {
- if(_activeIndex==-1)
- return null;
- else
- return TagAt(_activeIndex).Connection;
- }
- }
- //positionIndex偑堦抳偟丄excluding偱偼側偄拞偱師偵Active偵側傞岓曗傪曉偡丅側偗傟偽null
- public ConnectionTag GetCandidateOfActivation(int positionIndex, ConnectionTag excluding) {
- for(int i=_activatedOrder.Count-1; i>=0; i--) {
- ConnectionTag ct = (ConnectionTag)_activatedOrder[i];
- if(ct.PositionIndex==positionIndex && ct!=excluding) return ct;
- }
- return null;
- }
- //Preserved側傗偮傪傒偰寛掕
- public ConnectionTag GetCandidateOfLocation(int positionIndex, ConnectionTag excluding) {
- for(int i=_activatedOrder.Count-1; i>=0; i--) {
- ConnectionTag ct = (ConnectionTag)_activatedOrder[i];
- if(ct.PreservedPositionIndex==positionIndex && ct!=excluding) return ct;
- }
- return null;
- }
- public ConnectionTag ActiveTag {
- get {
- if(_activeIndex==-1)
- return null;
- else
- return TagAt(_activeIndex);
- }
- }
- public ConnectionTag TagAt(int index) {
- return (ConnectionTag)_connections[index];
- }
- public ConnectionTag FindTag(TerminalConnection con) {
- foreach(ConnectionTag t in _connections) {
- if(t.Connection==con) return t;
- }
- return null;
- }
- public int IndexOf(TerminalConnection con) {
- int i = 0;
- foreach(ConnectionTag t in _connections) {
- if(t.Connection==con) return i;
- i++;
- }
- return -1;
- }
- public int IndexOf(ConnectionTag tag) {
- int i = 0;
- foreach(ConnectionTag t in _connections) {
- if(t==tag) return i;
- i++;
- }
- return -1;
- }
- public ConnectionTag NextConnection(ConnectionTag c) {
- int i = IndexOf(c);
- return TagAt(i==_connections.Count-1? 0 : i+1);
- }
- public ConnectionTag PrevConnection(ConnectionTag c) {
- int i = IndexOf(c);
- return TagAt(i==0? _connections.Count-1 : i-1);
- }
- public void Reorder(int index, int newindex) {
- ConnectionTag ct = (ConnectionTag)_connections[index];
- _connections.RemoveAt(index);
- _connections.Insert(newindex, ct);
- if(_activeIndex==index) _activeIndex = newindex;
- }
- public ConnectionTag FirstMatch(TagCondition cond) {
- foreach(ConnectionTag ct in _connections) {
- if(cond(ct)) return ct;
- }
- return null;
- }
- internal void Dump() {
- Debug.WriteLine("Connection List");
- foreach(ConnectionTag ct in _connections) {
- Debug.WriteLine(String.Format("pos={0} close={1} pane={2} visible={3}", ct.PositionIndex, ct.Connection.IsClosed, (ct.Pane!=null), (ct.Pane!=null && ct.Pane.FakeVisible)));
- if(ct.Pane!=null) Debug.Assert(ct.Pane.Connection == ct.Connection);
- }
- }
- }
- public delegate bool TagCondition(ConnectionTag pane);
- //Invalidate偵昁梫側僷儔儊乕僞
- internal class InvalidateParam {
- private Delegate _delegate;
- private object[] _param;
- private bool _set;
- public void Set(Delegate d, object[] p) {
- _delegate = d;
- _param = p;
- _set = true;
- }
- public void Reset() {
- _set = false;
- }
- public void InvokeFor(Control c) {
- if(_set) c.Invoke(_delegate, _param);
- }
- }
- //愙懕偵懳偟偰娭楢晅偗傞僨乕僞
- [Serializable]
- public class ConnectionTag {
- private TerminalConnection _connection;
- private Control _tabButton; //僞僽偺拞偵擖傞儃僞儞
- private TerminalPane _pane; //儁僀儞丅旕昞帵偺偲偒偼null
- private TerminalDocument _document;
- private ITerminal _terminal;
- private TerminalDataReceiver _receiver;
- private RenderProfile _renderProfile;
- private IModalTerminalTask _modalTerminalTask;
- private Process _childProcess;
- private InvalidateParam _invalidateParam;
-
- //僂傿儞僪僂偺昞帵梡僥僉僗僩
- internal string _windowTitle; //儂僗僩OSC僔乕働儞僗偱巜掕偝傟偨僞僀僩儖
-
- private bool _terminated;
- private int _positionIndex;
- private int _preservedPositionIndex; //Single儌乕僪偵側偭偨偲偒偺偨傔偺戅旔梡
- private ThTimer _timer;
- public ConnectionTag(TerminalConnection c) {
- _connection = c;
- _pane = null;
- _invalidateParam = new InvalidateParam();
- _tabButton = null;
- _document = new TerminalDocument(_connection);
- _receiver = new TerminalDataReceiver(this);
- _terminated = false;
- _timer = null;
- _windowTitle = "";
- //null偺偲偒偼僨僼僅儖僩僾儘僼傽僀儖傪巊偆
- _renderProfile = c.Param.RenderProfile;
- //VT100巜掕偱傕xterm僔乕働儞僗傪憲偭偰偔傞傾僾儕働乕僔儑儞偑屻傪偨偨側偄偺偱
- _terminal = new XTerm(this, new JapaneseCharDecoder(_connection));
- /*
- if(c.Param.TerminalType==TerminalType.XTerm || c.Param.TerminalType==TerminalType.KTerm)
- _terminal = new XTerm(this, new JapaneseCharDecoder(_connection));
- else
- _terminal = new VT100Terminal(this, new JapaneseCharDecoder(_connection));
- */
- GEnv.Connections.KeepAlive.SetTimerToConnectionTag(this);
- }
- //僪僉儏儊儞僩峏怴捠抦 庴怣僗儗僢僪偱偺幚峴側偺偱拲堄
- public interface IEventReceiver {
- void OnUpdate();
- void OnDisconnect();
- }
- private IEventReceiver _eventReceiver;
- public IEventReceiver EventReceiver {
- get {
- return _eventReceiver;
- }
- set {
- _eventReceiver = value;
- }
- }
- public IModalTerminalTask ModalTerminalTask {
- get {
- return _modalTerminalTask;
- }
- set {
- _modalTerminalTask = value;
- }
- }
- public Process ChildProcess {
- get {
- return _childProcess;
- }
- set {
- _childProcess = value;
- }
- }
- internal InvalidateParam InvalidateParam {
- get {
- return _invalidateParam;
- }
- }
- public string WindowTitle {
- get {
- return _windowTitle;
- }
- set {
- _windowTitle = value;
- }
- }
- public string FormatTabText() {
- string t = _connection.Param.Caption;
- if(t==null || t.Length==0) t = _connection.Param.ShortDescription;
- if(_connection.IsClosed) t += GEnv.Strings.GetString("Caption.ConnectionTag.Disconnected");
- if(_modalTerminalTask!=null) t += "("+_modalTerminalTask.Caption+")";
- return t;
- }
- //Frame偺僉儍僾僔儑儞梡暥帤楍
- public string FormatFrameText() {
- string t = FormatTabText();
- if(_windowTitle.Length!=0 && _connection.Param.Caption!=_windowTitle) //TabCaption傪WindowTitle偵堦抳偝偣傞僆僾僔儑儞傪巊偭偰偄傞偲烼摡偟偔側傞偺偱堎側傞偲偒偺傒昞帵
- t += "[" + _windowTitle + "]";
- return t;
- }
- public Control Button {
- get {
- return _tabButton;
- }
- set {
- _tabButton = value;
- }
- }
- public TerminalDocument Document {
- get {
- return _document;
- }
- }
- public TerminalConnection Connection {
- get {
- return _connection;
- }
- }
- public TerminalPane AttachedPane {
- get {
- return _pane;
- }
- }
- public ITerminal Terminal {
- get {
- return _terminal;
- }
- }
- public TerminalDataReceiver Receiver {
- get {
- return _receiver;
- }
- }
-
- public RenderProfile RenderProfile {
- get {
- return _renderProfile;
- }
- set {
- _renderProfile = value;
- _connection.Param.RenderProfile = value;
- }
- }
- internal RenderProfile GetCurrentRenderProfile() {
- return _renderProfile==null? GEnv.DefaultRenderProfile : _renderProfile;
- }
-
- internal TerminalPane Pane {
- get {
- return _pane;
- }
- set {
- _pane = value;
- }
- }
- internal ThTimer Timer {
- get {
- return _timer;
- }
- set {
- _timer = value;
- }
- }
- internal void NotifyUpdate() {
- if(_pane!=null)
- _pane.DataArrived();
- _terminal.SignalData();
- if(_eventReceiver!=null)
- _eventReceiver.OnUpdate();
- }
- internal void NotifyDisconnect() {
- if(_eventReceiver!=null)
- _eventReceiver.OnDisconnect();
- }
- public int PositionIndex {
- get {
- return _positionIndex;
- }
- set {
- _positionIndex = value;
- }
- }
- public int PreservedPositionIndex {
- get {
- return _preservedPositionIndex;
- }
- set {
- _preservedPositionIndex = value;
- }
- }
- public bool IsTerminated {
- get {
- return _terminated;
- }
- set {
- _terminated = value;
- if(value && _childProcess!=null) {
- try {
- //_childProcess.Kill(); 幚偼Kill偡傞偲傑偢偄bash巆棷偱傑偢偄
- _childProcess = null;
- }
- catch(Exception) { //婛偵僜働僢僩愗抐偵婲場偡傟偽椺奜偵側傞偙偲傕偁傞偐傕偟傟側偄
- }
- }
- if(value) GEnv.Connections.KeepAlive.ClearTimerToConnectionTag(this);
- }
- }
- public void ImportProperties(ConnectionTag src) {
- _renderProfile = src.RenderProfile;
- _positionIndex = src.PositionIndex;
- _preservedPositionIndex = src.PreservedPositionIndex;
- if(src.Button!=null)
- src.Button.Tag = this;
- _tabButton = src.Button;
- }
- }
- }