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 GetRemoteEndpoint() const override; 38 39 // Registers |this| with the platform TlsDataRouterPosix. This is called 40 // automatically by TlsConnectionFactoryPosix after the handshake completes. 41 void RegisterConnectionWithDataRouter(PlatformClientPosix* platform_client); 42 socket_handle()43 const SocketHandle& socket_handle() const { return socket_->socket_handle(); } 44 45 protected: 46 friend class TlsConnectionFactoryPosix; 47 48 TlsConnectionPosix(IPEndpoint local_address, TaskRunner* task_runner); 49 TlsConnectionPosix(IPAddress::Version version, TaskRunner* task_runner); 50 TlsConnectionPosix(std::unique_ptr<StreamSocket> socket, 51 TaskRunner* task_runner); 52 53 private: 54 // Called on any thread, to post a task to notify the Client that an |error| 55 // has occurred. 56 void DispatchError(Error error); 57 58 TaskRunner* const task_runner_; 59 PlatformClientPosix* platform_client_ = nullptr; 60 61 Client* client_ = nullptr; 62 63 std::unique_ptr<StreamSocket> socket_; 64 bssl::UniquePtr<SSL> ssl_; 65 66 TlsWriteBuffer buffer_; 67 68 WeakPtrFactory<TlsConnectionPosix> weak_factory_{this}; 69 70 OSP_DISALLOW_COPY_AND_ASSIGN(TlsConnectionPosix); 71 }; 72 73 } // namespace openscreen 74 75 #endif // PLATFORM_IMPL_TLS_CONNECTION_POSIX_H_ 76