1 2 // Copyright (C) 2021 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 // limitations under the License. 15 #pragma once 16 #include <chrono> // for operator""ms, chrono_literals 17 #include <memory> // for shared_ptr 18 #include <string> // for string 19 20 #include "net/async_data_channel_connector.h" // for AsyncDataChannelConnector 21 22 namespace android { 23 namespace net { 24 class AsyncDataChannel; 25 } // namespace net 26 } // namespace android 27 28 namespace rootcanal { 29 class AsyncManager; 30 } // namespace rootcanal 31 32 using rootcanal::AsyncManager; 33 namespace android { 34 namespace net { 35 36 using namespace std::chrono_literals; 37 38 // A Posix compliant implementation of the AsyncDataChannelConnector interface. 39 // 40 // Supports both Darwin (freebsd) and linux. 41 class PosixAsyncSocketConnector : public AsyncDataChannelConnector { 42 public: 43 PosixAsyncSocketConnector(AsyncManager* am); 44 ~PosixAsyncSocketConnector() = default; 45 46 // Blocks and waits until a connection to the remote server has been 47 // established. Returns null in case of failure. In case of null the errno 48 // variable will be set with the encountered error. 49 // 50 // Note: This does not mean that the socket is fully opened! A server 51 // might not (yet?) have called accept on the socket. 52 std::shared_ptr<AsyncDataChannel> ConnectToRemoteServer( 53 const std::string& server, int port, 54 std::chrono::milliseconds timeout = 5000ms); 55 56 private: 57 AsyncManager* am_; 58 }; 59 } // namespace net 60 } // namespace android 61