/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * Licensed 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace apache { namespace thrift { namespace rocket { template void Parser::getReadBuffer(void** bufout, size_t* lenout) { switch (mode_) { case (detail::ParserMode::STRATEGY): frameLengthParser_->getReadBuffer(bufout, lenout); break; case (detail::ParserMode::ALLOCATING): allocatingParser_->getReadBuffer(bufout, lenout); break; } } template void Parser::readDataAvailable(size_t nbytes) noexcept { folly::DelayedDestruction::DestructorGuard dg(&this->owner_); try { switch (mode_) { case (detail::ParserMode::STRATEGY): frameLengthParser_->readDataAvailable(nbytes); break; case (detail::ParserMode::ALLOCATING): allocatingParser_->readDataAvailable(nbytes); break; } } catch (...) { auto exceptionStr = folly::exceptionStr(folly::current_exception()).toStdString(); LOG(ERROR) << "Bad frame received, closing connection: " << exceptionStr; owner_.close(transport::TTransportException(exceptionStr)); } } template void Parser::readEOF() noexcept { folly::DelayedDestruction::DestructorGuard dg(&this->owner_); owner_.close(transport::TTransportException( transport::TTransportException::TTransportExceptionType::END_OF_FILE, "Channel got EOF. Check for server hitting connection limit, " "connection age timeout, server connection idle timeout, and server crashes.")); } template void Parser::readErr(const folly::AsyncSocketException& ex) noexcept { folly::DelayedDestruction::DestructorGuard dg(&this->owner_); owner_.close(transport::TTransportException(ex)); } template void Parser::readBufferAvailable( std::unique_ptr buf) noexcept { folly::DelayedDestruction::DestructorGuard dg(&this->owner_); try { switch (mode_) { case (detail::ParserMode::STRATEGY): frameLengthParser_->readBufferAvailable(std::move(buf)); break; case (detail::ParserMode::ALLOCATING): // Will throw not implemented runtime exception allocatingParser_->readBufferAvailable(std::move(buf)); break; } } catch (...) { auto exceptionStr = folly::exceptionStr(folly::current_exception()).toStdString(); LOG(ERROR) << "Bad frame received, closing connection: " << exceptionStr; owner_.close(transport::TTransportException(exceptionStr)); } } } // namespace rocket } // namespace thrift } // namespace apache