• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <json/json.h>
30 #include <libwebsockets.h>
31 
32 namespace cuttlefish {
33 namespace webrtc_streaming {
34 
35 struct ServerConfig {
36   enum class Security {
37     kInsecure,
38     kAllowSelfSigned,
39     kStrict,
40   };
41 
42   // The ip address or domain name of the operator server.
43   std::string addr;
44   int port;
45   // The path component of the operator server's register url.
46   std::string path;
47   // The security level to use when connecting to the operator server.
48   Security security;
49   // A list of key value pairs to include as HTTP handshake headers when
50   // connecting to the operator.
51   std::vector<std::pair<std::string, std::string>> http_headers;
52 };
53 
54 class ServerConnectionObserver {
55  public:
56   virtual ~ServerConnectionObserver() = default;
57   // Called when the connection to the server has been established. This is the
58   // cue to start using Send().
59   virtual void OnOpen() = 0;
60   virtual void OnClose() = 0;
61   // Called when the connection to the server has failed with an unrecoverable
62   // error.
63   virtual void OnError(const std::string& error) = 0;
64   virtual void OnReceive(const uint8_t* msg, size_t length, bool is_binary) = 0;
65 };
66 
67 // Represents a connection to the signaling server. When a connection is created
68 // it connects with the server automatically but sends no info.
69 // Only Send() can be called from multiple threads simultaneously. Reconnect(),
70 // Send() and the destructor will run into race conditions if called
71 // concurrently.
72 class ServerConnection {
73  public:
74   static std::unique_ptr<ServerConnection> Connect(
75       const ServerConfig& conf,
76       std::weak_ptr<ServerConnectionObserver> observer);
77 
78   // Destroying the connection will disconnect from the signaling server and
79   // release any open fds.
80   virtual ~ServerConnection() = default;
81 
82   // Sends data to the server encoded as JSON.
83   virtual bool Send(const Json::Value&) = 0;
84 
85   // makes re-connect request
86   virtual void Reconnect();
87 
88  private:
89   virtual void Connect() = 0;
90 };
91 
92 }  // namespace webrtc_streaming
93 }  // namespace cuttlefish
94