예제 #1
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();
    }