atoulme commented on a change in pull request #38: v5 UDP packet handling
URL: https://github.com/apache/incubator-tuweni/pull/38#discussion_r335445849
 
 

 ##########
 File path: 
devp2p/src/main/kotlin/org/apache/tuweni/devp2p/v5/internal/DefaultPacketCodec.kt
 ##########
 @@ -0,0 +1,36 @@
+package org.apache.tuweni.devp2p.v5.internal
+
+import org.apache.tuweni.bytes.Bytes
+import org.apache.tuweni.crypto.SECP256K1
+import org.apache.tuweni.devp2p.v5.PacketCodec
+import org.apache.tuweni.devp2p.v5.packet.RandomMessage
+import org.apache.tuweni.devp2p.v5.packet.UdpMessage
+import org.apache.tuweni.devp2p.v5.packet.WhoAreYouMessage
+import org.apache.tuweni.rlp.RLP
+import java.lang.IllegalArgumentException
+import java.nio.ByteBuffer
+
+class DefaultPacketCodec(
+  val nodeId: Bytes
+): PacketCodec {
+
+  override fun encode(message: UdpMessage): ByteBuffer {
+    return message.encode()
+  }
+
+  override fun decode(buffer: ByteBuffer): UdpMessage {
+    val bytes = Bytes.wrapByteBuffer(buffer)
+    val tag = bytes.slice(0, UdpMessage.TAG_LENGTH)
+    val content = bytes.slice(UdpMessage.TAG_LENGTH)
+    if (RLP.isList(content)) {
+      if (UdpMessage.isMagic(tag, nodeId)) {
+        return WhoAreYouMessage.create(content, nodeId, nodeId)
+      }
+    } else {
+      val srcId = UdpMessage.getSourceFromTag(tag, nodeId)
+      return RandomMessage.create(content, srcId, nodeId)
+    }
+    throw IllegalArgumentException("Unknown message inbound...")
 
 Review comment:
   How is that exception caught and handled?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to