/// <summary> /// 通过配置文件添加配置. /// </summary> private void AppendAsync() { foreach (ConnectSetup T in ASyncSetup.AsynCollection) { if (!T.IsOpen) { continue; } eTerm443Async async = new eTerm443Async( T.Address, T.Port, T.userName, T.userPass, (byte)T.SID, (byte)T.RID) { LocalEP = (T.TSessionType ?? CertificationType.Address) == CertificationType.Address ? new IPEndPoint(IPAddress.Parse(T.LocalIp), 0) : null, SiText = T.SiText, IsSsl = T.IsSsl, OfficeCode = T.OfficeCode, GroupCode = T.GroupCode, AutoSi = T.AutoSi ?? false, groups = T.groups == null? new List <string>():T.groups }; AppendAsync(async); } }
/// <summary> /// Handles the Click event of the btnDispose control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void btnDispose_Click(object sender, EventArgs e) { foreach (ListViewItem item in this.lstAsync.SelectedItems) { eTerm443Async Async = item.Tag as eTerm443Async; Async.Close(); } }
/// <summary> /// Handles the Click event of the btnRelease control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void btnRelease_Click(object sender, EventArgs e) { foreach (ListViewItem item in this.lstAsync.SelectedItems) { eTerm443Async Async = item.Tag as eTerm443Async; Async.TSession = null; } }
/// <summary> /// Updates the A sync session. /// </summary> /// <param name="ASync">The A sync.</param> private void UpdateASyncSession(eTerm443Async ASync) { try { ConnectSetup TSession = ASyncSetup.AsynCollection.SingleOrDefault <ConnectSetup>(Fun => Fun.userPass == ASync.userPass && Fun.userName == ASync.userName); SocketTraffic Traffic = TSession.Traffics[TSession.Traffics.IndexOf(new SocketTraffic(DateTime.Now.ToString(@"yyyyMM")))]; Traffic.Traffic++; Traffic.UpdateDate = DateTime.Now; } catch { } }
/// <summary> /// Disconnects the A sync. /// </summary> /// <param name="ASync">The A sync.</param> private void disconnectASync(eTerm443Async ASync) { if (this.InvokeRequired) { this.BeginInvoke(new ASynConnectCallback(disconnectASync), ASync); return; } try { //SQLiteExecute.Instance.BeginExecute(ASync.userName, (ASync.AsyncSocket.RemoteEndPoint as IPEndPoint).Address.ToString(), new byte[]{}, @"ASynDisconnect"); appendASynConnect(ASync); string SessionId = string.Format(@"{0}{1}{2}", ASync.RemoteEP.ToString(), ASync.userName, ASync.IsSsl); ListViewItem item = this.lstAsync.Items[SessionId]; item.ImageKey = @"Circle_Red.png"; item.Group = group2; } catch { } }
/// <summary> /// Packets the A sync. /// </summary> /// <param name="ASync">The A sync.</param> private void packetASync(eTerm443Async ASync) { if (this.InvokeRequired) { this.BeginInvoke(new ASynConnectCallback(packetASync), ASync); return; } try { string SessionId = string.Format(@"{0}{1}{2}", ASync.RemoteEP.ToString(), ASync.userName, ASync.IsSsl); ListViewItem item = this.lstAsync.Items[SessionId]; item.SubItems[2].Text = string.Format(@"{0} KBytes", ASync.TotalBytes.ToString(@"f2")); item.SubItems[3].Text = ASync.TotalCount.ToString(); item.SubItems[5].Text = string.Format(@"{0} Bytes", ASync.CurrentBytes.ToString(@"f0")); item.SubItems[6].Text = ASync.LastActive.ToString(@"HH:mm:ss"); item.ImageKey = @"Circle_Green.png"; } catch { } }
/// <summary> /// Appends the A syn connect. /// </summary> /// <param name="ASync">The A sync.</param> private void appendASynConnect(eTerm443Async ASync) { if (this.InvokeRequired) { this.BeginInvoke(new ASynConnectCallback(appendASynConnect), ASync); return; } try { string SessionId = string.Format(@"{0}{1}{2}", ASync.RemoteEP.ToString(), ASync.userName, ASync.IsSsl); //SQLiteExecute.Instance.BeginExecute(ASync.userName, (ASync.AsyncSocket.RemoteEndPoint as IPEndPoint).Address.ToString(), new byte[]{}, @"ASynConnect"); ListViewItem connectItem; if (this.lstAsync.Items.ContainsKey(SessionId)) { connectItem = this.lstAsync.Items[SessionId]; connectItem.ImageKey = @"Circle_Green.png"; connectItem.SubItems[4].Text = ASync.ReconnectCount.ToString(); connectItem.Group = group1; return; } connectItem = new ListViewItem( new string[] { ASync.RemoteEP.ToString(), ASync.userName, ASync.TotalBytes.ToString("f2"), ASync.TotalCount.ToString(), ASync.ReconnectCount.ToString(), string.Format(@"{0} KBytes", ASync.CurrentBytes.ToString(@"f2")), ASync.LastActive.ToString(@"HH:mm:ss") }, group1) { Name = SessionId }; connectItem.Tag = ASync; connectItem.ImageKey = @"Circle_Green.png"; this.lstAsync.Items.Add(connectItem); } catch { } }
/// <summary> /// Updates the list by thread. /// </summary> /// <param name="ViewEx">The view ex.</param> /// <param name="Item">The item.</param> /// <param name="Id">The id.</param> private void UpdateListByThread(ListViewEx ViewEx, string ImageKey, string Id, float TotalBytes, string Reconnect) { if (this.InvokeRequired) { this.BeginInvoke(new UpdateListViewItem(UpdateListByThread), ViewEx, ImageKey, Id, TotalBytes, Reconnect); return; } try { if (TotalBytes <= 0) { //if (!ViewEx.Items.Contains(new ListViewItem() {Name=Id })) return; eTerm443Async Async = ViewEx.Items[Id].Tag as eTerm443Async; ViewEx.Items[Id].Remove(); //Thread.Sleep(5000); //AsyncStackNet.Instance.ReconnectAsync(Async); return; } ViewEx.Items[Id].SubItems[2].Text = TotalBytes.ToString("f2"); ViewEx.Items[Id].SubItems[3].Text = Reconnect; ViewEx.Items[Id].ImageKey = ImageKey; } catch { } }
/// <summary> /// Handles the Click event of the btnConnect control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void btnConnect_Click(object sender, EventArgs e) { if (this.btnConnect.Tag != null) { this.__ClientSocket.Close(); return; } eTerm.AsyncSDK.LicenceManager.Instance.BeginValidate(new AsyncCallback(delegate(IAsyncResult iar) { try { ResetButton(false); if (!eTerm.AsyncSDK.LicenceManager.Instance.EndValidate(iar)) { //PacketPush(@"认证失败", false, 0x00, 0x00); } else { //激活配置 //PacketPush(@"认证成功", true, 0x00, 0x00); this.__ClientSocket = new eTerm443Async(this.txtAddress.Text, int.Parse(this.txtPort.Value.ToString()), this.txtUserName.Text.Trim(), this.txtPassword.Text.Trim(), 0x00, 0x00) { IsSsl = chkIsSsl.Checked }; this.__ClientSocket.TSessionReconnectValidate = new eTerm.AsyncSDK.Base.AsyncBase<eTerm443Async, eTerm443Packet>.ValidateTSessionCallback(delegate(eTerm443Packet Packet, eTerm443Async Connect) { return false; }); this.__ClientSocket.OnAsynConnect += new EventHandler<AsyncEventArgs<eTerm443Async>>( delegate(object sender1, AsyncEventArgs<eTerm443Async> e1) { //PacketPush(string.Format(@"会话{0}开始连接", e1.Session.SessionId), false, 0x00, 0x00); } ); this.__ClientSocket.OnValidated += new EventHandler<AsyncEventArgs<eTerm443Packet, eTerm443Async>>( delegate(object sender1, AsyncEventArgs<eTerm443Packet, eTerm443Async> e1) { //PacketPush(string.Format(@"会话{0}认证完成", e1.Session.SessionId), true, 0x00, 0x00); ResetButton(false); e1.Session.SendPacket(@"IG"); } ); this.__ClientSocket.OnReadPacket += new EventHandler<AsyncEventArgs<eTerm443Packet, eTerm443Packet, eTerm443Async>>( delegate(object sender1, AsyncEventArgs<eTerm443Packet, eTerm443Packet, eTerm443Async> e1) { PacketPush(Encoding.GetEncoding("gb2312").GetString(e1.Session.UnOutPakcet(e1.InPacket)), true, e1.InPacket.OriginalBytes[0x0e], e1.InPacket.OriginalBytes[0x0f]); } ); this.__ClientSocket.OnAsyncDisconnect += new EventHandler<AsyncEventArgs<eTerm443Async>>( delegate(object sender1, AsyncEventArgs<eTerm443Async> e1) { //PacketPush(string.Format(@"会话{0}连接断开", e1.Session.SessionId), false, 0x00, 0x00); ResetButton(true); } ); this.__ClientSocket.Connect(this.txtAddress.Text, int.Parse(this.txtPort.Value.ToString()), chkIsSsl.Checked); } } catch (Exception ex) { MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } }), @"D:\SouceCode\Personal\eTerm.AsyncSDK3.0\ASyncSDK.Office\bin\Release\Key.Bin"); }
/// <summary> /// 追加活动配置. /// </summary> /// <param name="Async">配置实体.</param> public void AppendAsync(eTerm443Async Async) { #region 授权校验 if (__asyncList.Count >= LicenceManager.Instance.LicenceBody.MaxAsync) return; #endregion Async.Instruction = AsyncStackNet.Instance.ASyncSetup.SequenceDirective; Async.IgInterval = AsyncStackNet.Instance.ASyncSetup.SequenceRate ?? 5; Async.ReconnectDelay = ASyncSetup.ReconnectDelay; #region OnAsynConnect Async.OnAsynConnect += new EventHandler<AsyncEventArgs<eTerm443Async>>( delegate(object sender, AsyncEventArgs<eTerm443Async> e) { if (OnAsyncConnect != null) OnAsyncConnect(sender, e); } ); #endregion #region OnReadPacket Async.OnReadPacket += new EventHandler<eTerm.AsyncSDK.AsyncEventArgs<eTerm443Packet, eTerm443Packet, eTerm443Async>>( delegate(object sender, AsyncEventArgs<eTerm443Packet, eTerm443Packet, eTerm443Async> e) { if (e.Session.TSession != null) { byte[] PacketBytes = e.InPacket.OriginalBytes; PacketBytes[8] = e.Session.TSession.SID; PacketBytes[9] = e.Session.TSession.RID; e.Session.TSession.IsCompleted = true; e.Session.TSession.SendPacket(PacketBytes); } UpdateASyncSession(e.Session); if (LicenceManager.Instance.LicenceBody.AllowAfterValidate) { try { string Command = Encoding.GetEncoding("gb2312").GetString(e.Session.UnInPakcet(e.OutPacket)).Trim().ToLower(); foreach (var PlugIn in from entry in AsyncStackNet.Instance.ASyncSetup.PlugInCollection where Command.ToLower().StartsWith(entry.PlugInName.ToLower()) orderby entry.PlugInName ascending select entry) { if (PlugIn.ASyncInstance == null) continue; PlugIn.ASyncInstance.BeginExecute(new AsyncCallback(delegate(IAsyncResult iar) { PlugIn.ASyncInstance.EndExecute(iar); }), e.Session, e.InPacket, e.OutPacket, LicenceManager.Instance.LicenceBody); } } catch { } } if (this.OnAsyncReadPacket != null) this.OnAsyncReadPacket(sender, e); } ); #endregion #region OnAsyncTimeout Async.OnAsyncTimeout += new EventHandler<AsyncEventArgs<eTerm443Async>>( delegate(object sender, AsyncEventArgs<eTerm443Async> e) { if (e.Session.TSession != null) { e.Session.TSession.SendPacket(__eTerm443Packet.AsyncSocketTimeoutInfo(e.Session.TSession.SID, e.Session.TSession.RID)); } if (this.OnAsyncTimeout != null) this.OnAsyncTimeout(sender, e); } ); #endregion #region OnBeginConnect Async.OnBeginConnect += new EventHandler<AsyncEventArgs<eTerm443Async>>( delegate(object sender, AsyncEventArgs<eTerm443Async> e) { if (this.OnBeginConnect != null) this.OnBeginConnect(sender, e); } ); #endregion #region OnAsyncPacketSent Async.OnPacketSent += new EventHandler<AsyncEventArgs<eTerm443Packet, eTerm443Packet, eTerm443Async>>( delegate(object sender, AsyncEventArgs<eTerm443Packet, eTerm443Packet, eTerm443Async> e) { if (this.OnAsyncPacketSent != null) this.OnAsyncPacketSent(sender, e); } ); #endregion #region OnValidated Async.OnValidated += new EventHandler<AsyncEventArgs<eTerm443Packet, eTerm443Async>>( delegate(object sender, AsyncEventArgs<eTerm443Packet, eTerm443Async> e) { if (this.OnAsyncValidated != null) this.OnAsyncValidated(sender, e); string CurrentMonth = DateTime.Now.ToString(@"yyyyMM"); ConnectSetup TSession = ASyncSetup.AsynCollection.SingleOrDefault<ConnectSetup>(Fun => Fun.userPass ==e.Session.userPass && Fun.userName ==e.Session.userName); if (!TSession.Traffics.Contains(new SocketTraffic(CurrentMonth))) TSession.Traffics.Add(new SocketTraffic() { MonthString = CurrentMonth, Traffic = 0.0, UpdateDate = DateTime.Now }); SocketTraffic Traffic = TSession.Traffics[TSession.Traffics.IndexOf(new SocketTraffic(CurrentMonth))]; if (Traffic.Traffic >= TSession.FlowRate) { e.Session.ObligatoryReconnect = false; e.Session.Close(); return; } this.__asyncList.Add(Async); } ); #endregion #region CallBack Async.TSessionReconnectValidate =new AsyncBase<eTerm443Async,eTerm443Packet>.ValidateTSessionCallback(delegate(eTerm443Packet Packet,eTerm443Async ASync) { return (ASyncSetup.AutoReconnect??false)&&Async.ReconnectCount<(ASyncSetup.MaxReconnect??10); }); #endregion #region OnAsyncDisconnect Async.OnAsyncDisconnect += new EventHandler<AsyncEventArgs<eTerm443Async>>( delegate(object sender, AsyncEventArgs<eTerm443Async> e) { UpdateASyncSession(e.Session); this.__asyncList.Remove(sender as eTerm443Async); if (this.OnAsyncDisconnect != null) this.OnAsyncDisconnect(sender, e); if (e.Session.TSession == null) return; if (!e.Session.TSession.IsCompleted) e.Session.TSession.SendPacket(__eTerm443Packet.BuildSessionPacket(e.Session.TSession.SID, e.Session.TSession.RID, "指令错误!")); e.Session.TSession.Async443 = null; } ); #endregion #region 调用 Async.Connect(Async.Address, Async.Port, Async.IsSsl); #endregion }
/// <summary> /// Updates the A sync. /// </summary> /// <param name="ASync">The A sync.</param> private void updateASync(eTerm443Async ASync) { if (this.InvokeRequired) { this.BeginInvoke(new ASynConnectCallback(updateASync), ASync); return; } try { string SessionId = string.Format(@"{0}{1}{2}", ASync.RemoteEP.ToString(), ASync.userName, ASync.IsSsl); ListViewItem item = this.lstAsync.Items[SessionId]; item.SubItems[2].Text =string.Format(@"{0} KBytes", ASync.TotalBytes.ToString(@"f2")); item.SubItems[3].Text = ASync.TotalCount.ToString(); item.SubItems[5].Text = ASync.CurrentBytes.ToString(@"f2"); item.SubItems[6].Text = ASync.LastActive.ToString(@"HH:mm:ss"); item.ImageKey = @"Circle_Yellow.png"; } catch { } }
/// <summary> /// Connects the core. /// </summary> private void ConnectCore() { if ( string.IsNullOrEmpty(AsyncStackNet.Instance.ASyncSetup.CoreServer) || string.IsNullOrEmpty(AsyncStackNet.Instance.ASyncSetup.CoreAccount) || string.IsNullOrEmpty(AsyncStackNet.Instance.ASyncSetup.CorePass) || (AsyncStackNet.Instance.ASyncSetup.CoreServerPort ?? 0) == 0 ) { throw new NotImplementedException(@"系统缺少中心指令服务器相关设置,请联系发开商!"); } ///TODO:初始化认证连接,认证频率计时器为:30分钟 __CoreASync = new eTerm443Async( ASyncSetup.CoreServer, ASyncSetup.CoreServerPort.Value, ASyncSetup.CoreAccount, ASyncSetup.CorePass, 0x00, 0x00) { IsSsl = false, Instruction = string.Format(@"!UpdateDate {0}", LicenceManager.Instance.SerialNumber), IgInterval = 30, ReconnectDelay = ASyncSetup.ReconnectDelay }; __CoreASync.OnAsyncDisconnect += new EventHandler <AsyncEventArgs <eTerm443Async> >( delegate(object sender, AsyncEventArgs <eTerm443Async> e) { //this.LocalEndPoint = e.Session.AsyncSocket.LocalEndPoint as IPEndPoint; if (this.OnCoreDisconnect != null) { this.OnCoreDisconnect(sender, e); } } ); __CoreASync.TSessionReconnectValidate = new AsyncBase <eTerm443Async, eTerm443Packet> .ValidateTSessionCallback( delegate(eTerm443Packet Packet, eTerm443Async ASync) { return(true); }); __CoreASync.OnAsynConnect += new EventHandler <AsyncEventArgs <eTerm443Async> >( delegate(object sender, AsyncEventArgs <eTerm443Async> e) { this.LocalEndPoint = e.Session.AsyncSocket.LocalEndPoint as IPEndPoint; if (this.OnCoreConnect != null) { this.OnCoreConnect(sender, e); } } ); __CoreASync.OnReadPacket += new EventHandler <AsyncEventArgs <eTerm443Packet, eTerm443Packet, eTerm443Async> >( delegate(object sender, AsyncEventArgs <eTerm443Packet, eTerm443Packet, eTerm443Async> e) { try { if (e.InPacket.OriginalBytes[0] == 0x00) { return; } string coreDate = Regex.Match(Encoding.GetEncoding("gb2312").GetString(e.Session.UnOutPakcet(e.InPacket)), @"(\d{4}\-\d{1,2}\-\d{1,2})\s+\d{2,2}\:\d{2,2}\:\d{2,2}").Value; DateTime serverDate = DateTime.Parse(coreDate); if (((TimeSpan)(serverDate - DateTime.Now)).Days != 0) { SystemUtil.SetSysTime(serverDate); //日期比较 if (this.OnSystemException != null) { this.OnSystemException(sender, new ErrorEventArgs(new DataMisalignedException(@"为防止授权错误,不允许手工修改系统日期,请联系发开发商"))); } return; } if ( ((TimeSpan)(LicenceManager.Instance.LicenceBody.ExpireDate - DateTime.Now)).TotalDays <= 3 && ((TimeSpan)(LicenceManager.Instance.LicenceBody.ExpireDate - DateTime.Now)).TotalDays >= 1 ) { if (this.OnSystemException != null) { this.OnSystemException(sender, new ErrorEventArgs(new ArithmeticException(@"系统授权即将到期,如需继续使用请从开发商获取新授权"))); } BeginRateUpdate(new AsyncCallback(delegate(IAsyncResult iar) { EndRateUpdate(iar); iar.AsyncWaitHandle.Close(); __asyncServer.Stop(); })); return; } else if (((TimeSpan)(LicenceManager.Instance.LicenceBody.ExpireDate - DateTime.Now)).TotalDays <= 0) { LicenceManager.Instance.LicenceBody.RemainingMinutes = 0; LicenceManager.Instance.LicenceBody.ExpireDate = serverDate; BeginRateUpdate(new AsyncCallback(delegate(IAsyncResult iar) { EndRateUpdate(iar); })); if (OnSDKTimeout != null) { OnSDKTimeout(sender, new ErrorEventArgs(new TimeZoneNotFoundException(@"系统授权已到期,如需继续使用请从开发商获取新授权"))); } } else { if (this.OnSystemException != null) { this.OnSystemException(sender, new ErrorEventArgs(new ExecutionEngineException(@"认证信息验证完成"))); } } } catch (Exception ex) { if (this.OnSystemException != null) { this.OnSystemException(sender, new ErrorEventArgs(ex)); } } } ); __CoreASync.Connect(ASyncSetup.CoreServer, ASyncSetup.CoreServerPort ?? 350, false); }
/// <summary> /// 通过配置文件添加配置. /// </summary> private void AppendAsync() { foreach (ConnectSetup T in ASyncSetup.AsynCollection) { if (!T.IsOpen) continue; eTerm443Async async = new eTerm443Async( T.Address, T.Port, T.userName, T.userPass, (byte)T.SID, (byte)T.RID) { LocalEP = (T.TSessionType ?? CertificationType.Address) == CertificationType.Address ? new IPEndPoint(IPAddress.Parse(T.LocalIp), 0) : null, SiText = T.SiText, IsSsl = T.IsSsl, OfficeCode = T.OfficeCode, GroupCode = T.GroupCode, AutoSi = T.AutoSi ?? false, groups=T.groups==null? new List<string>():T.groups }; AppendAsync(async); } }
/// <summary> /// 追加活动配置. /// </summary> /// <param name="Async">配置实体.</param> public void AppendAsync(eTerm443Async Async) { #region 授权校验 if (__asyncList.Count >= LicenceManager.Instance.LicenceBody.MaxAsync) { return; } #endregion Async.Instruction = AsyncStackNet.Instance.ASyncSetup.SequenceDirective; Async.IgInterval = AsyncStackNet.Instance.ASyncSetup.SequenceRate ?? 5; Async.ReconnectDelay = ASyncSetup.ReconnectDelay; #region OnAsynConnect Async.OnAsynConnect += new EventHandler <AsyncEventArgs <eTerm443Async> >( delegate(object sender, AsyncEventArgs <eTerm443Async> e) { if (OnAsyncConnect != null) { OnAsyncConnect(sender, e); } } ); #endregion #region OnReadPacket Async.OnReadPacket += new EventHandler <eTerm.AsyncSDK.AsyncEventArgs <eTerm443Packet, eTerm443Packet, eTerm443Async> >( delegate(object sender, AsyncEventArgs <eTerm443Packet, eTerm443Packet, eTerm443Async> e) { if (e.Session.TSession != null) { byte[] PacketBytes = e.InPacket.OriginalBytes; PacketBytes[8] = e.Session.TSession.SID; PacketBytes[9] = e.Session.TSession.RID; e.Session.TSession.IsCompleted = true; e.Session.TSession.SendPacket(PacketBytes); } UpdateASyncSession(e.Session); if (LicenceManager.Instance.LicenceBody.AllowAfterValidate) { try { string Command = Encoding.GetEncoding("gb2312").GetString(e.Session.UnInPakcet(e.OutPacket)).Trim().ToLower(); foreach (var PlugIn in from entry in AsyncStackNet.Instance.ASyncSetup.PlugInCollection where Command.ToLower().StartsWith(entry.PlugInName.ToLower()) orderby entry.PlugInName ascending select entry) { if (PlugIn.ASyncInstance == null) { continue; } PlugIn.ASyncInstance.BeginExecute(new AsyncCallback(delegate(IAsyncResult iar) { PlugIn.ASyncInstance.EndExecute(iar); }), e.Session, e.InPacket, e.OutPacket, LicenceManager.Instance.LicenceBody); } } catch { } } if (this.OnAsyncReadPacket != null) { this.OnAsyncReadPacket(sender, e); } } ); #endregion #region OnAsyncTimeout Async.OnAsyncTimeout += new EventHandler <AsyncEventArgs <eTerm443Async> >( delegate(object sender, AsyncEventArgs <eTerm443Async> e) { if (e.Session.TSession != null) { e.Session.TSession.SendPacket(__eTerm443Packet.AsyncSocketTimeoutInfo(e.Session.TSession.SID, e.Session.TSession.RID)); } if (this.OnAsyncTimeout != null) { this.OnAsyncTimeout(sender, e); } } ); #endregion #region OnBeginConnect Async.OnBeginConnect += new EventHandler <AsyncEventArgs <eTerm443Async> >( delegate(object sender, AsyncEventArgs <eTerm443Async> e) { if (this.OnBeginConnect != null) { this.OnBeginConnect(sender, e); } } ); #endregion #region OnAsyncPacketSent Async.OnPacketSent += new EventHandler <AsyncEventArgs <eTerm443Packet, eTerm443Packet, eTerm443Async> >( delegate(object sender, AsyncEventArgs <eTerm443Packet, eTerm443Packet, eTerm443Async> e) { if (this.OnAsyncPacketSent != null) { this.OnAsyncPacketSent(sender, e); } } ); #endregion #region OnValidated Async.OnValidated += new EventHandler <AsyncEventArgs <eTerm443Packet, eTerm443Async> >( delegate(object sender, AsyncEventArgs <eTerm443Packet, eTerm443Async> e) { if (this.OnAsyncValidated != null) { this.OnAsyncValidated(sender, e); } string CurrentMonth = DateTime.Now.ToString(@"yyyyMM"); ConnectSetup TSession = ASyncSetup.AsynCollection.SingleOrDefault <ConnectSetup>(Fun => Fun.userPass == e.Session.userPass && Fun.userName == e.Session.userName); if (!TSession.Traffics.Contains(new SocketTraffic(CurrentMonth))) { TSession.Traffics.Add(new SocketTraffic() { MonthString = CurrentMonth, Traffic = 0.0, UpdateDate = DateTime.Now }); } SocketTraffic Traffic = TSession.Traffics[TSession.Traffics.IndexOf(new SocketTraffic(CurrentMonth))]; if (Traffic.Traffic >= TSession.FlowRate) { e.Session.ObligatoryReconnect = false; e.Session.Close(); return; } this.__asyncList.Add(Async); } ); #endregion #region CallBack Async.TSessionReconnectValidate = new AsyncBase <eTerm443Async, eTerm443Packet> .ValidateTSessionCallback(delegate(eTerm443Packet Packet, eTerm443Async ASync) { return((ASyncSetup.AutoReconnect ?? false) && Async.ReconnectCount < (ASyncSetup.MaxReconnect ?? 10)); }); #endregion #region OnAsyncDisconnect Async.OnAsyncDisconnect += new EventHandler <AsyncEventArgs <eTerm443Async> >( delegate(object sender, AsyncEventArgs <eTerm443Async> e) { UpdateASyncSession(e.Session); this.__asyncList.Remove(sender as eTerm443Async); if (this.OnAsyncDisconnect != null) { this.OnAsyncDisconnect(sender, e); } if (e.Session.TSession == null) { return; } if (!e.Session.TSession.IsCompleted) { e.Session.TSession.SendPacket(__eTerm443Packet.BuildSessionPacket(e.Session.TSession.SID, e.Session.TSession.RID, "指令错误!")); } e.Session.TSession.Async443 = null; } ); #endregion #region 调用 Async.Connect(Async.Address, Async.Port, Async.IsSsl); #endregion }
/// <summary> /// Connects the core. /// </summary> private void ConnectCore() { if ( string.IsNullOrEmpty(AsyncStackNet.Instance.ASyncSetup.CoreServer) || string.IsNullOrEmpty(AsyncStackNet.Instance.ASyncSetup.CoreAccount) || string.IsNullOrEmpty(AsyncStackNet.Instance.ASyncSetup.CorePass) || (AsyncStackNet.Instance.ASyncSetup.CoreServerPort ?? 0) == 0 ) throw new NotImplementedException(@"系统缺少中心指令服务器相关设置,请联系发开商!"); ///TODO:初始化认证连接,认证频率计时器为:30分钟 __CoreASync = new eTerm443Async( ASyncSetup.CoreServer, ASyncSetup.CoreServerPort.Value, ASyncSetup.CoreAccount, ASyncSetup.CorePass, 0x00, 0x00) { IsSsl = false, Instruction = string.Format( @"!UpdateDate {0}",LicenceManager.Instance.SerialNumber), IgInterval = 30, ReconnectDelay=ASyncSetup.ReconnectDelay }; __CoreASync.OnAsyncDisconnect += new EventHandler<AsyncEventArgs<eTerm443Async>>( delegate(object sender, AsyncEventArgs<eTerm443Async> e) { //this.LocalEndPoint = e.Session.AsyncSocket.LocalEndPoint as IPEndPoint; if (this.OnCoreDisconnect != null) this.OnCoreDisconnect(sender, e); } ); __CoreASync.TSessionReconnectValidate = new AsyncBase<eTerm443Async, eTerm443Packet>.ValidateTSessionCallback( delegate(eTerm443Packet Packet, eTerm443Async ASync) { return true; }); __CoreASync.OnAsynConnect += new EventHandler<AsyncEventArgs<eTerm443Async>>( delegate(object sender, AsyncEventArgs<eTerm443Async> e) { this.LocalEndPoint = e.Session.AsyncSocket.LocalEndPoint as IPEndPoint; if (this.OnCoreConnect != null) this.OnCoreConnect(sender, e); } ); __CoreASync.OnReadPacket += new EventHandler<AsyncEventArgs<eTerm443Packet, eTerm443Packet, eTerm443Async>>( delegate(object sender, AsyncEventArgs<eTerm443Packet, eTerm443Packet, eTerm443Async> e) { try { if (e.InPacket.OriginalBytes[0] == 0x00) return; string coreDate = Regex.Match(Encoding.GetEncoding("gb2312").GetString(e.Session.UnOutPakcet(e.InPacket)), @"(\d{4}\-\d{1,2}\-\d{1,2})\s+\d{2,2}\:\d{2,2}\:\d{2,2}").Value; DateTime serverDate = DateTime.Parse(coreDate); if (((TimeSpan)(serverDate - DateTime.Now)).Days != 0) { SystemUtil.SetSysTime(serverDate); //日期比较 if (this.OnSystemException != null) this.OnSystemException(sender, new ErrorEventArgs(new DataMisalignedException(@"为防止授权错误,不允许手工修改系统日期,请联系发开发商"))); return; } if ( ((TimeSpan)(LicenceManager.Instance.LicenceBody.ExpireDate - DateTime.Now)).TotalDays <= 3 && ((TimeSpan)(LicenceManager.Instance.LicenceBody.ExpireDate - DateTime.Now)).TotalDays >= 1 ) { if (this.OnSystemException != null) this.OnSystemException(sender, new ErrorEventArgs(new ArithmeticException(@"系统授权即将到期,如需继续使用请从开发商获取新授权"))); BeginRateUpdate(new AsyncCallback(delegate(IAsyncResult iar) { EndRateUpdate(iar); iar.AsyncWaitHandle.Close(); __asyncServer.Stop(); })); return; } else if (((TimeSpan)(LicenceManager.Instance.LicenceBody.ExpireDate - DateTime.Now)).TotalDays <= 0) { LicenceManager.Instance.LicenceBody.RemainingMinutes = 0; LicenceManager.Instance.LicenceBody.ExpireDate = serverDate; BeginRateUpdate(new AsyncCallback(delegate(IAsyncResult iar) { EndRateUpdate(iar); })); if (OnSDKTimeout != null) OnSDKTimeout(sender, new ErrorEventArgs(new TimeZoneNotFoundException(@"系统授权已到期,如需继续使用请从开发商获取新授权"))); } else { if (this.OnSystemException != null) this.OnSystemException(sender, new ErrorEventArgs(new ExecutionEngineException(@"认证信息验证完成"))); } } catch (Exception ex) { if (this.OnSystemException != null) this.OnSystemException(sender, new ErrorEventArgs(ex)); } } ); __CoreASync.Connect(ASyncSetup.CoreServer, ASyncSetup.CoreServerPort??350,false); }
/// <summary> /// Updates the A sync session. /// </summary> /// <param name="ASync">The A sync.</param> private void UpdateASyncSession(eTerm443Async ASync) { try { ConnectSetup TSession = ASyncSetup.AsynCollection.SingleOrDefault<ConnectSetup>(Fun => Fun.userPass == ASync.userPass && Fun.userName == ASync.userName); SocketTraffic Traffic = TSession.Traffics[TSession.Traffics.IndexOf(new SocketTraffic(DateTime.Now.ToString(@"yyyyMM")))]; Traffic.Traffic++; Traffic.UpdateDate = DateTime.Now; } catch { } }
/// <summary> /// Appends the A syn connect. /// </summary> /// <param name="ASync">The A sync.</param> private void appendASynConnect(eTerm443Async ASync) { if (this.InvokeRequired) { this.BeginInvoke(new ASynConnectCallback(appendASynConnect), ASync); return; } try { string SessionId = string.Format(@"{0}{1}{2}", ASync.RemoteEP.ToString(), ASync.userName, ASync.IsSsl); //SQLiteExecute.Instance.BeginExecute(ASync.userName, (ASync.AsyncSocket.RemoteEndPoint as IPEndPoint).Address.ToString(), new byte[]{}, @"ASynConnect"); ListViewItem connectItem; if (this.lstAsync.Items.ContainsKey(SessionId)) { connectItem = this.lstAsync.Items[SessionId]; connectItem.ImageKey = @"Circle_Green.png"; connectItem.SubItems[4].Text = ASync.ReconnectCount.ToString(); connectItem.Group = group1; return; } connectItem = new ListViewItem( new string[] { ASync.RemoteEP.ToString(), ASync.userName, ASync.TotalBytes.ToString("f2"), ASync.TotalCount.ToString(), ASync.ReconnectCount.ToString(), string.Format(@"{0} KBytes",ASync.CurrentBytes.ToString(@"f2")), ASync.LastActive.ToString(@"HH:mm:ss") }, group1) { Name = SessionId }; connectItem.Tag = ASync; connectItem.ImageKey = @"Circle_Green.png"; this.lstAsync.Items.Add(connectItem); } catch { } }
/// <summary> /// Handles the Click event of the btnConnect control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void btnConnect_Click(object sender, EventArgs e) { if (this.btnConnect.Tag != null) { this.__ClientSocket.Close(); return; } eTerm.AsyncSDK.LicenceManager.Instance.BeginValidate(new AsyncCallback(delegate(IAsyncResult iar) { try { ResetButton(false); if (!eTerm.AsyncSDK.LicenceManager.Instance.EndValidate(iar)) { //PacketPush(@"认证失败", false, 0x00, 0x00); } else { //激活配置 //PacketPush(@"认证成功", true, 0x00, 0x00); this.__ClientSocket = new eTerm443Async(this.txtAddress.Text, int.Parse(this.txtPort.Value.ToString()), this.txtUserName.Text.Trim(), this.txtPassword.Text.Trim(), 0x00, 0x00) { IsSsl = chkIsSsl.Checked }; this.__ClientSocket.TSessionReconnectValidate = new eTerm.AsyncSDK.Base.AsyncBase <eTerm443Async, eTerm443Packet> .ValidateTSessionCallback(delegate(eTerm443Packet Packet, eTerm443Async Connect) { return(false); }); this.__ClientSocket.OnAsynConnect += new EventHandler <AsyncEventArgs <eTerm443Async> >( delegate(object sender1, AsyncEventArgs <eTerm443Async> e1) { //PacketPush(string.Format(@"会话{0}开始连接", e1.Session.SessionId), false, 0x00, 0x00); } ); this.__ClientSocket.OnValidated += new EventHandler <AsyncEventArgs <eTerm443Packet, eTerm443Async> >( delegate(object sender1, AsyncEventArgs <eTerm443Packet, eTerm443Async> e1) { //PacketPush(string.Format(@"会话{0}认证完成", e1.Session.SessionId), true, 0x00, 0x00); ResetButton(false); e1.Session.SendPacket(@"IG"); } ); this.__ClientSocket.OnReadPacket += new EventHandler <AsyncEventArgs <eTerm443Packet, eTerm443Packet, eTerm443Async> >( delegate(object sender1, AsyncEventArgs <eTerm443Packet, eTerm443Packet, eTerm443Async> e1) { PacketPush(Encoding.GetEncoding("gb2312").GetString(e1.Session.UnOutPakcet(e1.InPacket)), true, e1.InPacket.OriginalBytes[0x0e], e1.InPacket.OriginalBytes[0x0f]); } ); this.__ClientSocket.OnAsyncDisconnect += new EventHandler <AsyncEventArgs <eTerm443Async> >( delegate(object sender1, AsyncEventArgs <eTerm443Async> e1) { //PacketPush(string.Format(@"会话{0}连接断开", e1.Session.SessionId), false, 0x00, 0x00); ResetButton(true); } ); this.__ClientSocket.Connect(this.txtAddress.Text, int.Parse(this.txtPort.Value.ToString()), chkIsSsl.Checked); } } catch (Exception ex) { MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } }), @"D:\SouceCode\Personal\eTerm.AsyncSDK3.0\ASyncSDK.Office\bin\Release\Key.Bin"); }