示例#1
0
        protected void OnReceiveFromInternal(int bytesTransferred, EndPoint endPoint)
        {
            Diag.AddBytesReceived(bytesTransferred);

            int handle;

            using (new ReadLock(rwlock))
            {
                if (!reverseMap.TryGetValue(endPoint, out handle))
                {
                    handle = 0;
                }
            }

            rxBuffer.Stretch(bytesTransferred);
            if (BufferTransform != null)
            {
                try
                {
                    BufferTransform.InverseTransform(rxBuffer, (int)rxBuffer.Length);
                }
                catch (Exception e)
                {
                    Trace.Error("{0} {1} buffer inv transform error: {2}", Name, handle, e.Message);
                    return;
                }
            }
            rxBuffer.Rewind();

            var deserializer = new Deserializer(rxBuffer, EventFactory);
            int typeId;

            try
            {
                deserializer.Read(out typeId);
            }
            catch (System.IO.EndOfStreamException)
            {
                // Need more
                return;
            }
            Event retrieved = CreateEvent(typeId);

            if (ReferenceEquals(retrieved, null))
            {
                Trace.Error("{0} {1} unknown event type id {2}",
                            Name, handle, typeId);
                return;
            }
            else
            {
                try
                {
                    retrieved.Deserialize(deserializer);
                }
                catch (Exception e)
                {
                    Trace.Error("{0} {1} error loading event {2}: {3}", Name, handle, retrieved.GetTypeId(), e.ToString());
                    return;
                }

                if (handle != 0)
                {
                    retrieved._Handle = handle;
                }

                Hub.Post(retrieved);

                Diag.IncrementEventsReceived();

                Trace.Debug("{0} {1} received event {2}", Name, handle, retrieved);
            }
        }
示例#2
0
        protected void OnReceiveInternal(int bytesTransferred)
        {
            Diag.AddBytesReceived(bytesTransferred);

            if (HasHeartbeatStrategy)
            {
                HeartbeatStrategy.OnReceive();
            }

            lock (syncRoot)
            {
                if (disposed)
                {
                    return;
                }
                rxBuffer.Stretch(bytesTransferred);
            }

            if (Config.TraceLevel <= TraceLevel.Trace)
            {
                Trace.Log("{0} {1} recv {2}: {3}", link.Name, InternalHandle,
                          bytesTransferred, rxBuffer.ToHexString());
            }

            if (rxBeginning)
            {
                rxBuffer.Rewind();

                if (!ParseHeader())
                {
                    BeginReceive(true);
                    return;
                }
            }

            // Handle split packets.
            if (rxBuffer.Length < lengthToReceive)
            {
                BeginReceive(false);
                return;
            }

            while (true)
            {
                rxBuffer.MarkToRead(lengthToReceive);

                Trace.Log("{0} {1} marked {2} byte(s) to read",
                          link.Name, InternalHandle, lengthToReceive);

                if (HasChannelStrategy && rxTransformed)
                {
                    try
                    {
                        ChannelStrategy.AfterReceive(rxBuffer, lengthToReceive);
                    }
                    catch (Exception e)
                    {
                        Trace.Error("{0} {1} buffer inv transform error: {2}",
                                    link.Name, InternalHandle, e.Message);
                        goto next;
                    }
                }
                rxBuffer.Rewind();

                var deserializer = new Deserializer(rxBuffer, link.EventFactory);
                int typeId;
                try
                {
                    deserializer.Read(out typeId);
                }
                catch (System.IO.EndOfStreamException)
                {
                    // Need more
                    goto next;
                }
                Event retrieved = link.CreateEvent(typeId);
                if (ReferenceEquals(retrieved, null))
                {
                    Trace.Error("{0} {1} unknown event type id {2}",
                                link.Name, InternalHandle, typeId);
                    goto next;
                }
                else
                {
                    try
                    {
                        retrieved.Deserialize(deserializer);
                    }
                    catch (Exception e)
                    {
                        Trace.Error("{0} {1} error loading event {2}: {3}",
                                    link.Name, InternalHandle, retrieved.GetTypeId(), e.ToString());
                        goto next;
                    }

                    OnEventReceived(retrieved);

                    // Consider subscribing/unsubscribing here.
                    bool processed = false;
                    if (HasChannelStrategy)
                    {
                        processed = ChannelStrategy.Process(retrieved);
                    }
                    if (!processed && HasHeartbeatStrategy)
                    {
                        processed = HeartbeatStrategy.Process(retrieved);
                    }
                    if (!processed)
                    {
                        processed = Process(retrieved);
                    }
                    if (!processed)
                    {
                        retrieved._Handle = Handle;

                        link.OnPreprocess(this, retrieved);

                        Hub.Post(retrieved);
                    }
                }
next:
                rxBuffer.Trim();
                if (rxBuffer.IsEmpty || !ParseHeader())
                {
                    break;
                }

                if (rxBuffer.Length < lengthToReceive)
                {
                    BeginReceive(false);
                    return;
                }
            }

            BeginReceive(true);
        }