muddesigner/MudEngine/Networking/Server.cs
Scionwest_cp 0f45ecec53 MudEngine:
- Updated all game commands to support the new Player.Send() method. Returning CommandResults is no longer supported.
 - Login command now executes the Look command upon completing login.
 - Look command now supports Rooms DetailDescription property. Use this for multi-line descriptions.
 - Several changes to the Restart command. Still not working fully however, it no longer calls duplicate methods.
 - Fixed Walk command not moving players around.
 - Loading engine commands no longer happens more than once.
 - Added additional Server Console log output
 - BaseCharacter.Send() now correctly displays content to the user and prints the "Command:" message to the player after every command completed.
 - The Server now sets Player.IsControlled to true when a player connects.
 - BaseCharacter.Send() checks if the game is multi-player or not. If it is, it prints to Console, if not then prints to telnet clients.

Mud Offline Example:
 - Updated Zeroth Realm creation to use Room.DetailDescription now that the Look command supports it.
 - Updated the Game loop. No longer needs to print to the console due to the player.Execute() command automatically printing content to the console.

Mud Server:
 - Removed player from server as it was not being used any longer.
2010-08-03 20:20:24 -07:00

115 lines
No EOL
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using MudEngine.GameObjects.Characters;
using MudEngine.GameManagement;
namespace MudEngine.Networking
{
public class Server
{
public Server()
{
stage = 0;
port = 0;
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
~Server()
{
stage = 0;
port = 0;
}
public bool Initialize(int p, ref BaseCharacter[] pbs)
{
if (stage != 0)
return false;
if (p <= 0)
return false;
port = p;
players = pbs;
clientThreads = new Thread[players.Length];
stage++;
return true;
}
public bool Start()
{
try
{
if (stage != 1)
return false;
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, port);
server.Bind(ipep);
server.Listen(10);
stage++;
serverThread = new Thread(ServerThread);
serverThread.Start();
}
catch (Exception)
{
return false;
}
return true;
}
public void EndServer()
{
stage = 0;
serverThread.Abort();
server.Close();
}
private void ServerThread()
{
while (stage == 2)
{
int sub = -1;
do
{
for (int i = 0; i < players.Length; i++)
{
if (!players[i].IsActive)
{
sub = i;
break;
}
}
} while (sub < 0);
players[sub].client = server.Accept();
players[sub].IsActive = true;
players[sub].IsControlled = true;
clientThreads[sub] = new Thread(ReceiveThread);
clientThreads[sub].Start((object)sub);
}
}
private void ReceiveThread(object obj)
{
int sub = (int)obj;
players[sub].Initialize();
while (stage == 2 && players[sub].IsActive)
{
players[sub].Receive(players[sub].ReadInput());
}
}
public void Disconnect(int sub)
{
if (sub > 0 && sub < players.Length)
{
clientThreads[sub].Abort();
if (players[sub].IsActive)
players[sub].Disconnect();
}
}
private Thread serverThread;
private Socket server;
private int stage;
private int port;
BaseCharacter[] players;
private Thread[] clientThreads;
}
}