GlowCommon.CommandServer.WaitForCommand C# (CSharp) Method

WaitForCommand() private method

private WaitForCommand ( DataReader reader ) : Task
reader Windows.Storage.Streams.DataReader
return Task
        private async Task<Command> WaitForCommand(DataReader reader)
        {
            // Wait for a new message, here we wait for enough data to represent the string size
            // if we don't get all of the data, the socket was closed.
            UInt32 sizeFieldCount = await reader.LoadAsync(sizeof(UInt32));
            if (sizeFieldCount != sizeof(uint))
            {
                // We didn't get it all, the socket is closed.
                throw new Exception("Socket closed");
            }

            // Read the string.
            uint stringLength = reader.ReadUInt32();
            uint actualStringLength = await reader.LoadAsync(stringLength);
            if (stringLength != actualStringLength)
            {
                // The underlying socket was closed before we were able to read the whole data.
                throw new Exception("Socket closed");
            }        
            
            // Get the actual string
            string commandString = reader.ReadString(actualStringLength);

            // Parse the command
            return Newtonsoft.Json.JsonConvert.DeserializeObject<Command>(commandString);
        }