コード例 #1
0
        public CommandResultProcessor Initialize(IPEndPoint bindingAddress)
        {
            var serverSetting = new NettyServerSetting(
                channel =>
            {
                var pipeline = channel.Pipeline;

                pipeline.AddLast(typeof(LengthFieldPrepender).Name, new LengthFieldPrepender(2));
                pipeline.AddLast(typeof(LengthFieldBasedFrameDecoder).Name, new LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2));
                pipeline.AddLast(typeof(RequestEncoder).Name, new RequestEncoder());
                pipeline.AddLast(typeof(RequestDecoder).Name, new RequestDecoder());
                pipeline.AddLast(typeof(CommandResultChannelHandler).Name, new CommandResultChannelHandler(this));
            }
                );

            _server = new NettyServer("CommandResultProcessor.RemotingServer", bindingAddress, serverSetting);

            _commandTaskDict = new ConcurrentDictionary <string, CommandTaskCompletionSource>();
            _commandExecutedMessageLocalQueue    = new BlockingCollection <CommandResult>(new ConcurrentQueue <CommandResult>());
            _domainEventHandledMessageLocalQueue = new BlockingCollection <DomainEventHandledMessage>(new ConcurrentQueue <DomainEventHandledMessage>());
            _commandExecutedMessageWorker        = new Worker("ProcessExecutedCommandMessage", () => ProcessExecutedCommandMessage(_commandExecutedMessageLocalQueue.Take()));
            _domainEventHandledMessageWorker     = new Worker("ProcessDomainEventHandledMessage", () => ProcessDomainEventHandledMessage(_domainEventHandledMessageLocalQueue.Take()));
            _jsonSerializer = ObjectContainer.Resolve <IJsonSerializer>();
            _logger         = ObjectContainer.Resolve <ILoggerFactory>().Create(GetType().FullName);
            BindingAddress  = bindingAddress;
            return(this);
        }
コード例 #2
0
        static void Main(string[] args)
        {
            NettyConfig.Port = 6666;
            NettyServer server = new NettyServer();

            server.RunServerAsync();
            while (true)
            {
                var data = server.GetNetSocket();
                Console.WriteLine(NettyConfig.NettyEncod.GetString(data.data));
            }
        }
コード例 #3
0
        /// <summary>
        ///     Defines the entry point of the application.
        /// </summary>
        /// <param name="args">The command-line arguments passed to the application.</param>
        public static int Main(string[] args)
        {
            var options = new Options();

            if (!CommandLine.Parser.Default.ParseArguments(args, options))
            {
                return(-1);
            }

            Console.Write("Starting Web Server ... ");

            int port = options.Port;

            if (port == -1)
            {
                port = NetworkUtility.FindRandomOpenPort();
            }

            var webServer = new NettyServer(options.PhysicalPath, options.VirtualPath, port);

            // Update an application setting, and then start the server
            webServer.Start();

            Console.WriteLine("Done.");
            Console.WriteLine("Listening at: {0}", webServer.Port);
            Console.WriteLine("Press [ENTER] to exit.");

            Console.ReadLine();

            // Stop the web server - this will restore the configuration to the original values
            Console.WriteLine("Stopping Web Server ... ");

            webServer.Stop();

            return(0);
        }
コード例 #4
0
        /// <summary>
        ///     Defines the entry point of the application.
        /// </summary>
        /// <param name="args">The command-line arguments passed to the application.</param>
        public static void Main(string[] args)
        {
            Console.Write("Starting Web Server ... ");

            // Setup a new web server using a random port.  The ports can be shared as long as
            // the virtual path is unique.
            var webServer = new NettyServer(@"..\..\..\SampleWebsite", "/Sample/");

            // Update an application setting, and then start the server
            webServer
            .AlterApplicationSetting("Key1", "I am updated.")
            .Start();

            Console.WriteLine("Done.");
            Console.WriteLine("Listening at: {0}", webServer.Port);
            Console.WriteLine("Press [ENTER] to exit.");

            Console.ReadLine();

            // Stop the web server - this will restore the configuration to the original values
            Console.WriteLine("Stopping Web Server ... ");

            webServer.Stop();
        }
コード例 #5
0
 public static void Startup()
 {
     _webServer = new NettyServer(@"..\..\..\SampleWebSite", "/", 9015);
     _webServer.Start();
 }
コード例 #6
0
        public async Task Should_Communicates_Between_Server_And_Client()
        {
            //Arrange
            var serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9019);

            var serverSetting = new NettyServerSetting(
                channel =>
            {
                var pipeline = channel.Pipeline;

                pipeline.AddLast(typeof(LengthFieldPrepender).Name, new LengthFieldPrepender(2));
                pipeline.AddLast(typeof(LengthFieldBasedFrameDecoder).Name, new LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2));
                pipeline.AddLast(typeof(RequestEncoder).Name, new RequestEncoder());
                pipeline.AddLast(typeof(RequestDecoder).Name, new RequestDecoder());
                pipeline.AddLast(typeof(ServerHandler).Name, new ServerHandler(ObjectContainer.Resolve <ServerMessageBox>()));
            }
                );
            var server = new NettyServer(serverEndPoint, serverSetting);

            server.Start();

            var clientChannelHandlerTypes = new List <ChannelHandlerInstance>();

            clientChannelHandlerTypes.Add(new ChannelHandlerInstance()
            {
                Type = typeof(ClientHandler), Args = new List <object>()
                {
                    ObjectContainer.Resolve <ClientMessageBox>()
                }
            });

            var clientSetting = new NettyClientSetting(
                channel =>
            {
                var pipeline = channel.Pipeline;

                pipeline.AddLast(typeof(LengthFieldPrepender).Name, new LengthFieldPrepender(2));
                pipeline.AddLast(typeof(LengthFieldBasedFrameDecoder).Name, new LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2));
                pipeline.AddLast(typeof(RequestEncoder).Name, new RequestEncoder());
                pipeline.AddLast(typeof(RequestDecoder).Name, new RequestDecoder());
                pipeline.AddLast(typeof(ClientHandler).Name, new ClientHandler(ObjectContainer.Resolve <ClientMessageBox>()));
            }
                );
            var client = new NettyClient(serverEndPoint, clientSetting);
            await client.StartAsync();

            var request = new Request()
            {
                Code = 1,
                Body = Encoding.UTF8.GetBytes("test")
            };

            //Act
            await client.Channel.WriteAndFlushAsync(request);

            await Task.Delay(300);

            //Assert
            var serverMessageBox = ObjectContainer.Resolve <ServerMessageBox>();
            var messages         = await serverMessageBox.GetAllAsync();

            messages.Count.ShouldBe(1);
            messages.Select(m => m as Request).FirstOrDefault(m => m.Code == request.Code).ShouldNotBeNull();
        }