5 Commits
wpf ... write

Author SHA1 Message Date
fabjuuuh
7b9ad2540c shit 2020-09-25 13:01:04 +02:00
fabjuuuh
5a425bf19b Merge remote-tracking branch 'origin/client' into write 2020-09-23 15:40:25 +02:00
fabjuuuh
8e24274261 Merge remote-tracking branch 'origin/client' into write 2020-09-23 15:38:16 +02:00
fabjuuuh
8c45d5eccd Test 2020-09-23 15:37:23 +02:00
fabjuuuh
5db3f28deb Write 2020-09-23 15:33:50 +02:00
4 changed files with 79 additions and 6 deletions

View File

@@ -85,6 +85,7 @@ namespace Client
bool isJson = DataParser.getJsonIdentifier(this.buffer, out identifier);
if (isJson)
{
throw new NotImplementedException();
}
else if (DataParser.isRawData(this.buffer))

View File

@@ -5,7 +5,7 @@ using System.Text;
namespace Client
{
class DataParser
public class DataParser
{
/// <summary>
/// makes the json object with LOGIN identifier and username and password
@@ -76,7 +76,7 @@ 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)
public static byte[] getMessage(byte[] payload, byte messageId, byte clientId)
{
byte[] res = new byte[payload.Length + 6];

View File

@@ -2,6 +2,8 @@
using System.Linq;
using System.Net.Sockets;
using System.Text;
using Client;
using Newtonsoft;
using Newtonsoft.Json;
namespace Server
@@ -26,6 +28,51 @@ namespace Server
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
}
/*private void OnRead(IAsyncResult ar)
{
try
{
int receivedBytes = stream.EndRead(ar);
}
catch (IOException)
{
communication.Disconnect(this);
return;
}
int counter = 0;
while (buffer.Length > counter)
{
//Console.WriteLine(buffer.Length);
byte[] lenghtBytes = new byte[4];
Array.Copy(buffer, counter, lenghtBytes, 0, 4);
int length = BitConverter.ToInt32(lenghtBytes);
Console.WriteLine(buffer[5]);
if (length == 0)
{
break;
}
else if (buffer[counter + 4] == 0x02)
{
}
else if (buffer[counter + 4] == 0x01)
{
byte[] packet = new byte[length];
Console.WriteLine(Encoding.ASCII.GetString(buffer) + " " + length);
Array.Copy(buffer, counter + 5, packet, 0, length);
Console.WriteLine(Encoding.ASCII.GetString(packet));
HandleData(Encoding.ASCII.GetString(packet));
}
counter += length;
}
Console.WriteLine("Done");
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
}*/
private void OnRead(IAsyncResult ar)
{
int receivedBytes = this.stream.EndRead(ar);
@@ -60,13 +107,13 @@ namespace Server
}
else if (buffer[4] == 0x02)
{
Console.WriteLine($"received raw data {BitConverter.ToString(buffer.Skip(5).Take(expectedMessageLength).ToArray())}");
Console.WriteLine($"received raw data {BitConverter.ToString(buffer.Skip(6).ToArray(), 16)}");
}
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);
Array.Copy(buffer, 6, packet, 0, expectedMessageLength - 6);
Console.WriteLine(Encoding.ASCII.GetString(packet));
HandleData(Encoding.ASCII.GetString(packet));
}
@@ -81,7 +128,28 @@ namespace Server
private void HandleData(string packet)
{
Console.WriteLine("Data " + packet);
JsonConvert.DeserializeObject(packet);
dynamic json = JsonConvert.DeserializeObject(packet);
Console.WriteLine("Name: "+json.data.username + "Password: "+json.data.password);
if (json.data.username == json.data.password)
{
dynamic payload = new
{
data = new
{
status = "ok"
}
};
Message.Message message = new Message.Message(Message.Identifier.LOGIN, JsonConvert.SerializeObject(payload));
Write(message.Serialize());
}
}
private void Write(string data)
{
byte[] bytes = DataParser.getMessage(Encoding.ASCII.GetBytes(data), 0x01, 0x01);
stream.Write(bytes, 0, bytes.Length);
stream.Flush();
Console.WriteLine("Wrote message");
}
}
}

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
@@ -9,4 +9,8 @@
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Client\Client.csproj" />
</ItemGroup>
</Project>