* Re-wrote the Server code again. Now much more reliable and passes all connections to the ConnectionManager properly.
* StandardCharacter re-wrote to support the new Server code. Also added event method support for various states. * ConnectionManager re-wrote to support the new server. * Work on Log message importance started * INetworked.Connect now requires a Socket as its parameter. * StandardGame no longer has Properties for MaxConnections and MaxQueuedConnections. This is handle via StandardGame.Start() parameters. * CommandLogin command added. Initial check-in and not fully implemented.
This commit is contained in:
parent
5b141c62ed
commit
27f7e31772
12 changed files with 314 additions and 163 deletions
|
@ -12,54 +12,94 @@ using MudEngine.Game.Characters;
|
|||
|
||||
namespace MudEngine.Networking
|
||||
{
|
||||
public static class ConnectionManager
|
||||
public class ConnectionManager
|
||||
{
|
||||
//Collection of currently connected players.
|
||||
public static List<StandardCharacter> Connections { get; set; }
|
||||
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 static void AddConnection(StandardGame game, Socket connection)
|
||||
public void AddConnection(StandardGame game, Socket connection)
|
||||
{
|
||||
//Exception checking.
|
||||
if (Connections == null)
|
||||
Connections = new List<StandardCharacter>();
|
||||
|
||||
//Instance a new character and provide it with the Socket.
|
||||
StandardCharacter character = new StandardCharacter(game, "New Player", "New networked client.", connection);
|
||||
|
||||
//Add it to the Connections collection
|
||||
Connections.Add(character);
|
||||
|
||||
//Invoke the Characters Server connection method
|
||||
character.Connect();
|
||||
character.Connect(connection);
|
||||
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();
|
||||
|
||||
while (character.Game.Server.Status == ServerStatus.Running &&
|
||||
character.Enabled)
|
||||
{
|
||||
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 static void RemoveConnection(StandardCharacter character)
|
||||
public void RemoveConnection(StandardCharacter character)
|
||||
{
|
||||
Connections.Remove(character);
|
||||
character.Disconnect();
|
||||
foreach (StandardCharacter c in this._ConnectedCharacters)
|
||||
{
|
||||
if (c.ID == character.ID)
|
||||
{
|
||||
Int32 index = _ConnectedCharacters.IndexOf(c);
|
||||
this._ConnectedCharacters.Remove(character);
|
||||
this._ConnectedThreads[index].Abort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disconnects all of the currently connected clients.
|
||||
/// </summary>
|
||||
public static void DisconnectAll()
|
||||
public void DisconnectAll()
|
||||
{
|
||||
if (Connections == null)
|
||||
return;
|
||||
|
||||
foreach (StandardCharacter character in Connections)
|
||||
for (Int32 i = 0; i < this._ConnectedCharacters.Count; i++)
|
||||
{
|
||||
character.Disconnect();
|
||||
this._ConnectedCharacters[i].Disconnect();
|
||||
this._ConnectedThreads[i].Abort();
|
||||
}
|
||||
Connections.Clear();
|
||||
|
||||
this._ConnectedCharacters.Clear();
|
||||
this._ConnectedThreads.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue