- 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;
|
CurrentRoom = ActiveGame.InitialRealm.InitialZone.InitialRoom;
|
||||||
IsActive = true;
|
IsActive = true;
|
||||||
}
|
}
|
||||||
internal void Receive(byte[] data)
|
internal void Receive(string data)
|
||||||
{
|
{
|
||||||
// convert that data to string
|
ExecuteCommand(data);
|
||||||
String str;
|
Send(data);
|
||||||
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));
|
|
||||||
if (!ActiveGame.IsRunning)
|
if (!ActiveGame.IsRunning)
|
||||||
Disconnect();
|
Disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Send(byte[] data)
|
internal void Send(string data)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
client.Send(data);
|
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
|
||||||
|
client.Send(encoding.GetBytes(data));
|
||||||
}
|
}
|
||||||
catch (Exception) // error, connection failed: close client
|
catch (Exception) // error, connection failed: close client
|
||||||
{
|
{
|
||||||
|
@ -183,6 +175,38 @@ 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 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;
|
internal Socket client;
|
||||||
}
|
}
|
||||||
|
|
|
@ -99,21 +99,7 @@ namespace MudEngine.Networking
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
byte[] buf = new byte[1];
|
players[sub].Receive(players[sub].ReadInput());
|
||||||
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]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception) // error receiving, close player
|
catch (Exception) // error receiving, close player
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue