/// <summary>
        /// Send a CoAP message to the server. Please note, you must handle all exceptions
        /// and no event is raised.
        /// </summary>
        /// <param name="coapMsg">The CoAP message to send to server</param>
        /// <returns>Number of bytes sent</returns>
        public override int Send(AbstractCoAPMessage coapMsg)
        {
            if (coapMsg == null)
            {
                throw new ArgumentNullException("Message is NULL");
            }
            if (this._clientSocket == null)
            {
                throw new InvalidOperationException("CoAP client not yet started");
            }
            int bytesSent = 0;

            byte[] coapBytes = coapMsg.ToByteStream();
            if (coapBytes.Length > AbstractNetworkUtils.GetMaxMessageSize())
            {
                throw new UnsupportedException("Message size too large. Not supported. Try block option");
            }
            bytesSent = this._clientSocket.Send(coapBytes);
            if (coapMsg.MessageType.Value == CoAPMessageType.CON)
            {
                //confirmable message...need to wait for a response
                coapMsg.DispatchDateTime = DateTime.Now;
            }

            return(bytesSent);
        }
예제 #2
0
 public byte[] ToBytes(AbstractCoAPMessage coapMsg)
 {
     byte[] coapBytes = coapMsg.ToByteStream();
     if (coapBytes.Length > AbstractNetworkUtils.GetMaxMessageSize())
     {
         throw new UnsupportedException("Message size too large. Not supported. Try block option");
     }
     return(coapBytes);
 }
        /// <summary>
        /// Send the CoAP message to client
        /// </summary>
        /// <param name="coapMsg">The CoAP message to send</param>
        /// <returns>Number of bytes sent</returns>
        public override int Send(AbstractCoAPMessage coapMsg)
        {
            if (coapMsg == null)
            {
                throw new ArgumentNullException("Message is NULL");
            }
            if (this._socket == null)
            {
                throw new InvalidOperationException("CoAP server not yet started");
            }

            int bytesSent = 0;

            try
            {
                //We do not want server to die when a socket send error occurs...
                //just clean all settings specific to the remote client and
                //keep going
                byte[] coapBytes = coapMsg.ToByteStream();
                bytesSent = this._socket.SendTo(coapBytes, coapMsg.RemoteSender);
                if (coapMsg.MessageType.Value == CoAPMessageType.CON)
                {
                    //confirmable message...need to wait for a response
                    coapMsg.DispatchDateTime = DateTime.Now;
                    this._msgPendingAckQ.AddToWaitQ(coapMsg);
                }
            }
            catch (Exception e)
            {
                this._msgPendingAckQ.RemoveFromWaitQ(coapMsg.ID.Value);
                if (coapMsg.GetType() == typeof(CoAPRequest))
                {
                    CoAPRequest coapReq = (CoAPRequest)coapMsg;
                    this._observers.RemoveResourceObserver(coapReq.GetURL(), coapReq.Token.Value);
                }
                else if (coapMsg.GetType() == typeof(CoAPResponse))
                {
                    CoAPResponse coapResp = (CoAPResponse)coapMsg;
                    this._observers.RemoveResourceObserver(coapResp);
                }
                this.HandleError(e, coapMsg);
            }

            return(bytesSent);
        }
        /// <summary>
        /// Send a CoAP message to the server
        /// </summary>
        /// <param name="coapMsg">The CoAP message to send to server</param>
        /// <returns>Number of bytes sent</returns>
        public override int Send(AbstractCoAPMessage coapMsg)
        {
            if (coapMsg == null)
            {
                throw new ArgumentNullException("Message is NULL");
            }
            if (this._clientSocket == null)
            {
                throw new InvalidOperationException("CoAP client not yet started");
            }
            int bytesSent = 0;

            try
            {
                byte[] coapBytes = coapMsg.ToByteStream();
                if (coapBytes.Length > AbstractNetworkUtils.GetMaxMessageSize())
                {
                    throw new UnsupportedException("Message size too large. Not supported. Try block option");
                }
                bytesSent = this._clientSocket.Send(coapBytes);
                if (coapMsg.MessageType.Value == CoAPMessageType.CON)
                {
                    //confirmable message...need to wait for a response
                    if (coapMsg.Timeout <= 0)
                    {
                        coapMsg.Timeout = AbstractCoAPChannel.DEFAULT_ACK_TIMEOUT_SECS;
                    }
                    coapMsg.DispatchDateTime = DateTime.Now;
                    this._msgPendingAckQ.AddToWaitQ(coapMsg);
                }
            }
            catch (Exception e)
            {
                this._msgPendingAckQ.RemoveFromWaitQ(coapMsg.ID.Value);
                this.HandleError(e, coapMsg);
            }

            return(bytesSent);
        }