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