/// <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); } }
/// <summary> /// Looks for clients that are connected but not doing anything and removes them. /// </summary> private void CheckForSolicitors() { for (int i = 0; i < clients.Count; i++) { if (clients[i].ConnectionTime != null && clients[i].LoggedInTime == null) { TimeSpan span = DateTime.Now - clients[i].ConnectionTime.Value; // If client has connected, but hasn't logged in within 3 seconds // then refuse the client. if (span.TotalMilliseconds > solicitorThreshold) { const ConnectionRefusedReason REASON = ConnectionRefusedReason.NoLogin; var content = new ConnectionRefusedContent(REASON, clients[i].Name); clients[i].SendPackageAsync((int)BaseCommand.ConnectionRefused, content); OnConnectionRefused(content); clientNames.Remove(clients[i].Name); clients[i].Dispose(); clients.RemoveAt(i); i--; if (clients.Count == 0) { break; } } } } }
/// <summary> /// Raises when a client received a completed <see cref="Package"/>. /// Not calling the base implementation of this methods will cause the client /// to malfunction. /// </summary> /// <param name="package">The package that has been received.</param> protected override void OnPackageReceived(Package package) { base.OnPackageReceived(package); switch ((BaseCommand)package.Command) { case BaseCommand.ConnectionRefused: ResetClient(); var args = ConnectionRefusedContent.Deserialize(package.Content); OnConnectionRefused(args); break; case BaseCommand.ServerClosed: var content = ServerClosedContent.Deserialize(package.Content); ResetClient(); OnServerClosed(content); break; } }
/// <summary> /// Raised when the server refuses the connection of a client. /// </summary> /// <param name="content">Describes why the client has been refused.</param> protected virtual void OnConnectionRefused(ConnectionRefusedContent content) { Pusher.Push(content); }