protected void ReceivedDataSegmentHandler(SegmentReceiveEventArgs e) { EventHandler <SegmentReceiveEventArgs> handler = ReceivedDataSegment; if (handler != null) { handler(this, e); } }
/// <summary> /// ��һ�θոս��յ�������������ݵ��������� /// </summary> /// <param name="args">���յ�����������</param> public void AppendNetworkData(SegmentReceiveEventArgs args) { if (_head == null) _head = _tail = _curUsedHead = _curGiveBackUsedHead = new ThriftSegmentNode(args); else { ThriftSegmentNode node = new ThriftSegmentNode(args); _tail.Next = node; node.Previous = _tail; _tail = (ThriftSegmentNode) _tail.Next; } }
/// <summary> /// 追加一段刚刚接收到的网络可用数据到此容器中 /// </summary> /// <param name="args">接收到的网络数据</param> public void AppendNetworkData(SegmentReceiveEventArgs args) { if (_head == null) { _head = _tail = _curUsedHead = _curGiveBackUsedHead = new ThriftSegmentNode(args); } else { ThriftSegmentNode node = new ThriftSegmentNode(args); _tail.Next = node; node.Previous = _tail; _tail = (ThriftSegmentNode)_tail.Next; } }
/// <summary> /// 追加一个新的数据段 /// </summary> /// <param name="args">数据段接受参数</param> public void Append(SegmentReceiveEventArgs args) { lock (_lockObj) { _container.AppendNetworkData(args); MessageIdentity identity; if (!_container.TryReadMessageIdentity(out identity)) { _container.ResetOffset(); return; } _container.ResetOffset(); Type msgType = _protocolStack.GetMessageType(identity.Command, false); if (msgType == null) { _tracing.Error("#Type of Thrift message has no any registered .NET type. #Type: {0}, seqID={1}, data: \r\n{2}", identity.Command, identity.SequenceId, _container.Dump()); } else { try { ThriftMessage msg; if (ThriftObjectEngine.TryGetObject(msgType, _container, out msg) != GetObjectResultTypes.Succeed) { return; } _container.UpdateOffset(); OnParseSucceed(new LightSingleArgEventArgs <List <ThriftMessage> >(new List <ThriftMessage> { msg })); } catch (Exception ex) { _tracing.Error(ex, null); } } } }
/// <summary> /// 追加一个新的数据段 /// </summary> /// <param name="args">数据段接受参数</param> public void Append(SegmentReceiveEventArgs args) { #region Step 1, check-on args. if (_head == null) { _head = _tail = new SegmentNode(args); } else { _tail.Next = new SegmentNode(args); _tail = _tail.Next; } #endregion #region Step 2, check bytes enough & pickup data. int msgSize; //RESET next node for current expression. SegmentNode nextNode = _head; List <T> msgs = new List <T>(); //check whatever bytes can be parse. while (nextNode != null && (msgSize = CheckBytes(nextNode)) > 0) { //direct parse. if (nextNode.RemainingSize >= msgSize) { List <T> list = _protocolStack.Parse <T>(nextNode.Args.GetStub().Segment.Segment.Array, nextNode.Args.GetStub().Segment.UsedOffset, msgSize); if (list != null) { msgs.AddRange(list); } nextNode.Args.GetStub().Segment.UsedBytes += msgSize; //ChannelCounter.Instance.RateOfDirectParse.Increment(); if (nextNode.RemainingSize > 0) { continue; } //giveup current fixed stub. nextNode.Args.Complete(); _head = nextNode = nextNode.Next; if (_head != null) { continue; } //Tail node must be null, if the head node has no value. _tail = null; //publish messages. if (msgs.Count > 0) { ParseSucceedHandler(new LightSingleArgEventArgs <List <T> >(msgs)); } return; } //create sub-list for all ava-data segment path. int remainingSize = msgSize - nextNode.RemainingSize; SegmentNode childHead = (SegmentNode)nextNode.Clone(); SegmentNode childTail = (SegmentNode)nextNode.Clone(); SegmentNode tempNode = nextNode; SegmentNode lastRealNode = null; SegmentNode cloneLastRealNode = null; do { tempNode = tempNode.Next; if (tempNode == null) { break; } remainingSize -= tempNode.RemainingSize; //clone target node for child-list. lastRealNode = tempNode; SegmentNode cloneNode = (SegmentNode)tempNode.Clone(); cloneNode.Next = null; childTail.Next = cloneNode; childTail = childTail.Next; } while (remainingSize > 0); //cannot get enough length for message really binary data. if (remainingSize > 0) { if (msgs.Count > 0) { ParseSucceedHandler(new LightSingleArgEventArgs <List <T> >(msgs)); } return; } //copy data from child-list! int dataOffset = 0; byte[] data = new byte[msgSize]; int dataRemainingCount = data.Length; int usedBytes; int lastUserBytes = 0; while (childHead != null && dataRemainingCount > 0) { cloneLastRealNode = childHead; Buffer.BlockCopy(childHead.Args.GetStub().Segment.Segment.Array, childHead.Args.GetStub().Segment.UsedOffset, data, dataOffset, (usedBytes = (childHead.RemainingSize > dataRemainingCount ? dataRemainingCount : childHead.RemainingSize))); dataOffset += usedBytes; dataRemainingCount -= usedBytes; childHead.Args.GetStub().Segment.UsedBytes += usedBytes; lastUserBytes = childHead.Args.GetStub().Segment.UsedBytes; if (childHead.RemainingSize <= 0) { //giveup current fixed stub. childHead.Args.Complete(); childHead = childHead.Next; } } if (cloneLastRealNode.RemainingSize - lastUserBytes == 0 && lastRealNode.Next == null) { _head = nextNode = null; } else if (cloneLastRealNode.RemainingSize - lastUserBytes == 0 && lastRealNode.Next != null) { _head = nextNode = lastRealNode.Next; } else { lastRealNode.Args.GetStub().Segment.UsedBytes = lastUserBytes; _head = nextNode = lastRealNode; } //_head = nextNode = ((cloneLastRealNode.RemainingSize == 0 && lastRealNode.Next == null) ? null : lastRealNode); List <T> list1 = _protocolStack.Parse <T>(data); if (list1 != null) { msgs.AddRange(list1); } } //publish messages. if (msgs.Count > 0) { ParseSucceedHandler(new LightSingleArgEventArgs <List <T> >(msgs)); } #endregion }
/// <summary> /// Thrift网络协议内部所使用的内存片段 /// </summary> /// <param name="value">Socket接收到的内存片段</param> public ThriftSegmentNode(SegmentReceiveEventArgs value) : base(value) { }
/// <summary> /// 接收数据 /// <para>* IOCP接收数据线程重入函数</para> /// </summary> protected virtual void RecvData(object sender, SegmentReceiveEventArgs e) { ReceivedDataSegmentHandler(e); }
void ReceivedDataSegment(object sender, SegmentReceiveEventArgs e) { _parser.Append(e); }
/// <summary> /// 数据段线性链表节点 /// </summary> public SegmentNode(SegmentReceiveEventArgs value) { Args = value; }