Exemplo n.º 1
0
        private void OnClientAccepted(object?sender, SocketAsyncEventArgs e)
        {
            TUser user = ActivatorUtilities.CreateInstance <TUser>(_serviceProvider);

            if (!_connectedUsers.TryAdd(user.Id, user))
            {
                throw new LiteNetworkException($"Failed to add user with id: '{user.Id}'. An user with same id already exists.");
            }

            if (e.AcceptSocket is null)
            {
                throw new LiteNetworkException($"The accepted socket is null.");
            }

            user.Initialize(e.AcceptSocket);
            _logger?.LogInformation($"New user connected from '{user.Socket.RemoteEndPoint}' with id '{user.Id}'.");
            user.OnConnected();
            _receiver.StartReceiving(user);
        }