1 /*
2 * Copyright (C) 2021 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 #include <sstream>
16 #include <cstring>
17 #include <sys/socket.h>
18 #include <arpa/inet.h>
19 #include <unistd.h>
20 #include "include/sp_server_socket.h"
21 #include "include/sp_log.h"
22 namespace OHOS {
23 namespace SmartPerf {
SpServerSocket()24 SpServerSocket::SpServerSocket() : sockPort(0) {}
25
~SpServerSocket()26 SpServerSocket::~SpServerSocket()
27 {
28 Close();
29 }
30
Init(ProtoType type)31 int SpServerSocket::Init(ProtoType type)
32 {
33 if (type == ProtoType::TCP) {
34 sock = socket(AF_INET, SOCK_STREAM, 0);
35 sockPort = tcpPort;
36 }
37 if (type == ProtoType::UDP || type == ProtoType::UDPEX) {
38 sock = socket(AF_INET, SOCK_DGRAM, 0);
39 sockPort = (type == ProtoType::UDP ? udpPort : udpExPort);
40 }
41 if (sock < 0) {
42 LOGE("SpServerSocket::Init Socket Create Failed");
43 return -1;
44 }
45 if (type == ProtoType::TCP) {
46 int optval = 1;
47 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
48 LOGE("SpServerSocket::Init Socket Setsockopt failed prot(%d)", sockPort);
49 return -1;
50 }
51 }
52 local.sin_family = AF_INET;
53 local.sin_port = htons(sockPort);
54 local.sin_addr.s_addr = inet_addr("127.0.0.1");
55 if (::bind(sock, reinterpret_cast<struct sockaddr *>(&local), sizeof(local)) < 0) {
56 LOGE("SpServerSocket::Init Socket Bind failed prot(%d)", sockPort);
57 return -1;
58 }
59 if (type == ProtoType::TCP) {
60 if (listen(sock, listenCount) < 0) {
61 LOGE("SpServerSocket::Init Socket Listen failed");
62 return -1;
63 }
64 }
65
66 LOGD("SpServerSocket::Init OK,prot(%d)", sockPort);
67 return 0;
68 }
69
Accept()70 int SpServerSocket::Accept()
71 {
72 connFd = accept(sock, nullptr, nullptr);
73 return connFd;
74 }
75
Sendto(const std::string & sendBuf)76 int SpServerSocket::Sendto(const std::string &sendBuf)
77 {
78 LOGD("SpServerSocket Sendto sendBuf(%s)", sendBuf.c_str());
79 socklen_t len = sizeof(sockaddr_in);
80 sendto(sock, sendBuf.c_str(), sendBuf.size(), 0, reinterpret_cast<struct sockaddr *>(&client), len);
81 return 0;
82 }
83
Send(const std::string & sendBuf) const84 int SpServerSocket::Send(const std::string &sendBuf) const
85 {
86 int sendBytes = send(connFd, sendBuf.c_str(), sendBuf.size(), 0);
87 if (sendBytes < 0) {
88 LOGE("SpServerSocket::Send Error(%d) fd(%d)", sendBytes, connFd);
89 return -1;
90 }
91 return 0;
92 }
93
Close()94 void SpServerSocket::Close()
95 {
96 int optval = 1;
97 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
98 local.sin_family = AF_INET;
99 local.sin_port = htons(sockPort);
100 local.sin_addr.s_addr = inet_addr("127.0.0.1");
101 if (::bind(sock, reinterpret_cast<struct sockaddr *>(&local), sizeof(local)) < 0) {
102 LOGE("SpServerSocket::Init Socket Bind failed");
103 }
104 }
105
Recvfrom()106 int SpServerSocket::Recvfrom()
107 {
108 bzero(rbuf, sizeof(rbuf));
109 socklen_t len = sizeof(sockaddr_in);
110 int l = recvfrom(sock, rbuf, sizeof(rbuf) - 1, 0, reinterpret_cast<struct sockaddr *>(&client), &len);
111 if (l > 0) {
112 std::cout << "Client:" << rbuf << std::endl;
113 } else {
114 LOGE("SpServerSocket::Recvfrom Error(%d)", l);
115 return -1;
116 }
117 return l;
118 }
119
Recv()120 int SpServerSocket::Recv()
121 {
122 bzero(rbuf, sizeof(rbuf));
123 int l = recv(connFd, rbuf, sizeof(rbuf) - 1, 0);
124 if (l > 0) {
125 std::cout << "Client:" << rbuf << std::endl;
126 } else {
127 LOGE("SpServerSocket::recv Error(%d) fd(%d)", l, connFd);
128 return -1;
129 }
130 return l;
131 }
132
RecvBuf() const133 std::string SpServerSocket::RecvBuf() const
134 {
135 std::string recvBuf = rbuf;
136 return recvBuf;
137 }
138
139 }
140 }
141