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/RunLoop.h> 20 21 #include <sys/socket.h> 22 23 struct BufferedSocket { 24 explicit BufferedSocket(std::shared_ptr<RunLoop> rl, int sock); 25 virtual ~BufferedSocket(); 26 27 BufferedSocket(const BufferedSocket &) = delete; 28 BufferedSocket &operator=(const BufferedSocket &) = delete; 29 30 virtual void postRecv(RunLoop::AsyncFunction fn) = 0; 31 virtual void postSend(RunLoop::AsyncFunction fn) = 0; 32 recvBufferedSocket33 ssize_t recv(void *data, size_t size) { 34 return recvfrom(data, size, nullptr, nullptr); 35 } 36 37 virtual ssize_t recvfrom( 38 void *data, 39 size_t size, 40 sockaddr *address, 41 socklen_t *addressLen) = 0; 42 sendBufferedSocket43 ssize_t send(const void *data, size_t size) { 44 return sendto(data, size, nullptr, 0); 45 } 46 47 virtual ssize_t sendto( 48 const void *data, 49 size_t size, 50 const sockaddr *addr, 51 socklen_t addrLen) = 0; 52 53 virtual void postFlush(RunLoop::AsyncFunction fn) = 0; 54 55 int fd() const; 56 57 protected: 58 RunLoop *runLoop(); 59 60 private: 61 std::shared_ptr<RunLoop> mRunLoop; 62 int mSock; 63 }; 64