1 /* 2 * Copyright (c) 2024 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 RS_PROFILER_SOCKET_H 17 #define RS_PROFILER_SOCKET_H 18 19 #include <cstddef> 20 #include <cstdint> 21 22 namespace OHOS::Rosen { 23 24 enum class SocketState { 25 INITIAL, 26 CREATE, 27 CONNECTED, 28 SHUTDOWN 29 }; 30 31 class Socket final { 32 public: 33 Socket() = default; 34 Socket(const Socket& other) = delete; 35 Socket(Socket&& other) = delete; 36 Socket& operator=(const Socket& other) = delete; 37 Socket& operator=(Socket&& other) = delete; 38 ~Socket(); 39 40 SocketState GetState() const; 41 bool Connected() const; 42 void Shutdown(); 43 void Open(uint16_t port); 44 void AcceptClient(); 45 46 bool SendWhenReady(const void* data, size_t size); 47 int PollSend(int timeout); 48 int PollReceive(int timeout); 49 50 size_t Available(); 51 bool Receive(void* data, size_t& size); 52 bool ReceiveWhenReady(void* data, size_t size); 53 54 private: 55 int32_t socket_ = -1; 56 int32_t client_ = -1; 57 SocketState state_ = SocketState::INITIAL; 58 }; 59 60 } // namespace OHOS::Rosen 61 62 #endif // RS_PROFILER_SOCKET_H