예제 #1
0
파일: RnetClient.cs 프로젝트: wasabii/rnet
        /// <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));
        }
예제 #2
0
파일: RnetClient.cs 프로젝트: wasabii/rnet
        /// <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);
        }
예제 #3
0
        /// <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);
        }
예제 #4
0
        /// <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);
        }
예제 #5
0
        /// <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);
        }