예제 #1
0
        INetNode CreateNetNode(StatefulServiceContext initParams)
        {
            var logger = new LoggerConfiguration()
                         .ConfigureMOUSETypesDestructure()
                         .MinimumLevel.Verbose()
                         .WriteTo.Seq("http://localhost:5341/")
                         .CreateLogger();

            var coreEventsLogger    = new SerilogCoreEvents(logger);
            var lidgrenEventsLogger = new SerilogLidgrenEvents(logger);


            var messageSerializer = new ProtobufMessageSerializer(typeof(Message).Assembly, typeof(Say).Assembly);

            var endPoint = initParams
                           .CodePackageActivationContext
                           .GetEndpoint("ServiceEndPoint");

            var nodeContext = FabricRuntime.GetNodeContext();

            string nodeName = $"{nodeContext.NodeName}:{nodeContext.NodeId}";

            var transport = new LidgrenNetProvider(
                new NetPeerConfiguration("Public")
            {
                MaximumConnections        = 10,
                AcceptIncomingConnections = true,
                Port         = endPoint.Port,
                LocalAddress = Dns.GetHostAddresses(nodeContext.IPAddressOrFQDN).First(x => x.AddressFamily == AddressFamily.InterNetwork)
            },
                lidgrenEventsLogger);

            var netConfig = new NetNodeConfig()
            {
                ConnectTimeoutSec = 30,
                SendTimeoutSec    = 30
            };

            var bufferPool = new WcfBufferPool();

            var node = new NetNode <NetChannel>(nodeName, transport, coreEventsLogger, messageSerializer,
                                                config: netConfig,
                                                channelFactory: (owner, transportChannel) =>
                                                new AsyncProcessingNetChannel(builder => builder
                                                                              .UseFiber(Fiber, config => config
                                                                                        .Lock <Say>(LockType.Read)
                                                                                        .Lock <JoinRoom>(LockType.Write)
                                                                                        .Lock <PeerDisconnected>(LockType.Write))
                                                                              .UseConfigurableDispatcher(config => config
                                                                                                         .HandleMessageAsync <Say>(OnSay)
                                                                                                         .HandleMessageAsync <JoinRoom>(OnRoomJoin)
                                                                                                         .HandleMessageAsync <PeerDisconnected>((msg, o) => OnRoomLeave(o.ReplyChannel))),
                                                                              owner, transportChannel, messageSerializer, coreEventsLogger, netConfig, bufferPool));

            return(node);
        }
        static void Main(string[] args)
        {
            int port = 12345;

            var messageSerializer = new ProtobufMessageSerializer(
                new Message[] { new JoinRoom(), new Say(), new RoomEvent(), new JoinRoomResponse() },
                new MessageHeader[] { new OperationHeader() });

            var logger = new LoggerConfiguration()
                         .ConfigureMOUSETypesDestructure()
                         .MinimumLevel.Verbose()
                         .WriteTo.ColoredConsole()
                         .CreateLogger();

            var coreEventLogger = new SerilogCoreEvents(logger);

            var transport = new LidgrenNetProvider(
                new NetPeerConfiguration("Public")
            {
                MaximumConnections        = 10,
                AcceptIncomingConnections = true,
                Port         = port,
                LocalAddress = IPAddress.Loopback
            },
                new SerilogLidgrenEvents(logger));

            var netConfig = new NetNodeConfig()
            {
                ConnectTimeoutSec = 30,
                SendTimeoutSec    = 30
            };

            var bufferPool = new WcfBufferPool();

            var node = new NetNode <NetChannel>("server", transport, coreEventLogger, messageSerializer, config: netConfig,
                                                channelFactory: (n, transportChannel) =>
                                                new AsyncProcessingNetChannel(builder => builder
                                                                              .UseFiber(Fiber, config => config
                                                                                        .Lock <Say>(LockType.Read)
                                                                                        .Lock <JoinRoom>(LockType.Write)
                                                                                        .Lock <PeerDisconnected>(LockType.Write))
                                                                              .UseConfigurableDispatcher(config => config
                                                                                                         .HandleMessage <Say>(OnSay)
                                                                                                         .HandleMessage <JoinRoom>(OnRoomJoin)
                                                                                                         .HandleMessage <PeerDisconnected>((msg, o) => OnRoomLeave(o.ReplyChannel))),
                                                                              n, transportChannel, messageSerializer, coreEventLogger, netConfig, bufferPool));

            node.Start();
            Console.ReadKey();
        }