OnRead for client updated
This commit is contained in:
@@ -9,8 +9,9 @@ namespace Client
|
|||||||
private TcpClient client;
|
private TcpClient client;
|
||||||
private NetworkStream stream;
|
private NetworkStream stream;
|
||||||
private byte[] buffer = new byte[1024];
|
private byte[] buffer = new byte[1024];
|
||||||
private int bytesReceived;
|
|
||||||
private bool connected;
|
private bool connected;
|
||||||
|
private byte[] totalBuffer = new byte[1024];
|
||||||
|
private int totalBufferReceived = 0;
|
||||||
|
|
||||||
|
|
||||||
public Client() : this("localhost", 5555)
|
public Client() : this("localhost", 5555)
|
||||||
@@ -21,7 +22,6 @@ namespace Client
|
|||||||
public Client(string adress, int port)
|
public Client(string adress, int port)
|
||||||
{
|
{
|
||||||
this.client = new TcpClient();
|
this.client = new TcpClient();
|
||||||
this.bytesReceived = 0;
|
|
||||||
this.connected = false;
|
this.connected = false;
|
||||||
client.BeginConnect(adress, port, new AsyncCallback(OnConnect), null);
|
client.BeginConnect(adress, port, new AsyncCallback(OnConnect), null);
|
||||||
}
|
}
|
||||||
@@ -54,31 +54,20 @@ namespace Client
|
|||||||
private void OnRead(IAsyncResult ar)
|
private void OnRead(IAsyncResult ar)
|
||||||
{
|
{
|
||||||
int receivedBytes = this.stream.EndRead(ar);
|
int receivedBytes = this.stream.EndRead(ar);
|
||||||
byte[] lengthBytes = new byte[4];
|
|
||||||
|
|
||||||
Array.Copy(this.buffer, 0, lengthBytes, 0, 4);
|
if (totalBufferReceived + receivedBytes > 1024)
|
||||||
|
|
||||||
int expectedMessageLength = BitConverter.ToInt32(lengthBytes);
|
|
||||||
|
|
||||||
if (expectedMessageLength > this.buffer.Length)
|
|
||||||
{
|
{
|
||||||
throw new OutOfMemoryException("buffer to small");
|
throw new OutOfMemoryException("buffer too small");
|
||||||
}
|
}
|
||||||
|
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, receivedBytes);
|
||||||
|
totalBufferReceived += receivedBytes;
|
||||||
|
|
||||||
if (expectedMessageLength > this.bytesReceived + receivedBytes)
|
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||||
|
while (totalBufferReceived >= expectedMessageLength)
|
||||||
{
|
{
|
||||||
//message hasn't completely arrived yet
|
//volledig packet binnen
|
||||||
this.bytesReceived += receivedBytes;
|
byte[] messageBytes = new byte[expectedMessageLength];
|
||||||
this.stream.BeginRead(this.buffer, this.bytesReceived, this.buffer.Length - this.bytesReceived, new AsyncCallback(OnRead), null);
|
Array.Copy(totalBuffer, 0, messageBytes, 0, expectedMessageLength);
|
||||||
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//message completely arrived
|
|
||||||
if (expectedMessageLength != this.bytesReceived + receivedBytes)
|
|
||||||
{
|
|
||||||
Console.WriteLine("something has gone completely wrong");
|
|
||||||
}
|
|
||||||
|
|
||||||
string identifier;
|
string identifier;
|
||||||
bool isJson = DataParser.getJsonIdentifier(this.buffer, out identifier);
|
bool isJson = DataParser.getJsonIdentifier(this.buffer, out identifier);
|
||||||
@@ -90,7 +79,11 @@ namespace Client
|
|||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
totalBufferReceived -= expectedMessageLength;
|
||||||
|
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null);
|
this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ namespace Client
|
|||||||
}
|
}
|
||||||
byte messageId = bytes[4];
|
byte messageId = bytes[4];
|
||||||
|
|
||||||
if (messageId == 1)
|
if (messageId == 0x01)
|
||||||
{
|
{
|
||||||
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(bytes.Skip(5).ToArray()));
|
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(bytes.Skip(5).ToArray()));
|
||||||
identifier = json.identifier;
|
identifier = json.identifier;
|
||||||
|
|||||||
@@ -63,6 +63,14 @@ namespace Server
|
|||||||
{
|
{
|
||||||
//Console.WriteLine("Data " + packet);
|
//Console.WriteLine("Data " + packet);
|
||||||
//JsonConvert.DeserializeObject(packet);
|
//JsonConvert.DeserializeObject(packet);
|
||||||
|
if (message[4] == 0x01)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
else if (message[4] == 0x02)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user