• 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 #include "network_factory.h"
17 #include "client/tcp_client.h"
18 #include "client/udp_client.h"
19 #include "common/media_log.h"
20 #include "server/tcp_server.h"
21 #include "server/udp_server.h"
22 
23 namespace OHOS {
24 namespace Sharing {
NetworkFactory()25 NetworkFactory::NetworkFactory()
26 {
27     SHARING_LOGD("trace.");
28 }
29 
~NetworkFactory()30 NetworkFactory ::~NetworkFactory()
31 {
32     SHARING_LOGD("trace.");
33 }
34 
CreateTcpServer(uint16_t port,const IServerCallback::Ptr & callback,ServerPtr & serverPtr,const std::string & host)35 bool NetworkFactory::CreateTcpServer(uint16_t port, const IServerCallback::Ptr &callback, ServerPtr &serverPtr,
36                                      const std::string &host)
37 {
38     SHARING_LOGD("trace.");
39     serverPtr = std::make_shared<TcpServer>();
40     if (serverPtr) {
41         serverPtr->RegisterCallback(callback);
42         if (serverPtr->Start(port, host)) {
43             return true;
44         }
45     }
46     SHARING_LOGE("createTcpServer failed!");
47     return false;
48 }
49 
CreateTcpClient(const std::string & peerIp,uint16_t peerPort,const IClientCallback::Ptr & callback,ClientPtr & clientPtr,const std::string & localIp,uint16_t localPort)50 bool NetworkFactory::CreateTcpClient(const std::string &peerIp, uint16_t peerPort, const IClientCallback::Ptr &callback,
51                                      ClientPtr &clientPtr, const std::string &localIp, uint16_t localPort)
52 {
53     SHARING_LOGD("trace.");
54     clientPtr = std::make_shared<TcpClient>();
55     if (clientPtr) {
56         clientPtr->RegisterCallback(callback);
57         if (clientPtr->Connect(peerIp, peerPort, localIp, localPort)) {
58             return true;
59         }
60     }
61     SHARING_LOGE("createTcpClient failed!");
62     return false;
63 }
64 
CreateUdpServer(const uint16_t localPort,const std::string & localIp,const IServerCallback::Ptr & callback,ServerPtr & serverPtr)65 bool NetworkFactory::CreateUdpServer(const uint16_t localPort, const std::string &localIp,
66                                      const IServerCallback::Ptr &callback, ServerPtr &serverPtr)
67 {
68     SHARING_LOGD("trace.");
69     serverPtr = std::make_shared<UdpServer>();
70     if (serverPtr) {
71         serverPtr->RegisterCallback(callback);
72         if (serverPtr->Start(localPort, localIp)) {
73             return true;
74         }
75     }
76     SHARING_LOGE("createUdpServer failed!");
77     return false;
78 }
79 
CreateUdpClient(const std::string & peerIp,uint16_t peerPort,const std::string & localIp,uint16_t localPort,const IClientCallback::Ptr & callback,ClientPtr & clientPtr)80 bool NetworkFactory::CreateUdpClient(const std::string &peerIp, uint16_t peerPort, const std::string &localIp,
81                                      uint16_t localPort, const IClientCallback::Ptr &callback, ClientPtr &clientPtr)
82 {
83     SHARING_LOGD("trace.");
84     clientPtr = std::make_shared<UdpClient>();
85     if (clientPtr) {
86         clientPtr->RegisterCallback(callback);
87         if (clientPtr->Connect(peerIp, peerPort, localIp, localPort)) {
88             return true;
89         }
90     }
91     SHARING_LOGE("createUdpClient failed!");
92     return false;
93 }
94 } // namespace Sharing
95 } // namespace OHOS