///// <summary> // /// 尝试发送数据包 ///// </summary> ///// <param name="errorNo"></param> ///// <param name="ProtocolType"></param> ///// <param name="payload"></param> ///// <returns></returns> // public Boolean TrySend(Int32 errorNo, UInt32 ProtocolType, byte[] payload) // { // if (payload == null) // { // return ProtocolSession.TrySend(new FixedHeaderPackageInfo<ProtocolHeader>(Guid.NewGuid().ToString("N"), new ProtocolHeader() { proType = ProtocolType, errornum = (UInt32)errorNo, dataLen = 0 }, null)); // } // else // { // return ProtocolSession.TrySend(new FixedHeaderPackageInfo<ProtocolHeader>(Guid.NewGuid().ToString("N"), new ProtocolHeader() { proType = ProtocolType, errornum = (UInt32)errorNo, dataLen = (uint)payload.Length }, payload)); // } // } /// <summary> /// 尝试发送数据包 /// </summary> /// <param name="errorNo"></param> /// <param name="protocolType"></param> /// <param name="payload"></param> /// <param name="tryTimes">尝试重新发送的次数</param> /// <param name="tryInterval">尝试重新发送的间隔,单位毫秒</param> /// <returns></returns> public Boolean TrySend(Int32 errorNo, UInt32 protocolType, byte[] payload, Int32 tryTimes = 0, Int32 tryInterval = 0) { int i = 0; do { if (payload == null) { if (ProtocolSession.TrySend(new FixedHeaderPackageInfo <ProtocolHeader>(Guid.NewGuid().ToString("N"), new ProtocolHeader() { proType = protocolType, errornum = (UInt32)errorNo, dataLen = 0 }, null)) == true) { return(true); } } else { if (ProtocolSession.TrySend(new FixedHeaderPackageInfo <ProtocolHeader>(Guid.NewGuid().ToString("N"), new ProtocolHeader() { proType = protocolType, errornum = (UInt32)errorNo, dataLen = (uint)payload.Length }, payload)) == true) { return(true); } } ++i; System.Threading.Thread.Sleep(tryInterval); } while (i < tryTimes); return(false); }
/// <summary> /// Execute benchmark. /// </summary> public void Attach(ProtocolSession session) { if (session == null) { throw new ArgumentNullException("session"); } this.session = session; this.exitEvent.Reset(); this.IsAttached = true; ThreadPool.QueueUserWorkItem(delegate { this.sessionMonitor = new ProtocolFramesMonitor(this.session, this.SessionFramesFilter); this.streamMonitor = new ProtocolFramesMonitor(this.session, f => !this.SessionFramesFilter(f)); this.sessionMonitor.Attach(); this.streamMonitor.Attach(); this.session.OnClose += this.HandleCloseSession; this.session.OnStreamOpened += this.HandleStreamOpened; this.sessionMonitor.OnFrameReceived += this.SessionMonitorOnFrameReceived; this.sessionMonitor.OnFrameSent += this.SessionMonitorOnFrameSent; this.streamMonitor.OnFrameReceived += this.StreamMonitorOnFrameReceived; this.streamMonitor.OnFrameSent += this.StreamMonitorOnFrameSent; this.totals.BeginTime = DateTime.Now; this.State = MonitorState.MonitorOn; this.exitEvent.WaitOne(); }); }
/// <summary> /// Creates PackageSession for protocol session. /// </summary> /// <param name="session">The session.</param> /// <param name="protocolSession">The protocol session.</param> /// <param name="nodeId"></param> /// <returns>Package session</returns> public static PackageSession CreateForProtocolSession(Session session, ProtocolSession protocolSession, string nodeId) { var result = session.FindObject <PackageSession>(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("SessionId = ?", protocolSession.SessionId)); if (result == null) { result = new PackageSession(session) { SessionId = protocolSession.SessionId, StartedOn = protocolSession.StartedOn, CommitedOn = protocolSession.CommitedOn }; if (protocolSession is ExternalProtocolSession) { result.Route = ((ExternalProtocolSession)protocolSession).Route; } result.Route += '\n' + nodeId; if (protocolSession.Parent != null) { result.Parent = CreateForProtocolSession(session, protocolSession.Parent, nodeId); } } return(result); }
/// <summary> /// /// </summary> /// <param name="disposing"></param> protected override void Dispose(bool disposing) { if (disposing == true) { if (ProtocolSession != null && ProtocolSession.Connected) { ProtocolSession.Close(); ProtocolSession = null; } } }
/// <summary> /// Initializes a new instance of the <see cref="Http2Stream"/> class. /// </summary> /// <param name="id">The stream id.</param> /// <param name="session">The stream session.</param> public Http2Stream(int id, ProtocolSession session) { this.StreamId = id; this.Session = session; this.Headers = new ProtocolHeaders(); this.protocol = session.Protocol; this.CurrentWindowBalanceFromServer = session.CurrentWindowBalanceFromServer; this.protocol.OnStreamFrame += this.OnProtocolData; this.protocol.OnStreamError += this.OnStreamError; this.protocol.OnSessionFrame += this.OnSessionFrame; }
/// <summary> /// 发送数据包 /// </summary> /// <param name="errorNo"></param> /// <param name="ProtocolType"></param> /// <param name="payload"></param> public void Send(Int32 errorNo, UInt32 ProtocolType, byte[] payload) { if (payload == null) { ProtocolSession.Send(new FixedHeaderPackageInfo <ProtocolHeader>(Guid.NewGuid().ToString("N"), new ProtocolHeader() { proType = ProtocolType, errornum = (UInt32)errorNo, dataLen = 0 }, null)); } else { ProtocolSession.Send(new FixedHeaderPackageInfo <ProtocolHeader>(Guid.NewGuid().ToString("N"), new ProtocolHeader() { proType = ProtocolType, errornum = (UInt32)errorNo, dataLen = (uint)payload.Length }, payload)); } }
private void CreateSession(VirtualSocket socket) { try { Console.WriteLine("New connection"); var options = new ProtocolOptions(); ProtocolSession session = new ProtocolSession(socket, options); session.OnStreamOpened += session_OnStreamOpened; session.OnError += session_OnError; session.Open(); } catch (Exception e) { Console.WriteLine(e.Message); } }