• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SRC_INSPECTOR_SOCKET_H_
17 #define SRC_INSPECTOR_SOCKET_H_
18 
19 #include <string>
20 #include <vector>
21 
22 #include "inspector_utils.h"
23 #include "uv.h"
24 
25 namespace jsvm {
26 namespace inspector {
27 
28 class ProtocolHandler;
29 
30 // HTTP Wrapper around a uv_tcp_t
31 class InspectorSocket {
32 private:
33     InspectorSocket() = default;
34 public:
35     using Pointer = std::unique_ptr<InspectorSocket>;
36 
37     InspectorSocket(const InspectorSocket&) = delete;
38 
39     InspectorSocket& operator=(const InspectorSocket&) = delete;
40 
41     ~InspectorSocket();
42 
43     class Delegate {
44     public:
45         virtual void OnWsFrame(const std::vector<char>& frame) = 0;
46         virtual ~Delegate() = default;
47         virtual void OnHttpGet(const std::string& host, const std::string& path) = 0;
48         virtual void OnSocketUpgrade(const std::string& host,
49                                      const std::string& path,
50                                      const std::string& acceptKey) = 0;
51     };
52 
53     using DelegatePointer = std::unique_ptr<Delegate>;
54 
55     static Pointer Accept(uv_stream_t* server, DelegatePointer delegate);
56 
57     void AcceptUpgrade(const std::string& acceptKey);
58 
59     void SwitchProtocol(ProtocolHandler* handler);
60 
61     void Write(const char* data, size_t len);
62 
63     void CancelHandshake();
64 
65     std::string GetHost();
66 
67 private:
68     static void Shutdown(ProtocolHandler* handler);
69 
70     DeleteFnPtr<ProtocolHandler, Shutdown> protocolHandler;
71 };
72 
73 } // namespace inspector
74 } // namespace jsvm
75 
76 #endif // SRC_INSPECTOR_SOCKET_H_
77