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 PLATFORM_IMPL_TLS_CONNECTION_POSIX_H_ 6 #define PLATFORM_IMPL_TLS_CONNECTION_POSIX_H_ 7 8 #include <openssl/ssl.h> 9 10 #include <memory> 11 12 #include "platform/api/tls_connection.h" 13 #include "platform/impl/platform_client_posix.h" 14 #include "platform/impl/stream_socket_posix.h" 15 #include "platform/impl/tls_write_buffer.h" 16 #include "util/weak_ptr.h" 17 18 namespace openscreen { 19 20 class TaskRunner; 21 class TlsConnectionFactoryPosix; 22 23 class TlsConnectionPosix : public TlsConnection { 24 public: 25 ~TlsConnectionPosix() override; 26 27 // Sends any available bytes from this connection's buffer_. 28 virtual void SendAvailableBytes(); 29 30 // Read out a block/message, if one is available, and notify this instance's 31 // TlsConnection::Client. 32 virtual void TryReceiveMessage(); 33 34 // TlsConnection overrides. 35 void SetClient(Client* client) override; 36 bool Send(const void* data, size_t len) override; 37 IPEndpoint GetLocalEndpoint() const override; 38 IPEndpoint GetRemoteEndpoint() const override; 39 40 // Registers |this| with the platform TlsDataRouterPosix. This is called 41 // automatically by TlsConnectionFactoryPosix after the handshake completes. 42 void RegisterConnectionWithDataRouter(PlatformClientPosix* platform_client); 43 socket_handle()44 const SocketHandle& socket_handle() const { return socket_->socket_handle(); } 45 46 protected: 47 friend class TlsConnectionFactoryPosix; 48 49 TlsConnectionPosix(IPEndpoint local_address, TaskRunner* task_runner); 50 TlsConnectionPosix(IPAddress::Version version, TaskRunner* task_runner); 51 TlsConnectionPosix(std::unique_ptr<StreamSocket> socket, 52 TaskRunner* task_runner); 53 54 private: 55 // Called on any thread, to post a task to notify the Client that an |error| 56 // has occurred. 57 void DispatchError(Error error); 58 59 TaskRunner* const task_runner_; 60 PlatformClientPosix* platform_client_ = nullptr; 61 62 Client* client_ = nullptr; 63 64 std::unique_ptr<StreamSocket> socket_; 65 bssl::UniquePtr<SSL> ssl_; 66 67 TlsWriteBuffer buffer_; 68 69 WeakPtrFactory<TlsConnectionPosix> weak_factory_{this}; 70 71 OSP_DISALLOW_COPY_AND_ASSIGN(TlsConnectionPosix); 72 }; 73 74 } // namespace openscreen 75 76 #endif // PLATFORM_IMPL_TLS_CONNECTION_POSIX_H_ 77