ivandasch commented on a change in pull request #191:
URL: https://github.com/apache/ignite-3/pull/191#discussion_r667826398



##########
File path: 
modules/client-common/src/main/java/org/apache/ignite/client/ClientMessageDecoder.java
##########
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.client;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
+import io.netty.util.CharsetUtil;
+import org.apache.ignite.lang.IgniteException;
+import org.msgpack.core.MessageFormat;
+import org.msgpack.core.MessagePack;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Decodes full client messages.
+ */
+public class ClientMessageDecoder extends ByteToMessageDecoder {
+    /** Magic bytes before handshake. */
+    public static final byte[] MAGIC_BYTES = new byte[]{0x49, 0x47, 0x4E, 
0x49}; // IGNI
+
+    /** */
+    private byte[] data = new byte[4]; // TODO: Pooled buffers.
+
+    /** */
+    private int cnt = -4;
+
+    /** */
+    private int msgSize;
+
+    /** */
+    private boolean magicDecoded;
+
+    /** */
+    private boolean magicFailed;
+
+    /** */
+    private MessageFormat sizeFormat = null;
+
+    @Override
+    protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, 
List<Object> list)
+            throws IOException {
+        if (!readMagic(byteBuf))
+            return;
+
+        while (read(byteBuf))
+            list.add(data);
+    }
+
+    private boolean readMagic(ByteBuf byteBuf) {
+        if (magicFailed)
+            return false;
+
+        if (magicDecoded)
+            return true;
+
+        for (; cnt < 0 && byteBuf.readableBytes() > 0; cnt++)
+            data[4 + cnt] = byteBuf.readByte();
+
+        if (cnt < 0)
+            return false;
+
+        magicDecoded = true;
+        cnt = -1;
+        msgSize = 0;
+
+        if (Arrays.equals(data, MAGIC_BYTES))
+            return true;
+
+        magicFailed = true;
+
+        throw new IgniteException("Invalid magic header in thin client 
connection. " +
+                "Expected 'IGNI', but was '" + new String(data, 
CharsetUtil.US_ASCII) + "'.");
+    }
+
+    /**
+     * Reads the buffer.
+     *
+     * @param buf Buffer.
+     * @return True when a complete message has been received; false otherwise.
+     */
+    private boolean read(ByteBuf buf) throws IOException {
+        if (buf.readableBytes() == 0)
+            return false;
+
+        if (cnt < 0) {
+            // Read varint message size.
+            if (sizeFormat == null) {
+                byte firstByte = buf.readByte();
+                sizeFormat = MessageFormat.valueOf(firstByte);
+
+                switch (sizeFormat) {

Review comment:
       Excuse me, but why not just use `ByteBuf` api? i.e. 
`ByteBuf#readUnsignedInt()` etc.? Reading byte by byte is not efficient here

##########
File path: 
modules/client-common/src/main/java/org/apache/ignite/client/ClientMessageDecoder.java
##########
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.client;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
+import io.netty.util.CharsetUtil;
+import org.apache.ignite.lang.IgniteException;
+import org.msgpack.core.MessageFormat;
+import org.msgpack.core.MessagePack;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Decodes full client messages.
+ */
+public class ClientMessageDecoder extends ByteToMessageDecoder {
+    /** Magic bytes before handshake. */
+    public static final byte[] MAGIC_BYTES = new byte[]{0x49, 0x47, 0x4E, 
0x49}; // IGNI
+
+    /** */
+    private byte[] data = new byte[4]; // TODO: Pooled buffers.
+
+    /** */
+    private int cnt = -4;
+
+    /** */
+    private int msgSize;
+
+    /** */
+    private boolean magicDecoded;
+
+    /** */
+    private boolean magicFailed;
+
+    /** */
+    private MessageFormat sizeFormat = null;
+
+    @Override
+    protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, 
List<Object> list)
+            throws IOException {
+        if (!readMagic(byteBuf))
+            return;
+
+        while (read(byteBuf))
+            list.add(data);
+    }
+
+    private boolean readMagic(ByteBuf byteBuf) {
+        if (magicFailed)
+            return false;
+
+        if (magicDecoded)
+            return true;
+
+        for (; cnt < 0 && byteBuf.readableBytes() > 0; cnt++)
+            data[4 + cnt] = byteBuf.readByte();
+
+        if (cnt < 0)
+            return false;
+
+        magicDecoded = true;
+        cnt = -1;
+        msgSize = 0;
+
+        if (Arrays.equals(data, MAGIC_BYTES))
+            return true;
+
+        magicFailed = true;
+
+        throw new IgniteException("Invalid magic header in thin client 
connection. " +
+                "Expected 'IGNI', but was '" + new String(data, 
CharsetUtil.US_ASCII) + "'.");
+    }
+
+    /**
+     * Reads the buffer.
+     *
+     * @param buf Buffer.
+     * @return True when a complete message has been received; false otherwise.
+     */
+    private boolean read(ByteBuf buf) throws IOException {
+        if (buf.readableBytes() == 0)
+            return false;
+
+        if (cnt < 0) {
+            // Read varint message size.
+            if (sizeFormat == null) {
+                byte firstByte = buf.readByte();
+                sizeFormat = MessageFormat.valueOf(firstByte);
+
+                switch (sizeFormat) {

Review comment:
       Excuse me, but why not just use `ByteBuf` api? i.e. 
`ByteBuf#readUnsignedInt()` etc.? Reading byte by byte is not efficient here. 
Actually, there is a lot fun stuff inside `LengthFieldBasedFrameDecoder`, it is 
possible to use ideas from this class, just with a little modification.

##########
File path: 
modules/client-common/src/main/java/org/apache/ignite/client/ClientMessageDecoder.java
##########
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.client;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
+import io.netty.util.CharsetUtil;
+import org.apache.ignite.lang.IgniteException;
+import org.msgpack.core.MessageFormat;
+import org.msgpack.core.MessagePack;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Decodes full client messages.
+ */
+public class ClientMessageDecoder extends ByteToMessageDecoder {
+    /** Magic bytes before handshake. */
+    public static final byte[] MAGIC_BYTES = new byte[]{0x49, 0x47, 0x4E, 
0x49}; // IGNI
+
+    /** */
+    private byte[] data = new byte[4]; // TODO: Pooled buffers.
+
+    /** */
+    private int cnt = -4;
+
+    /** */
+    private int msgSize;
+
+    /** */
+    private boolean magicDecoded;
+
+    /** */
+    private boolean magicFailed;
+
+    /** */
+    private MessageFormat sizeFormat = null;
+
+    @Override
+    protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, 
List<Object> list)
+            throws IOException {
+        if (!readMagic(byteBuf))
+            return;
+
+        while (read(byteBuf))
+            list.add(data);
+    }
+
+    private boolean readMagic(ByteBuf byteBuf) {
+        if (magicFailed)
+            return false;
+
+        if (magicDecoded)
+            return true;
+
+        for (; cnt < 0 && byteBuf.readableBytes() > 0; cnt++)
+            data[4 + cnt] = byteBuf.readByte();
+
+        if (cnt < 0)
+            return false;
+
+        magicDecoded = true;
+        cnt = -1;
+        msgSize = 0;
+
+        if (Arrays.equals(data, MAGIC_BYTES))
+            return true;
+
+        magicFailed = true;
+
+        throw new IgniteException("Invalid magic header in thin client 
connection. " +
+                "Expected 'IGNI', but was '" + new String(data, 
CharsetUtil.US_ASCII) + "'.");
+    }
+
+    /**
+     * Reads the buffer.
+     *
+     * @param buf Buffer.
+     * @return True when a complete message has been received; false otherwise.
+     */
+    private boolean read(ByteBuf buf) throws IOException {
+        if (buf.readableBytes() == 0)
+            return false;
+
+        if (cnt < 0) {
+            // Read varint message size.
+            if (sizeFormat == null) {
+                byte firstByte = buf.readByte();
+                sizeFormat = MessageFormat.valueOf(firstByte);
+
+                switch (sizeFormat) {

Review comment:
       See also this code from tarantool
   
https://github.com/tarantool/cartridge-java/blob/1496c1425f834c997422864c055cfb9d7533d478/src/main/java/io/tarantool/driver/codecs/MessagePackFrameDecoder.java

##########
File path: 
modules/client-common/src/main/java/org/apache/ignite/client/ClientMessageDecoder.java
##########
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.client;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
+import io.netty.util.CharsetUtil;
+import org.apache.ignite.lang.IgniteException;
+import org.msgpack.core.MessageFormat;
+import org.msgpack.core.MessagePack;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Decodes full client messages.
+ */
+public class ClientMessageDecoder extends ByteToMessageDecoder {
+    /** Magic bytes before handshake. */
+    public static final byte[] MAGIC_BYTES = new byte[]{0x49, 0x47, 0x4E, 
0x49}; // IGNI
+
+    /** */
+    private byte[] data = new byte[4]; // TODO: Pooled buffers.
+
+    /** */
+    private int cnt = -4;
+
+    /** */
+    private int msgSize;
+
+    /** */
+    private boolean magicDecoded;
+
+    /** */
+    private boolean magicFailed;
+
+    /** */
+    private MessageFormat sizeFormat = null;
+
+    @Override
+    protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, 
List<Object> list)
+            throws IOException {
+        if (!readMagic(byteBuf))
+            return;
+
+        while (read(byteBuf))
+            list.add(data);
+    }
+
+    private boolean readMagic(ByteBuf byteBuf) {
+        if (magicFailed)
+            return false;
+
+        if (magicDecoded)
+            return true;
+
+        for (; cnt < 0 && byteBuf.readableBytes() > 0; cnt++)
+            data[4 + cnt] = byteBuf.readByte();
+
+        if (cnt < 0)
+            return false;
+
+        magicDecoded = true;
+        cnt = -1;
+        msgSize = 0;
+
+        if (Arrays.equals(data, MAGIC_BYTES))
+            return true;
+
+        magicFailed = true;
+
+        throw new IgniteException("Invalid magic header in thin client 
connection. " +
+                "Expected 'IGNI', but was '" + new String(data, 
CharsetUtil.US_ASCII) + "'.");
+    }
+
+    /**
+     * Reads the buffer.
+     *
+     * @param buf Buffer.
+     * @return True when a complete message has been received; false otherwise.
+     */
+    private boolean read(ByteBuf buf) throws IOException {
+        if (buf.readableBytes() == 0)
+            return false;
+
+        if (cnt < 0) {
+            // Read varint message size.
+            if (sizeFormat == null) {
+                byte firstByte = buf.readByte();
+                sizeFormat = MessageFormat.valueOf(firstByte);
+
+                switch (sizeFormat) {

Review comment:
       Pavel, sorry if my message hurts you. But may be my comments can give 
you some insights and helps. Sorry again.

##########
File path: 
modules/client-common/src/main/java/org/apache/ignite/client/ClientMessageDecoder.java
##########
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.client;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
+import io.netty.util.CharsetUtil;
+import org.apache.ignite.lang.IgniteException;
+import org.msgpack.core.MessageFormat;
+import org.msgpack.core.MessagePack;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Decodes full client messages.
+ */
+public class ClientMessageDecoder extends ByteToMessageDecoder {
+    /** Magic bytes before handshake. */
+    public static final byte[] MAGIC_BYTES = new byte[]{0x49, 0x47, 0x4E, 
0x49}; // IGNI
+
+    /** */
+    private byte[] data = new byte[4]; // TODO: Pooled buffers.
+
+    /** */
+    private int cnt = -4;
+
+    /** */
+    private int msgSize;
+
+    /** */
+    private boolean magicDecoded;
+
+    /** */
+    private boolean magicFailed;
+
+    /** */
+    private MessageFormat sizeFormat = null;
+
+    @Override
+    protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, 
List<Object> list)
+            throws IOException {
+        if (!readMagic(byteBuf))
+            return;
+
+        while (read(byteBuf))
+            list.add(data);
+    }
+
+    private boolean readMagic(ByteBuf byteBuf) {
+        if (magicFailed)
+            return false;
+
+        if (magicDecoded)
+            return true;
+
+        for (; cnt < 0 && byteBuf.readableBytes() > 0; cnt++)
+            data[4 + cnt] = byteBuf.readByte();
+
+        if (cnt < 0)
+            return false;
+
+        magicDecoded = true;
+        cnt = -1;
+        msgSize = 0;
+
+        if (Arrays.equals(data, MAGIC_BYTES))
+            return true;
+
+        magicFailed = true;
+
+        throw new IgniteException("Invalid magic header in thin client 
connection. " +
+                "Expected 'IGNI', but was '" + new String(data, 
CharsetUtil.US_ASCII) + "'.");
+    }
+
+    /**
+     * Reads the buffer.
+     *
+     * @param buf Buffer.
+     * @return True when a complete message has been received; false otherwise.
+     */
+    private boolean read(ByteBuf buf) throws IOException {
+        if (buf.readableBytes() == 0)
+            return false;
+
+        if (cnt < 0) {
+            // Read varint message size.
+            if (sizeFormat == null) {
+                byte firstByte = buf.readByte();
+                sizeFormat = MessageFormat.valueOf(firstByte);
+
+                switch (sizeFormat) {

Review comment:
       I meant that you can use sizeFormat spec and based on it just read all 
you need from ByteBuf immediately




-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to