コード例 #1
0
        private void BeginSendTo(Event e)
        {
            int handle = e._Handle;

            EndPoint endPoint;

            using (new ReadLock(rwlock))
            {
                int count = map.Count;

                if (count == 0)
                {
                    Trace.Error("{0} no known peers - dropped event {1}", Name, e);
                    goto next;
                }

                if (count == 1 && handle == 0)
                {
                    endPoint = map.Values[0];
                }
                else
                {
                    if (!map.TryGetValue(handle, out endPoint))
                    {
                        Trace.Error("{0} unknown handle {1} - dropped event {2}",
                                    Name, handle, e);
                        goto next;
                    }
                }
            }

            // Apply the datagram length limit.
            int length = e.GetLength();

            if (length > txBuffer.BlockSize)
            {
                Trace.Error("{0} dropped big event {1}", Name, e);
                goto next;
            }

            txBuffer.Reset();
            Serializer serializer = new Serializer(txBuffer);

            serializer.Write(e.GetTypeId());
            e.Serialize(serializer);

            if (BufferTransform != null)
            {
                BufferTransform.Transform(txBuffer, (int)txBuffer.Length);
            }

            try
            {
                SendToInternal(endPoint);

                Diag.IncrementEventsSent();

                if (Trace.Handler != null && Config.TraceLevel <= TraceLevel.Debug)
                {
                    // e.ToString() may crash if a composite property (list for example)
                    // of the event is changed in other threads.
                    string description;
                    try
                    {
                        description = e.ToString();
                    }
                    catch
                    {
                        description = e.GetTypeTag().RuntimeType.Name;
                    }

                    Trace.Emit(TraceLevel.Debug, "{0} {1} sent event {2}",
                               Name, handle, description);
                }

                return;
            }
            catch (ObjectDisposedException)
            {
                return;
            }
            catch (Exception ex)
            {
                Trace.Info("{0} send error {1}", Name, ex);
            }

next:
            OnSendToInternal(0);
        }
コード例 #2
0
        internal void BeginSend()
        {
            lock (syncRoot)
            {
                if (eventsToSend.Count == 0)
                {
                    return;
                }
                // Swap the event buffers.
                if (eventsSending.Count != 0)
                {
                    eventsSending.Clear();
                }
                List <Event> temp = eventsSending;
                eventsSending = eventsToSend;
                eventsToSend  = temp;
                temp          = null;
            }

            // Capture send buffers.
            txBufferList.Clear();
            lengthToSend = 0;
            int count       = eventsSending.Count;
            int bufferCount = txBuffers.Count;

            if (bufferCount < count)
            {
                for (int i = 0, n = count - bufferCount; i < n; ++i)
                {
                    txBuffers.Add(new SendBuffer());
                }
            }
            else
            {
                for (int i = 0, n = bufferCount - count; i < n; ++i)
                {
                    int j = bufferCount - (i + 1);
                    txBuffers[j].Dispose();
                    txBuffers.RemoveAt(j);
                }
            }
            for (int i = 0; i < count; ++i)
            {
                Event e = eventsSending[i];

                var sendBuffer = txBuffers[i];
                sendBuffer.Reset();

                int typeId = e.GetTypeId();

                Trace.Log("{0} {1} buffering event type {2} to send (#{3}/{4})",
                          link.Name, InternalHandle, typeId, i + 1, count);

                Serializer serializer = new Serializer(sendBuffer.Buffer);
                serializer.Write(typeId);
                e.Serialize(serializer);

                bool transformed = false;

                if (HasChannelStrategy && e._Transform)
                {
                    transformed = ChannelStrategy.BeforeSend(sendBuffer.Buffer, (int)sendBuffer.Buffer.Length);
                }

                BuildHeader(sendBuffer, transformed);

                sendBuffer.ListOccupiedSegments(txBufferList);
                lengthToSend += sendBuffer.Length;

                OnEventSent(e);
            }

            Trace.Log("{0} {1} serialized {2} events to send ({3} bytes)",
                      link.Name, InternalHandle, count, lengthToSend);

            SendInternal();
        }