public ServerEventNetworkLayer(ulong threadCount, IClientRepository clientRepository) : base(threadCount,
                                                                                                     clientRepository)
        {
            Alt.OnPlayerConnect += (player, reason) =>
            {
                var playerClient = new PlayerClient(threadCount, player.Id.ToString(), player);
                player.SetEntitySyncClient(playerClient);
                clientRepository.Add(playerClient);
                Task.Factory.StartNew(async obj =>
                {
                    var currPlayerClient = (PlayerClient)obj;
                    var channel          = Channel.CreateUnbounded <ServerEntityEvent>(new UnboundedChannelOptions
                    {
                        SingleReader = true
                    });
                    lock (serverEventChannels)
                    {
                        serverEventChannels[currPlayerClient] = channel;
                    }

                    var channelReader = channel.Reader;
                    while (await channelReader.WaitToReadAsync())
                    {
                        while (channelReader.TryRead(out var entityEvent))
                        {
                            try
                            {
                                switch (entityEvent.EventType)
                                {
                                case 1:
                                    if (entityEvent.ChangedData != null)
                                    {
                                        currPlayerClient.Emit("entitySync:create", entityEvent.Entity.Id,
                                                              entityEvent.Entity.Type, entityEvent.Entity.Position, entityEvent.ChangedData);
                                    }
                                    else
                                    {
                                        currPlayerClient.Emit("entitySync:create", entityEvent.Entity.Id,
                                                              entityEvent.Entity.Type, entityEvent.Entity.Position);
                                    }

                                    break;

                                case 2:
                                    currPlayerClient.Emit("entitySync:remove", entityEvent.Entity.Id,
                                                          entityEvent.Entity.Type);
                                    break;

                                case 3:
                                    currPlayerClient.Emit("entitySync:updatePosition", entityEvent.Entity.Id,
                                                          entityEvent.Entity.Type, entityEvent.Position);
                                    break;

                                case 4:
                                    currPlayerClient.Emit("entitySync:updateData", entityEvent.Entity.Id,
                                                          entityEvent.Entity.Type, entityEvent.ChangedData);
                                    break;

                                case 5:
                                    currPlayerClient.Emit("entitySync:clearCache", entityEvent.Entity.Id,
                                                          entityEvent.Entity.Type);
                                    break;
                                }
                            }
                            catch (Exception exception)
                            {
                                Console.WriteLine(exception);
                            }
                        }
                    }
                }, playerClient);
            };
            Alt.OnPlayerDisconnect += (player, reason) =>
            {
                var client = clientRepository.Remove(player.Id.ToString());
                if (client == null)
                {
                    return;
                }
                Channel <ServerEntityEvent> channel;
                lock (serverEventChannels)
                {
                    if (!serverEventChannels.Remove(client, out channel))
                    {
                        return;
                    }
                }

                channel.Writer.Complete();
            };
        }
 internal static void SetEntitySyncClient(this IPlayer player, PlayerClient client)
 {
     player.SetData(PlayerSyncClientKey, client);
 }
 public static bool TryGetEntitySyncClient(this IPlayer player, out PlayerClient client)
 {
     return(player.GetData(PlayerSyncClientKey, out client));
 }
Exemplo n.º 4
0
        public PedSyncerNetworkLayer(ulong threadCount, IClientRepository clientRepository) : base(threadCount,
                                                                                                   clientRepository)
        {
            Alt.OnPlayerConnect += (player, reason) =>
            {
                var playerClient = new PlayerClient(threadCount, player.Id.ToString(), player);
                player.SetEntitySyncClient(playerClient);
                clientRepository.Add(playerClient);
                Task.Factory.StartNew(async obj =>
                {
                    var currPlayerClient = (PlayerClient)obj;
                    var channel          = Channel.CreateUnbounded <ServerEntityEvent>(new UnboundedChannelOptions
                    {
                        SingleReader = true
                    });
                    lock (serverEventChannels)
                    {
                        serverEventChannels[currPlayerClient] = channel;
                    }

                    var channelReader = channel.Reader;
                    while (await channelReader.WaitToReadAsync())
                    {
                        while (channelReader.TryRead(out var entityEvent))
                        {
                            try
                            {
                                switch (entityEvent.EventType)
                                {
                                case 1:
                                    if (entityEvent.ChangedData != null)
                                    {
                                        currPlayerClient.Emit("entitySync:create", entityEvent.Entity.Id,
                                                              entityEvent.Entity.Type, entityEvent.Entity.Position,
                                                              entityEvent.ChangedData);
                                    }
                                    else
                                    {
                                        currPlayerClient.Emit("entitySync:create", entityEvent.Entity.Id,
                                                              entityEvent.Entity.Type, entityEvent.Entity.Position);
                                    }

                                    break;

                                case 2:
                                    currPlayerClient.Emit("entitySync:remove", entityEvent.Entity.Id,
                                                          entityEvent.Entity.Type);
                                    break;

                                case 3:
                                    if (entityEvent.Entity.NetOwner != currPlayerClient)
                                    {
                                        currPlayerClient.Emit("entitySync:updatePosition", entityEvent.Entity.Id,
                                                              entityEvent.Entity.Type, entityEvent.Position);
                                    }
                                    break;

                                case 4:
                                    /*Dictionary<string, object> UpdatedData = new Dictionary<string, object>();
                                     * Ped ped = Ped.GetByID(entityEvent.Entity.Id);
                                     * if (ped == null) continue;
                                     *
                                     * foreach (string key in ped.UpdatedData)
                                     * {
                                     *  if (!UpdatedData.ContainsKey(key)) UpdatedData.Add(key, ped.GetValue(key));
                                     * }*/

                                    if (entityEvent.Entity.NetOwner != currPlayerClient)
                                    {
                                        currPlayerClient.Emit("entitySync:updateData", entityEvent.Entity.Id,
                                                              //entityEvent.Entity.Type, UpdatedData);
                                                              entityEvent.Entity.Type, entityEvent.ChangedData);
                                    }
                                    break;

                                case 5:
                                    currPlayerClient.Emit("entitySync:clearCache", entityEvent.Entity.Id,
                                                          entityEvent.Entity.Type);
                                    break;

                                case 6:
                                    currPlayerClient.Emit("entitySync:netOwner", entityEvent.Entity.Id,
                                                          entityEvent.Entity.Type, entityEvent.NetOwner);
                                    break;
                                }
                            }
                            catch (Exception exception)
                            {
                                Console.WriteLine(exception);
                            }
                        }
                    }
                }, playerClient);
            };
            Alt.OnPlayerDisconnect += (player, reason) =>
            {
                var client = clientRepository.Remove(player.Id.ToString());
                if (client == null)
                {
                    return;
                }
                Channel <ServerEntityEvent> channel;
                lock (serverEventChannels)
                {
                    if (!serverEventChannels.Remove(client, out channel))
                    {
                        return;
                    }
                }

                channel.Writer.Complete();
            };
        }