private void OnSessionBeforeHandlerCall(HandlerCallContext callContext) { // If we profile the RPC handler calls start a stopwatch for the calling thread. if (ProfileMethodCalls) { if (_callTimeCounter == null) { _callTimeCounter = new Stopwatch(); } _callTimeCounter.Stop(); _callTimeCounter.Start(); } var clientSessionOwner = callContext.Handler as IClientSessionOwner <TSession>; if (clientSessionOwner != null) { TSession session = (TSession)callContext.Context; clientSessionOwner.Session = session; if (!clientSessionOwner.Authenticate(callContext.Method)) { if (_callTimeCounter != null && _callTimeCounter.IsRunning) { _callTimeCounter.Stop(); } throw new InvalidOperationException("RPC method call " + callContext.Handler.GetType() + "." + callContext.Method.Name + " was not authenticated. The session is: " + session); } } }
private void OnSessionAfterHandlerCall(HandlerCallContext callContext) { var clientSessionOwner = callContext.Handler as IClientSessionOwner <TSession>; if (clientSessionOwner != null) { clientSessionOwner.Session = default(TSession); } // If we profile the RPC handler calls calculate and store the data for this call. if (_callTimeCounter != null && _callTimeCounter.IsRunning) { long elapsedTicks = _callTimeCounter.ElapsedTicks; _callTimeCounter.Stop(); string methodName = callContext.HandlerType.Name + "." + callContext.Method.Name; lock (_methodNameToCallInfo) { RpcCallInfo info; if (!_methodNameToCallInfo.TryGetValue(methodName, out info)) { info = _methodNameToCallInfo[methodName] = new RpcCallInfo { MethondName = methodName, }; } ++info.NumCalls; long us = (long)((elapsedTicks / (double)Stopwatch.Frequency) * 1000000); info.TotalTimeUs += us; if (us > info.MaxTimeUs) { info.MaxTimeUs = us; } if (us < info.MinTimeUs) { info.MinTimeUs = us; } } } }
private void ProcessHandlerCall(BinaryReader reader, byte ifaceOrdinal, object context) { HandlerData handlerData; if (!_handlersData.TryGetValue(ifaceOrdinal, out handlerData)) { Type ifaceType = RpcInterface.GetRpcInterface(ifaceOrdinal); if (ifaceType == null) { throw new ProtocolViolationException("Unknown RPC interface ordinal received: " + ifaceOrdinal); } MethodInfo mi = RpcInterface.GetOrderedMethods(ifaceType)[reader.ReadByte()].MethodInfo; throw new ProtocolViolationException("RPC call received for " + ifaceType.FullName + "." + mi.Name + " which does not have a handler registered."); } byte methodOrdinal = reader.ReadByte(); RpcMethodInfo rpcMethodInfo = handlerData.InterfaceMethods[methodOrdinal]; MethodInfo methodInfo = rpcMethodInfo.MethodInfo; byte callId = 0; if (rpcMethodInfo.HasReturnType) { callId = reader.ReadByte(); } var paramsInfo = methodInfo.GetParameters(); var paramValues = new object[paramsInfo.Length]; for (int i = 0, n = paramValues.Length; i < n; ++i) { paramValues[i] = BinarySerializer.DeserializeParameter(reader, paramsInfo[i]); } var callContext = new HandlerCallContext { HandlerType = handlerData.RpcInterfaceType, Handler = handlerData.Handler, Method = methodInfo, CallId = callId, Context = context, }; if (OnBeforeHandlerCall != null) { OnBeforeHandlerCall(callContext); } callContext.Result = methodInfo.Invoke(handlerData.Handler, paramValues); if (callContext.Result != null && OnDataOut != null) { Type resultFutureType = callContext.Result.GetType(); var stream = new MemoryStream(64); var writer = new BinaryWriter(stream); var header = (byte)(ResponseTypeFlag | callId); writer.Write((byte)header); resultFutureType.GetMethod("Serialize", BindingFlags.NonPublic | BindingFlags.Instance) .Invoke(callContext.Result, new object[] { writer }); OnDataOut(stream.ToArray(), 0, (int)stream.Length); } if (OnAfterHandlerCall != null) { OnAfterHandlerCall(callContext); } }