* XMLData now contains a GetData() method for returning data from the stored data collection. * XMLData now contains a Load() method for loading a previously saved XML data file. * StandardCharacter now automatically generates a filename. * StandardCharacter.Connected added. Use Connected to check if they are connected to the server regardless of the values for Enabled and LoggedIn. LoggedIn is now true once the Login command is completed. * Default Character Role is now Player. * Server.ServerOwner property added. When a character is logged in matching the ServerOwner name, it will automatically be assigned the Admin role. * StandardCharacter.ExecuteSilentCommand() method added for executing a command and not having the "Command: " line printed to the screen when the command is completed. Useful for daisy chained commands. * StandardCharacter login code is now 100% completed. Including save/load code and new character creation. * StandardCharacter.SetRole() method added. Admins can set the role of any other character in the game if they want to. * BaseScript & StandardCharacter now have their Load() code fully implemented. They can save and load their files now. * Player creation command added. Can only be executed from within the login command. If it is executed from any other object it will bail. * Stop command now only works when a Character with Role = Admin issues the command. During development of a MUD Game, this would typically be the Server.ServerOwner character who will have Admin rights. * ConnectionManager had some bugs fixed such as not removing Threads from the Thread collection when a character disconnected. Also re-organized the character connection code some.
109 lines
3.6 KiB
C#
109 lines
3.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Threading;
|
|
|
|
using MudEngine.Game;
|
|
using MudEngine.Game.Characters;
|
|
|
|
namespace MudEngine.Networking
|
|
{
|
|
public class ConnectionManager
|
|
{
|
|
//Collection of currently connected players.
|
|
internal List<Thread> _ConnectedThreads;
|
|
internal List<StandardCharacter> _ConnectedCharacters;
|
|
|
|
public ConnectionManager(Int32 maxPlayers)
|
|
{
|
|
this._ConnectedCharacters = new List<StandardCharacter>(maxPlayers);
|
|
this._ConnectedThreads = new List<Thread>(maxPlayers);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new character for the player and sets it up on the server.
|
|
/// </summary>
|
|
/// <param name="game"></param>
|
|
/// <param name="connection"></param>
|
|
public void AddConnection(StandardGame game, Socket connection)
|
|
{
|
|
//Instance a new character and provide it with the Socket.
|
|
StandardCharacter character = new StandardCharacter(game, "New Player", "New networked client.", connection);
|
|
|
|
//Invoke the Characters Server connection method
|
|
this._ConnectedCharacters.Add(character);
|
|
this._ConnectedThreads.Add(new Thread(ReceiveDataThread));
|
|
|
|
Int32 index = this._ConnectedThreads.Count - 1;
|
|
this._ConnectedThreads[index].Start(index);
|
|
}
|
|
|
|
public StandardCharacter[] GetConnectedCharacters()
|
|
{
|
|
return this._ConnectedCharacters.ToArray();
|
|
}
|
|
|
|
private void ReceiveDataThread(Object index)
|
|
{
|
|
StandardCharacter character = this._ConnectedCharacters[(Int32)index];
|
|
|
|
character.Initialize();
|
|
character.Connect();
|
|
|
|
while (character.Game.Server.Status == ServerStatus.Running &&
|
|
character.Connected)
|
|
{
|
|
try
|
|
{
|
|
character.ExecuteCommand(character.GetInput());
|
|
}
|
|
catch
|
|
{
|
|
RemoveConnection(character);
|
|
}
|
|
}
|
|
|
|
RemoveConnection(character);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes the specified player character from the server.
|
|
/// </summary>
|
|
/// <param name="character"></param>
|
|
public void RemoveConnection(StandardCharacter character)
|
|
{
|
|
character.Save(character.Filename);
|
|
character.Disconnect();
|
|
foreach (StandardCharacter c in this._ConnectedCharacters)
|
|
{
|
|
if (c.ID == character.ID)
|
|
{
|
|
Int32 index = _ConnectedCharacters.IndexOf(c);
|
|
this._ConnectedCharacters.Remove(character);
|
|
Thread t = this._ConnectedThreads[index];
|
|
this._ConnectedThreads.Remove(this._ConnectedThreads[index]);
|
|
t.Abort();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Disconnects all of the currently connected clients.
|
|
/// </summary>
|
|
public void DisconnectAll()
|
|
{
|
|
for (Int32 i = 0; i < this._ConnectedCharacters.Count; i++)
|
|
{
|
|
this._ConnectedCharacters[i].Disconnect();
|
|
this._ConnectedThreads[i].Abort();
|
|
}
|
|
|
|
this._ConnectedCharacters.Clear();
|
|
this._ConnectedThreads.Clear();
|
|
}
|
|
}
|
|
}
|