Skip to content

Instantly share code, notes, and snippets.

@xgenvn
Created October 19, 2016 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xgenvn/4bc29e58480d4f88a387874f3d3a083e to your computer and use it in GitHub Desktop.
Save xgenvn/4bc29e58480d4f88a387874f3d3a083e to your computer and use it in GitHub Desktop.
public async Task Read()
{
DataReader reader = new DataReader(socket.InputStream);
while (true)
{
if (ActionManager.GetCurrentState() == ActionManager.AppState.DISCONNECTING)
{
ActionManager.SetCurrentState(ActionManager.AppState.DISCONNECTED_CONTROL);
break;
}
// Read first 4 bytes (length of the subsequent string).
await reader.LoadAsync(4);
var res = new byte[4];
reader.ReadBytes(res); //reader.ReadUInt32() not work
var msgLength = BitConverter.ToUInt32(res, 0);
//Debug.Log(msgLength);
uint actualStringLength = await reader.LoadAsync(msgLength);
if (msgLength != actualStringLength)
{
//The underlying socket was closed before we were able to read the whole dat
throw new SocketException(-9999);
}
res = new byte[msgLength];
//read next string
reader.ReadBytes(res);
await OnDataReceived(EventArgs.Empty, res);
await Task.Delay(50);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment