Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | #ifndef THRIFT_PY_PROTOCOL_H |
| 21 | #define THRIFT_PY_PROTOCOL_H |
| 22 | |
| 23 | #include "ext/types.h" |
| 24 | |
| 25 | namespace apache { |
| 26 | namespace thrift { |
| 27 | namespace py { |
| 28 | |
| 29 | template <typename Impl> |
| 30 | class ProtocolBase { |
| 31 | |
| 32 | public: |
| 33 | ProtocolBase() : stringLimit_(INT32_MAX), containerLimit_(INT32_MAX), output_(NULL) {} |
| 34 | inline virtual ~ProtocolBase(); |
| 35 | |
| 36 | bool prepareDecodeBufferFromTransport(PyObject* trans); |
| 37 | |
| 38 | PyObject* readStruct(PyObject* output, PyObject* klass, PyObject* spec_seq); |
| 39 | |
| 40 | |
| 41 | bool prepareEncodeBuffer(); |
| 42 | |
| 43 | bool encodeValue(PyObject* value, TType type, PyObject* typeargs); |
| 44 | |
| 45 | PyObject* getEncodedValue(); |
| 46 | |
| 47 | long stringLimit() const { return stringLimit_; } |
| 48 | void setStringLengthLimit(long limit) { stringLimit_ = limit; } |
| 49 | |
| 50 | long containerLimit() const { return containerLimit_; } |
| 51 | void setContainerLengthLimit(long limit) { containerLimit_ = limit; } |
| 52 | |
| 53 | protected: |
| 54 | bool readBytes(char** output, int len); |
| 55 | |
| 56 | bool readByte(uint8_t& val) { |
| 57 | char* buf; |
| 58 | if (!readBytes(&buf, 1)) { |
| 59 | return false; |
| 60 | } |
| 61 | val = static_cast<uint8_t>(buf[0]); |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | bool writeBuffer(char* data, size_t len); |
| 66 | |
| 67 | void writeByte(uint8_t val) { writeBuffer(reinterpret_cast<char*>(&val), 1); } |
| 68 | |
| 69 | PyObject* decodeValue(TType type, PyObject* typeargs); |
| 70 | |
| 71 | bool skip(TType type); |
| 72 | |
| 73 | inline bool checkType(TType got, TType expected); |
| 74 | inline bool checkLengthLimit(int32_t len, long limit); |
| 75 | |
| 76 | inline bool isUtf8(PyObject* typeargs); |
| 77 | |
| 78 | private: |
| 79 | Impl* impl() { return static_cast<Impl*>(this); } |
| 80 | |
| 81 | long stringLimit_; |
| 82 | long containerLimit_; |
| 83 | EncodeBuffer* output_; |
| 84 | DecodeBuffer input_; |
| 85 | }; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | #include "ext/protocol.tcc" |
| 91 | |
| 92 | #endif // THRIFT_PY_PROTOCOL_H |