removed clientId

This commit is contained in:
shinichi
2020-09-25 12:51:54 +02:00
parent dded1a5b24
commit 2139fcf2b2
3 changed files with 20 additions and 67 deletions

View File

@@ -11,7 +11,6 @@ namespace Client
private byte[] buffer = new byte[1024];
private int bytesReceived;
private bool connected;
private byte clientId = 0;
public Client() : this("localhost", 5555)
@@ -41,7 +40,7 @@ namespace Client
Console.WriteLine("enter password");
string password = Console.ReadLine();
byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(username, password), this.clientId);
byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(username, password));
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
@@ -109,7 +108,7 @@ namespace Client
{
throw new ArgumentNullException("no bytes");
}
byte[] message = DataParser.GetRawDataMessage(bytes, clientId);
byte[] message = DataParser.GetRawDataMessage(bytes);
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
}
@@ -119,7 +118,7 @@ namespace Client
{
throw new ArgumentNullException("no bytes");
}
byte[] message = DataParser.GetRawDataMessage(bytes, clientId);
byte[] message = DataParser.GetRawDataMessage(bytes);
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
}

View File

@@ -36,7 +36,7 @@ namespace Client
/// <returns>if it sucseeded</returns>
public static bool getJsonIdentifier(byte[] bytes, out string identifier)
{
if (bytes.Length <= 6)
if (bytes.Length <= 5)
{
throw new ArgumentException("bytes to short");
}
@@ -44,7 +44,7 @@ namespace Client
if (messageId == 1)
{
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(bytes.Skip(6).ToArray()));
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(bytes.Skip(5).ToArray()));
identifier = json.identifier;
return true;
}
@@ -62,7 +62,7 @@ namespace Client
/// <returns>if message contains raw data</returns>
public static bool isRawData(byte[] bytes)
{
if (bytes.Length <= 6)
if (bytes.Length <= 5)
{
throw new ArgumentException("bytes to short");
}
@@ -76,14 +76,13 @@ namespace Client
/// <param name="messageId"></param>
/// <param name="clientId"></param>
/// <returns>the message ready for sending</returns>
private static byte[] getMessage(byte[] payload, byte messageId, byte clientId)
private static byte[] getMessage(byte[] payload, byte messageId)
{
byte[] res = new byte[payload.Length + 6];
byte[] res = new byte[payload.Length + 5];
Array.Copy(BitConverter.GetBytes(payload.Length + 6), 0, res, 0, 4);
Array.Copy(BitConverter.GetBytes(payload.Length + 5), 0, res, 0, 4);
res[4] = messageId;
res[5] = clientId;
Array.Copy(payload, 0, res, 6, payload.Length);
Array.Copy(payload, 0, res, 5, payload.Length);
return res;
}
@@ -94,9 +93,9 @@ namespace Client
/// <param name="payload"></param>
/// <param name="clientId"></param>
/// <returns>the message ready for sending</returns>
public static byte[] GetRawDataMessage(byte[] payload, byte clientId)
public static byte[] GetRawDataMessage(byte[] payload)
{
return getMessage(payload, 0x02, clientId);
return getMessage(payload, 0x02);
}
/// <summary>
@@ -105,9 +104,9 @@ namespace Client
/// <param name="payload"></param>
/// <param name="clientId"></param>
/// <returns>the message ready for sending</returns>
public static byte[] getJsonMessage(byte[] payload, byte clientId)
public static byte[] getJsonMessage(byte[] payload)
{
return getMessage(payload, 0x01, clientId);
return getMessage(payload, 0x01);
}
/// <summary>
@@ -116,9 +115,9 @@ namespace Client
/// <param name="message"></param>
/// <param name="clientId"></param>
/// <returns>the message ready for sending</returns>
public static byte[] getJsonMessage(string message, byte clientId)
public static byte[] getJsonMessage(string message)
{
return getJsonMessage(Encoding.ASCII.GetBytes(message), clientId);
return getJsonMessage(Encoding.ASCII.GetBytes(message));
}

View File

@@ -13,7 +13,6 @@ namespace Server
private NetworkStream stream;
private byte[] buffer = new byte[1024];
private byte[] totalBuffer = new byte[1024];
private int bytesReceived;
private int totalBufferReceived = 0;
@@ -30,7 +29,6 @@ namespace Server
private void OnRead(IAsyncResult ar)
{
int receivedBytes = this.stream.EndRead(ar);
byte[] lengthBytes = new byte[4];
if (totalBufferReceived + receivedBytes > 1024)
{
@@ -45,7 +43,7 @@ namespace Server
{
//volledig packet binnen
byte[] packetBytes = new byte[expectedMessageLength];
Array.Copy(totalBuffer, 6, packetBytes, 0, expectedMessageLength - 6);
Array.Copy(totalBuffer, 5, packetBytes, 0, expectedMessageLength - 5);
Console.WriteLine(Encoding.ASCII.GetString(packetBytes) + " " + expectedMessageLength);
@@ -57,59 +55,16 @@ namespace Server
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
}
/*
Array.Copy(this.buffer, 0, lengthBytes, 0, 4);
int expectedMessageLength = BitConverter.ToInt32(lengthBytes);
if (expectedMessageLength > this.buffer.Length)
{
throw new OutOfMemoryException("buffer to small");
}
if (expectedMessageLength > this.bytesReceived + receivedBytes)
{
//message hasn't completely arrived yet
this.bytesReceived += receivedBytes;
Console.WriteLine("segmented message, {0} arrived", receivedBytes);
this.stream.BeginRead(this.buffer, this.bytesReceived, this.buffer.Length - this.bytesReceived, new AsyncCallback(OnRead), null);
}
else
{
//message completely arrived
if (expectedMessageLength != this.bytesReceived + receivedBytes)
{
Console.WriteLine("something has gone completely wrong");
Console.WriteLine($"expected: {expectedMessageLength} bytesReceive: {bytesReceived} receivedBytes: {receivedBytes}");
Console.WriteLine($"received WEIRD data {BitConverter.ToString(buffer.Take(receivedBytes).ToArray())} string {Encoding.ASCII.GetString(buffer.Take(receivedBytes).ToArray())}");
}
else if (buffer[4] == 0x02)
{
Console.WriteLine($"received raw data {BitConverter.ToString(buffer.Skip(5).Take(expectedMessageLength).ToArray())}");
}
else if (buffer[4] == 0x01)
{
byte[] packet = new byte[expectedMessageLength];
Console.WriteLine(Encoding.ASCII.GetString(buffer) + " " + expectedMessageLength);
Array.Copy(buffer, 5, packet, 0, expectedMessageLength - 5);
Console.WriteLine(Encoding.ASCII.GetString(packet));
HandleData(Encoding.ASCII.GetString(packet));
}
this.bytesReceived = 0;
}
*/
this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null);
}
private void HandleData(string packet)
private void HandleData(byte[] message)
{
Console.WriteLine("Data " + packet);
JsonConvert.DeserializeObject(packet);
//Console.WriteLine("Data " + packet);
//JsonConvert.DeserializeObject(packet);
}
}
}