Ejemplo n.º 1
0
        public NetworkSystems(IMessageSender messageSender,
                              IConnectionlessMessageSender connectionlessMessageSender,
                              MessagePool messagePool,
                              IConnectionManager connectionManager,
                              TransportGroupRouter groupRouter,
                              MessageRouter defaultMessageRouter,
                              ReplicatedObjectStore objectStore,
                              IReadOnlyDictionary <Type, MessageType> messageTypes,
                              IReadOnlyDictionary <Type, MessageType> objectMessageTypes,
                              Replicator replicator,
                              ObjectMessageParser objectMessageParser)
        {
            _messageSender = messageSender;
            _messagePool   = messagePool;
            _connectionlessMessageSender = connectionlessMessageSender;
            _connectionManager           = connectionManager;
            _groupRouter          = groupRouter;
            _defaultMessageRouter = defaultMessageRouter;
            _objectStore          = objectStore;
            _objectMessageParser  = objectMessageParser;
            _objectMessageTypes   = objectMessageTypes;
            _messageTypes         = messageTypes;

            _replicator = replicator;
        }
Ejemplo n.º 2
0
 public static Action <ConnectionId, IPEndPoint, BasicMessage.ToObject, NetBuffer> ToObject(
     ReplicatedObjectStore objectStore)
 {
     return((connectionId, endpoint, message, reader) => {
         // TODO How many bytes of the reader should be read?
         objectStore.DispatchMessage(connectionId, message.ReceiverId, reader);
     });
 }
Ejemplo n.º 3
0
        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));
        }
Ejemplo n.º 4
0
 public static Action <MessageMetaData, BasicMessage.ReplicatePreExistingObject, NetBuffer> ReplicatePreExistingObject(
     ReplicatedObjectStore objectStore,
     IDictionary <Guid, GameObject> preExistingObjects)
 {
     return((metadata, message, reader) => {
         var instance = preExistingObjects[message.GlobalObjectId];
         objectStore.AddExistingInstance(message.ObjectRole, metadata.ConnectionId,
                                         instance, message.NetworkObjectId, message.GlobalObjectId);
     });
 }
Ejemplo n.º 5
0
 public static Action <MessageMetaData, BasicMessage.ToObject, NetBuffer> ToObject(
     ObjectMessageParser messageParser,
     ReplicatedObjectStore objectStore)
 {
     return((metadata, message, reader) => {
         var objectMessage = messageParser.Parse(reader);
         //Debug.Log("receiving message of type " + objectMessage.Content.GetType() + " for " + message.ReceiverId);
         objectStore.DispatchMessage(metadata.ConnectionId, message.ReceiverId, objectMessage,
                                     metadata.SequenceNumber, metadata.Latency);
     });
 }
Ejemplo n.º 6
0
        public Replicator(
            ReplicatedObjectStore store,
            int growth = 256)
        {
            var initialCapacity = (int)store.Capacity;

            _buffer          = new NetBuffer();
            _currentCapacity = 0;
            _growth          = growth;
            _store           = store;
            _unusedObjectIds = new Stack <ObjectId>(initialCapacity);
            GenerateAdditionalObjectIds(initialCapacity);
        }
Ejemplo n.º 7
0
 public static Action <ConnectionId, IPEndPoint, BasicMessage.CreateObject, NetBuffer> CreateObject(
     ReplicatedObjectStore objectStore)
 {
     return((connectionId, endpoint, message, reader) => {
         var instance = objectStore.AddReplicatedInstance(message.ObjectType, message.ObjectRole,
                                                          message.ObjectId, connectionId);
         objectStore.DispatchMessages(connectionId,
                                      message.ObjectId,
                                      message.AdditionalData,
                                      message.AdditionalData.WriterPosition());
         instance.Activate();
     });
 }
Ejemplo n.º 8
0
 public NetworkSystems(IMessageSender messageSender,
                       IConnectionlessMessageSender connectionlessMessageSender,
                       MessagePool messagePool,
                       IConnectionManager connectionManager, TransportGroupRouter groupRouter,
                       ReplicatedObjectStore objectStore, IDictionary <Type, MessageType> messageTypes)
 {
     _messageSender = messageSender;
     _messagePool   = messagePool;
     _connectionlessMessageSender = connectionlessMessageSender;
     _connectionManager           = connectionManager;
     _groupRouter  = groupRouter;
     _objectStore  = objectStore;
     _messageTypes = messageTypes;
 }
Ejemplo n.º 9
0
        public static void ReplicateEverything(
            this IMessageSender messageSender,
            MessagePool messagePool,
            ConnectionId receiver,
            ReplicatedObjectStore store)
        {
            var existingObjectIds = store.ObjectIds;

            for (int i = 0; i < existingObjectIds.Count; i++)
            {
                var objectId = existingObjectIds[i];
                var @object  = store.Find(objectId);
                messageSender.Replicate(messagePool, receiver, @object);
            }
        }
Ejemplo n.º 10
0
 public static void RemoveAll(this ReplicatedObjectStore store,
                              ConnectionId connectionId)
 {
     ObjectIdCache.Clear();
     store.ObjectIds.CopyListInto(ObjectIdCache);
     for (int i = 0; i < ObjectIdCache.Count; i++)
     {
         var objectId = ObjectIdCache[i];
         var instance = store.Find(objectId);
         if (instance.OwnerConnectionId == connectionId || instance.AuthorityConnectionId == connectionId)
         {
             store.RemoveReplicatedInstance(connectionId, objectId);
         }
     }
 }
Ejemplo n.º 11
0
 public static Action <MessageMetaData, BasicMessage.CreateObject, NetBuffer> CreateObject(
     ObjectMessageParser messageParser,
     ReplicatedObjectStore objectStore)
 {
     return((metadata, message, reader) => {
         var instance = objectStore.Instantiate(message.ObjectType, message.ObjectRole,
                                                message.ObjectId, metadata.ConnectionId, message.Position, message.Rotation);
         var bytesRead = 0;
         while (bytesRead < message.AdditionalData.WriterPosition())
         {
             var objectMessage = messageParser.Parse(message.AdditionalData);
             bytesRead = message.AdditionalData.ReaderPosition();
             objectStore.DispatchMessage(metadata.ConnectionId, message.ObjectId, objectMessage,
                                         new SequenceNumber(1), metadata.Latency);
         }
         objectStore.Activate(instance);
     });
 }
Ejemplo n.º 12
0
        public Replicator(
            ReplicatedObjectStore store,
            IConnectionGroup connectionGroup,
            NetworkReplicator networkReplicator,
            ObjectMessageParser messageParser,
            int growth = 256)
        {
            var initialCapacity = (int)store.Capacity;

            _initialStateBuffer = new NetBuffer();
            _currentCapacity    = 0;
            _growth             = growth;
            _messageParser      = messageParser;
            _store             = store;
            _unusedObjectIds   = new Queue <ObjectId>(initialCapacity);
            _networkReplicator = networkReplicator;
            GenerateAdditionalObjectIds(initialCapacity);

            _connectionGroup             = connectionGroup;
            _connectionGroup.PeerJoined += PeerJoined;
            _connectionGroup.PeerLeft   += PeerLeft;
        }
Ejemplo n.º 13
0
 public static Action <MessageMetaData, BasicMessage.DeleteObject> DeleteObject(ReplicatedObjectStore objectStore)
 {
     return((metadata, message) => {
         objectStore.RemoveReplicatedInstance(metadata.ConnectionId, message.ObjectId);
     });
 }
Ejemplo n.º 14
0
        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));
        }
Ejemplo n.º 15
0
 public static Action <ConnectionId, BasicMessage.DeleteObject> DeleteObject(ReplicatedObjectStore objectStore)
 {
     return((connectionId, message) => {
         objectStore.RemoveReplicatedInstance(connectionId, message.ObjectId);
     });
 }