Created
October 19, 2016 08:53
-
-
Save xgenvn/4bc29e58480d4f88a387874f3d3a083e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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