1 /* 2 * Copyright (C) 2019 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 */ 16 17 #pragma once 18 19 #include <https/BufferedSocket.h> 20 #include <https/RunLoop.h> 21 22 #include <memory> 23 #include <vector> 24 25 struct BaseConnection : public std::enable_shared_from_this<BaseConnection> { 26 explicit BaseConnection(std::shared_ptr<RunLoop> runLoop, int sock); 27 virtual ~BaseConnection() = default; 28 29 void run(); 30 31 BaseConnection(const BaseConnection &) = delete; 32 BaseConnection &operator=(const BaseConnection &) = delete; 33 34 protected: 35 // Return -EAGAIN to indicate that not enough data was provided (yet). 36 // Return a positive (> 0) value to drain some amount of data. 37 // Return values <= 0 are considered an error. 38 virtual ssize_t processClientRequest(const void *data, size_t size) = 0; 39 40 virtual void onDisconnect(int err) = 0; 41 42 void send(const void *_data, size_t size); 43 44 int fd() const; 45 46 private: 47 std::shared_ptr<RunLoop> mRunLoop; 48 49 std::unique_ptr<BufferedSocket> mSocket; 50 51 std::vector<uint8_t> mInBuffer; 52 size_t mInBufferLen; 53 54 bool mSendPending; 55 std::vector<uint8_t> mOutBuffer; 56 57 void receiveClientRequest(); 58 void sendOutputData(); 59 60 void onClientRequest(); 61 }; 62