public async void ConnectAndRead(string hostAddress, ushort port, NetworkingStream stream)
{
try
{
serverHost = new HostName(hostAddress);
// Try to connect asynchronously
await socket.ConnectAsync(serverHost, port.ToString());
Connected = true;
OnConnected();
SendAsync(stream);
byte[] bytes = null;
Task tReadResponse = Task.Run(async () =>
{
DataReader reader = new DataReader(socket.InputStream);
uint messageSize = await reader.LoadAsync(sizeof(uint));
if (messageSize != sizeof(uint))
{
Disconnect();
// socket was closed
return;
}
bytes = new byte[messageSize];
reader.ReadBytes(bytes);
messageSize = BitConverter.ToUInt32(bytes, 0);
await reader.LoadAsync(messageSize);
bytes = new byte[messageSize];
// TODO: This may read the first 4 bytes again for the size, make sure it doesn't
reader.ReadBytes(bytes);
});
tReadResponse.Wait();
Disconnect();
BMSByte tmp = new BMSByte();
tmp.Clone(bytes);
//return new NetworkingStream(Networking.ProtocolType.TCP).Consume(this, null, tmp);
}
catch (Exception e)
{
ErrorDisconnect(e.Message);
}
}