/// <summary> /// Constructor</summary> /// <param name="target">The target.</param> /// <param name="defaultPortNumber">Default port number</param> /// <param name="canEditPortNumber">True iff can edit port number</param> /// <param name="protocols">List of supported protocols</param> public TargetEditDialog( Target target, int defaultPortNumber, bool canEditPortNumber, string[] protocols ) { InitializeComponent(); if (protocols != null && protocols.Length > 0) cmbProtocol.DataSource = protocols; else cmbProtocol.Enabled = false; if (target != null) { Text = "Edit Target".Localize(); txtName.Text = target.Name; txtHost.Text = target.Host; txtPort.Text = target.Port.ToString(); txtPort.ReadOnly = !canEditPortNumber; if(cmbProtocol.Enabled) cmbProtocol.SelectedItem = target.Protocol; m_target = target; } else { Text = "Add Target".Localize(); if (defaultPortNumber > 0) { txtPort.Text = defaultPortNumber.ToString(); txtPort.ReadOnly = !canEditPortNumber; } if (cmbProtocol.Enabled) cmbProtocol.SelectedIndex = 0; } }
/// <summary> /// Creates a client TCP socket using an already connected socket with a default /// maximum message size</summary> /// <param name="s">An already connected socket</param> public TargetTcpSocket(Socket s) : this(5000) { if (s == null) throw new ArgumentNullException(); m_theSocket = s; if (!IsConnected) throw new Exception("only accept connected socket"); IPEndPoint endpt = (IPEndPoint)s.RemoteEndPoint; m_curTarget = new Target("client", endpt.Address.ToString(), endpt.Port); m_curTarget.IsConnected = true; }
/// <summary> /// Adds a new target. If the target already exists, an exception is thrown.</summary> /// <param name="name">Name of the target machine</param> /// <param name="host">Host name or IP address</param> /// <param name="port">Port number</param> public void AddTarget(string name, string host, int port) { if (StringUtil.IsNullOrEmptyOrWhitespace(name) || StringUtil.IsNullOrEmptyOrWhitespace(host)) throw new ArgumentNullException(); if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort) throw new ArgumentOutOfRangeException(); if (m_targets.ContainsKey(name)) throw new Exception(name + " already exist"); Target t = new Target(name.Trim(), host.Trim(), port); m_targets.Add(t.Name, t); }
/// <summary> /// Creates a new object that is a copy of the current instance</summary> /// <returns>New object that is a copy of the current instance</returns> public object Clone() { Target t = new Target(m_name, m_host,m_port); t.m_connected = m_connected; t.m_selected = m_selected; t.m_tag = m_tag; t.m_protocol = m_protocol; return t; }
private bool ValidateAndFillInData() { string strName = txtName.Text.Trim(); string strHost = txtHost.Text.Trim(); string strPort = txtPort.Text; int port = 0; if (StringUtil.IsNullOrEmptyOrWhitespace(strName)) { MessageBox.Show(this, "Fill target name".Localize()); txtName.Focus(); return false; } if (StringUtil.IsNullOrEmptyOrWhitespace(strHost)) { MessageBox.Show(this, "Fill host name".Localize()); txtHost.Focus(); return false; } // validate port number try { port = int.Parse(strPort); if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort) throw new Exception(); } catch { MessageBox.Show(this, string.Format("Invalid port number".Localize(), IPEndPoint.MinPort, IPEndPoint.MaxPort)); return false; } if (m_target == null) { m_target = new Target(strName, strHost, port); m_changed = true; } else { if (m_target.Name != strName || m_target.Host != strHost || m_target.Port != port) { m_target.Set(strName, strHost, port); m_changed = true; } } if (cmbProtocol.Enabled) { m_target.Protocol = (string)cmbProtocol.SelectedItem; m_changed = true; } return true; }
private void m_btnCancel_Click(object sender, EventArgs e) { m_target = null; }
private void OnConnect(Target trg) { ConnectHandler handler = Connected; m_curTarget = trg; m_curTarget.IsConnected = true; if (handler != null) { m_cctx.Send(delegate { handler(this, trg); }, null); } }
/// <summary> /// Connects using IPEndPoint</summary> /// <param name="target">Target</param> public void Connect(Target target) { lock (m_syncSocket) { if (m_ConnectionInProgress) return; } if (IsConnected) return; lock (m_syncSocket) { if (m_theSocket != null) { m_theSocket.Close(); m_theSocket = null; m_curTarget = null; } } // try to connect; try { lock (m_syncSocket) { IPAddress ipaddr = target.IPAddress; m_theSocket = new Socket(ipaddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp); m_theSocket.Blocking = true; m_theSocket.SendTimeout = 5000; //m_theSocket.ExclusiveAddressUse = true; m_theSocket.BeginConnect(ipaddr,target.Port, m_connectClb,target); m_ConnectionInProgress = true; } } catch (Exception ex) { lock (m_syncSocket) { if (m_theSocket != null) { m_theSocket.Close(); m_theSocket = null; m_curTarget = null; } } OnUnHandledException(ex); } }
private ListViewItem CreateListViewItem(Target target) { ListViewItem item = new ListViewItem(target.ToString()); item.Name = target.Name; item.Tag = target; item.Checked = target.Selected; return item; }