muddesigner/MudEngine/Communication/Server.cs
Scionwest_cp 3bd493c1cd Mud Designer:
Removed old forms and prepared for new designer layouts

MudEngine:
Server - Now starts and runs, however sending information to the client crashes telnet clients for some reason.
BaseCharacter - Removed OnConnect() source code and changed method into an abstract method.  This is now implemented via MudGame.MudCharacter
BaseCommand - BaseCommand.Name now implemented.  Constructor defaults to the Type name.  Note all command names must begin with 'Command'
BaseGame - Now has a Server propety
BaseServer - Added IsRunning property
ICharacter - Added Send() method
ICommunicate - Added Shutdown() method for use with servers.
CommandSystem - Bug fix.  Was checking to see if the command was an Interface rather than checking if it inherits from one.
CommandSystem - Bug fix.  No longer attempts to add abstract commands to collection
ScriptSystem - Bug fix.  Corrected the name of the Mud Compiler during compiler loading.

MudGame:
Added initial MudCharacter class.  Will be the standard class that all characters will inherit from.
Added CommandLogin for processing user server logins.
MudGame - Now calls Server.Initialize during Game.Initialize()
MudGame - Uses Server.IsRunning property checks rather than Server.EnableServer
MudGame - Added initial code for player connections.
2011-10-02 20:40:51 -07:00

94 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using MudEngine.Core;
namespace MudEngine.Communication
{
public class Server : BaseServer
{
//Listens for incoming connections
private TcpListener _Listener;
//Dedicated thread for accepting and processing new connections
private Thread _Connect;
private Dictionary<BaseCharacter, Thread> _ClientCollection;
/// <summary>
/// Server constructor. Requires a currently active game to start.
/// </summary>
/// <param name="game"></param>
public Server(BaseGame game) : base(game)
{
this._Listener = new TcpListener(IPAddress.Any, 555);
this._ClientCollection = new Dictionary<BaseCharacter, Thread>();
}
/// <summary>
/// Server constructor. Requires a currently active game and desired port number to start.
/// </summary>
/// <param name="game"></param>
/// <param name="port"></param>
public Server(BaseGame game, int port)
: base(game)
{
this._Listener = new TcpListener(IPAddress.Any, port);
this._ClientCollection = new Dictionary<BaseCharacter, Thread>();
}
//Listens for incoming client connections.
private void ListenForConnections()
{
this._Listener.Start();
while (true)
{
TcpClient client = this._Listener.AcceptTcpClient();
//Someone connects, send them to a new thread.
Thread newClient = new Thread(new ParameterizedThreadStart(OnConnect));
newClient.Start(client);
}
}
public override void Initialize()
{
this._Connect = new Thread(new ThreadStart(ListenForConnections));
this._Connect.Start();
}
public override void Shutdown()
{
}
public override void OnConnect(object client)
{
TcpClient c = (TcpClient)client;
NetworkStream ns = c.GetStream();
ASCIIEncoding encoder = new ASCIIEncoding();
ns.Write(encoder.GetBytes("WELCOME!!!!"), 0, "WELCOME!!!!".Length);
ns.Flush();
// this.ActiveGame.OnConnect((TcpClient)client);
}
public override void OnDisconnect(object client)
{
throw new NotImplementedException();
}
public override void RecieveData(string data)
{
//TODO: command needs to be executed.
}
public override void SendData(string data)
{
}
}
}