• 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_NETWORK_SESSION_H
17 #define OHOS_SHARING_BASE_NETWORK_SESSION_H
18 #include "event_handler.h"
19 #include "network/eventhandler/event_descriptor_listener.h"
20 #include "network/interfaces/inetwork_session.h"
21 
22 namespace OHOS {
23 namespace Sharing {
24 using namespace OHOS::AppExecFwk;
25 
26 class BaseNetworkSession;
27 
28 class BaseSessionEventListener : public EventDescriptorListener {
29 public:
30     void OnReadable(int32_t fd) override;
31     void OnWritable(int32_t fd) override;
32     void OnShutdown(int32_t fd) override;
33     void OnException(int32_t fd) override;
34 
GetSession()35     virtual std::weak_ptr<BaseNetworkSession> &GetSession()
36     {
37         MEDIA_LOGD("trace.");
38         return session_;
39     }
40 
SetSession(const std::shared_ptr<BaseNetworkSession> session)41     virtual void SetSession(const std::shared_ptr<BaseNetworkSession> session)
42     {
43         MEDIA_LOGD("trace.");
44         session_ = session;
45     }
46 
47 protected:
48     std::weak_ptr<BaseNetworkSession> session_;
49 };
50 
51 class BaseSessionEventHandler : public OHOS::AppExecFwk::EventHandler {
52 public:
OnServerReadable(int32_t fd)53     virtual void OnServerReadable(int32_t fd) {}
OnServerWritable(int32_t fd)54     virtual void OnServerWritable(int32_t fd) {}
OnServerShutdown(int32_t fd)55     virtual void OnServerShutdown(int32_t fd) {}
OnServerException(int32_t fd)56     virtual void OnServerException(int32_t fd) {}
57 
GetSession()58     virtual std::weak_ptr<BaseNetworkSession> &GetSession()
59     {
60         MEDIA_LOGD("trace.");
61         return session_;
62     }
63 
SetSession(const std::shared_ptr<BaseNetworkSession> session)64     virtual void SetSession(const std::shared_ptr<BaseNetworkSession> session)
65     {
66         MEDIA_LOGD("trace.");
67         session_ = session;
68     }
69 
ProcessEvent(const OHOS::AppExecFwk::InnerEvent::Pointer & event)70     void ProcessEvent(const OHOS::AppExecFwk::InnerEvent::Pointer &event) override {}
71 
72 protected:
73     std::weak_ptr<BaseNetworkSession> session_;
74 };
75 
76 class BaseNetworkSession : public INetworkSession,
77                            public EventDescriptorListener,
78                            public std::enable_shared_from_this<BaseNetworkSession> {
79 public:
80     using Ptr = std::shared_ptr<BaseNetworkSession>;
81 
82     virtual ~BaseNetworkSession();
83     explicit BaseNetworkSession(SocketInfo::Ptr socket);
84 
85     bool Start() override;
86     void Shutdown() override;
87     bool Send(const char *buf, int32_t nSize) override;
88     bool Send(const DataBuffer::Ptr &buf, int32_t nSize) override;
89 
90     SocketInfo::Ptr GetSocketInfo() override;
91     std::shared_ptr<INetworkSessionCallback> &GetCallback() override;
92     void RegisterCallback(std::shared_ptr<INetworkSessionCallback> callback) override;
93 
OnSessionReadble(int32_t fd)94     virtual void OnSessionReadble(int32_t fd) {}
95 
96 protected:
97     SocketInfo::Ptr socket_ = nullptr;
98     std::shared_ptr<INetworkSessionCallback> callback_ = nullptr;
99     std::shared_ptr<BaseSessionEventHandler> eventHandler_ = nullptr;
100     std::shared_ptr<BaseSessionEventListener> eventListener_ = nullptr;
101 };
102 } // namespace Sharing
103 } // namespace OHOS
104 #endif