keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | #include "SocketStream.h" |
| 17 | #include <cutils/sockets.h> |
| 18 | #include <errno.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <unistd.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | #ifndef _WIN32 |
| 25 | #include <netinet/in.h> |
| 26 | #include <netinet/tcp.h> |
| 27 | #include <sys/un.h> |
| 28 | #else |
| 29 | #include <ws2tcpip.h> |
| 30 | #endif |
| 31 | |
| 32 | SocketStream::SocketStream(size_t bufSize) : |
| 33 | IOStream(bufSize), |
| 34 | m_sock(-1), |
| 35 | m_bufsize(bufSize), |
| 36 | m_buf(NULL) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | SocketStream::SocketStream(int sock, size_t bufSize) : |
| 41 | IOStream(bufSize), |
| 42 | m_sock(sock), |
| 43 | m_bufsize(bufSize), |
| 44 | m_buf(NULL) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | SocketStream::~SocketStream() |
| 49 | { |
| 50 | if (m_sock >= 0) { |
| 51 | #ifdef _WIN32 |
| 52 | closesocket(m_sock); |
| 53 | #else |
| 54 | ::close(m_sock); |
| 55 | #endif |
| 56 | } |
| 57 | if (m_buf != NULL) { |
| 58 | free(m_buf); |
| 59 | m_buf = NULL; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | |
| 64 | void *SocketStream::allocBuffer(size_t minSize) |
| 65 | { |
| 66 | size_t allocSize = (m_bufsize < minSize ? minSize : m_bufsize); |
| 67 | if (!m_buf) { |
| 68 | m_buf = (unsigned char *)malloc(allocSize); |
| 69 | } |
| 70 | else if (m_bufsize < allocSize) { |
| 71 | unsigned char *p = (unsigned char *)realloc(m_buf, allocSize); |
| 72 | if (p != NULL) { |
| 73 | m_buf = p; |
| 74 | m_bufsize = allocSize; |
| 75 | } else { |
| 76 | ERR("%s: realloc (%zu) failed\n", __FUNCTION__, allocSize); |
| 77 | free(m_buf); |
| 78 | m_buf = NULL; |
| 79 | m_bufsize = 0; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return m_buf; |
| 84 | }; |
| 85 | |
| 86 | int SocketStream::commitBuffer(size_t size) |
| 87 | { |
| 88 | return writeFully(m_buf, size); |
| 89 | } |
| 90 | |
| 91 | int SocketStream::writeFully(const void* buffer, size_t size) |
| 92 | { |
| 93 | if (!valid()) return -1; |
| 94 | |
| 95 | size_t res = size; |
| 96 | int retval = 0; |
| 97 | |
| 98 | while (res > 0) { |
| 99 | ssize_t stat = ::send(m_sock, (const char *)buffer + (size - res), res, 0); |
| 100 | if (stat < 0) { |
| 101 | if (errno != EINTR) { |
| 102 | retval = stat; |
| 103 | ERR("%s: failed: %s\n", __FUNCTION__, strerror(errno)); |
| 104 | break; |
| 105 | } |
| 106 | } else { |
| 107 | res -= stat; |
| 108 | } |
| 109 | } |
| 110 | return retval; |
| 111 | } |
| 112 | |
| 113 | const unsigned char *SocketStream::readFully(void *buf, size_t len) |
| 114 | { |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 115 | if (!valid()) return NULL; |
| 116 | if (!buf) { |
| 117 | return NULL; // do not allow NULL buf in that implementation |
| 118 | } |
| 119 | size_t res = len; |
| 120 | while (res > 0) { |
| 121 | ssize_t stat = ::recv(m_sock, (char *)(buf) + len - res, res, 0); |
| 122 | if (stat > 0) { |
| 123 | res -= stat; |
| 124 | continue; |
| 125 | } |
| 126 | if (stat == 0 || errno != EINTR) { // client shutdown or error |
| 127 | return NULL; |
| 128 | } |
| 129 | } |
| 130 | return (const unsigned char *)buf; |
| 131 | } |
| 132 | |
David Reveman | 8bc3255 | 2019-06-28 10:32:41 -0400 | [diff] [blame] | 133 | const unsigned char *SocketStream::commitBufferAndReadFully(size_t size, void *buf, size_t len) |
| 134 | { |
Roman Kiryanov | d0dbf74 | 2019-07-08 13:19:59 -0700 | [diff] [blame] | 135 | return commitBuffer(size) ? NULL : readFully(buf, len); |
David Reveman | 8bc3255 | 2019-06-28 10:32:41 -0400 | [diff] [blame] | 136 | } |
| 137 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 138 | const unsigned char *SocketStream::read( void *buf, size_t *inout_len) |
| 139 | { |
| 140 | if (!valid()) return NULL; |
| 141 | if (!buf) { |
| 142 | return NULL; // do not allow NULL buf in that implementation |
| 143 | } |
| 144 | |
| 145 | int n; |
| 146 | do { |
| 147 | n = recv(buf, *inout_len); |
| 148 | } while( n < 0 && errno == EINTR ); |
| 149 | |
| 150 | if (n > 0) { |
| 151 | *inout_len = n; |
| 152 | return (const unsigned char *)buf; |
| 153 | } |
| 154 | |
| 155 | return NULL; |
| 156 | } |
| 157 | |
| 158 | int SocketStream::recv(void *buf, size_t len) |
| 159 | { |
| 160 | if (!valid()) return int(ERR_INVALID_SOCKET); |
| 161 | int res = 0; |
| 162 | while(true) { |
| 163 | res = ::recv(m_sock, (char *)buf, len, 0); |
| 164 | if (res < 0) { |
| 165 | if (errno == EINTR) { |
| 166 | continue; |
| 167 | } |
| 168 | } |
| 169 | break; |
| 170 | } |
| 171 | return res; |
| 172 | } |