• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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_SHARING_BASE_CLIENT_H
17 #define OHOS_SHARING_BASE_CLIENT_H
18 
19 #include <cstdint>
20 #include "event_handler.h"
21 #include "network/data/socket_info.h"
22 #include "network/eventhandler/event_descriptor_listener.h"
23 #include "network/interfaces/iclient.h"
24 
25 namespace OHOS {
26 namespace Sharing {
27 using namespace OHOS::AppExecFwk;
28 
29 class BaseClient;
30 class BaseClientEventListener : public EventDescriptorListener {
31 public:
32     void OnReadable(int32_t fd) override;
33     void OnWritable(int32_t fd) override;
34     void OnShutdown(int32_t fd) override;
35     void OnException(int32_t fd) override;
36 
GetClient()37     virtual std::weak_ptr<BaseClient> &GetClient()
38     {
39         SHARING_LOGD("trace.");
40         return client_;
41     }
42 
SetClient(const std::shared_ptr<BaseClient> client)43     virtual void SetClient(const std::shared_ptr<BaseClient> client)
44     {
45         SHARING_LOGD("trace.");
46         client_ = client;
47     }
48 
49 protected:
50     std::weak_ptr<BaseClient> client_;
51 };
52 
53 class BaseClientEventHandler : public OHOS::AppExecFwk::EventHandler {
54 public:
OnClientReadable(int32_t fd)55     virtual void OnClientReadable(int32_t fd) {}
OnClientWritable(int32_t fd)56     virtual void OnClientWritable(int32_t fd) {}
OnClientShutdown(int32_t fd)57     virtual void OnClientShutdown(int32_t fd) {}
OnClientException(int32_t fd)58     virtual void OnClientException(int32_t fd) {}
59 
GetClient()60     virtual std::weak_ptr<BaseClient> &GetClient()
61     {
62         SHARING_LOGD("trace.");
63         return client_;
64     }
65 
SetClient(const std::shared_ptr<BaseClient> client)66     virtual void SetClient(const std::shared_ptr<BaseClient> client)
67     {
68         SHARING_LOGD("trace.");
69         client_ = client;
70     }
71 
ProcessEvent(const OHOS::AppExecFwk::InnerEvent::Pointer & event)72     void ProcessEvent(const OHOS::AppExecFwk::InnerEvent::Pointer &event) override {}
73 
74 protected:
75     std::weak_ptr<BaseClient> client_;
76 };
77 
78 class BaseClient : public IClient,
79                    public std::enable_shared_from_this<BaseClient> {
80 public:
81     BaseClient();
82     ~BaseClient() override;
83 
OnClientReadable(int32_t fd)84     virtual void OnClientReadable(int32_t fd) {}
85     virtual std::weak_ptr<IClientCallback> &GetCallback();
86 
87     void SetRecvOption(int32_t flags) override;
88     void RegisterCallback(std::weak_ptr<IClientCallback> callback) override;
89 
90 protected:
91     int32_t flags_ = 0;
92 
93     std::weak_ptr<IClientCallback> callback_;
94     std::shared_ptr<BaseClientEventHandler> eventHandler_ = nullptr;
95     std::shared_ptr<BaseClientEventListener> eventListener_ = nullptr;
96 };
97 } // namespace Sharing
98 } // namespace OHOS
99 #endif