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:
parent
bc05eba56e
commit
b3a672503f
3 changed files with 23 additions and 49 deletions
|
@ -32,7 +32,7 @@ namespace MudEngine.Commands
|
|||
|
||||
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.");
|
||||
foundName = true;
|
||||
|
|
|
@ -140,6 +140,9 @@ namespace MudEngine.GameObjects.Characters
|
|||
|
||||
internal void Initialize()
|
||||
{
|
||||
client.Receive(new byte[255]);
|
||||
Log.Write("New Player Connected.");
|
||||
|
||||
string result = ExecuteCommand("Login");
|
||||
|
||||
|
||||
|
@ -148,7 +151,7 @@ namespace MudEngine.GameObjects.Characters
|
|||
}
|
||||
internal void Receive(string data)
|
||||
{
|
||||
ExecuteCommand(data);
|
||||
data = ExecuteCommand(data);
|
||||
Send(data);
|
||||
if (!ActiveGame.IsRunning)
|
||||
Disconnect();
|
||||
|
@ -169,7 +172,7 @@ namespace MudEngine.GameObjects.Characters
|
|||
|
||||
client.Send(encoding.GetBytes(data));
|
||||
}
|
||||
catch (Exception) // error, connection failed: close client
|
||||
catch (Exception)
|
||||
{
|
||||
Disconnect();
|
||||
}
|
||||
|
@ -185,14 +188,18 @@ namespace MudEngine.GameObjects.Characters
|
|||
}
|
||||
|
||||
internal void Disconnect()
|
||||
{
|
||||
if (IsActive)
|
||||
{
|
||||
string filePath = Path.Combine(ActiveGame.DataPaths.Players, Filename);
|
||||
this.Save(filePath);
|
||||
|
||||
Send("Disconnecting...");
|
||||
IsActive = false;
|
||||
client.Close();
|
||||
// TODO: Reset game so it can be used again
|
||||
|
||||
Log.Write("Player " + this.Name + " disconnected.");
|
||||
}
|
||||
}
|
||||
internal string ReadInput()
|
||||
{
|
||||
|
@ -212,20 +219,7 @@ namespace MudEngine.GameObjects.Characters
|
|||
buffer.RemoveAt(buffer.Count-1);
|
||||
|
||||
String str;
|
||||
List<byte> correctedBuffer = new List<byte>();
|
||||
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());
|
||||
return str;
|
||||
}
|
||||
|
@ -233,9 +227,10 @@ namespace MudEngine.GameObjects.Characters
|
|||
buffer.Add(buf[0]);
|
||||
}
|
||||
}
|
||||
catch (Exception) // error receiving, close player
|
||||
catch (Exception e)
|
||||
{
|
||||
Disconnect();
|
||||
return e.Message;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,12 +9,6 @@ using System.Threading;
|
|||
using MudEngine.GameObjects.Characters;
|
||||
using MudEngine.GameManagement;
|
||||
|
||||
/* Usage:
|
||||
* Server MUDServer = new Server();
|
||||
* MUDServer.InitializeUDP(666); or MUDServer.InitializeTCP(666);
|
||||
* MUDServer.Start();
|
||||
*/
|
||||
|
||||
namespace MudEngine.Networking
|
||||
{
|
||||
public class Server
|
||||
|
@ -30,7 +24,7 @@ namespace MudEngine.Networking
|
|||
stage = 0;
|
||||
port = 0;
|
||||
}
|
||||
public bool Initialize(int p, ref /*List<BaseCharacter>*/BaseCharacter[] pbs)
|
||||
public bool Initialize(int p, ref BaseCharacter[] pbs)
|
||||
{
|
||||
if (stage != 0)
|
||||
return false;
|
||||
|
@ -74,7 +68,7 @@ namespace MudEngine.Networking
|
|||
int sub = -1;
|
||||
do
|
||||
{
|
||||
for (int i = 0; i < players./*Count*/Length; i++)
|
||||
for (int i = 0; i < players.Length; i++)
|
||||
{
|
||||
if (!players[i].IsActive)
|
||||
{
|
||||
|
@ -85,8 +79,6 @@ namespace MudEngine.Networking
|
|||
} while (sub < 0);
|
||||
players[sub].client = server.Accept();
|
||||
players[sub].Initialize();
|
||||
Log.Write("New Player Connected.");
|
||||
//ParameterizedThreadStart start = new ParameterizedThreadStart(ReceiveThread);
|
||||
clientThreads[sub] = new Thread(ReceiveThread);
|
||||
clientThreads[sub].Start((object)sub);
|
||||
}
|
||||
|
@ -94,30 +86,19 @@ namespace MudEngine.Networking
|
|||
private void ReceiveThread(object obj)
|
||||
{
|
||||
int sub = (int)obj;
|
||||
List<byte> buffer = new List<byte>();
|
||||
//players[sub].Initialize();
|
||||
while (stage == 2 && players[sub].IsActive)
|
||||
{
|
||||
try
|
||||
{
|
||||
players[sub].Receive(players[sub].ReadInput());
|
||||
}
|
||||
catch (Exception) // error receiving, close player
|
||||
{
|
||||
this.Disconnect(sub);
|
||||
}
|
||||
}
|
||||
}
|
||||
public void Disconnect(int sub)
|
||||
{
|
||||
if (sub > 0 && sub < players./*Capacity*/Length)
|
||||
if (sub > 0 && sub < players.Length)
|
||||
{
|
||||
clientThreads[sub].Abort();
|
||||
if (players[sub].IsActive)
|
||||
{
|
||||
Log.Write("Disconnecting player " + players[sub].Name);
|
||||
players[sub].Disconnect();
|
||||
Log.Write("Player disconnected.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -126,10 +107,8 @@ namespace MudEngine.Networking
|
|||
private int stage;
|
||||
private int port;
|
||||
|
||||
//List<BaseCharacter> players;
|
||||
BaseCharacter[] players;
|
||||
|
||||
// TCP Stuff:
|
||||
private Thread[] clientThreads;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue