- Fixed server

- Works for multiplayer now, exit doesn't shut down everything.
- Less memory usage.
- TODO: If startup message, handle it.
This commit is contained in:
u8sand_cp 2010-07-30 16:41:54 -07:00
parent e822537128
commit 7f39821216
3 changed files with 21 additions and 10 deletions

View file

@ -17,7 +17,10 @@ namespace MudEngine.Commands
public CommandResults Execute(string command, BaseCharacter player) public CommandResults Execute(string command, BaseCharacter player)
{ {
player.ActiveGame.Shutdown(); if (player.ActiveGame.IsMultiplayer)
player.Disconnect();
else
player.ActiveGame.Shutdown();
return new CommandResults(); return new CommandResults();
} }

View file

@ -119,7 +119,7 @@ namespace MudEngine.GameObjects.Characters
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
Send(encoding.GetBytes(str)); Send(encoding.GetBytes(str));
if (!ActiveGame.IsRunning) if (!ActiveGame.IsRunning)
Clear(); Disconnect();
} }
internal void Send(byte[] data) internal void Send(byte[] data)
@ -130,10 +130,10 @@ namespace MudEngine.GameObjects.Characters
} }
catch (Exception) // error, connection failed: close client catch (Exception) // error, connection failed: close client
{ {
Clear(); Disconnect();
} }
} }
internal void Clear() internal void Disconnect()
{ {
// TODO: Save(); // TODO: Save();
Save(); Save();
@ -142,6 +142,7 @@ namespace MudEngine.GameObjects.Characters
client.Close(); client.Close();
// TODO: Reset game so it can be used again // TODO: Reset game so it can be used again
} }
internal Socket client; internal Socket client;
} }
} }

View file

@ -101,17 +101,23 @@ namespace MudEngine.Networking
{ {
byte[] buf = new byte[1]; byte[] buf = new byte[1];
int recved = players[sub].client.Receive(buf); int recved = players[sub].client.Receive(buf);
if (recved > 0) if (recved > 0)
buffer.Add(buf[0]);
else if (buffer.Count > 0)
{ {
players[sub].Send(buffer.ToArray()); if (buf[0] == '\n' && buffer.Count > 0)
buffer.Clear(); {
if (buffer[buffer.Count-1] == '\r')
buffer.RemoveAt(buffer.Count-1);
players[sub].Receive(buffer.ToArray());
buffer.Clear();
}
else
buffer.Add(buf[0]);
} }
} }
catch (Exception) // error receiving, close player catch (Exception) // error receiving, close player
{ {
players[sub].Clear(); this.Disconnect(sub);
} }
} }
} }
@ -120,7 +126,8 @@ namespace MudEngine.Networking
if (sub > 0 && sub < players./*Capacity*/Length) if (sub > 0 && sub < players./*Capacity*/Length)
{ {
clientThreads[sub].Abort(); clientThreads[sub].Abort();
players[sub].Clear(); if(players[sub].IsActive)
players[sub].Disconnect();
} }
} }