Example #1
0
        /// <summary>
        /// Analyzes the login package to see if it is a valid login. If it is, then login the client.
        /// </summary>
        private void ProcessLoginPackage(ServerSideClient client, Package package)
        {
            // The client does not have its name yet. Use the name provided by its login attempt.
            LoginContent loginContent = LoginContent.Deserialize(package.Content);
            var          reason       = GetRefuseReason(loginContent.ClientName);

            // Here I am not rejecting the user and we will send the package out for
            // further processing.
            if (reason == ConnectionRefusedReason.None)
            {
                client.Name   = loginContent.ClientName;
                client.Status = ClientStatus.LoggedIn;
                BroadcastPackageAsync(package);
                client.TimeStampLogin();
                clientNames.Add(client.Name);
                OnClientLoggedIn(client.Name);
            }
            else
            {
                var args = new ConnectionRefusedContent(reason, loginContent.ClientName);
                // Here I am rejecting the user, we will send the reject message to the appropriate client
                var pkgResp = new Package(BaseCommand.ConnectionRefused, args.Serialize());
                client.SendPackageAsync(pkgResp);
                clientNames.Remove(client.Name);
                clients.Remove(client);
                client.Socket.Shutdown(SocketShutdown.Both);
                client.Dispose();
                OnConnectionRefused(args);
            }
        }
Example #2
0
        /// <summary>
        /// Sends the specified package asynchronously to all connected clients except the specified one.
        /// </summary>
        /// <param name="package">The <see cref="Package"/> to send to the clients.</param>
        /// <param name="excludeClient">The client to exclude from the broadcast</param>
        /// <param name="mustBeLoggedIn">Whether the package is sent to all connected clients or
        /// just the ones that are logged in.</param>
        protected void BroadcastPackageAsync(Package package, ServerSideClient excludeClient, bool mustBeLoggedIn = true)
        {
            foreach (ServerSideClient client in clients)
            {
                if (mustBeLoggedIn && client.Status != ClientStatus.LoggedIn)
                {
                    continue;
                }

                if (client != excludeClient)
                {
                    client.SendPackageAsync(package);
                }
            }
        }
Example #3
0
        private void AcceptCallback(IAsyncResult AR)
        {
            if (enabled)
            {
                Socket socket = serverSocket.EndAccept(AR);

                UiContext.Default.Invoke(() =>
                {
                    ServerSideClient client = new ServerSideClient(socket);
                    clients.Add(client);
                    client.PackageReceived = OnPackageReceivedCore;
                    serverSocket.BeginAccept(AcceptCallback, null);
                });
            }
        }
Example #4
0
        private void OnPackageReceivedCore(ServerSideClient client, Package e)
        {
            switch ((BaseCommand)e.Command)
            {
            case BaseCommand.ClientNames: SendClientNames(client); break;

            case BaseCommand.Logout:
                clients.Remove(client);
                BroadcastPackageAsync(e, client);
                clientNames.Remove(client.Name);
                client.Dispose();
                OnClientLoggedOut(LogoutContent.Deserialize(e.Content));
                break;

            case BaseCommand.Login: ProcessLoginPackage(client, e); break;

            case BaseCommand.Sync:
                Synchronize(client);
                break;
            }

            OnPackageReceived(client, e);
        }
Example #5
0
 /// <summary>
 /// Sends synchronization data to the specified client.
 /// </summary>
 /// <param name="client">The client to send sync data to.</param>
 protected virtual void Synchronize(ServerSideClient client)
 {
     SendClientNames(client);
 }
Example #6
0
        /// <summary>
        /// Sends the names of all of the clients which are logged in to a specified client.
        /// </summary>
        /// <param name="client">The client to send the names to.</param>
        private void SendClientNames(ServerSideClient client)
        {
            var contents = new ClientNamesContent(clients.Select(c => c.Name).ToArray());

            client.SendPackageAsync((int)BaseCommand.ClientNames, contents);
        }
Example #7
0
 /// <summary>
 /// When a client receives an entire package, this method is to be used to process it.
 /// </summary>
 protected abstract void OnPackageReceived(ServerSideClient client, Package package);