1 //
2 // Copyright (C) 2020 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 //
15 
16 #pragma once
17 
18 #include <string.h>
19 
20 #include <deque>
21 #include <functional>
22 #include <map>
23 #include <memory>
24 #include <mutex>
25 #include <string>
26 #include <thread>
27 #include <vector>
28 
29 #include <libwebsockets.h>
30 
31 class WsConnectionObserver {
32  public:
33   virtual ~WsConnectionObserver() = default;
34   virtual void OnOpen() = 0;
35   virtual void OnClose() = 0;
36   virtual void OnError(const std::string& error) = 0;
37   virtual void OnReceive(const uint8_t* msg, size_t length, bool is_binary) = 0;
38 };
39 
40 class WsConnection {
41  public:
42   enum class Security {
43     kInsecure,
44     kAllowSelfSigned,
45     kStrict,
46   };
47 
48   virtual ~WsConnection() = default;
49 
50   virtual void Connect() = 0;
51 
52   virtual bool Send(const uint8_t* data, size_t len, bool binary = false) = 0;
53 
54  protected:
55   WsConnection() = default;
56 };
57 
58 class WsConnectionContext {
59  public:
60   static std::shared_ptr<WsConnectionContext> Create();
61 
62   virtual ~WsConnectionContext() = default;
63 
64   virtual std::shared_ptr<WsConnection> CreateConnection(
65       int port, const std::string& addr, const std::string& path,
66       WsConnection::Security secure,
67       std::weak_ptr<WsConnectionObserver> observer) = 0;
68 
69  protected:
70   WsConnectionContext() = default;
71 };
72