Example #1
0
 /// <inheritdoc/>
 public void DeliverRequest(Exchange exchange)
 {
     exchange.SendReject();
 }
Example #2
0
 /// <inheritdoc/>
 public void SendEmptyMessage(Exchange exchange, EmptyMessage message)
 {
     _executor.Start(() => _coapStack.SendEmptyMessage(exchange, message));
 }
Example #3
0
        private void ReceiveData(DataReceivedEventArgs e)
        {
            IMessageDecoder decoder = Spec.NewMessageDecoder(e.Data);

            if (decoder.IsRequest)
            {
                Request request;
                try
                {
                    request = decoder.DecodeRequest();
                }
                catch (Exception)
                {
                    if (decoder.IsReply)
                    {
                        if (log.IsWarnEnabled)
                        {
                            log.Warn("Message format error caused by " + e.EndPoint);
                        }
                    }
                    else
                    {
                        // manually build RST from raw information
                        EmptyMessage rst = new EmptyMessage(MessageType.RST);
                        rst.Destination = e.EndPoint;
                        rst.ID          = decoder.ID;

                        Fire(SendingEmptyMessage, rst);

                        _channel.Send(Serialize(rst), rst.Destination);

                        if (log.IsWarnEnabled)
                        {
                            log.Warn("Message format error caused by " + e.EndPoint + " and reseted.");
                        }
                    }
                    return;
                }

                request.Source = e.EndPoint;

                Fire(ReceivingRequest, request);

                if (!request.IsCancelled)
                {
                    Exchange exchange = _matcher.ReceiveRequest(request);
                    if (exchange != null)
                    {
                        exchange.EndPoint = this;
                        _coapStack.ReceiveRequest(exchange, request);
                    }
                }
            }
            else if (decoder.IsResponse)
            {
                Response response = decoder.DecodeResponse();
                response.Source = e.EndPoint;

                Fire(ReceivingResponse, response);

                if (!response.IsCancelled)
                {
                    Exchange exchange = _matcher.ReceiveResponse(response);
                    if (exchange != null)
                    {
                        response.RTT      = (DateTime.Now - exchange.Timestamp).TotalMilliseconds;
                        exchange.EndPoint = this;
                        _coapStack.ReceiveResponse(exchange, response);
                    }
                    else if (response.Type != MessageType.ACK)
                    {
                        if (log.IsDebugEnabled)
                        {
                            log.Debug("Rejecting unmatchable response from " + e.EndPoint);
                        }
                        Reject(response);
                    }
                }
            }
            else if (decoder.IsEmpty)
            {
                EmptyMessage message = decoder.DecodeEmptyMessage();
                message.Source = e.EndPoint;

                Fire(ReceivingEmptyMessage, message);

                if (!message.IsCancelled)
                {
                    // CoAP Ping
                    if (message.Type == MessageType.CON || message.Type == MessageType.NON)
                    {
                        if (log.IsDebugEnabled)
                        {
                            log.Debug("Responding to ping by " + e.EndPoint);
                        }
                        Reject(message);
                    }
                    else
                    {
                        Exchange exchange = _matcher.ReceiveEmptyMessage(message);
                        if (exchange != null)
                        {
                            exchange.EndPoint = this;
                            _coapStack.ReceiveEmptyMessage(exchange, message);
                        }
                    }
                }
            }
            else if (log.IsDebugEnabled)
            {
                log.Debug("Silently ignoring non-CoAP message from " + e.EndPoint);
            }
        }
Example #4
0
 /// <inheritdoc/>
 public void SendResponse(Exchange exchange, Response response)
 {
     _executor.Start(() => _coapStack.SendResponse(exchange, response));
 }