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 <iostream>
17 #include <sstream>
18 #include <thread>
19 #include <unistd.h>
20 #include "common/sharing_log.h"
21 #include "network/interfaces/iclient_callback.h"
22 #include "network/network_factory.h"
23
24 using namespace std;
25 using namespace OHOS::Sharing;
26
27 class ClientAgent final : public IClientCallback,
28 public std::enable_shared_from_this<ClientAgent> {
29 public:
~ClientAgent()30 ~ClientAgent()
31 {
32 SHARING_LOGD("trace.");
33 clientPtr_->Disconnect();
34 }
ClientAgent()35 ClientAgent()
36 {
37 SHARING_LOGD("trace.");
38 }
39
UdpConnect()40 void UdpConnect()
41 {
42 bool ret = NetworkFactory::CreateUdpClient("127.0.0.1", 9999, "127.0.0.1", 0, shared_from_this(), clientPtr_);
43 if (ret) {
44 SHARING_LOGD("CreateUdpClient success");
45 } else {
46 SHARING_LOGE("CreateUdpClient failed");
47 }
48 }
49
TcpConnect()50 void TcpConnect()
51 {
52 bool ret = NetworkFactory::CreateTcpClient("127.0.0.1", 8888, shared_from_this(), clientPtr_);
53 if (ret) {
54 SHARING_LOGD("CreateTcpClient success");
55 } else {
56 SHARING_LOGE("CreateTcpClient failed");
57 }
58 }
59
OnClientConnect(bool isSuccess)60 void OnClientConnect(bool isSuccess) override
61 {
62 if (isSuccess) {
63 SHARING_LOGD("OnClientConnect success");
64 } else {
65 SHARING_LOGE("OnClientConnect failed");
66 }
67 }
68
OnClientReadData(int32_t fd,DataBuffer::Ptr buf)69 void OnClientReadData(int32_t fd, DataBuffer::Ptr buf) override
70 {
71 SHARING_LOGD("OnClientReadData");
72 }
73
OnClientWriteable(int32_t fd)74 void OnClientWriteable(int32_t fd) override
75 {
76 SHARING_LOGD("OnClientWriteable");
77 static int index = 0;
78 string msg = "tcp/udp client OnClientConnect message.index=" + std::to_string(index++);
79 DataBuffer::Ptr bufSend = std::make_shared<DataBuffer>();
80 bufSend->PushData(msg.c_str(), msg.size());
81 clientPtr_->Send(bufSend, bufSend->Size());
82 }
83
OnClientClose(int32_t fd)84 void OnClientClose(int32_t fd) override
85 {
86 SHARING_LOGD("OnClientClose");
87 clientPtr_->Disconnect();
88 }
89
OnClientException(int32_t fd)90 void OnClientException(int32_t fd) override
91 {
92 SHARING_LOGD("OnClientException");
93 clientPtr_->Disconnect();
94 }
95
96 NetworkFactory::ClientPtr clientPtr_{nullptr};
97 };
98
main()99 int main()
100 {
101 SHARING_LOGD("trace");
102 std::thread run_client([]() {
103 std::string inputCmd;
104 auto clientAgent = std::make_shared<ClientAgent>();
105
106 SHARING_LOGD("Please input command.tcp-1,udp-2:");
107 getline(std::cin, inputCmd);
108 SHARING_LOGD("Get command: %{public}s.", inputCmd.c_str());
109 if (inputCmd == "quit") {
110 } else if (inputCmd == "1") {
111 clientAgent->TcpConnect();
112 } else if (inputCmd == "2") {
113 clientAgent->UdpConnect();
114 } else if (inputCmd == "3") {
115 SHARING_LOGD("send message:");
116 }
117 while (1) {
118 sleep(1);
119 }
120 });
121 run_client.join();
122 return 0;
123 }