HolyLow commented on code in PR #3060: URL: https://github.com/apache/celeborn/pull/3060#discussion_r1909981331
########## cpp/celeborn/network/MessageDecoder.h: ########## @@ -0,0 +1,84 @@ +/* + * 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. + */ + +#pragma once + +#include <folly/io/Cursor.h> +#include <wangle/codec/ByteToMessageDecoder.h> + +namespace celeborn { +namespace network { +/** + * A complete Message encoding/decoding frame is: + * ----------------------------------------------------------------------- + * | encodedLength | msgType | bodyLength | encodedContent | bodyContent | + * ----------------------------------------------------------------------- + * The size of each part is: + * ----------------------------------------------------------------------- + * | 4 | 1 | 4 | #encodedLength | #bodyLength | + * ----------------------------------------------------------------------- + * So the #headerLength is 4 + 1 + 4, + * and the complete frameLength is: + * #frameLength = #headerLength + #encodedLength + #bodyLength. + */ + +class MessageDecoder : public wangle::ByteToByteDecoder { + public: + MessageDecoder(bool networkByteOrder = true) + : networkByteOrder_(networkByteOrder) {} + + bool decode( + Context* ctx, + folly::IOBufQueue& buf, + std::unique_ptr<folly::IOBuf>& result, + size_t&) override { + if (buf.chainLength() < headerLength_) { + return false; + } + + folly::io::Cursor c(buf.front()); + int encodedLength, bodyLength; + if (networkByteOrder_) { + encodedLength = c.readBE<int32_t>(); + c.skip(1); + bodyLength = c.readBE<int32_t>(); + } else { + encodedLength = c.readLE<int32_t>(); + c.skip(1); + bodyLength = c.readLE<int32_t>(); + } + + uint64_t frameLength = headerLength_ + encodedLength + bodyLength; + if (buf.chainLength() < frameLength) { + return false; + } + + result = buf.split(frameLength); Review Comment: I don't get it. The complete header, encodedContent and bodyContent form a frame, and a frame should contain all the infos we need, including the messageBody you mentioned. Maybe you could refer to the annotation in FrameDecoder.h, or refer to tests in FrameDecoderTest.cpp to see what's going on. Or maybe you could explain more in detail in case I am not getting your point. -- 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]
