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(int32_t sockId,int32_t domain)25 HttpSocket(int32_t sockId, int32_t domain) : sockId_(sockId), domain_(domain) {} 26 ~HttpSocket(); 27 28 bool CreateSocket(int32_t domain); 29 bool Bind(int32_t port); 30 bool Listen(int32_t 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 int32_t GetFd() const 40 { 41 return sockId_; 42 } 43 44 private: 45 int32_t sockId_ = -1; 46 int32_t domain_ = 0; 47 using SOCKET = int32_t; 48 #if !is_mingw 49 const int32_t SOCKET_ERROR = -1; 50 #endif 51 const SOCKET INVALID_SOCKET = -1; 52 }; 53 } // namespace TraceStreamer 54 } // namespace SysTuning 55 #endif // RPC_HTTPSOCKET_H 56