Vladimir Chtchetkine | a8fc491 | 2010-11-30 09:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | /* |
| 18 | * Contains helper routines dealing with syncronous access to a non-blocking |
| 19 | * sokets. |
| 20 | */ |
| 21 | |
| 22 | #ifndef ANDROID_SYNC_UTILS_H |
| 23 | #define ANDROID_SYNC_UTILS_H |
| 24 | |
Vladimir Chtchetkine | 0d4c882 | 2010-12-01 17:29:09 -0800 | [diff] [blame] | 25 | /* Descriptor for a connected non-blocking socket providing synchronous I/O */ |
Vladimir Chtchetkine | a8fc491 | 2010-11-30 09:32:55 -0800 | [diff] [blame] | 26 | typedef struct SyncSocket SyncSocket; |
| 27 | |
Vladimir Chtchetkine | 0d4c882 | 2010-12-01 17:29:09 -0800 | [diff] [blame] | 28 | /* |
| 29 | * Connect to a non-blocking socket for further synchronous I/O. |
| 30 | * Note: this routine will explicitly call socket_set_nonblock on the fd passed |
| 31 | * to this routine. |
| 32 | * Param: |
| 33 | * fd - File descriptor for the socket, created with socket_create_xxx routine. |
| 34 | * sockaddr - Address of the socket to connect to. |
| 35 | * timeout - Time out (in milliseconds) to wait for the connection to occur. |
| 36 | * Return: |
| 37 | * Initialized SyncSocket descriptor on success, or NULL on failure. |
| 38 | */ |
Vladimir Chtchetkine | a8fc491 | 2010-11-30 09:32:55 -0800 | [diff] [blame] | 39 | SyncSocket* syncsocket_connect(int fd, SockAddress* sockaddr, int timeout); |
| 40 | |
Vladimir Chtchetkine | 0d4c882 | 2010-12-01 17:29:09 -0800 | [diff] [blame] | 41 | /* |
| 42 | * Closes SyncSocket descriptor obtained from syncsocket_connect routine. |
| 43 | * Param: |
| 44 | * ssocket - SyncSocket descriptor obtained from syncsocket_connect routine. |
| 45 | */ |
Vladimir Chtchetkine | a8fc491 | 2010-11-30 09:32:55 -0800 | [diff] [blame] | 46 | void syncsocket_close(SyncSocket* ssocket); |
| 47 | |
Vladimir Chtchetkine | 0d4c882 | 2010-12-01 17:29:09 -0800 | [diff] [blame] | 48 | /* |
| 49 | * Frees memory allocated for SyncSocket descriptor obtained from |
Vladimir Chtchetkine | d87b080 | 2010-12-06 10:55:11 -0800 | [diff] [blame^] | 50 | * syncsocket_connect routine. Note that this routine will also close socket |
| 51 | * connection. |
Vladimir Chtchetkine | 0d4c882 | 2010-12-01 17:29:09 -0800 | [diff] [blame] | 52 | * Param: |
| 53 | * ssocket - SyncSocket descriptor obtained from syncsocket_connect routine. |
| 54 | */ |
Vladimir Chtchetkine | a8fc491 | 2010-11-30 09:32:55 -0800 | [diff] [blame] | 55 | void syncsocket_free(SyncSocket* ssocket); |
| 56 | |
Vladimir Chtchetkine | 0d4c882 | 2010-12-01 17:29:09 -0800 | [diff] [blame] | 57 | /* |
| 58 | * Prepares the socket for read. |
| 59 | * Note: this routine must be called before calling into syncsocket_read_xxx |
| 60 | * routines. |
| 61 | * Param: |
| 62 | * ssocket - SyncSocket descriptor obtained from syncsocket_connect routine. |
| 63 | * Return: |
| 64 | * 0 on success, or -1 on failure. |
| 65 | */ |
Vladimir Chtchetkine | a8fc491 | 2010-11-30 09:32:55 -0800 | [diff] [blame] | 66 | int syncsocket_start_read(SyncSocket* ssocket); |
| 67 | |
Vladimir Chtchetkine | 0d4c882 | 2010-12-01 17:29:09 -0800 | [diff] [blame] | 68 | /* |
| 69 | * Clears the socket after reading. |
| 70 | * Note: this routine must be called after all expected data has been read from |
| 71 | * the socket. |
| 72 | * Param: |
| 73 | * ssocket - SyncSocket descriptor obtained from syncsocket_connect routine. |
| 74 | * Return: |
| 75 | * 0 on success, or -1 on failure. |
| 76 | */ |
Vladimir Chtchetkine | a8fc491 | 2010-11-30 09:32:55 -0800 | [diff] [blame] | 77 | int syncsocket_stop_read(SyncSocket* ssocket); |
| 78 | |
Vladimir Chtchetkine | 0d4c882 | 2010-12-01 17:29:09 -0800 | [diff] [blame] | 79 | /* |
Vladimir Chtchetkine | d87b080 | 2010-12-06 10:55:11 -0800 | [diff] [blame^] | 80 | * Prepares the socket for write. |
| 81 | * Note: this routine must be called before calling into syncsocket_write_xxx |
| 82 | * routines. |
| 83 | * Param: |
| 84 | * ssocket - SyncSocket descriptor obtained from syncsocket_connect routine. |
| 85 | * Return: |
| 86 | * 0 on success, or -1 on failure. |
| 87 | */ |
| 88 | int syncsocket_start_write(SyncSocket* ssocket); |
| 89 | |
| 90 | /* |
| 91 | * Clears the socket after writing. |
| 92 | * Note: this routine must be called after all data has been written to the |
| 93 | * socket. |
| 94 | * Param: |
| 95 | * ssocket - SyncSocket descriptor obtained from syncsocket_connect routine. |
| 96 | * Return: |
| 97 | * 0 on success, or -1 on failure. |
| 98 | */ |
| 99 | int syncsocket_stop_write(SyncSocket* ssocket); |
| 100 | |
| 101 | /* |
Vladimir Chtchetkine | 0d4c882 | 2010-12-01 17:29:09 -0800 | [diff] [blame] | 102 | * Synchronously reads from the socket. |
| 103 | * Note: syncsocket_start_read must be called before first call to this routine. |
| 104 | * Once syncsocket_start_read has been called, multiple syncsocket_read_xxx can |
| 105 | * be called to read all necessary data from the socket. When all necessary data |
| 106 | * has been read, syncsocket_stop_read must be called. |
| 107 | * Param: |
| 108 | * ssocket - SyncSocket descriptor obtained from syncsocket_connect routine. |
| 109 | * buf - Buffer where to read data. |
| 110 | * size - Number of bytes to read. |
| 111 | * deadline - Absoulte deadline time to complete the reading. |
| 112 | * Return: |
Vladimir Chtchetkine | d87b080 | 2010-12-06 10:55:11 -0800 | [diff] [blame^] | 113 | * Number of bytes read on success, or -1 on failure. |
Vladimir Chtchetkine | 0d4c882 | 2010-12-01 17:29:09 -0800 | [diff] [blame] | 114 | */ |
Vladimir Chtchetkine | d87b080 | 2010-12-06 10:55:11 -0800 | [diff] [blame^] | 115 | ssize_t syncsocket_read_absolute(SyncSocket* ssocket, |
| 116 | void* buf, |
| 117 | size_t size, |
| 118 | int64_t deadline); |
Vladimir Chtchetkine | a8fc491 | 2010-11-30 09:32:55 -0800 | [diff] [blame] | 119 | |
Vladimir Chtchetkine | 0d4c882 | 2010-12-01 17:29:09 -0800 | [diff] [blame] | 120 | /* |
| 121 | * Synchronously reads from the socket. |
| 122 | * Note: syncsocket_start_read must be called before first call to this routine. |
| 123 | * Once syncsocket_start_read has been called, multiple syncsocket_read_xxx can |
| 124 | * be called to read all necessary data from the socket. When all necessary data |
| 125 | * has been read, syncsocket_stop_read must be called. |
| 126 | * Param: |
| 127 | * ssocket - SyncSocket descriptor obtained from syncsocket_connect routine. |
| 128 | * buf - Buffer where to read data. |
| 129 | * size - Number of bytes to read. |
| 130 | * timeout - Timeout (in milliseconds) to complete the reading. |
| 131 | * Return: |
Vladimir Chtchetkine | d87b080 | 2010-12-06 10:55:11 -0800 | [diff] [blame^] | 132 | * Number of bytes read on success, or -1 on failure. |
Vladimir Chtchetkine | 0d4c882 | 2010-12-01 17:29:09 -0800 | [diff] [blame] | 133 | */ |
Vladimir Chtchetkine | d87b080 | 2010-12-06 10:55:11 -0800 | [diff] [blame^] | 134 | ssize_t syncsocket_read(SyncSocket* ssocket, void* buf, size_t size, int timeout); |
Vladimir Chtchetkine | 0d4c882 | 2010-12-01 17:29:09 -0800 | [diff] [blame] | 135 | |
| 136 | /* |
Vladimir Chtchetkine | d87b080 | 2010-12-06 10:55:11 -0800 | [diff] [blame^] | 137 | * Synchronously writes to the socket. |
| 138 | * Note: syncsocket_start_write must be called before first call to this routine. |
| 139 | * Once syncsocket_start_write has been called, multiple syncsocket_write_xxx can |
| 140 | * be called to write all necessary data to the socket. When all necessary data |
| 141 | * has been written, syncsocket_stop_write must be called. |
Vladimir Chtchetkine | 0d4c882 | 2010-12-01 17:29:09 -0800 | [diff] [blame] | 142 | * Param: |
| 143 | * ssocket - SyncSocket descriptor obtained from syncsocket_connect routine. |
Vladimir Chtchetkine | d87b080 | 2010-12-06 10:55:11 -0800 | [diff] [blame^] | 144 | * buf - Buffer containing data to write. |
| 145 | * size - Number of bytes to write. |
| 146 | * deadline - Absoulte deadline time to complete the writing. |
Vladimir Chtchetkine | 0d4c882 | 2010-12-01 17:29:09 -0800 | [diff] [blame] | 147 | * Return: |
Vladimir Chtchetkine | d87b080 | 2010-12-06 10:55:11 -0800 | [diff] [blame^] | 148 | * Number of bytes written on success,or -1 on failure. |
Vladimir Chtchetkine | 0d4c882 | 2010-12-01 17:29:09 -0800 | [diff] [blame] | 149 | */ |
Vladimir Chtchetkine | d87b080 | 2010-12-06 10:55:11 -0800 | [diff] [blame^] | 150 | ssize_t syncsocket_write_absolute(SyncSocket* ssocket, |
| 151 | const void* buf, |
Vladimir Chtchetkine | 0d4c882 | 2010-12-01 17:29:09 -0800 | [diff] [blame] | 152 | size_t size, |
| 153 | int64_t deadline); |
| 154 | |
| 155 | /* |
Vladimir Chtchetkine | d87b080 | 2010-12-06 10:55:11 -0800 | [diff] [blame^] | 156 | * Synchronously writes to the socket. |
| 157 | * Note: syncsocket_start_write must be called before first call to this routine. |
| 158 | * Once syncsocket_start_write has been called, multiple syncsocket_write_xxx can |
| 159 | * be called to write all necessary data to the socket. When all necessary data |
| 160 | * has been written, syncsocket_stop_write must be called. |
| 161 | * Param: |
| 162 | * ssocket - SyncSocket descriptor obtained from syncsocket_connect routine. |
| 163 | * buf - Buffer containing data to write. |
| 164 | * size - Number of bytes to write. |
| 165 | * timeout - Timeout (in milliseconds) to complete the writing. |
| 166 | * Return: |
| 167 | * Number of bytes written on success, or -1 on failure. |
| 168 | */ |
| 169 | ssize_t syncsocket_write(SyncSocket* ssocket, |
| 170 | const void* buf, |
| 171 | size_t size, |
| 172 | int timeout); |
| 173 | |
| 174 | /* |
| 175 | * Synchronously reads a line terminated with '\n' from the socket. |
| 176 | * Note: syncsocket_start_read must be called before first call to this routine. |
| 177 | * Param: |
| 178 | * ssocket - SyncSocket descriptor obtained from syncsocket_connect routine. |
| 179 | * buffer - Buffer where to read line. |
| 180 | * size - Number of characters the buffer can contain. |
| 181 | * deadline - Absoulte deadline time to complete the reading. |
| 182 | * Return: |
| 183 | * Number of chracters read on success, 0 on deadline expiration, |
| 184 | * or -1 on failure. |
| 185 | */ |
| 186 | ssize_t syncsocket_read_line_absolute(SyncSocket* ssocket, |
| 187 | char* buffer, |
| 188 | size_t size, |
| 189 | int64_t deadline); |
| 190 | |
| 191 | /* |
Vladimir Chtchetkine | 0d4c882 | 2010-12-01 17:29:09 -0800 | [diff] [blame] | 192 | * Synchronously reads a line terminated with '\n' from the socket. |
| 193 | * Note: syncsocket_start_read must be called before first call to this routine. |
| 194 | * Param: |
| 195 | * ssocket - SyncSocket descriptor obtained from syncsocket_connect routine. |
| 196 | * buffer - Buffer where to read line. |
| 197 | * size - Number of characters the buffer can contain. |
| 198 | * timeout - Timeout (in milliseconds) to complete the reading. |
| 199 | * Return: |
| 200 | * Number of chracters read on success, 0 on deadline expiration, |
| 201 | * or -1 on failure. |
| 202 | */ |
Vladimir Chtchetkine | d87b080 | 2010-12-06 10:55:11 -0800 | [diff] [blame^] | 203 | ssize_t syncsocket_read_line(SyncSocket* ssocket, |
| 204 | char* buffer, |
| 205 | size_t size, |
| 206 | int timeout); |
Vladimir Chtchetkine | a8fc491 | 2010-11-30 09:32:55 -0800 | [diff] [blame] | 207 | |
| 208 | #endif // ANDROID_SYNC_UTILS_H |