You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tuweni.apache.org by GitBox <gi...@apache.org> on 2019/07/09 16:56:52 UTC

[GitHub] [incubator-tuweni] jrhea commented on a change in pull request #24: Move to hobbits 3 with binary messages

jrhea commented on a change in pull request #24: Move to hobbits 3 with binary messages
URL: https://github.com/apache/incubator-tuweni/pull/24#discussion_r301692837
 
 

 ##########
 File path: hobbits/src/main/kotlin/org/apache/tuweni/hobbits/Message.kt
 ##########
 @@ -38,48 +40,29 @@ class Message(
      * Reads a message from a byte buffer.
      * @param message the message bytes
      * @return the message interpreted by the codec, or null if the message is too short.
+     * @throws IllegalArgumentException if the message doesn't start with the correct preamble.
      */
     @JvmStatic
     fun readMessage(message: Bytes): Message? {
-      var requestLineBytes: Bytes? = null
-      for (i in 0 until message.size()) {
-        if (message.get(i) == '\n'.toByte()) {
-          requestLineBytes = message.slice(0, i)
-          break
-        }
-      }
-      if (requestLineBytes == null) {
+      if (message.size() < MESSAGE_HEADER_LENGTH) {
         return null
       }
-      val requestLine = String(requestLineBytes.toArrayUnsafe(), StandardCharsets.UTF_8)
-      val segments = Splitter.on(" ").split(requestLine).iterator()
-
-      val protocol = segments.next()
-      if (!segments.hasNext()) {
-        return null
-      }
-      val version = segments.next()
-      if (!segments.hasNext()) {
-        return null
-      }
-      val command = segments.next()
-      if (!segments.hasNext()) {
-        return null
-      }
-      val headersLength = segments.next().toInt()
-      if (!segments.hasNext()) {
-        return null
+      if (message.slice(0, PREAMBLE.size) != Bytes.wrap(PREAMBLE)) {
+        throw IllegalArgumentException("Message doesn't start with correct preamble")
       }
-      val bodyLength = segments.next().toInt()
+      val version = message.getInt(PREAMBLE.size)
+      val protocol = Protocol.fromByte(message.get(PREAMBLE.size + 4))
+      val headersLength = message.getInt(PREAMBLE.size + 4 + 1)
+      val bodyLength = message.getInt(PREAMBLE.size + 4 + 1 + 4)
 
-      if (message.size() < requestLineBytes.size() + 1 + headersLength + bodyLength) {
+      if (message.size() < PREAMBLE.size + java.lang.Integer.BYTES * 3 + 1 + headersLength + bodyLength) {
 
 Review comment:
   just use: `MESSAGE_HEADER_LENGTH` instead of `PREAMBLE.size + java.lang.Integer.BYTES * 3 + 1`

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tuweni.apache.org
For additional commands, e-mail: dev-help@tuweni.apache.org