1 // Copyright 2019 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 #include "cast/receiver/public/receiver_socket_factory.h"
6 
7 #include "util/osp_logging.h"
8 
9 namespace openscreen {
10 namespace cast {
11 
ReceiverSocketFactory(Client * client,CastSocket::Client * socket_client)12 ReceiverSocketFactory::ReceiverSocketFactory(Client* client,
13                                              CastSocket::Client* socket_client)
14     : client_(client), socket_client_(socket_client) {
15   OSP_DCHECK(client);
16   OSP_DCHECK(socket_client);
17 }
18 
19 ReceiverSocketFactory::~ReceiverSocketFactory() = default;
20 
OnAccepted(TlsConnectionFactory * factory,std::vector<uint8_t> der_x509_peer_cert,std::unique_ptr<TlsConnection> connection)21 void ReceiverSocketFactory::OnAccepted(
22     TlsConnectionFactory* factory,
23     std::vector<uint8_t> der_x509_peer_cert,
24     std::unique_ptr<TlsConnection> connection) {
25   IPEndpoint endpoint = connection->GetRemoteEndpoint();
26   auto socket =
27       std::make_unique<CastSocket>(std::move(connection), socket_client_);
28   client_->OnConnected(this, endpoint, std::move(socket));
29 }
30 
OnConnected(TlsConnectionFactory * factory,std::vector<uint8_t> der_x509_peer_cert,std::unique_ptr<TlsConnection> connection)31 void ReceiverSocketFactory::OnConnected(
32     TlsConnectionFactory* factory,
33     std::vector<uint8_t> der_x509_peer_cert,
34     std::unique_ptr<TlsConnection> connection) {
35   OSP_LOG_FATAL << "This factory is accept-only";
36 }
37 
OnConnectionFailed(TlsConnectionFactory * factory,const IPEndpoint & remote_address)38 void ReceiverSocketFactory::OnConnectionFailed(
39     TlsConnectionFactory* factory,
40     const IPEndpoint& remote_address) {
41   OSP_DVLOG << "Receiving connection from endpoint failed: " << remote_address;
42   client_->OnError(this, Error(Error::Code::kConnectionFailed,
43                                "Accepting connection failed."));
44 }
45 
OnError(TlsConnectionFactory * factory,Error error)46 void ReceiverSocketFactory::OnError(TlsConnectionFactory* factory,
47                                     Error error) {
48   client_->OnError(this, error);
49 }
50 
51 }  // namespace cast
52 }  // namespace openscreen
53