thanks johan
This commit is contained in:
23
RH-Engine/JSONParser.cs
Normal file
23
RH-Engine/JSONParser.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Text;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace RH_Engine
|
||||||
|
{
|
||||||
|
class JSONParser
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// parses the given response from the server into strings
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="msg">the message gotten from the server, without the length prefix</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string[] Parse(string msg)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,38 +5,28 @@ using System.Text;
|
|||||||
|
|
||||||
namespace RH_Engine
|
namespace RH_Engine
|
||||||
{
|
{
|
||||||
class Program
|
internal class Program
|
||||||
{
|
{
|
||||||
static void Main(string[] args)
|
private static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
TcpClient client = new TcpClient("145.48.6.10", 6666);
|
TcpClient client = new TcpClient("145.48.6.10", 6666);
|
||||||
|
|
||||||
WriteTextMessage(client, "{\"id\" : \"session/list\"}");
|
WriteTextMessage(client, "{\r\n\"id\" : \"session/list\"\r\n}");
|
||||||
ReadPrefMessage(client.GetStream());
|
ReadPrefMessage(client.GetStream());
|
||||||
Console.WriteLine("got response " + ReadTextMessage(client));
|
Console.WriteLine("got response " + ReadTextMessage(client));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static void WriteTextMessage(TcpClient client, string message)
|
public static void WriteTextMessage(TcpClient client, string message)
|
||||||
{
|
{
|
||||||
|
|
||||||
byte[] msg = Encoding.ASCII.GetBytes(message);
|
byte[] msg = Encoding.ASCII.GetBytes(message);
|
||||||
byte[] res = new byte[msg.Length + 4];
|
byte[] res = new byte[msg.Length + 4];
|
||||||
|
|
||||||
Array.Copy(GetPacketLength(msg.Length), 0, res, 0, 4);
|
Array.Copy(GetPacketLength(msg.Length), 0, res, 0, 4);
|
||||||
|
|
||||||
Array.Copy(msg, 0, res, 4, msg.Length);
|
Array.Copy(msg, 0, res, 4, msg.Length);
|
||||||
var stream = new StreamWriter(client.GetStream(), Encoding.Unicode);
|
|
||||||
{
|
client.GetStream().Write(res);
|
||||||
stream.Write(res);
|
|
||||||
stream.Flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.WriteLine("sent message " + message);
|
Console.WriteLine("sent message " + message);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string ReadTextMessage(TcpClient client)
|
public static string ReadTextMessage(TcpClient client)
|
||||||
@@ -45,17 +35,16 @@ namespace RH_Engine
|
|||||||
{
|
{
|
||||||
Console.WriteLine("reading...");
|
Console.WriteLine("reading...");
|
||||||
|
|
||||||
|
|
||||||
int length = stream.Read();
|
int length = stream.Read();
|
||||||
Console.WriteLine(length);
|
Console.WriteLine(length);
|
||||||
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] GetPacketLength(int length)
|
private static byte[] GetPacketLength(int length)
|
||||||
{
|
{
|
||||||
|
Console.WriteLine("length got: " + length);
|
||||||
byte[] packetLength = new byte[4];
|
byte[] packetLength = new byte[4];
|
||||||
|
|
||||||
for (int i = 0; i < 4; i++)
|
for (int i = 0; i < 4; i++)
|
||||||
@@ -63,30 +52,29 @@ namespace RH_Engine
|
|||||||
packetLength[i] = (byte)(length >> (8 * i));
|
packetLength[i] = (byte)(length >> (8 * i));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("packet length: " + BitConverter.ToString(packetLength));
|
||||||
return packetLength;
|
return packetLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string ReadPrefMessage(NetworkStream stream)
|
public static string ReadPrefMessage(NetworkStream stream)
|
||||||
{
|
{
|
||||||
byte[] lengthBytes = new byte[4];
|
byte[] lengthBytes = new byte[4];
|
||||||
|
|
||||||
stream.Read(lengthBytes, 0, 4);
|
stream.Read(lengthBytes, 0, 4);
|
||||||
|
|
||||||
int length = (int)(lengthBytes[0] | (lengthBytes[1] << 8) | (lengthBytes[1] << 16) | (lengthBytes[1] << 24));
|
int length = BitConverter.ToInt32(lengthBytes);
|
||||||
|
|
||||||
byte[] buffer = new byte[length];
|
byte[] buffer = new byte[length];
|
||||||
int totalRead = 0;
|
int totalRead = 0;
|
||||||
|
|
||||||
//read bytes until stream indicates there are no more
|
//read bytes until stream indicates there are no more
|
||||||
do
|
|
||||||
{
|
|
||||||
int read = stream.Read(buffer, totalRead, buffer.Length - totalRead);
|
int read = stream.Read(buffer, totalRead, buffer.Length - totalRead);
|
||||||
totalRead += read;
|
totalRead += read;
|
||||||
Console.WriteLine("ReadMessage: " + read);
|
Console.WriteLine("ReadMessage: " + read);
|
||||||
} while (stream.DataAvailable);
|
Console.WriteLine(Encoding.UTF8.GetString(buffer, 4, length - 4));
|
||||||
|
|
||||||
return Encoding.ASCII.GetString(buffer, 0, totalRead);
|
return Encoding.ASCII.GetString(buffer, 0, totalRead);
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user