• 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 
16 #ifndef SOCKET_CONTEXT_H
17 #define SOCKET_CONTEXT_H
18 
19 #include <cstdint>
20 #ifndef NO_PROTOBUF
21 #include <google/protobuf/message.h>
22 #endif
23 #include <thread>
24 
25 #if defined(__i386__) || defined(__x86_64__)
26 const static char DEFAULT_UNIX_SOCKET_PATH[] = "hiprofiler_unix_socket";
27 #else
28 const static char DEFAULT_UNIX_SOCKET_PATH[] = "/data/local/tmp/hiprofiler_unix_socket";
29 const static char DEFAULT_UNIX_SOCKET_HOOK_PATH[] = "/data/local/tmp/hook_unix_socket";
30 #endif
31 
32 class SocketContext;
33 class ServiceBase;
34 
35 enum ClientState {
36     CLIENT_STAT_WORKING,
37     CLIENT_STAT_WAIT_THREAD_EXIT,
38     CLIENT_STAT_THREAD_EXITED,
39 };
40 
41 enum RawProtocol {
42     RAW_PROTOCOL_POINTTO_SERVICE = 1,
43 };
44 struct RawPointToService {
45     char serviceName_[64];
46 };
47 
48 class SocketContext {
49 public:
50     SocketContext();
51     virtual ~SocketContext();
52 
53     bool SendRaw(uint32_t pnum, const int8_t* data, uint32_t size, int sockfd = -1);
54 #ifndef NO_PROTOBUF
55     bool SendProtobuf(uint32_t pnum, google::protobuf::Message& pmsg);
56 #endif
57     bool SendFileDescriptor(int fd);
58     bool SendHookConfig(uint64_t value);
59     int ReceiveFileDiscriptor();
GetClientState()60     enum ClientState GetClientState() { return clientState_; }
61 protected:
62     int socketHandle_;
63     bool CreateRecvThread();
64     enum ClientState clientState_;
65     uint64_t lastProcMS_;
66 
67     ServiceBase* serviceBase_;
68 
69     virtual int RawProtocolProc(uint32_t pnum, const int8_t* buf, const uint32_t size);
70 
71 private:
72     static bool ReceiveData(int sock, uint8_t* databuf, uint32_t size);
73     static void* UnixSocketRecv(void* pp);
74     std::thread recvThread_;
75 };
76 
77 #endif