Skip to content

Commit

Permalink
Server/Client sample fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcidev committed Apr 20, 2017
1 parent 00694a6 commit c39a350
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
15 changes: 6 additions & 9 deletions ClientSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,11 @@ private static async Task RunClient()
packet.Write("Hello Server from fully encrypted packet!");
client.SendPacket(packet);
packet.Dispose();

Thread.Sleep(3000);
IEnumerable<Packet> packets = null;
do

bool end = false;
while (!end)
{
packets = client.ReceiveData(true);
if (packets == null)
break;

var packets = await client.ReceiveDataAsync(true);
foreach (var pck in packets)
{
switch (pck.OpcodeNumber)
Expand All @@ -66,12 +62,13 @@ private static async Task RunClient()
break;
case (UInt16)ServerPacketTypes.SMSG_INIT_RESPONSE_ENCRYPTED_AES:
Console.WriteLine(pck.ReadString());
end = true;
break;
}

pck.Dispose();
}
} while (true);
}

// CleanUP at the end
aes.Dispose();
Expand Down
14 changes: 7 additions & 7 deletions ServerSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;

namespace ServerSample
Expand All @@ -29,16 +30,13 @@ private static async Task RunServer()
var tcpClient = await server.AcceptClientAsync();
// Creates client instance
var client = new Client(tcpClient);
byte[] data = null;
do
{
// Gets received data (everything that is in network pipe)
data = client.ReceiveData();
if (data == null)
continue;

var data = await client.ReceiveDataAsync();
var end = false;
do
{
data = data.Any() ? data : await client.ReceiveDataAsync();
var length = BitConverter.ToUInt16(data, 0);
data = data.Skip(sizeof(UInt16)).ToArray();
var packetData = data.Take(length).ToArray();
Expand All @@ -59,6 +57,7 @@ private static async Task RunServer()
Console.WriteLine(packet.ReadString());
response = new Packet(ServerPacketTypes.SMSG_INIT_RESPONSE_ENCRYPTED_AES);
response.Write("Hello Client, We are now fully encrypted!");
end = true;
break;
}

Expand All @@ -69,11 +68,12 @@ private static async Task RunServer()
}

data = data.Skip(length).ToArray();
} while (data.Any());
} while (!end);

break;
} while (true);

Thread.Sleep(1000);
aes.Dispose();
client.Dispose();
server.Dispose();
Expand Down

0 comments on commit c39a350

Please sign in to comment.