- Added ReadInput() Method, returns string of what is sent to server.
- Send method receives a string and sends the data - Receive method receives a string instead of bytes now. - Server simply uses ReadInput method.
This commit is contained in:
parent
607bd673a5
commit
942b038b1b
2 changed files with 39 additions and 29 deletions
|
@ -146,28 +146,20 @@ namespace MudEngine.GameObjects.Characters
|
|||
CurrentRoom = ActiveGame.InitialRealm.InitialZone.InitialRoom;
|
||||
IsActive = true;
|
||||
}
|
||||
internal void Receive(byte[] data)
|
||||
internal void Receive(string data)
|
||||
{
|
||||
// convert that data to string
|
||||
String str;
|
||||
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
|
||||
str = enc.GetString(data);
|
||||
|
||||
// execute, and get result
|
||||
str = ExecuteCommand(str);
|
||||
|
||||
// convert the result back to bytes and send it back
|
||||
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
|
||||
Send(encoding.GetBytes(str));
|
||||
ExecuteCommand(data);
|
||||
Send(data);
|
||||
if (!ActiveGame.IsRunning)
|
||||
Disconnect();
|
||||
}
|
||||
|
||||
internal void Send(byte[] data)
|
||||
internal void Send(string data)
|
||||
{
|
||||
try
|
||||
{
|
||||
client.Send(data);
|
||||
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
|
||||
client.Send(encoding.GetBytes(data));
|
||||
}
|
||||
catch (Exception) // error, connection failed: close client
|
||||
{
|
||||
|
@ -183,6 +175,38 @@ namespace MudEngine.GameObjects.Characters
|
|||
client.Close();
|
||||
// TODO: Reset game so it can be used again
|
||||
}
|
||||
internal string ReadInput()
|
||||
{
|
||||
List<byte> buffer = new List<byte>();
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] buf = new byte[1];
|
||||
int recved = client.Receive(buf);
|
||||
|
||||
if (recved > 0)
|
||||
{
|
||||
if (buf[0] == '\n' && buffer.Count > 0)
|
||||
{
|
||||
if (buffer[buffer.Count-1] == '\r')
|
||||
buffer.RemoveAt(buffer.Count-1);
|
||||
|
||||
String str;
|
||||
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
|
||||
str = enc.GetString(buffer.ToArray());
|
||||
return str;
|
||||
}
|
||||
else
|
||||
buffer.Add(buf[0]);
|
||||
}
|
||||
}
|
||||
catch (Exception) // error receiving, close player
|
||||
{
|
||||
Disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal Socket client;
|
||||
}
|
||||
|
|
|
@ -99,21 +99,7 @@ namespace MudEngine.Networking
|
|||
{
|
||||
try
|
||||
{
|
||||
byte[] buf = new byte[1];
|
||||
int recved = players[sub].client.Receive(buf);
|
||||
|
||||
if (recved > 0)
|
||||
{
|
||||
if (buf[0] == '\n' && buffer.Count > 0)
|
||||
{
|
||||
if (buffer[buffer.Count-1] == '\r')
|
||||
buffer.RemoveAt(buffer.Count-1);
|
||||
players[sub].Receive(buffer.ToArray());
|
||||
buffer.Clear();
|
||||
}
|
||||
else
|
||||
buffer.Add(buf[0]);
|
||||
}
|
||||
players[sub].Receive(players[sub].ReadInput());
|
||||
}
|
||||
catch (Exception) // error receiving, close player
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue