• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     FAULTS,
29     WAIT,
30 };
31 
32 class ResponseMessageReceiver
33     : public OHOS::AppExecFwk::FileDescriptorListener
34     , public std::enable_shared_from_this<ResponseMessageReceiver> {
35 public:
36     static constexpr uint32_t RESPONSE_MAX_SIZE = 16 * 1024;
37     static constexpr uint32_t RESPONSE_MAGIC_NUM = 0x43434646;
38 
39     ResponseMessageReceiver(IResponseMessageHandler *handler, int32_t sockFd);
40     void BeginReceive();
41     void Shutdown(void);
42 
43 private:
44     static int32_t Int64FromParcel(int64_t &num, char *&parcel, int32_t &size);
45     static int32_t Uint64FromParcel(uint64_t &num, char *&parcel, int32_t &size);
46     static int32_t Int32FromParcel(int32_t &num, char *&parcel, int32_t &size);
47     static int32_t Uint32FromParcel(uint32_t &num, char *&parcel, int32_t &size);
48     static int32_t Int16FromParcel(int16_t &num, char *&parcel, int32_t &size);
49     static int32_t StateFromParcel(State &state, char *&parcel, int32_t &size);
50     static int32_t ActionFromParcel(Action &action, char *&parcel, int32_t &size);
51     static int32_t VersionFromParcel(Version &version, char *&parcel, int32_t &size);
52     static int32_t SubscribeTypeFromParcel(SubscribeType &type, char *&parcel, int32_t &size);
53     static int32_t StringFromParcel(std::string &str, char *&parcel, int32_t &size);
54     static int32_t ResponseHeaderFromParcel(
55         std::map<std::string, std::vector<std::string>> &headers, char *&parcel, int32_t &size);
56     static int32_t ProgressExtrasFromParcel(std::map<std::string, std::string> &extras, char *&parcel, int32_t &size);
57     void OnReadable(int32_t fd) override;
58     bool ReadUdsData(char *buffer, int32_t readSize, int32_t &length);
59     static int32_t VecInt64FromParcel(std::vector<int64_t> &vec, char *&parcel, int32_t &size);
60     static int32_t MsgHeaderParcel(int32_t &msgId, int16_t &msgType, int16_t &bodySize, char *&parcel, int32_t &size);
61     static int32_t ResponseFromParcel(std::shared_ptr<Response> &response, char *&parcel, int32_t &size);
62     static int32_t TaskStatesFromParcel(std::vector<TaskState> &taskStates, char *&parcel, int32_t &size);
63     static int32_t NotifyDataFromParcel(std::shared_ptr<NotifyData> &notifyData, char *&parcel, int32_t &size);
64     static int32_t ReasonFromParcel(Reason &reason, char *&parcel, int32_t &size);
65     static int32_t ReasonDataFromParcel(std::shared_ptr<int32_t> &tid, std::shared_ptr<SubscribeType> &type,
66         std::shared_ptr<Reason> &reason, char *&parcel, int32_t &size);
67     void HandResponseData(char *&leftBuf, int32_t &leftLen);
68     void HandNotifyData(char *&leftBuf, int32_t &leftLen);
69     void HandFaultsData(char *&leftBuf, int32_t &leftLen);
70     void HandWaitData(char *&leftBuf, int32_t &leftLen);
71     void OnShutdown(int32_t fd) override;
72     void OnException(int32_t fd) override;
73     void ShutdownChannel();
74 
75 private:
76     IResponseMessageHandler *handler_;
77     int32_t messageId_{ 1 };
78     int32_t sockFd_{ -1 };
79     std::mutex sockFdMutex_;
80 };
81 
82 } // namespace OHOS::Request
83 
84 #endif // OHOS_REQUEST_RESPONSE_MESSAGE_RECEIVER_H