protected override void OnEventSent(Event e) { TraceLevel traceLevel = (e.GetTypeId() == BuiltinEventType.HeartbeatEvent ? TraceLevel.Trace : TraceLevel.Debug); if (Trace.Handler != null && Config.TraceLevel <= traceLevel) { // 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, "{0} {1} sending event {2}", link.Name, InternalHandle, description); } base.OnEventSent(e); }
/// <summary> /// Called internally to dspatch the specified event to registered handlers. /// </summary> protected void Dispatch(Event e) { // Safeguard for exclusive exception handling environments, // like Unity3D. if (handlerChain.Count != 0) { handlerChain.Clear(); } int chainLength = binding.BuildHandlerChain(e, equivalent, handlerChain); if (chainLength == 0) { // unhandled event return; } Handler handler; for (int i = 0, count = handlerChain.Count; i < count; ++i) { handler = handlerChain[i]; try { // Now using DateTime.UtcNow, instead of slow Stopwatch DateTime begin = DateTime.UtcNow; handler.Invoke(e); DateTime end = DateTime.UtcNow; long totalMilliseconds = (long)(end - begin).TotalMilliseconds; if (totalMilliseconds >= SlowHandlerLogThreshold) { var methodInfo = handler.Action.Method; Trace.Emit(SlowHandlerTraceLevel, "{0} slow handler {1:#,0}ms {2}.{3} on {4}", Name, totalMilliseconds, methodInfo.DeclaringType, methodInfo.Name, e); } } catch (Exception ex) { ExceptionHandler( String.Format("{0} {1} {2}", Name, handler.ToString(), e.ToString()), ex); } } handlerChain.Clear(); }
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); }