client stuff
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Linq;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
|
|
||||||
namespace Client
|
namespace Client
|
||||||
@@ -6,14 +8,17 @@ namespace Client
|
|||||||
class Client
|
class Client
|
||||||
{
|
{
|
||||||
private TcpClient client;
|
private TcpClient client;
|
||||||
|
private NetworkStream stream;
|
||||||
|
private byte[] buffer = new byte[1024];
|
||||||
|
private int bytesReceived;
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Hello World!");
|
Console.WriteLine("Hello World!");
|
||||||
//connect fiets
|
//connect fiets?
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Client client = new Client();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,7 +30,75 @@ namespace Client
|
|||||||
public Client(string adress, int port)
|
public Client(string adress, int port)
|
||||||
{
|
{
|
||||||
this.client = new TcpClient();
|
this.client = new TcpClient();
|
||||||
client.BeginConnect(adress, 15243, new AsyncCallback(OnConnect), null);
|
this.bytesReceived = 0;
|
||||||
|
client.BeginConnect(adress, port, new AsyncCallback(OnConnect), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnConnect(IAsyncResult ar)
|
||||||
|
{
|
||||||
|
this.client.EndConnect(ar);
|
||||||
|
Console.WriteLine("Verbonden!");
|
||||||
|
this.stream = this.client.GetStream();
|
||||||
|
|
||||||
|
//TODO File in lezen
|
||||||
|
Console.WriteLine("enter username");
|
||||||
|
string username = Console.ReadLine();
|
||||||
|
Console.WriteLine("enter password");
|
||||||
|
string password = Console.ReadLine();
|
||||||
|
|
||||||
|
byte[] payload = DataParser.GetLoginJson(username, password);
|
||||||
|
|
||||||
|
this.stream.BeginWrite(payload, 0, payload.Length, new AsyncCallback(onWrite), null);
|
||||||
|
|
||||||
|
this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(onRead), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onRead(IAsyncResult ar)
|
||||||
|
{
|
||||||
|
int receivedBytes = this.stream.EndRead(ar);
|
||||||
|
byte[] lengthBytes = new byte[4];
|
||||||
|
|
||||||
|
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;
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
string identifier;
|
||||||
|
bool isJson = DataParser.getJsonIdentifier(this.buffer, out identifier);
|
||||||
|
if (isJson)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
else if (DataParser.isRawData(this.buffer))
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onWrite(IAsyncResult ar)
|
||||||
|
{
|
||||||
|
this.stream.EndWrite(ar);
|
||||||
|
//stuff idk
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
58
Client/DataParser.cs
Normal file
58
Client/DataParser.cs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Client
|
||||||
|
{
|
||||||
|
class DataParser
|
||||||
|
{
|
||||||
|
public static byte[] GetLoginJson(string mUsername, string mPassword)
|
||||||
|
{
|
||||||
|
dynamic json = new
|
||||||
|
{
|
||||||
|
identifier = "LOGIN",
|
||||||
|
data = new
|
||||||
|
{
|
||||||
|
username = mUsername,
|
||||||
|
password = mPassword,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool getJsonIdentifier(byte[] bytes, out string identifier)
|
||||||
|
{
|
||||||
|
if (bytes.Length <= 5)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("bytes to short");
|
||||||
|
}
|
||||||
|
byte messageId = bytes[4];
|
||||||
|
|
||||||
|
if (messageId == 1)
|
||||||
|
{
|
||||||
|
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(bytes.Skip(5).ToArray()));
|
||||||
|
identifier = json.identifier;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
identifier = "";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool isRawData(byte[] bytes)
|
||||||
|
{
|
||||||
|
if (bytes.Length <= 5)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("bytes to short");
|
||||||
|
}
|
||||||
|
return bytes[5] == 0x02;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user