Ejemplo n.º 1
0
        public T GetService <T>()
            where T : class
        {
            if (!typeof(T).IsInterface)
            {
                throw new Exception($"type {typeof(T)} must be interface");
            }
            var result = httpClient.GetStringAsync($"http://{hostName}:{port}/ip/TCPRegister/ConnectoToService/{typeof(T).Name}").Result;
            var data   = JObject.Parse(result).ToObject <Response>();

            if (!data.Success)
            {
                throw new ServiceUnavailableException();
            }
            var transporter         = new TCPTransporter(hostName, data.Port, data.Password, loggerFactory.CreateLogger <TCPTransporter>());
            var disposableInterface = DisposableWrapper(typeof(T));
            T   worker;

            if (disposableInterface == typeof(T))
            {
                worker = RemoteWorker.Create(disposableInterface, transporter, 0) as T;
            }
            else
            {
                worker = RemoteWorker.Create(disposableInterface, transporter, 0, true) as T;
            }
            return(worker);
        }
Ejemplo n.º 2
0
 private static object Create(Type targetType, RemoteWorker worker)
 {
     try
     {
         return(proxyGenerator.CreateInterfaceProxyWithoutTarget(
                    targetType,
                    worker));
     }
     catch
     {
         return(proxyGenerator.CreateClassProxy(
                    targetType,
                    worker));
     }
 }
Ejemplo n.º 3
0
        public void Intercept(IInvocation invocation)
        {
            if (aborted)
            {
                throw new ServiceUnavailableException();
            }
            InvokeMessage message = new InvokeMessage
            {
                Id          = Guid.NewGuid(),
                MethodName  = invocation.Method.Name,
                SubObjectId = objectId,
                Args        = new Dictionary <string, Value>(),
                Streams     = new Dictionary <string, Stream>()
            };

            if (invocation.Method.Name == "Dispose" && wrapped)
            {
                Dispose();
                return;
            }

            int i = 0;

            foreach (var param in invocation.Method.GetParameters())
            {
                var argument = invocation.Arguments[i++];
                if (argument == null)
                {
                    message.Args[param.Name] = new Value {
                        Type = Messages.ValueType.Null
                    };
                }
                else
                if (argument is Stream stream)
                {
                    message.Streams[param.Name] = stream;
                    continue;
                }
                Value value = new Value();
                if (argument == null)
                {
                    value.Type = Messages.ValueType.Null;
                }
                else
                if (PrimitiveTypes.Contains(argument.GetType()))
                {
                    value.Type = Messages.ValueType.Primitive;
                    value.Data = JToken.FromObject(argument);
                }
                else
                {
                    value.Type = Messages.ValueType.HardObject;
                    CodeInvoker     newInvoker      = new CodeInvoker(argument, param.ParameterType);
                    InstanceWrapper instanceWrapper = new InstanceWrapper(newInvoker, transporter);
                    value.Data = instanceWrapper.Id;
                }
                message.Args[param.Name] = value;
            }
            var data = JsonConvert.SerializeObject(message, Formatting.Indented);

            Console.WriteLine(data);

            ManualResetEvent resetEvent = new ManualResetEvent(false);

            waiters.TryAdd(message.Id, new WaitPair {
                ResetEvent = resetEvent
            });

            transporter.SendMessage(message).Wait();

            while (!resetEvent.WaitOne(TimeSpan.FromSeconds(0.5)))
            {
                if (aborted)
                {
                    throw new ServiceUnavailableException();
                }
            }

            if (!waiters.TryRemove(message.Id, out var waitPair))
            {
                throw new Exception("Blya blya blya nety waitera!!!!");
            }
            var result = waitPair.Result;

            switch (result.ResultType)
            {
            case ResultType.Primitive:
                Console.WriteLine($"Primitive, value: {result.PrimitiveValue} for resuest {message.MethodName}");
                invocation.ReturnValue = result.PrimitiveValue.ToObject(invocation.Method.ReturnType);
                return;

            case ResultType.Null:
                Console.WriteLine($"null value for request {message.MethodName} or void method");
                return;

            case ResultType.Stream:
                Console.WriteLine($"Stream, length: {result.StreamValue.Length} for request {message.MethodName}");
                invocation.ReturnValue = result.StreamValue;
                return;

            case ResultType.Exception:
                throw new Exception(result.ExceptionMessage);

            case ResultType.HardObject:
                Console.WriteLine($"Hard object, id: {result.HardObjectId} for request {message.MethodName}");
                var worker = new RemoteWorker(transporter, result.HardObjectId, false);
                invocation.ReturnValue = Create(invocation.Method.ReturnType, worker);
                return;
            }
        }
Ejemplo n.º 4
0
 private object CreateRemoteInvoker(long invokerId, Type invokerType)
 {
     return(RemoteWorker.Create(invokerType, transporter, invokerId));
 }