* 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.
113 lines
3.2 KiB
C#
113 lines
3.2 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Threading;
|
|
|
|
using MudEngine.Core;
|
|
using MudEngine.Core.Interfaces;
|
|
using MudEngine.Game;
|
|
using MudEngine.Game.Characters;
|
|
|
|
namespace MudEngine.Networking
|
|
{
|
|
public enum ServerStatus
|
|
{
|
|
Stopped = 0,
|
|
Starting = 1,
|
|
Running = 2,
|
|
}
|
|
|
|
[Category("Networking")]
|
|
public class Server
|
|
{
|
|
public ServerStatus Status { get; private set; }
|
|
|
|
public Int32 Port { get; private set; }
|
|
|
|
public Int32 MaxConnections { get; private set; }
|
|
|
|
public Int32 MaxQueuedConnections { get; private set; }
|
|
|
|
public ConnectionManager ConnectionManager { get; private set; }
|
|
|
|
public Boolean Enabled { get; private set; }
|
|
|
|
public String MOTD { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or Sets the owning Character that has access to all Admin commands.
|
|
/// Only the ServerOwner can change the Roles of other Characters. If there
|
|
/// is no ServerOwner specified then other Characters can not have their Roles
|
|
/// elevated from that of Player.
|
|
/// </summary>
|
|
public String ServerOwner { get; set; }
|
|
|
|
public Server(StandardGame game, Int32 port)
|
|
{
|
|
this.Port = port;
|
|
this.Status = ServerStatus.Stopped;
|
|
this.MaxConnections = 100;
|
|
this.MaxQueuedConnections = 10;
|
|
|
|
this._Game = game;
|
|
this._Server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
}
|
|
|
|
public void Start(Int32 maxConnections, Int32 maxQueueSize)
|
|
{
|
|
if (this.Status != ServerStatus.Stopped)
|
|
return;
|
|
|
|
this.Status = ServerStatus.Starting;
|
|
|
|
this.MaxConnections = maxConnections;
|
|
this.ConnectionManager = new ConnectionManager(this.MaxConnections);
|
|
|
|
try
|
|
{
|
|
IPEndPoint ip = new IPEndPoint(IPAddress.Any, this.Port);
|
|
this._Server.Bind(ip);
|
|
this._Server.Listen(this.MaxQueuedConnections);
|
|
|
|
this.Status = ServerStatus.Running;
|
|
this.Enabled = true;
|
|
|
|
this._ServerThread = new Thread(ServerLoop);
|
|
this._ServerThread.Start();
|
|
}
|
|
catch
|
|
{
|
|
this.Status = ServerStatus.Stopped;
|
|
}
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
this.ConnectionManager.DisconnectAll();
|
|
|
|
this._ServerThread.Abort();
|
|
|
|
this._Server.Close();
|
|
this._Server = null;
|
|
|
|
this.Enabled = false;
|
|
this.Status = ServerStatus.Stopped;
|
|
}
|
|
|
|
private void ServerLoop()
|
|
{
|
|
while (this.Status == ServerStatus.Running)
|
|
{
|
|
this.ConnectionManager.AddConnection(this._Game, this._Server.Accept());
|
|
}
|
|
}
|
|
|
|
private StandardGame _Game;
|
|
private Socket _Server;
|
|
private Thread _ServerThread;
|
|
}
|
|
}
|