/// <summary> /// Sends a message. /// </summary> /// <param name="message"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task Send(RnetMessage message, CancellationToken cancellationToken) { Contract.Requires<ArgumentNullException>(message != null); // attempt to restablish a connection if it was lost while (Connection.State != RnetConnectionState.Open && !cancellationToken.IsCancellationRequested) await OpenConnection(cancellationToken); // check for cancelled if (cancellationToken.IsCancellationRequested) return; // send message await Connection.Send(message, cancellationToken); // successfully sent OnMessageSent(new RnetMessageEventArgs(message)); }
/// <summary> /// Sends a message. /// </summary> /// <param name="message"></param> /// <returns></returns> public Task Send(RnetMessage message) { Contract.Requires<ArgumentNullException>(message != null); return Send(message, CancellationToken.None); }
/// <summary> /// Sends a message to the connection. /// </summary> /// <param name="message"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public virtual Task Send(RnetMessage message, CancellationToken cancellationToken) { Contract.Requires<ArgumentNullException>(message != null); Contract.Assert(writer != null); if (State != RnetConnectionState.Open) throw new RnetConnectionException("Connection is not open."); // write in the background to prevent blocking the calling thread on a socket return Task.Run(async () => await WriteMessage(message, cancellationToken), cancellationToken); }
/// <summary> /// Writes the message with the writer. /// </summary> /// <param name="message"></param> /// <param name="cancellationToken"></param> /// <returns></returns> async Task WriteMessage(RnetMessage message, CancellationToken cancellationToken) { Contract.Requires<ArgumentNullException>(message != null); Contract.Requires<InvalidOperationException>(writer != null); using (await write.LockAsync(cancellationToken)) message.Write(writer); }
/// <summary> /// Bus has received a message from the given device. /// </summary> /// <param name="message"></param> internal async Task SentMessage(RnetMessage message) { Contract.Requires<ArgumentNullException>(message != null); var hmsg = message as RnetHandshakeMessage; if (hmsg != null) await ReceiveHandshakeMessage(hmsg); var rmsg = message as RnetRequestDataMessage; if (rmsg != null) await ReceiveRequestDataMessage(rmsg); var dmsg = message as RnetSetDataMessage; if (dmsg != null) await ReceiveSetDataMessage(dmsg); var emsg = message as RnetEventMessage; if (emsg != null) await ReceiveEventMessage(emsg); }