1 /* 2 * Copyright (C) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_REQUEST_RESPONSE_MESSAGE_RECEIVER_H 17 #define OHOS_REQUEST_RESPONSE_MESSAGE_RECEIVER_H 18 19 #include "event_handler.h" 20 #include "event_runner.h" 21 #include "i_response_message_handler.h" 22 23 namespace OHOS::Request { 24 25 enum MessageType { 26 HTTP_RESPONSE = 0, 27 NOTIFY_DATA, 28 }; 29 30 class ResponseMessageReceiver 31 : public OHOS::AppExecFwk::FileDescriptorListener 32 , public std::enable_shared_from_this<ResponseMessageReceiver> { 33 public: 34 static constexpr uint32_t RESPONSE_MAX_SIZE = 8 * 1024; 35 static constexpr uint32_t RESPONSE_MAGIC_NUM = 0x43434646; 36 37 ResponseMessageReceiver(IResponseMessageHandler *handler, int32_t sockFd); 38 void BeginReceive(); 39 void Shutdown(void); 40 41 private: 42 static int32_t Int64FromParcel(int64_t &num, char *&parcel, int32_t &size); 43 static int32_t Uint64FromParcel(uint64_t &num, char *&parcel, int32_t &size); 44 static int32_t Int32FromParcel(int32_t &num, char *&parcel, int32_t &size); 45 static int32_t Uint32FromParcel(uint32_t &num, char *&parcel, int32_t &size); 46 static int32_t Int16FromParcel(int16_t &num, char *&parcel, int32_t &size); 47 static int32_t StateFromParcel(State &state, char *&parcel, int32_t &size); 48 static int32_t ActionFromParcel(Action &action, char *&parcel, int32_t &size); 49 static int32_t VersionFromParcel(Version &version, char *&parcel, int32_t &size); 50 static int32_t SubscribeTypeFromParcel(SubscribeType &type, char *&parcel, int32_t &size); 51 static int32_t StringFromParcel(std::string &str, char *&parcel, int32_t &size); 52 static int32_t ResponseHeaderFromParcel( 53 std::map<std::string, std::vector<std::string>> &headers, char *&parcel, int32_t &size); 54 static int32_t ProgressExtrasFromParcel(std::map<std::string, std::string> &extras, char *&parcel, int32_t &size); 55 void OnReadable(int32_t fd) override; 56 static int32_t VecInt64FromParcel(std::vector<int64_t> &vec, char *&parcel, int32_t &size); 57 static int32_t MsgHeaderParcel(int32_t &msgId, int16_t &msgType, int16_t &bodySize, char *&parcel, int32_t &size); 58 static int32_t ResponseFromParcel(std::shared_ptr<Response> &response, char *&parcel, int32_t &size); 59 static int32_t TaskStatesFromParcel(std::vector<TaskState> &taskStates, char *&parcel, int32_t &size); 60 static int32_t NotifyDataFromParcel(std::shared_ptr<NotifyData> ¬ifyData, char *&parcel, int32_t &size); 61 void OnShutdown(int32_t fd) override; 62 void OnException(int32_t fd) override; 63 64 private: 65 IResponseMessageHandler *handler_; 66 int32_t sockFd_{ -1 }; 67 int32_t messageId_{ 1 }; 68 }; 69 70 } // namespace OHOS::Request 71 72 #endif // OHOS_REQUEST_RESPONSE_MESSAGE_RECEIVER_H