/// <summary>Asynchronously simulates S101 communication.</summary> /// <param name="client">The <see cref="S101Client"/> to use.</param> /// <param name="types">The types to pass to the internal <see cref="EmberConverter"/>, which is used to convert /// between XML payload and EmBER payload.</param> /// <param name="logReader">The <see cref="XmlReader"/> to read the messages from. The messages that are /// expected to be received from the remote party as well as the ones that will be sent are read with this /// reader. The format needs to match the one written by <see cref="S101Logger"/>.</param> /// <param name="sendFirstMessage"><c>true</c> to send the first <see cref="EmberData"/> message read with /// <paramref name="logReader"/>; <c>false</c> to wait for the first message from the remote party and match it /// to the first <see cref="EmberData"/> message read with <paramref name="logReader"/>.</param> /// <returns>A <see cref="Task"/> object representing the communication. This task completes when one of the /// following events occurs: /// <list type="bullet"> /// <item>The last message in the log has been sent/received.</item> /// <item>The <see cref="S101Client.ConnectionLost"/> event occurred on the client passed to /// <see cref="RunAsync"/>.</item> /// </list></returns> /// <exception cref="ArgumentNullException"><paramref name="client"/>, <paramref name="types"/> and/or /// <paramref name="logReader"/> equal <c>null</c>.</exception> /// <exception cref="S101Exception"><list type="bullet"> /// <item>There was a mismatch between an incoming message and one read from the log.</item> /// <item>The <see cref="S101Client.ConnectionLost"/> event occurred on the client passed to /// <see cref="RunAsync"/>.</item> /// </list></exception> /// <exception cref="XmlException">The XML read with <paramref name="logReader"/> is invalid, see /// <see cref="Exception.Message"/> for details.</exception> /// <remarks> /// <para>Reads messages with <paramref name="logReader"/> and depending on the direction either sends them to /// the remote party or matches them to messages received from the remote party. If a message received from the /// remote party does not match the one in the log then an appropriate exception is thrown.</para> /// <para>Subsequent messages read with <paramref name="logReader"/> that match the direction of the first /// message read with <paramref name="logReader"/> are sent if <paramref name="sendFirstMessage"/> equals /// <c>true</c>; otherwise such messages are matched to the ones received from the remote party. The opposite /// happens with log messages of opposite direction.</para> /// </remarks> public static async Task RunAsync( S101Client client, EmberTypeBag types, XmlReader logReader, bool sendFirstMessage) { var robot = new S101Robot(client, types, logReader, sendFirstMessage); try { await robot.WaitAsync(); } finally { robot.Dispose(); } }