Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b9ad2540c | ||
|
|
5a425bf19b | ||
|
|
8e24274261 | ||
|
|
8c45d5eccd | ||
|
|
5db3f28deb |
@@ -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))
|
||||
|
||||
@@ -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];
|
||||
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user