public static NetworkSystems Create(
            IConnectionTransporter transporter,
            IConnectionlessTransporter connectionlessTransporter,
            TransportRouterConfig groupRouterConfig,
            IDictionary <ObjectType, GameObject> replicationPrefabs,
            IMessageSender messageSender,
            IConnectionManager connectionManager)
        {
            ILatencyInfo latencyInfo         = transporter;
            var          networkMessageTypes = MessageExtensions.GenerateNetworkIds(MessageExtensions.GetNetworkMessageTypes());
            var          objectMessageTypes  = MessageExtensions.GenerateNetworkIds(MessageExtensions.GetObjectMessageTypes());

            var connectionlessMessageSender = new ConnectionlessMessageSender(connectionlessTransporter);

            var messagePool        = new MessagePool(MessageExtensions.CreateMessagePools(networkMessageTypes));
            var objectMessagePools = MessageExtensions.CreateMessagePools(objectMessageTypes);

            var groupRouter = new TransportGroupRouter(transporter, groupRouterConfig);

            Func <ObjectMessageRouter> objectMessageDispatcherFactory =
                () => new ObjectMessageRouter(latencyInfo, objectMessageTypes);
            var networkMessagePool = new BasicObjectPool <ReplicatedObjectStore.ObjectMessageSender.MulticastNetworkMessage>(
                pool => new ReplicatedObjectStore.ObjectMessageSender.MulticastNetworkMessage(pool, networkMessageTypes[typeof(BasicMessage.ToObject)]));
            var objectMessageSenderFactory = ReplicatedObjectStore.ObjectMessageSender.CreateFactory(
                messageSender,
                groupRouter,
                new TransportGroupId(2),
                networkMessagePool);

            var objectDependencies = new List <object> {
                latencyInfo
            };

            foreach (var pool in objectMessagePools)
            {
                objectDependencies.Add(pool.Value);
            }
            var replicationDecorator = ReplicatedObjectStore.GameObjectReplicationDecorator(objectMessageDispatcherFactory,
                                                                                            objectMessageSenderFactory,
                                                                                            objectDependencies,
                                                                                            objectMessageTypes);
            var replicatedObjectPools = replicationPrefabs
                                        .Select(kvPair => new KeyValuePair <ObjectType, Func <GameObject> >(kvPair.Key, kvPair.Value.ToFactory()))
                                        .ToDictionary(kvPair => kvPair.Key, kvPair => kvPair.Value);
            int replicatedObjectCapacity = 256;
            var objectMessageParser      = new ObjectMessageParser(objectMessageTypes);
            var replicatedObjectStore    = new ReplicatedObjectStore(objectMessageParser, replicatedObjectPools,
                                                                     replicationDecorator, replicatedObjectCapacity);

            return(new NetworkSystems(
                       messageSender,
                       connectionlessMessageSender,
                       messagePool,
                       connectionManager,
                       groupRouter,
                       replicatedObjectStore,
                       networkMessageTypes));
        }
Beispiel #2
0
 // Use this for initialization
 void Start()
 {
     dataController   = FindObjectOfType <DataController>();
     roundData        = dataController.GetCurrentRoundData(roundNum);
     answerButtonPool = FindObjectOfType <BasicObjectPool>();
     questionData     = roundData.questions;
     ShowQuestions();
     playerScore   = 0;
     timer         = roundData.timeLimitInSeconds;
     isRoundActive = true;
 }
        public static NetworkSystems Create(
            IConnectionTransporter transporter,
            IConnectionlessTransporter connectionlessTransporter,
            TransportRouterConfig groupRouterConfig,
            TransportGroupId objectGroupId,
            IReadOnlyDictionary <ObjectType, Func <GameObject> > replicatedObjectPools,
            IMessageSender messageSender,
            IConnectionManager connectionManager,
            ILatencyInfo latencyInfo,
            DependencyContainer globalDependencies)
        {
            var connectionlessMessageSender = new ConnectionlessMessageSender(connectionlessTransporter);

            var messagePools        = MessageTypes.CreateMessagePool(MessageTypes.NetworkMessageTypes);
            var objectMessagePools  = MessageTypes.CreateMessagePool(MessageTypes.ObjectMessageTypes);
            var objectMessageParser = new ObjectMessageParser(MessageTypes.ObjectMessageTypes);

            var dependencies = globalDependencies.Copy();

            dependencies.AddDependency("latencyInfo", latencyInfo);
            foreach (var pool in messagePools.Pools)
            {
                dependencies.AddDependency(pool.ToString(), pool);
            }
            foreach (var pool in objectMessagePools.Pools)
            {
                dependencies.AddDependency(pool.ToString(), pool);
            }

            var groupRouter = new TransportGroupRouter(transporter, groupRouterConfig);

            Func <GameObject, ObjectMessageRouter> objectMessageDispatcherFactory =
                gameObject => new ObjectMessageRouter(MessageTypes.ObjectMessageTypes, gameObject);
            var networkMessagePool = new BasicObjectPool <ReplicatedObjectStore.ObjectMessageSender.MulticastNetworkMessage>(
                pool => new ReplicatedObjectStore.ObjectMessageSender.MulticastNetworkMessage(pool, MessageTypes.NetworkMessageTypes[typeof(BasicMessage.ToObject)]));
            var objectMessageSenderFactory = ReplicatedObjectStore.ObjectMessageSender.CreateFactory(
                messageSender,
                groupRouter,
                objectGroupId,
                networkMessagePool);

            var replicationDecorator = ReplicatedObjectStore.GameObjectReplicationDecorator(objectMessageDispatcherFactory,
                                                                                            objectMessageSenderFactory,
                                                                                            dependencies,
                                                                                            MessageTypes.ObjectMessageTypes);
            int replicatedObjectCapacity = 256;
            var replicatedObjectStore    = new ReplicatedObjectStore(replicatedObjectPools,
                                                                     replicationDecorator, replicatedObjectCapacity);

            var replicator = new Replicator(replicatedObjectStore,
                                            groupRouter.GetConnectionGroup(objectGroupId),
                                            new NetworkReplicator(messageSender, messagePools),
                                            objectMessageParser);

            var defaultMessageRouter = new MessageRouter(MessageTypes.NetworkMessageTypes, latencyInfo);

            groupRouter.SetDataHandler(groupRouterConfig.DefaultGroup, defaultMessageRouter);

            return(new NetworkSystems(
                       messageSender,
                       connectionlessMessageSender,
                       messagePools,
                       connectionManager,
                       groupRouter,
                       defaultMessageRouter,
                       replicatedObjectStore,
                       MessageTypes.NetworkMessageTypes,
                       MessageTypes.ObjectMessageTypes,
                       replicator,
                       objectMessageParser));
        }
Beispiel #4
0
    public RamnetBase(
        ICoroutineScheduler coroutineScheduler,
        LidgrenNetworkTransporter transporter,
        IPEndPoint natFacilitatorEndpoint,
        IMessageSender messageSender,
        IDictionary <ObjectType, GameObject> prefabs,
        DependencyContainer dependencyContainer)
    {
        _messageSender = messageSender;
        _transporter   = transporter;
        ILatencyInfo latencyInfo = _transporter;

        var networkMessageTypes = MessageExtensions.GenerateNetworkIds(MessageExtensions.GetNetworkMessageTypes(Assembly.GetExecutingAssembly()));
        var objectMessageTypes  = MessageExtensions.GenerateNetworkIds(MessageExtensions.GetObjectMessageTypes(Assembly.GetExecutingAssembly()));

        _messagePool = new MessagePool(MessageExtensions.CreateMessagePools(networkMessageTypes));
        var objectMessagePools = MessageExtensions.CreateMessagePools(objectMessageTypes);

        var routerConfig = new TransportRouterConfig(
            new KeyValuePair <TransportGroupId, TransportGroupConfig>(NetworkGroup.Default,
                                                                      new TransportGroupConfig(32)));

        _natFacilitatorConnection = new LidgrenNatFacilitatorConnection(natFacilitatorEndpoint, _transporter);
        _natPunchClient           = new LidgrenNatPunchClient(coroutineScheduler, _natFacilitatorConnection, transporter);
        _connectionManager        = new LidgrenPunchThroughFacilitator(
            _transporter,
            coroutineScheduler,
            connectionAttemptTimeout: 10f,
            natPunchClient: _natPunchClient);
        _groupRouter = new TransportGroupRouter(_transporter, routerConfig);

        Func <ObjectMessageRouter> objectMessageDispatcherFactory =
            () => new ObjectMessageRouter(latencyInfo, objectMessageTypes);
        var networkMessagePool = new BasicObjectPool <ReplicatedObjectStore.ObjectMessageSender.MulticastNetworkMessage>(
            pool => new ReplicatedObjectStore.ObjectMessageSender.MulticastNetworkMessage(pool, networkMessageTypes[typeof(BasicMessage.ToObject)]));
        var messageSenderFactory = ReplicatedObjectStore.ObjectMessageSender.CreateFactory(
            messageSender,
            _groupRouter,
            NetworkGroup.Default,
            networkMessagePool);

        var objectDependencies = new List <object>();

        objectDependencies.Add(latencyInfo);
        foreach (var pool in objectMessagePools)
        {
            objectDependencies.Add(pool.Value);
        }
        var replicationDecorator = ReplicatedObjectStore.GameObjectReplicationDecorator(objectMessageDispatcherFactory,
                                                                                        messageSenderFactory,
                                                                                        objectDependencies,
                                                                                        objectMessageTypes);

        var gameObjectFactories = new Dictionary <ObjectType, Func <GameObject> >();

        foreach (var kvPair in prefabs)
        {
            var objectType = kvPair.Key;
            var prefab     = kvPair.Value;
            gameObjectFactories[objectType] = () => {
                var instance = GameObject.Instantiate(prefab);
                // TODO Copy dependencies into global object dependency container
                DependencyInjection.Inject(instance, dependencyContainer);
                return(instance);
            };
        }
        int replicatedObjectCapacity = 256;
        var objectMessageParser      = new ObjectMessageParser(objectMessageTypes);

        _replicatedObjectStore = new ReplicatedObjectStore(objectMessageParser, gameObjectFactories, replicationDecorator, replicatedObjectCapacity);

        _messageRouter = new MessageRouter(networkMessageTypes);
        _groupRouter.SetDataHandler(NetworkGroup.Default, _messageRouter);

        _connectionlessMessageSender = new ConnectionlessMessageSender(_transporter);

        _preExistingObjects = RamjetAnvil.RamNet.PreExistingObjects.FindAll();
    }