예제 #1
0
        /// <summary>
        /// send data to server
        /// </summary>
        /// <param name="client">client is sended</param>
        /// <param name="callerName">methos name</param>
        /// <param name="attibName">service name</param>
        /// <param name="args">method parameters</param>
        /// <returns></returns>
        internal static object SendData(this OperationCalls client, string callerName, string attibName, params object[] args)
        {
            MethodCallInfo callInfo = new MethodCallInfo();

            if (string.IsNullOrEmpty(attibName))
            {
                callInfo.ServiceName = client.GetType().GetCustomAttributes <ServiceContractAttribute>(true).FirstOrDefault().Name;
            }
            else
            {
                callInfo.ServiceName = attibName;
            }

            callInfo.MethodName = callerName;
            foreach (var item in args)
            {
                callInfo.Parameters.Add(new Shared.Models.ParameterInfo()
                {
                    Value = JsonConvert.SerializeObject(item), Type = item?.GetType().FullName
                });
            }
            var guid = Guid.NewGuid().ToString();

            callInfo.Guid = guid;
            return(SendData(client.Connector, callInfo, args.Length == 1 && args[0] != null && args[0].GetType() == typeof(StreamInfo) ? (StreamInfo)args[0] : null));
        }
        public static object CallClientCallbackMethod(this OperationCalls client, string methodName, params object[] values)
        {
            if (SynchronizationContext.Current != null && ServerBase.AllDispatchers.ContainsKey(SynchronizationContext.Current))
            {
                throw new Exception("Cannot call method from class Constractor or main Thread");
            }
            MethodCallInfo callInfo = new MethodCallInfo();

#if (NETSTANDARD1_6 || NETCOREAPP1_1)
            callInfo.ServiceName = ((ServiceContractAttribute)client.GetType().GetTypeInfo().GetCustomAttributes(typeof(ServiceContractAttribute), true).FirstOrDefault()).Name;
#else
            callInfo.ServiceName = ((ServiceContractAttribute)client.GetType().GetCustomAttributes(typeof(ServiceContractAttribute), true).FirstOrDefault()).Name;
#endif
            callInfo.MethodName = methodName;
            foreach (var item in values)
            {
                callInfo.Parameters.Add(new Shared.Models.ParameterInfo()
                {
                    Value = ServerSerializationHelper.SerializeObject(item, client.ServerBase), Type = item.GetType().FullName
                });
            }
            var guid = Guid.NewGuid().ToString();
            callInfo.Guid = guid;
            var waitedMethodsForResponse = client.ServerBase.WaitedMethodsForResponse[client.CurrentClient];
            waitedMethodsForResponse.TryAdd(guid, new KeyValue <AutoResetEvent, MethodCallbackInfo>(new AutoResetEvent(false), null));
            client.ServerBase.CallClientMethod(client.CurrentClient, callInfo);
            var seted = waitedMethodsForResponse[guid].Key.WaitOne(client.ServerBase.ProviderSetting.ReceiveDataTimeout);
            if (!seted)
            {
                client.ServerBase.CheckClient(client.CurrentClient);
                waitedMethodsForResponse.Remove(guid);
                throw new TimeoutException();
            }
            if (waitedMethodsForResponse[guid].Value.IsException)
            {
                var data = waitedMethodsForResponse[guid].Value.Data;
                waitedMethodsForResponse.Remove(guid);
                throw new Exception("call method return exception: " + data);
            }
            var result = waitedMethodsForResponse[guid].Value.Data;
            waitedMethodsForResponse.Remove(guid);
            return(result);
        }
예제 #3
0
        /// <summary>
        /// send data to server
        /// </summary>
        /// <param name="client">client is sended</param>
        /// <param name="serviceName">methos name</param>
        /// <param name="attibName">service name</param>
        /// <param name="args">method parameters</param>
        /// <returns></returns>
        internal static object SendData(this OperationCalls client, string serviceName, string attibName, params Shared.Models.ParameterInfo[] args)
        {
            if (string.IsNullOrEmpty(attibName))
            {
                serviceName = client.GetType().GetServerServiceName(false);
            }
            else
            {
                serviceName = attibName;
            }
#if (NET40 || NET35)
            return(SendData(client.Connector, serviceName, serviceName, args));
#else
            return(SendDataAsync(client.Connector, serviceName, serviceName, args));
#endif
        }
        static object SendData(this OperationCalls client, string callerName, params object[] args)
        {
            var attribute = client.GetType().GetCustomAttributes <ServiceContractAttribute>(true).FirstOrDefault();

            if (client.ServerBase.ClientRegistredMethods[client.CurrentClient].ContainsKey(attribute.Name))
            {
                if (client.ServerBase.ClientRegistredMethods[client.CurrentClient][attribute.Name].Contains(callerName))
                {
                    return(SendDataNow(client, callerName, args));
                }
            }
            else if (client.ServerBase.RegisteredCallbacksTypes.ContainsKey(attribute.Name))
            {
                return(SendDataNow(client, callerName, args));
            }
            return(null);
        }
        static object SendDataNow(this OperationCalls client, string callerName, params object[] args)
        {
            if (SynchronizationContext.Current != null && ServerBase.AllDispatchers.ContainsKey(SynchronizationContext.Current) && ServerBase.AllDispatchers[SynchronizationContext.Current].MainContext == SynchronizationContext.Current)
            {
                throw new Exception("Cannot call method from class Constractor or main Thread");
            }
            var            attribute = client.GetType().GetCustomAttributes <ServiceContractAttribute>(true).FirstOrDefault();
            MethodCallInfo callInfo  = new MethodCallInfo();

            callInfo.ServiceName = attribute.Name;
            callInfo.MethodName  = callerName;
            foreach (var item in args)
            {
                callInfo.Parameters.Add(new Shared.Models.ParameterInfo()
                {
                    Value = item == null ? null : ServerSerializationHelper.SerializeObject(item, client.ServerBase), Type = item == null ? null : item.GetType().FullName
                });
            }
            var guid = Guid.NewGuid().ToString();

            callInfo.Guid = guid;
            var waitedMethodsForResponse = client.ServerBase.WaitedMethodsForResponse[client.CurrentClient];

            waitedMethodsForResponse.TryAdd(guid, new KeyValue <AutoResetEvent, MethodCallbackInfo>(new AutoResetEvent(false), null));
            client.ServerBase.CallClientMethod(client.CurrentClient, callInfo);
            var seted = waitedMethodsForResponse[guid].Key.WaitOne(client.ServerBase.ProviderSetting.SendDataTimeout);

            if (!seted)
            {
                client.ServerBase.CheckClient(client.CurrentClient);
                waitedMethodsForResponse.Remove(guid);
                throw new TimeoutException();
            }
            if (waitedMethodsForResponse[guid].Value.IsException)
            {
                var data = waitedMethodsForResponse[guid].Value.Data;
                waitedMethodsForResponse.Remove(guid);
                throw new Exception("method call exception: " + data);
            }
            var result = waitedMethodsForResponse[guid].Value.Data;

            waitedMethodsForResponse.Remove(guid);
            return(result);
        }