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 #include <https/PlainSocket.h>
18 
19 #include <sys/socket.h>
20 
PlainSocket(std::shared_ptr<RunLoop> rl,int sock)21 PlainSocket::PlainSocket(std::shared_ptr<RunLoop> rl, int sock)
22     : BufferedSocket(rl, sock) {
23 }
24 
postRecv(RunLoop::AsyncFunction fn)25 void PlainSocket::postRecv(RunLoop::AsyncFunction fn) {
26     runLoop()->postSocketRecv(fd(), fn);
27 }
28 
postSend(RunLoop::AsyncFunction fn)29 void PlainSocket::postSend(RunLoop::AsyncFunction fn) {
30     runLoop()->postSocketSend(fd(), fn);
31 }
32 
recvfrom(void * data,size_t size,sockaddr * address,socklen_t * addressLen)33 ssize_t PlainSocket::recvfrom(
34         void *data,
35         size_t size,
36         sockaddr *address,
37         socklen_t *addressLen) {
38     return ::recvfrom(fd(), data, size, 0, address, addressLen);
39 }
40 
sendto(const void * data,size_t size,const sockaddr * addr,socklen_t addrLen)41 ssize_t PlainSocket::sendto(
42         const void *data,
43         size_t size,
44         const sockaddr *addr,
45         socklen_t addrLen) {
46     if (!addr) {
47         return ::send(fd(), data, size, 0);
48     }
49     return ::sendto(fd(), data, size, 0, addr, addrLen);
50 }
51 
postFlush(RunLoop::AsyncFunction fn)52 void PlainSocket::postFlush(RunLoop::AsyncFunction fn) {
53     fn();
54 }
55