Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            var logger = new LoggerStub();

            // using (var logger = new ConsoleLoggerWrapper(new LoggerStub()))
            // {
            Helpers.LogCurrentMemoryUsage(logger);
            // Console.ReadLine();

            var messagePackSerializerFactory = new MessagePackSerializerFactory();
            var rpcTcpServicePublisher       = new RpcTcpServicePublisher(messagePackSerializerFactory, logger);
            var defaultServiceTimeout        = TimeSpan.FromMinutes(1);
            var testService = new TestService(logger);

            using (new DisposableCollectionHolder(
                       rpcTcpServicePublisher.PublishUnsecured <ITestService>(
                           testService,
                           defaultServiceTimeout)))
            {
                using (ServiceHostContainerFactory.Create <TestService, ITestService>())
                {
                    Console.WriteLine("Services started. Press Enter to stop.");
                    Console.ReadLine();

                    Helpers.LogCurrentMemoryUsage(logger);
                }
            }
            // }

            Console.WriteLine("Services stopped.");
        }
Exemplo n.º 2
0
        private static RpcTcpServer <ITestService> GetTestService(ISerializerFactory serializerFactory)
        {
            var callsCount       = 0;
            var serviceInstance  = new TestService(() => callsCount++);
            var servicePublisher = new RpcTcpServicePublisher(serializerFactory, new LoggerStub());

            return(servicePublisher.PublishUnsecured <ITestService>(serviceInstance, TimeSpan.FromMinutes(1)));
        }
Exemplo n.º 3
0
        public void ServerTest()
        {
            var callsCount        = 0;
            var serviceInstance   = new TestService(() => callsCount++);
            var serializerFactory = new MessagePackSerializerFactory();
            var servicePublisher  = new RpcTcpServicePublisher(serializerFactory, new LoggerStub());

            using (servicePublisher.PublishUnsecured <ITestService>(serviceInstance, TimeSpan.FromMinutes(1)))
            {
                PerformTestWithConnectedNetworkStream(networkStream =>
                {
                    var argumentsData = new byte[1][];
                    argumentsData[0]  = serializerFactory
                                        .CreateSerializer <SerializableObject>()
                                        .Serialize(SerializableObject.GetTestInstance());
                    var rpcMessage = serializerFactory.CreateSerializer <RpcMessage>().Serialize(new RpcMessage()
                    {
                        ServiceCode   = typeof(ITestService).FullName.GetHashCode(),
                        OperationCode = 0,
                        ArgumentsData = argumentsData
                    });

                    networkStream.Write(rpcMessage, 0, rpcMessage.Length);
                    networkStream.WriteEndOfSessionMessage();

                    var data      = new List <byte>();
                    var dataChunk = new byte[128];
                    do
                    {
                        var bytesRead = networkStream.Read(dataChunk, 0, dataChunk.Length);
                        data.AddRange(dataChunk.Take(bytesRead).ToArray());
                    } while (networkStream.DataAvailable);

                    var serviceCallResult = serializerFactory.CreateSerializer <ServiceCallResult>().Deserialize(data.ToArray());
                    var result            = serializerFactory.CreateSerializer <int>().Deserialize(serviceCallResult.ReturnValue);

                    Assert.Equal(SerializableObject.TestInt, result);
                });

                PerformTestWithConnectedNetworkStream(networkStream =>
                {
                    var argumentsData = new byte[2][];
                    argumentsData[0]  = serializerFactory.CreateSerializer <int>().Serialize(0);
                    argumentsData[1]  = serializerFactory.CreateSerializer <int>().Serialize(2);

                    var rpcMessage = serializerFactory.CreateSerializer <RpcMessage>().Serialize(new RpcMessage()
                    {
                        ServiceCode   = typeof(ITestService).FullName.GetHashCode(),
                        OperationCode = 3,
                        ArgumentsData = argumentsData
                    });

                    networkStream.Write(rpcMessage, 0, rpcMessage.Length);
                    networkStream.WriteEndOfSessionMessage();

                    var data      = new List <byte>();
                    var dataChunk = new byte[128];
                    do
                    {
                        var bytesRead = networkStream.Read(dataChunk, 0, dataChunk.Length);
                        data.AddRange(dataChunk.Take(bytesRead).ToArray());
                    } while (networkStream.DataAvailable);

                    var serviceCallResult = serializerFactory.CreateSerializer <ServiceCallResult>().Deserialize(data.ToArray());
                    var resultTuple       = serializerFactory.CreateSerializer <(int count, SerializableObject[] objects)>()
                                            .Deserialize(serviceCallResult.ReturnValue);

                    Assert.Equal(2, resultTuple.count);
                    Assert.Equal(SerializableObject.TestInt, resultTuple.objects[0].IntProperty);
                    Assert.Equal(SerializableObject.TestString, resultTuple.objects[0].StringProperty);
                    Assert.Equal(SerializableObject.TestDouble, resultTuple.objects[0].NestedObject.DoubleProperty);
                });
            }
        }