106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci#include <sstream>
1606f6ba60Sopenharmony_ci#include <cstring>
1706f6ba60Sopenharmony_ci#include <sys/socket.h>
1806f6ba60Sopenharmony_ci#include <arpa/inet.h>
1906f6ba60Sopenharmony_ci#include <unistd.h>
2006f6ba60Sopenharmony_ci#include "include/sp_server_socket.h"
2106f6ba60Sopenharmony_ci#include "include/sp_log.h"
2206f6ba60Sopenharmony_cinamespace OHOS {
2306f6ba60Sopenharmony_cinamespace SmartPerf {
2406f6ba60Sopenharmony_ciSpServerSocket::SpServerSocket() : sockPort(0) {}
2506f6ba60Sopenharmony_ci
2606f6ba60Sopenharmony_ciSpServerSocket::~SpServerSocket()
2706f6ba60Sopenharmony_ci{
2806f6ba60Sopenharmony_ci    Close();
2906f6ba60Sopenharmony_ci}
3006f6ba60Sopenharmony_ci
3106f6ba60Sopenharmony_ciint SpServerSocket::Init(ProtoType type)
3206f6ba60Sopenharmony_ci{
3306f6ba60Sopenharmony_ci    if (type == ProtoType::TCP) {
3406f6ba60Sopenharmony_ci        sock = socket(AF_INET, SOCK_STREAM, 0);
3506f6ba60Sopenharmony_ci        sockPort = tcpPort;
3606f6ba60Sopenharmony_ci    }
3706f6ba60Sopenharmony_ci    if (type == ProtoType::UDP || type == ProtoType::UDPEX) {
3806f6ba60Sopenharmony_ci        sock = socket(AF_INET, SOCK_DGRAM, 0);
3906f6ba60Sopenharmony_ci        sockPort = (type == ProtoType::UDP ? udpPort : udpExPort);
4006f6ba60Sopenharmony_ci    }
4106f6ba60Sopenharmony_ci    if (sock < 0) {
4206f6ba60Sopenharmony_ci        LOGE("SpServerSocket::Init Socket Create Failed");
4306f6ba60Sopenharmony_ci        return -1;
4406f6ba60Sopenharmony_ci    }
4506f6ba60Sopenharmony_ci    if (type == ProtoType::TCP) {
4606f6ba60Sopenharmony_ci        int optval = 1;
4706f6ba60Sopenharmony_ci        if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
4806f6ba60Sopenharmony_ci            LOGE("SpServerSocket::Init Socket Setsockopt failed prot(%d)", sockPort);
4906f6ba60Sopenharmony_ci            return -1;
5006f6ba60Sopenharmony_ci        }
5106f6ba60Sopenharmony_ci    }
5206f6ba60Sopenharmony_ci    local.sin_family = AF_INET;
5306f6ba60Sopenharmony_ci    local.sin_port = htons(sockPort);
5406f6ba60Sopenharmony_ci    local.sin_addr.s_addr = inet_addr("127.0.0.1");
5506f6ba60Sopenharmony_ci    if (::bind(sock, reinterpret_cast<struct sockaddr *>(&local), sizeof(local)) < 0) {
5606f6ba60Sopenharmony_ci        LOGE("SpServerSocket::Init Socket Bind failed prot(%d)", sockPort);
5706f6ba60Sopenharmony_ci        return -1;
5806f6ba60Sopenharmony_ci    }
5906f6ba60Sopenharmony_ci    if (type == ProtoType::TCP) {
6006f6ba60Sopenharmony_ci        if (listen(sock, listenCount) < 0) {
6106f6ba60Sopenharmony_ci            LOGE("SpServerSocket::Init Socket Listen failed");
6206f6ba60Sopenharmony_ci            return -1;
6306f6ba60Sopenharmony_ci        }
6406f6ba60Sopenharmony_ci    }
6506f6ba60Sopenharmony_ci
6606f6ba60Sopenharmony_ci    LOGI("SpServerSocket::Init OK,prot(%d)", sockPort);
6706f6ba60Sopenharmony_ci    return 0;
6806f6ba60Sopenharmony_ci}
6906f6ba60Sopenharmony_ci
7006f6ba60Sopenharmony_ciint SpServerSocket::Accept()
7106f6ba60Sopenharmony_ci{
7206f6ba60Sopenharmony_ci    connFd = accept(sock, nullptr, nullptr);
7306f6ba60Sopenharmony_ci    return connFd;
7406f6ba60Sopenharmony_ci}
7506f6ba60Sopenharmony_ci
7606f6ba60Sopenharmony_ciint SpServerSocket::Sendto(std::string &sendBuf)
7706f6ba60Sopenharmony_ci{
7806f6ba60Sopenharmony_ci    socklen_t len = sizeof(sockaddr_in);
7906f6ba60Sopenharmony_ci    sendto(sock, sendBuf.c_str(), sendBuf.size(), 0, reinterpret_cast<struct sockaddr *>(&client), len);
8006f6ba60Sopenharmony_ci    return 0;
8106f6ba60Sopenharmony_ci}
8206f6ba60Sopenharmony_ci
8306f6ba60Sopenharmony_ciint SpServerSocket::Send(const std::string &sendBuf) const
8406f6ba60Sopenharmony_ci{
8506f6ba60Sopenharmony_ci    int sendBytes = send(connFd, sendBuf.c_str(), sendBuf.size(), 0);
8606f6ba60Sopenharmony_ci    if (sendBytes < 0) {
8706f6ba60Sopenharmony_ci        LOGE("SpServerSocket::Send Error(%d) fd(%d)", sendBytes, connFd);
8806f6ba60Sopenharmony_ci        return -1;
8906f6ba60Sopenharmony_ci    }
9006f6ba60Sopenharmony_ci    return 0;
9106f6ba60Sopenharmony_ci}
9206f6ba60Sopenharmony_ci
9306f6ba60Sopenharmony_civoid SpServerSocket::Close()
9406f6ba60Sopenharmony_ci{
9506f6ba60Sopenharmony_ci    int optval = 1;
9606f6ba60Sopenharmony_ci    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
9706f6ba60Sopenharmony_ci    local.sin_family = AF_INET;
9806f6ba60Sopenharmony_ci    local.sin_port = htons(sockPort);
9906f6ba60Sopenharmony_ci    local.sin_addr.s_addr = inet_addr("127.0.0.1");
10006f6ba60Sopenharmony_ci    if (::bind(sock, reinterpret_cast<struct sockaddr *>(&local), sizeof(local)) < 0) {
10106f6ba60Sopenharmony_ci        LOGI("SpServerSocket::Init Socket Bind failed");
10206f6ba60Sopenharmony_ci    }
10306f6ba60Sopenharmony_ci}
10406f6ba60Sopenharmony_ci
10506f6ba60Sopenharmony_ciint SpServerSocket::Recvfrom()
10606f6ba60Sopenharmony_ci{
10706f6ba60Sopenharmony_ci    bzero(rbuf, sizeof(rbuf));
10806f6ba60Sopenharmony_ci    socklen_t len = sizeof(sockaddr_in);
10906f6ba60Sopenharmony_ci    int l = recvfrom(sock, rbuf, sizeof(rbuf) - 1, 0, reinterpret_cast<struct sockaddr *>(&client), &len);
11006f6ba60Sopenharmony_ci    if (l > 0) {
11106f6ba60Sopenharmony_ci        std::cout << "Client:" << rbuf << std::endl;
11206f6ba60Sopenharmony_ci    }
11306f6ba60Sopenharmony_ci    return l;
11406f6ba60Sopenharmony_ci}
11506f6ba60Sopenharmony_ci
11606f6ba60Sopenharmony_ciint SpServerSocket::Recv()
11706f6ba60Sopenharmony_ci{
11806f6ba60Sopenharmony_ci    bzero(rbuf, sizeof(rbuf));
11906f6ba60Sopenharmony_ci    int l = recv(connFd, rbuf, sizeof(rbuf) - 1, 0);
12006f6ba60Sopenharmony_ci    if (l > 0) {
12106f6ba60Sopenharmony_ci        std::cout << "Client:" << rbuf << std::endl;
12206f6ba60Sopenharmony_ci    } else {
12306f6ba60Sopenharmony_ci        LOGE("SpServerSocket::recv Error(%d) fd(%d)", l, connFd);
12406f6ba60Sopenharmony_ci        return -1;
12506f6ba60Sopenharmony_ci    }
12606f6ba60Sopenharmony_ci    return l;
12706f6ba60Sopenharmony_ci}
12806f6ba60Sopenharmony_ci
12906f6ba60Sopenharmony_cistd::string SpServerSocket::RecvBuf() const
13006f6ba60Sopenharmony_ci{
13106f6ba60Sopenharmony_ci    std::string recvBuf = rbuf;
13206f6ba60Sopenharmony_ci    return recvBuf;
13306f6ba60Sopenharmony_ci}
13406f6ba60Sopenharmony_ci}
13506f6ba60Sopenharmony_ci}
136