Example #1
0
 private void ReceiveData(Object sender, DataReceivedEventArgs e)
 {
     _executor.Start(() => ReceiveData(e));
 }
Example #2
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;
                        _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;
                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;

                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;

                // 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);
            }
        }
 private void FireDataReceived(object sender, DataReceivedEventArgs e)
 {
     if (DataReceived != null)
         DataReceived(this, e);
 }