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