• 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 #ifndef SP_THREAD_SOCKET_H
16 #define SP_THREAD_SOCKET_H
17 #include "sp_server_socket.h"
18 namespace OHOS {
19 namespace SmartPerf {
20 enum class SocketConnectType {
21     CMD_SOCKET,
22     EDITOR_SOCKET,
23 };
24 
25 enum class SocketErrorType {
26     OK,
27     TOKEN_CHECK_FAILED,
28     INIT_FAILED,
29     START_FAILED,
30     STOP_FAILED,
31     START_RECORD_FAILED,
32     STOP_RECORD_FAILED,
33 };
34 class SpThreadSocket {
35 public:
GetInstance()36     static SpThreadSocket &GetInstance()
37     {
38         static SpThreadSocket instance;
39         return instance;
40     }
41 
42     std::string MapToString(std::map<std::string, std::string> dataMap) const;
43     std::string SplitMsg(const std::string &recvBuf) const;
44     void Process(ProtoType type);
45     SocketErrorType CheckToken(std::string recvStr, SpServerSocket &spSocket, std::string recvStrNoToken) const;
46     void TypeTcp(SpServerSocket &spSocket);
47     void InitRecv(std::string recvStr, SpServerSocket &spSocket, SocketConnectType type) const;
48     void StartRecv(SpServerSocket &spSocket);
49     void StartRecvRealtime(SpServerSocket &spSocket) const;
50     void StopRecvRealtime(SpServerSocket &spSocket);
51     void StartRecvRecord(SpServerSocket &spSocket) const;
52     void StopRecvRecord(SpServerSocket &spSocket) const;
53     void SendTokenFailedMessage(SpServerSocket &socket, std::string &message) const;
54     void DealMsg(std::string recvStr, SpServerSocket &spSocket, SocketErrorType tokenStatus);
55     void EditorRecv(std::string recvStr, const SpServerSocket &spSocket) const;
56     void BackDesktop() const;
57     void HandleMsg(SpServerSocket &spSocket) const;
58     void HandleUDPMsg(SpServerSocket &spSocket, std::map<std::string, std::string> data, std::string retCode,
59         std::unordered_map<MessageType, std::string>::const_iterator iterator) const;
60     void SocketHeartbeat() const;
61     void FetchCpuStats(SpServerSocket &spSocket, std::map<std::string, std::string> data) const;
62     void HandleNullMsg(SpServerSocket &spSocket, SpProfiler *profiler, std::string retCode, std::string recvBuf,
63         std::unordered_map<MessageType, std::string>::const_iterator iterator) const;
64     void HandleNullAddMsg(SpServerSocket &spSocket, SpProfiler *profiler, std::string retCode, std::string recvBuf,
65         std::unordered_map<MessageType, std::string>::const_iterator iterator) const;
66     std::string SocketErrorTypeToString(SocketErrorType errorType) const;
67     std::map<std::string, std::string> GetLogProcess(SpProfiler *profilerItem, std::string buffer) const;
68     void ResetValue(std::string retCode) const;
69     void GetProcessIdByPkgName(std::unordered_map<MessageType, std::string>::const_iterator iterator) const;
70     std::string SpGetPkg(const std::string &spMsg) const;
71     std::string SpGetPid(const std::string &spMsg) const;
72     bool IsPreset(const std::string &spMsg) const;
73 
74 private:
75     bool flagRunning = false;
76     bool socketConnect = true;
77 };
78 }
79 }
80 #endif