ptupitsyn commented on a change in pull request #191: URL: https://github.com/apache/ignite-3/pull/191#discussion_r667833425
########## 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: Because varint is variable length. Please note: this PR is work in progress, everything is a subject to change. My approach is [make it work, make it right, make it fast](https://wiki.c2.com/?MakeItWorkMakeItRightMakeItFast). It does not make sense to discuss small details at this point. ########## 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: Ivan, no problem at all, no hurt feelings :) The comments are helpful and I'll get back to them at a later stage, just letting you know the status. ########## 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: Ivan, no problem at all, no hurt feelings :) The comments are helpful and I'll get back to them at a later stage, just letting you know the status. Thank you. -- 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]
