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 16 #ifndef RPC_HTTPSOCKET_H 17 #define RPC_HTTPSOCKET_H 18 19 #include <string> 20 namespace SysTuning { 21 namespace TraceStreamer { 22 class HttpSocket { 23 public: HttpSocket()24 HttpSocket() {} HttpSocket(int sockId,int domain)25 HttpSocket(int sockId, int domain) : sockId_(sockId), domain_(domain) {} 26 ~HttpSocket(); 27 28 bool CreateSocket(int domain); 29 bool Bind(int port); 30 bool Listen(int maxConn); 31 bool Accept(HttpSocket& client); 32 bool Recv(void* data, size_t& len); 33 bool Send(const void* data, size_t len); 34 void Close(); IsValid()35 bool IsValid() const 36 { 37 return sockId_ != INVALID_SOCKET; 38 } GetFd()39 int GetFd() const 40 { 41 return sockId_; 42 } 43 44 private: 45 int sockId_ = -1; 46 int domain_ = 0; 47 using SOCKET = int; 48 const int SOCKET_ERROR = -1; 49 const SOCKET INVALID_SOCKET = -1; 50 }; 51 } // namespace TraceStreamer 52 } // namespace SysTuning 53 #endif // RPC_HTTPSOCKET_H 54