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); } }
public void ReceiveData(MockQueueItem item) { IMessageDecoder decoder = Spec.NewMessageDecoder(item.ItemData); if (decoder.IsRequest) { Request request = decoder.DecodeRequest(); request.Source = item.Source; request.Destination = item.Destination; Exchange exchange = Matcher.ReceiveRequest(request); exchange.EndPoint = this; request.Session = Session; Stack.ReceiveRequest(exchange, request); } else if (decoder.IsResponse) { Response response = decoder.DecodeResponse(); response.Source = item.Source; response.Session = Session; Exchange exchange = Matcher.ReceiveResponse(response); if (exchange != null) { exchange.EndPoint = this; Stack.ReceiveResponse(exchange, response); } else if (response.Type != MessageType.ACK) { Reject(response); } } else if (decoder.IsEmpty) { EmptyMessage message; try { message = decoder.DecodeEmptyMessage(); } catch (Exception ex) { return; } message.Source = item.Source; if (!message.IsCancelled) { // CoAP Ping if (message.Type == MessageType.CON || message.Type == MessageType.NON) { Reject(message); } else { Exchange exchange = Matcher.ReceiveEmptyMessage(message); if (exchange != null) { exchange.EndPoint = this; Stack.ReceiveEmptyMessage(exchange, message); } } } } else { Assert.Fail(); } }