• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef OSP_PUBLIC_PROTOCOL_CONNECTION_SERVER_H_
6 #define OSP_PUBLIC_PROTOCOL_CONNECTION_SERVER_H_
7 
8 #include <memory>
9 #include <ostream>
10 #include <string>
11 #include <vector>
12 
13 #include "osp/public/endpoint_request_ids.h"
14 #include "osp/public/message_demuxer.h"
15 #include "osp/public/protocol_connection.h"
16 #include "osp/public/server_config.h"
17 #include "platform/base/error.h"
18 #include "platform/base/ip_address.h"
19 #include "platform/base/macros.h"
20 
21 namespace openscreen {
22 namespace osp {
23 
24 class ProtocolConnectionServer {
25  public:
26   enum class State {
27     kStopped = 0,
28     kStarting,
29     kRunning,
30     kStopping,
31     kSuspended,
32   };
33 
34   class Observer : public ProtocolConnectionServiceObserver {
35    public:
36     virtual ~Observer() = default;
37 
38     // Called when the state becomes kSuspended.
39     virtual void OnSuspended() = 0;
40 
41     virtual void OnIncomingConnection(
42         std::unique_ptr<ProtocolConnection> connection) = 0;
43   };
44 
45   virtual ~ProtocolConnectionServer();
46 
47   // Starts the server, listening for new connections on the endpoints in the
48   // config object.  Returns true if state() == kStopped and the service will be
49   // started, false otherwise.
50   virtual bool Start() = 0;
51 
52   // Stops the server and frees any resources associated with the server
53   // instance.  Returns true if state() != (kStopped|kStopping).
54   virtual bool Stop() = 0;
55 
56   // NOTE: We need to decide if suspend/resume semantics for QUIC connections
57   // are well defined, and if we can resume the server and existing connections
58   // in a consistent and useful state.
59 
60   // Temporarily stops accepting new connections and sending/receiving data on
61   // existing connections.  Any resources associated with existing connections
62   // are not freed.
63   virtual bool Suspend() = 0;
64 
65   // Resumes exchange of data on existing connections and acceptance of new
66   // connections.
67   virtual bool Resume() = 0;
68 
69   // Synchronously open a new connection to an endpoint identified by
70   // |endpoint_id|.  Returns nullptr if it can't be completed synchronously
71   // (e.g. there are no existing open connections to that endpoint).
72   virtual std::unique_ptr<ProtocolConnection> CreateProtocolConnection(
73       uint64_t endpoint_id) = 0;
74 
message_demuxer()75   MessageDemuxer* message_demuxer() const { return demuxer_; }
76 
endpoint_request_ids()77   EndpointRequestIds* endpoint_request_ids() { return &endpoint_request_ids_; }
78 
79   // Returns the current state of the listener.
state()80   State state() const { return state_; }
81 
82   // Returns the last error reported by this client.
last_error()83   const Error& last_error() const { return last_error_; }
84 
85  protected:
86   explicit ProtocolConnectionServer(MessageDemuxer* demuxer,
87                                     Observer* observer);
88 
89   State state_ = State::kStopped;
90   Error last_error_;
91   MessageDemuxer* const demuxer_;
92   EndpointRequestIds endpoint_request_ids_;
93   Observer* const observer_;
94 
95   OSP_DISALLOW_COPY_AND_ASSIGN(ProtocolConnectionServer);
96 };
97 
98 std::ostream& operator<<(std::ostream& os,
99                          ProtocolConnectionServer::State state);
100 
101 }  // namespace osp
102 }  // namespace openscreen
103 
104 #endif  // OSP_PUBLIC_PROTOCOL_CONNECTION_SERVER_H_
105