Beispiel #1
0
        public EntityThread(ulong threadIndex, EntityThreadRepository entityThreadRepository,
                            ClientThreadRepository clientThreadRepository,
                            SpatialPartition spatialPartition, int syncRate, bool netOwnerEvents,
                            Action <IClient, IEntity, LinkedList <string> > onEntityCreate, Action <IClient, IEntity> onEntityRemove,
                            Action <IClient, IEntity, LinkedList <string> > onEntityDataChange,
                            Action <IClient, IEntity, Vector3> onEntityPositionChange, Action <IClient, IEntity> onEntityClearCache,
                            Action <IClient, IEntity, bool> onEntityNetOwnerChange)
        {
            this.threadIndex = threadIndex;

            this.entityThreadRepository = entityThreadRepository ??
                                          throw new ArgumentException("entityThreadRepository should not be null.");
            this.clientThreadRepository = clientThreadRepository;
            this.spatialPartition       =
                spatialPartition ?? throw new ArgumentException("spatialPartition should not be null.");
            this.syncRate           = syncRate;
            this.netOwnerEvents     = netOwnerEvents;
            this.onEntityCreate     = onEntityCreate ?? throw new ArgumentException("onEntityCreate should not be null.");
            this.onEntityRemove     = onEntityRemove ?? throw new ArgumentException("onEntityRemove should not be null.");
            this.onEntityDataChange = onEntityDataChange ??
                                      throw new ArgumentException("onEntityDataChange should not be null.");
            this.onEntityPositionChange = onEntityPositionChange ??
                                          throw new ArgumentException("onEntityPositionChange should not be null.");
            this.onEntityClearCache = onEntityClearCache ??
                                      throw new ArgumentException("onEntityPositionChange should not be null.");
            this.onEntityNetOwnerChange = onEntityNetOwnerChange ??
                                          throw new ArgumentException("onEntityNetOwnerChange should not be null.");

            thread = new Thread(OnLoop)
            {
                IsBackground = true
            };
            thread.Start();
        }
Beispiel #2
0
        public EntitySyncServer(long threadCount, int syncRate,
                                Func <IClientRepository, NetworkLayer> createNetworkLayer,
                                Func <SpatialPartition> createSpatialPartition, IIdProvider <ulong> idProvider)
        {
            if (threadCount < 1)
            {
                throw new ArgumentException("threadCount must be >= 1");
            }

            entityThreads            = new EntityThread[threadCount];
            entityThreadRepositories = new EntityThreadRepository[threadCount];
            clientThreadRepositories = new ClientThreadRepository[threadCount];

            for (var i = 0; i < threadCount; i++)
            {
                var clientThreadRepository = new ClientThreadRepository();
                var entityThreadRepository = new EntityThreadRepository();
                entityThreadRepositories[i] = entityThreadRepository;
                clientThreadRepositories[i] = clientThreadRepository;
                entityThreads[i]            = new EntityThread(entityThreadRepository, clientThreadRepository, createSpatialPartition(), syncRate,
                                                               OnEntityCreate,
                                                               OnEntityRemove, OnEntityDataChange, OnEntityPositionChange, OnEntityClearCache);
            }

            entityRepository = new EntityRepository(entityThreadRepositories);
            clientRepository = new ClientRepository(clientThreadRepositories);
            networkLayer     = createNetworkLayer(clientRepository);
            networkLayer.OnConnectionConnect    += OnConnectionConnect;
            networkLayer.OnConnectionDisconnect += OnConnectionDisconnect;
            this.idProvider = idProvider;
        }
Beispiel #3
0
        public EntityThread(EntityThreadRepository entityThreadRepository,
                            ClientThreadRepository clientThreadRepository,
                            SpatialPartition spatialPartition, int syncRate,
                            Action <IClient, IEntity, LinkedList <string> > onEntityCreate, Action <IClient, IEntity> onEntityRemove,
                            Action <IClient, IEntity, LinkedList <string> > onEntityDataChange,
                            Action <IClient, IEntity, Vector3> onEntityPositionChange, Action <IClient, IEntity> onEntityClearCache)
        {
            if (spatialPartition == null)
            {
                throw new ArgumentException("spatialPartition should not be null.");
            }

            if (entityThreadRepository == null)
            {
                throw new ArgumentException("entityThreadRepository should not be null.");
            }

            if (onEntityCreate == null)
            {
                throw new ArgumentException("onEntityCreate should not be null.");
            }

            if (onEntityRemove == null)
            {
                throw new ArgumentException("onEntityRemove should not be null.");
            }

            if (onEntityDataChange == null)
            {
                throw new ArgumentException("onEntityDataChange should not be null.");
            }

            if (onEntityPositionChange == null)
            {
                throw new ArgumentException("onEntityPositionChange should not be null.");
            }

            if (onEntityClearCache == null)
            {
                throw new ArgumentException("onEntityPositionChange should not be null.");
            }

            thread = new Thread(OnLoop)
            {
                IsBackground = true
            };
            thread.Start();
            this.entityThreadRepository = entityThreadRepository;
            this.clientThreadRepository = clientThreadRepository;
            this.spatialPartition       = spatialPartition;
            this.syncRate               = syncRate;
            this.onEntityCreate         = onEntityCreate;
            this.onEntityRemove         = onEntityRemove;
            this.onEntityDataChange     = onEntityDataChange;
            this.onEntityPositionChange = onEntityPositionChange;
            this.onEntityClearCache     = onEntityClearCache;
        }
Beispiel #4
0
        public EntitySyncServer(ulong threadCount, int syncRate, Func <ulong, bool> netOwnerEvents,
                                Func <ulong, IClientRepository, NetworkLayer> createNetworkLayer,
                                Func <IEntity, ulong, ulong> entityThreadId,
                                Func <ulong, ulong, ulong, ulong> entityIdAndTypeThreadId,
                                Func <ulong, SpatialPartition> createSpatialPartition, IIdProvider <ulong> idProvider)
        {
            if (threadCount < 1)
            {
                throw new ArgumentException("threadCount must be >= 1");
            }

            if (syncRate < 0)
            {
                throw new ArgumentException("syncRate must be >= 0");
            }

            entityThreads            = new EntityThread[threadCount];
            entityThreadRepositories = new EntityThreadRepository[threadCount];
            clientThreadRepositories = new ClientThreadRepository[threadCount];
            spatialPartitions        = new SpatialPartition[threadCount];

            for (ulong i = 0; i < threadCount; i++)
            {
                var clientThreadRepository = new ClientThreadRepository();
                var entityThreadRepository = new EntityThreadRepository();
                var spatialPartition       = createSpatialPartition(i);
                entityThreadRepositories[i] = entityThreadRepository;
                clientThreadRepositories[i] = clientThreadRepository;
                spatialPartitions[i]        = spatialPartition;
                entityThreads[i]            = new EntityThread(i, entityThreadRepository, clientThreadRepository, spatialPartition, syncRate, netOwnerEvents(i),
                                                               OnEntityCreate,
                                                               OnEntityRemove, OnEntityDataChange, OnEntityPositionChange, OnEntityClearCache, OnEntityNetOwnerChange);
            }

            entityRepository = new EntityRepository(entityThreadRepositories, entityThreadId, entityIdAndTypeThreadId);
            clientRepository = new ClientRepository(clientThreadRepositories);
            networkLayer     = createNetworkLayer(threadCount, clientRepository);
            networkLayer.OnConnectionConnect    += OnConnectionConnect;
            networkLayer.OnConnectionDisconnect += OnConnectionDisconnect;
            this.idProvider = idProvider;
        }