Example #1
0
        /// <summary>
        /// Creates the proxy.
        /// </summary>
        /// <returns>Contract</returns>
        public TContract CreateProxy()
        {
            ConstructorInfo         c     = mRealProxyType.GetConstructor(new Type[] { typeof(Channel), typeof(String) });
            ProxyBase               proxy = (ProxyBase)c.Invoke(new object[] { mChannel, mChannel.Connect(mRemoteEndPoint) });
            WellKnownObjectModeEnum mode  = WellKnownObjectModeEnum.PerSession;

            if (ContractValidator.GetWellKnownObjectMode(mContractInterface, out mode) && mode == WellKnownObjectModeEnum.PerSession)
            {
                ServiceBase.RegisterProxy(mChannel, mContractInterface, proxy.GetType(), proxy.SessionId, proxy.ProxyId, proxy);
            }
            return((TContract)(IRemoteContract)proxy);
        }
Example #2
0
        /// <summary>
        /// Channels the receive message event handler.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Forge.Net.Remoting.Channels.ReceiveMessageEventArgs"/> instance containing the event data.</param>
        protected override void ChannelReceiveMessageEventHandler(object sender, ReceiveMessageEventArgs e)
        {
            Channel  channel   = (Channel)sender;
            IMessage message   = e.Message;
            String   sessionId = e.SessionId;

            if (message.MessageType == MessageTypeEnum.Acknowledge || message.MessageType == MessageTypeEnum.Response)
            {
                // HIBA: nem megfelelő üzenet típus (implementációs hiba?)
                String errorMsg = String.Format("Invalid message type received: {0}. This may be an implementation error in the client proxy.", message.MessageType.ToString());
                if (LOGGER.IsErrorEnabled)
                {
                    LOGGER.Error(errorMsg);
                }
            }
            else
            {
                RequestMessage rm = (RequestMessage)message;

                if (MessageInvokeModeEnum.RequestService.Equals(rm.MessageInvokeMode))
                {
                    return;
                }

                String contractName = rm.ContractName;
                Type   contractType = null;
                try
                {
                    contractType = TypeHelper.GetTypeFromString(contractName);
                }
                catch (Exception ex)
                {
                    // HIBA: ez a típus nem szerepel a ClassPath-ban
                    try
                    {
                        SendResponse(channel, sessionId, rm, typeof(void), null, new MethodInvocationException(String.Format("Unable to resolve this type of contract '{0}'. Type has not found.", rm.ContractName), ex), channel.DefaultErrorResponseTimeout);
                    }
                    catch (Exception innerEx)
                    {
                        // válaszüzenet küldése sikertelen
                        if (LOGGER.IsErrorEnabled)
                        {
                            LOGGER.Error(String.Format(AUTO_SEND_REPLY_ERROR_MSG, innerEx.Message));
                        }
                    }
                    return;
                }
                {
                    WellKnownObjectModeEnum mode = WellKnownObjectModeEnum.PerSession;
                    if (ContractValidator.GetWellKnownObjectMode(contractType, out mode) && mode != WellKnownObjectModeEnum.PerSession)
                    {
                        // HIBA: nem PerSession a contract, amit meghívnak
                        try
                        {
                            SendResponse(channel, sessionId, rm, typeof(void), null, new MethodInvocationException(String.Format("Contract {0} must be configured to {1}.", typeof(WellKnownObjectModeEnum).Name, WellKnownObjectModeEnum.PerSession.ToString())), channel.DefaultErrorResponseTimeout);
                        }
                        catch (Exception innerEx)
                        {
                            // válaszüzenet küldése sikertelen
                            if (LOGGER.IsErrorEnabled)
                            {
                                LOGGER.Error(String.Format(AUTO_SEND_REPLY_ERROR_MSG, innerEx.Message));
                            }
                        }
                        return;
                    }
                }
                lock (mContractDescriptors)
                {
                    if (!mContractDescriptors.ContainsKey(contractType))
                    {
                        // HIBA: ez a contract nincs nyilvántartva
                        try
                        {
                            SendResponse(channel, sessionId, rm, typeof(void), null, new MethodInvocationException(String.Format("This type of contract '{0}' has not been registered.", contractType.Name)), channel.DefaultErrorResponseTimeout);
                        }
                        catch (Exception innerEx)
                        {
                            // válaszüzenet küldése sikertelen
                            if (LOGGER.IsErrorEnabled)
                            {
                                LOGGER.Error(String.Format(AUTO_SEND_REPLY_ERROR_MSG, innerEx.Message));
                            }
                        }
                        return;
                    }
                }
                long proxyId = -1;
                try
                {
                    proxyId = (long)rm.Context[ProxyBase.PROXY_ID];
                }
                catch (Exception ex)
                {
                    // HIBA: hiányzó vagy rossz formátumú ProxyId
                    try
                    {
                        SendResponse(channel, sessionId, rm, typeof(void), null, new MethodInvocationException("Unable to parse PROXY_ID from call context.", ex), channel.DefaultErrorResponseTimeout);
                    }
                    catch (Exception innerEx)
                    {
                        // válaszüzenet küldése sikertelen
                        if (LOGGER.IsErrorEnabled)
                        {
                            LOGGER.Error(String.Format(AUTO_SEND_REPLY_ERROR_MSG, innerEx.Message));
                        }
                    }
                    return;
                }
                ProxyBase proxy = null;
                lock (mContractAndInstancePerSessionAndChannel)
                {
                    foreach (ContractAndInstanceStruct s in mContractAndInstancePerSessionAndChannel)
                    {
                        if (s.Channel.Equals(channel) && s.ContractType.Equals(contractType) && s.SessionId.Equals(sessionId) && s.ProxyId == proxyId)
                        {
                            proxy = s.Instance;
                            break;
                        }
                    }
                    if (proxy == null)
                    {
                        // HIBA: nem létezik a hivatkozott proxy példány
                        try
                        {
                            SendResponse(channel, sessionId, rm, typeof(void), null, new MethodInvocationException(String.Format("Unable to find proxy on client side with ID: {0}", proxyId)), channel.DefaultErrorResponseTimeout);
                        }
                        catch (Exception innerEx)
                        {
                            // válaszüzenet küldése sikertelen
                            if (LOGGER.IsErrorEnabled)
                            {
                                LOGGER.Error(String.Format(AUTO_SEND_REPLY_ERROR_MSG, innerEx.Message));
                            }
                        }
                        return;
                    }
                }
                // meg van az instance, lehet reflection-nel hívni a metódusára
                Type[]   methodParamTypes = null;
                Object[] paramValues      = null;
                if (rm.MethodParameters != null)
                {
                    methodParamTypes = new Type[rm.MethodParameters.Length];
                    paramValues      = new Object[rm.MethodParameters.Length];
                    for (int i = 0; i < rm.MethodParameters.Length; i++)
                    {
                        try
                        {
                            methodParamTypes[i] = TypeHelper.GetTypeFromString(rm.MethodParameters[i].ClassName);
                        }
                        catch (Exception ex)
                        {
                            // HIBA: a paraméter egy típusa nem szerepel a CLASSPATH-ban, így nem feloldható, ismeretlen
                            try
                            {
                                SendResponse(channel, sessionId, rm, typeof(void), null, new MethodInvocationException(String.Format("Unable to resolve parameter type '{0}'. Type has not found.", rm.MethodParameters[i].ClassName), ex), channel.DefaultErrorResponseTimeout);
                            }
                            catch (Exception innerEx)
                            {
                                // válaszüzenet küldése sikertelen
                                if (LOGGER.IsErrorEnabled)
                                {
                                    LOGGER.Error(String.Format(AUTO_SEND_REPLY_ERROR_MSG, innerEx.Message));
                                }
                            }
                            return;
                        }
                        paramValues[i] = rm.MethodParameters[i].Value;
                    }
                }

                MethodInfo m = null;
                try
                {
                    m = FindMethod(proxy.GetType(), rm.MethodName, methodParamTypes);
                }
                catch (Exception ex)
                {
                    // HIBA: a metódus név és paraméterlista alapján nem található
                    try
                    {
                        SendResponse(channel, sessionId, rm, typeof(void), null, new MethodInvocationException(String.Format("Unable to find method '{0}' with parameter list: '{1}'.", rm.MethodName, methodParamTypes), ex), channel.DefaultErrorResponseTimeout);
                    }
                    catch (Exception innerEx)
                    {
                        // válaszüzenet küldése sikertelen
                        if (LOGGER.IsErrorEnabled)
                        {
                            LOGGER.Error(String.Format(AUTO_SEND_REPLY_ERROR_MSG, innerEx.Message));
                        }
                    }
                    return;
                }
                long returnTimeout = OperationContractAttribute.DEFAULT_METHOD_TIMEOUT;
                if (rm.MessageType == MessageTypeEnum.Request)
                {
                    MethodParameter[] mps = null;
                    if (m.GetParameters().Length > 0)
                    {
                        mps = new MethodParameter[m.GetParameters().Length];
                        for (int i = 0; i < m.GetParameters().Length; i++)
                        {
                            ParameterInfo pi = m.GetParameters()[i];
                            mps[i] = new MethodParameter(i, string.Format("{0}, {1}", pi.ParameterType.FullName, new AssemblyName(pi.ParameterType.Assembly.FullName).Name), null);
                        }
                    }
                    returnTimeout = ServiceBase.GetTimeoutByMethod(contractType, m.Name, mps, MethodTimeoutEnum.ReturnTimeout);
                    lock (mCallContextForReply)
                    {
                        mCallContextForReply.Add(Thread.CurrentThread, new CallContextForReply(channel, sessionId, rm, m.ReturnType, returnTimeout));
                    }
                }

                // visszatérési értéket fogadni
                Exception methodException = null;
                Object    result          = null;
                try
                {
                    result = m.Invoke(proxy, paramValues);
                }
                catch (Exception ex)
                {
                    methodException = ex;
                }

                // válaszüzenet küldése
                bool needToSendResponse = false;
                Type returnType         = typeof(void);
                if (rm.MessageType == MessageTypeEnum.Request)
                {
                    lock (mCallContextForReply)
                    {
                        if (mCallContextForReply.ContainsKey(Thread.CurrentThread))
                        {
                            needToSendResponse = true;
                            returnType         = mCallContextForReply[Thread.CurrentThread].ReturnType;
                        }
                    }
                }
                if (needToSendResponse)
                {
                    try
                    {
                        SendResponse(channel, sessionId, rm, returnType, result, methodException, returnTimeout);
                    }
                    catch (Exception ex)
                    {
                        // válaszüzenet küldése sikertelen
                        if (LOGGER.IsErrorEnabled)
                        {
                            LOGGER.Error(String.Format(AUTO_SEND_REPLY_ERROR_MSG, ex.Message));
                        }
                    }
                    finally
                    {
                        lock (mCallContextForReply)
                        {
                            mCallContextForReply.Remove(Thread.CurrentThread);
                        }
                    }
                }
            }
        }