Few Fixes

- Receives junk sent by telnet client upon established connection
- Fixed up some problems with ReadInput
- Fixed up Disconnect
- Replaced some Log() calls.
- Commented out Initialize on a new thread, will be uncommented when CommandEngine is fixed.
- CommandEngine doesn't want to do things while other things are happening even though they are on different threads... Fix that?
This commit is contained in:
u8sand_cp 2010-08-01 20:07:29 -07:00
parent bc05eba56e
commit b3a672503f
3 changed files with 23 additions and 49 deletions

View file

@ -32,7 +32,7 @@ namespace MudEngine.Commands
foreach (BaseCharacter bc in player.ActiveGame.PlayerCollection) foreach (BaseCharacter bc in player.ActiveGame.PlayerCollection)
{ {
if (bc.Name == input) if (bc.Name == input) // TODO: Check if that player is still online
{ {
player.Send("Character name already taken."); player.Send("Character name already taken.");
foundName = true; foundName = true;

View file

@ -140,6 +140,9 @@ namespace MudEngine.GameObjects.Characters
internal void Initialize() internal void Initialize()
{ {
client.Receive(new byte[255]);
Log.Write("New Player Connected.");
string result = ExecuteCommand("Login"); string result = ExecuteCommand("Login");
@ -148,7 +151,7 @@ namespace MudEngine.GameObjects.Characters
} }
internal void Receive(string data) internal void Receive(string data)
{ {
ExecuteCommand(data); data = ExecuteCommand(data);
Send(data); Send(data);
if (!ActiveGame.IsRunning) if (!ActiveGame.IsRunning)
Disconnect(); Disconnect();
@ -169,7 +172,7 @@ namespace MudEngine.GameObjects.Characters
client.Send(encoding.GetBytes(data)); client.Send(encoding.GetBytes(data));
} }
catch (Exception) // error, connection failed: close client catch (Exception)
{ {
Disconnect(); Disconnect();
} }
@ -186,13 +189,17 @@ namespace MudEngine.GameObjects.Characters
internal void Disconnect() internal void Disconnect()
{ {
string filePath = Path.Combine(ActiveGame.DataPaths.Players, Filename); if (IsActive)
this.Save(filePath); {
string filePath = Path.Combine(ActiveGame.DataPaths.Players, Filename);
this.Save(filePath);
Send("Disconnecting..."); IsActive = false;
IsActive = false; client.Close();
client.Close(); // TODO: Reset game so it can be used again
// TODO: Reset game so it can be used again
Log.Write("Player " + this.Name + " disconnected.");
}
} }
internal string ReadInput() internal string ReadInput()
{ {
@ -212,20 +219,7 @@ namespace MudEngine.GameObjects.Characters
buffer.RemoveAt(buffer.Count-1); buffer.RemoveAt(buffer.Count-1);
String str; String str;
List<byte> correctedBuffer = new List<byte>();
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding(); System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
/*
foreach (byte i in buffer)
{
if (i == 255)
continue;
else if (i == 251)
continue;
else
correctedBuffer.Add(i);
}
*/
str = enc.GetString(buffer.ToArray()); str = enc.GetString(buffer.ToArray());
return str; return str;
} }
@ -233,9 +227,10 @@ namespace MudEngine.GameObjects.Characters
buffer.Add(buf[0]); buffer.Add(buf[0]);
} }
} }
catch (Exception) // error receiving, close player catch (Exception e)
{ {
Disconnect(); Disconnect();
return e.Message;
} }
} }
} }

View file

@ -9,12 +9,6 @@ using System.Threading;
using MudEngine.GameObjects.Characters; using MudEngine.GameObjects.Characters;
using MudEngine.GameManagement; using MudEngine.GameManagement;
/* Usage:
* Server MUDServer = new Server();
* MUDServer.InitializeUDP(666); or MUDServer.InitializeTCP(666);
* MUDServer.Start();
*/
namespace MudEngine.Networking namespace MudEngine.Networking
{ {
public class Server public class Server
@ -30,7 +24,7 @@ namespace MudEngine.Networking
stage = 0; stage = 0;
port = 0; port = 0;
} }
public bool Initialize(int p, ref /*List<BaseCharacter>*/BaseCharacter[] pbs) public bool Initialize(int p, ref BaseCharacter[] pbs)
{ {
if (stage != 0) if (stage != 0)
return false; return false;
@ -74,7 +68,7 @@ namespace MudEngine.Networking
int sub = -1; int sub = -1;
do do
{ {
for (int i = 0; i < players./*Count*/Length; i++) for (int i = 0; i < players.Length; i++)
{ {
if (!players[i].IsActive) if (!players[i].IsActive)
{ {
@ -85,8 +79,6 @@ namespace MudEngine.Networking
} while (sub < 0); } while (sub < 0);
players[sub].client = server.Accept(); players[sub].client = server.Accept();
players[sub].Initialize(); players[sub].Initialize();
Log.Write("New Player Connected.");
//ParameterizedThreadStart start = new ParameterizedThreadStart(ReceiveThread);
clientThreads[sub] = new Thread(ReceiveThread); clientThreads[sub] = new Thread(ReceiveThread);
clientThreads[sub].Start((object)sub); clientThreads[sub].Start((object)sub);
} }
@ -94,30 +86,19 @@ namespace MudEngine.Networking
private void ReceiveThread(object obj) private void ReceiveThread(object obj)
{ {
int sub = (int)obj; int sub = (int)obj;
List<byte> buffer = new List<byte>(); //players[sub].Initialize();
while (stage == 2 && players[sub].IsActive) while (stage == 2 && players[sub].IsActive)
{ {
try players[sub].Receive(players[sub].ReadInput());
{
players[sub].Receive(players[sub].ReadInput());
}
catch (Exception) // error receiving, close player
{
this.Disconnect(sub);
}
} }
} }
public void Disconnect(int sub) public void Disconnect(int sub)
{ {
if (sub > 0 && sub < players./*Capacity*/Length) if (sub > 0 && sub < players.Length)
{ {
clientThreads[sub].Abort(); clientThreads[sub].Abort();
if (players[sub].IsActive) if (players[sub].IsActive)
{
Log.Write("Disconnecting player " + players[sub].Name);
players[sub].Disconnect(); players[sub].Disconnect();
Log.Write("Player disconnected.");
}
} }
} }
@ -126,10 +107,8 @@ namespace MudEngine.Networking
private int stage; private int stage;
private int port; private int port;
//List<BaseCharacter> players;
BaseCharacter[] players; BaseCharacter[] players;
// TCP Stuff:
private Thread[] clientThreads; private Thread[] clientThreads;
} }
} }