📄 frmmain.cs
字号:
// =====================================================================
//
// EtermDEMO - A DEMO for Building and Testing eTerm SDK and Web eTerm
//
// by Xsharp
// Copyright Leey.Net, Oct 2007
// http://www.leey.net
//
// =====================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Text.RegularExpressions;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using Leey.Net.eTerm;
using System.Xml;
using RemotableObjects;
namespace Eterm
{
public partial class frmMain : Form, IMessageFilter, IObserver
{
delegate void NotifyDelegate(object sender, string text);
private MyRemotableObject remotableObject = null;
//private RichTextBox temprb = new RichTextBox();
//private StringBuilder tempsb = null;
private EtermConnection ec;
public frmMain()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
//此处请设置为从代理商处取得的激活码
Leey.Net.eTerm.LicenseManager.Instance.Key = "ceshiceshiceshi";
ec = new EtermConnection();
ec.OnReadPacket += new EtermConnection.CmdResultHandler(ec_OnReadPacket);
ec.OnSendPacket += new EtermConnection.PacketHandler(ec_OnSendPacket);
ec.OnLogin +=new Leey.Net.eTerm.Net.ObjectHandler(ec_OnLogin);
ec.OnUnLogin+=new Leey.Net.eTerm.Net.ObjectHandler(ec_OnUnLogin);
ec.OnEtermConnectionStateChanged+=new EtermConnection.EtermConnectionStateHandler(ec_OnEtermConnectionStateChanged);
ec.OnGetChkCode += new EtermConnection.ChkCodeHandler(ec_OnGetChkCode);
ec.OnError += new Leey.Net.eTerm.Net.ErrorHandler(ec_OnError);
//ec.LocalIP = "10.108.16.20";
//连续执行指令之间的间隔。 对于新申请的航信配置可适当设置此参数,避免指令执行过快出现 Transation in process 错误。
ec.ExeInterval = 1000;
//实现REMOTING
HttpServerChannel channel = new HttpServerChannel(8081);
ChannelServices.RegisterChannel(channel,false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyRemotableObject), "HelloWorld.rem", WellKnownObjectMode.Singleton);
RemotableObjects.Cache.Attach(this);
//启动日志记录
LogManager.Instance.LoadConfiguration();
LogManager.Instance.Log.Info("应用程序启动");
}
//网页上发送来的指令,都会被传到Notify函数中, 如需对网页上执行的命令进行限制, 可在Notify函数里手工加入判断限制代码。
#region IObserver Members
public void Notify(object sender, string text)
{
if (InvokeRequired)
{
Invoke(new NotifyDelegate(Notify), sender, text);
return;
}
richTextBox1.SelectionColor = Color.LimeGreen;
richTextBox1.SelectionFont = new Font("宋体", 9);
richTextBox1.AppendText("\r\n" + text);
richTextBox1.AppendText("\r\n");
if (ec.m_ConnectionState == StateClass.ConnectionState.Logined)
{
remotableObject = (MyRemotableObject)sender;
if (text.ToUpper().Trim().StartsWith("RT"))
{
//此构造函数可以提取团队中的姓名等特殊数据
RTWithNMAndPrice(text);
}
else if (text.ToUpper().Trim().StartsWith("AV"))
{
GetFlights(text);
}
}
else
{
remotableObject = (MyRemotableObject)sender;
remotableObject.ResultString = "服务器ETERM未登陆 或 执行了RT以外的指令\r\rCommand:" + text;
remotableObject.Set();
}
}
#endregion
#region 更加灵活的指令执行方式。GetFlights实现了自动翻页取得所有航班信息, RTWithNMAndPrice自己提取编码取得姓名信息并同时取得价格信息
private void EvaluatorTeamNM(CmdResult result)
{
if (Regex.IsMatch(ec.CurrentCmdPacket.CmdString.ToUpper().Trim(), "RT[0-9A-Z]{5}"))
{
if (Regex.IsMatch(result.Data, @"\sNM([0-9]{1,3})\s"))
{
result.DiscardResult = true;
CmdPacket c= new CmdPacket("RTN", CmdCB, false, new CmdResultEvaluator(EvaluatorTeamNM), null);
c.EcChannel = ec;
ec.CurrentCmdPacket.SubCmdPacket = c;
}
else
{
CmdPacket c= new CmdPacket("PAT:A", CmdCB, false, new CmdResultEvaluator(EvaluatorTeamNM),
null);
c.EcChannel = ec;
ec.CurrentCmdPacket.SubCmdPacket = c;
}
}
else if (ec.CurrentCmdPacket.CmdString.ToUpper().Trim() == "RTN")
{
if (Regex.IsMatch(ec.TempResultBuffer + result.Data, @"NM([0-9]{1,3}).*?\1\."))
{
CmdPacket c = new CmdPacket("PAT:A", CmdCB, false, new CmdResultEvaluator(EvaluatorTeamNM), null);
c.EcChannel = ec;
ec.CurrentCmdPacket.SubCmdPacket = c;
}
else
{
CmdPacket c = new CmdPacket("PN", CmdCB, true, new CmdResultEvaluator(EvaluatorTeamNM), null);
c.EcChannel = ec;
ec.CurrentCmdPacket.SubCmdPacket = c;
}
}
else if (ec.CurrentCmdPacket.CmdString.ToUpper().Trim() == "PN")
{
if (Regex.IsMatch(ec.TempResultBuffer + result.Data, @"NM([0-9]{1,3}).*?\1\."))
{
CmdPacket c = new CmdPacket("PAT:A", CmdCB, false, new CmdResultEvaluator(EvaluatorTeamNM), null);
c.EcChannel = ec;
ec.CurrentCmdPacket.SubCmdPacket = c;
}
else
{
ec.CurrentCmdPacket.Repeat = true;
}
}
else if (ec.CurrentCmdPacket.CmdString.ToUpper().Trim() == "PAT:A")
{
if (!result.Data.ToUpper().Contains("TOTAL:"))
{
CmdPacket c = new CmdPacket("PAT:", CmdCB, false, new CmdResultEvaluator(EvaluatorTeamNM), null);
c.EcChannel = ec;
ec.CurrentCmdPacket.SubCmdPacket = c;
}
}
}
private void EvaluatorAVH(CmdResult result)
{
AirFlights afs = new AirFlights();
afs.Load(result.Data);
result.Airflights = afs;
string resultdate = result.GetAvhdate();
string cmdate = ec.ParentCmdPacket.GetAvhdate();
if (resultdate == cmdate)
{
if (afs.Count > 5)
{
CmdPacket c = new CmdPacket("PN", CmdCB, false, new CmdResultEvaluator(EvaluatorAVH), null);
c.EcChannel = ec;
ec.CurrentCmdPacket.SubCmdPacket = c;
}
}
else
{
result.DiscardResult = true;
}
}
public void RTWithNMAndPrice(string s)
{
CmdPacket c = new CmdPacket(s, CmdCB, new CmdResultEvaluator(EvaluatorTeamNM), null);
c.EcChannel = ec;
c.Send();
//ec.SendCmd(c);
}
public void GetFlights(string s)
{
CmdPacket c = new CmdPacket(s, CmdCB, new CmdResultEvaluator(EvaluatorAVH), null);
c.EcChannel = ec;
c.Send();
//ec.SendCmd(c);
}
#endregion
#region 键盘消息过滤函数,主要处理小回车,F12的输入问题
public bool PreFilterMessage(ref System.Windows.Forms.Message m )
{
if ((Keys)m.WParam.ToInt32() == Keys.Escape)
{
if (m.LParam.ToInt32() == 65537 )
{
richTextBox1.SelectedText = ""+(char)9632;
return true;
}
}
if (Control.ModifierKeys == Keys.Control && (Keys)m.WParam.ToInt32() == Keys.V && richTextBox1.Focused)
{
if (m.LParam.ToInt32() == 3080193)
{
richTextBox1.AppendText(Clipboard.GetText());
return true;
}
}
if ((Keys)m.WParam.ToInt32() == Keys.Enter)
{
if (m.LParam.ToInt32() == 18612225 && m.Msg == 256 && richTextBox1.Focused)
{
DealwithCmd();
}
else
{
return false;
}
}
if ((Keys)m.WParam.ToInt32() == Keys.F12 && richTextBox1.Focused)
{
if (m.LParam.ToInt32() == 5767169)
{
DealwithCmd();
}
}
return false;
}
#endregion
private void DealwithCmd()
{
string s = GetCommandLine().Trim();
if (s != "")
{
if (s.ToUpper().StartsWith("BENPRINT"))
{
string[] splits=Regex.Split(s,@"\s{1,}");
if (splits.Length != 3) { throw new Exception("BENPRINT格式不对"); }
Packet c = new BenPrintPacket(splits[1], splits[2], CmdCB);
c.EcChannel = ec;
c.Send();
}
else
{
CmdPacket c = new CmdPacket(s, CmdCB);
c.EcChannel = ec;
c.Send();
}
}
else
{
richTextBox1.Text += "\r\n" + "请检查命令格式!";
richTextBox1.Text += "\r\n" + (char)9632;
}
}
private void Open(object sender)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -