blob: f522e27a2697c640bc428618c2f3b048f60a16e4 [file] [log] [blame]
Vladimir Chtchetkinea8fc4912010-11-30 09:32:55 -08001/*
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 Chtchetkine0d4c8822010-12-01 17:29:09 -080025/* Descriptor for a connected non-blocking socket providing synchronous I/O */
Vladimir Chtchetkinea8fc4912010-11-30 09:32:55 -080026typedef struct SyncSocket SyncSocket;
27
Vladimir Chtchetkine0d4c8822010-12-01 17:29:09 -080028/*
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 Chtchetkinea8fc4912010-11-30 09:32:55 -080039SyncSocket* syncsocket_connect(int fd, SockAddress* sockaddr, int timeout);
40
Vladimir Chtchetkine0d4c8822010-12-01 17:29:09 -080041/*
42 * Closes SyncSocket descriptor obtained from syncsocket_connect routine.
43 * Param:
44 * ssocket - SyncSocket descriptor obtained from syncsocket_connect routine.
45 */
Vladimir Chtchetkinea8fc4912010-11-30 09:32:55 -080046void syncsocket_close(SyncSocket* ssocket);
47
Vladimir Chtchetkine0d4c8822010-12-01 17:29:09 -080048/*
49 * Frees memory allocated for SyncSocket descriptor obtained from
Vladimir Chtchetkined87b0802010-12-06 10:55:11 -080050 * syncsocket_connect routine. Note that this routine will also close socket
51 * connection.
Vladimir Chtchetkine0d4c8822010-12-01 17:29:09 -080052 * Param:
53 * ssocket - SyncSocket descriptor obtained from syncsocket_connect routine.
54 */
Vladimir Chtchetkinea8fc4912010-11-30 09:32:55 -080055void syncsocket_free(SyncSocket* ssocket);
56
Vladimir Chtchetkine0d4c8822010-12-01 17:29:09 -080057/*
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 Chtchetkinea8fc4912010-11-30 09:32:55 -080066int syncsocket_start_read(SyncSocket* ssocket);
67
Vladimir Chtchetkine0d4c8822010-12-01 17:29:09 -080068/*
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 Chtchetkinea8fc4912010-11-30 09:32:55 -080077int syncsocket_stop_read(SyncSocket* ssocket);
78
Vladimir Chtchetkine0d4c8822010-12-01 17:29:09 -080079/*
Vladimir Chtchetkined87b0802010-12-06 10:55:11 -080080 * 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 */
88int 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 */
99int syncsocket_stop_write(SyncSocket* ssocket);
100
101/*
Vladimir Chtchetkine0d4c8822010-12-01 17:29:09 -0800102 * 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 Chtchetkined87b0802010-12-06 10:55:11 -0800113 * Number of bytes read on success, or -1 on failure.
Vladimir Chtchetkine0d4c8822010-12-01 17:29:09 -0800114 */
Vladimir Chtchetkined87b0802010-12-06 10:55:11 -0800115ssize_t syncsocket_read_absolute(SyncSocket* ssocket,
116 void* buf,
117 size_t size,
118 int64_t deadline);
Vladimir Chtchetkinea8fc4912010-11-30 09:32:55 -0800119
Vladimir Chtchetkine0d4c8822010-12-01 17:29:09 -0800120/*
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 Chtchetkined87b0802010-12-06 10:55:11 -0800132 * Number of bytes read on success, or -1 on failure.
Vladimir Chtchetkine0d4c8822010-12-01 17:29:09 -0800133 */
Vladimir Chtchetkined87b0802010-12-06 10:55:11 -0800134ssize_t syncsocket_read(SyncSocket* ssocket, void* buf, size_t size, int timeout);
Vladimir Chtchetkine0d4c8822010-12-01 17:29:09 -0800135
136/*
Vladimir Chtchetkined87b0802010-12-06 10:55:11 -0800137 * 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 Chtchetkine0d4c8822010-12-01 17:29:09 -0800142 * Param:
143 * ssocket - SyncSocket descriptor obtained from syncsocket_connect routine.
Vladimir Chtchetkined87b0802010-12-06 10:55:11 -0800144 * buf - Buffer containing data to write.
145 * size - Number of bytes to write.
146 * deadline - Absoulte deadline time to complete the writing.
Vladimir Chtchetkine0d4c8822010-12-01 17:29:09 -0800147 * Return:
Vladimir Chtchetkined87b0802010-12-06 10:55:11 -0800148 * Number of bytes written on success,or -1 on failure.
Vladimir Chtchetkine0d4c8822010-12-01 17:29:09 -0800149 */
Vladimir Chtchetkined87b0802010-12-06 10:55:11 -0800150ssize_t syncsocket_write_absolute(SyncSocket* ssocket,
151 const void* buf,
Vladimir Chtchetkine0d4c8822010-12-01 17:29:09 -0800152 size_t size,
153 int64_t deadline);
154
155/*
Vladimir Chtchetkined87b0802010-12-06 10:55:11 -0800156 * 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 */
169ssize_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 */
186ssize_t syncsocket_read_line_absolute(SyncSocket* ssocket,
187 char* buffer,
188 size_t size,
189 int64_t deadline);
190
191/*
Vladimir Chtchetkine0d4c8822010-12-01 17:29:09 -0800192 * 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 Chtchetkined87b0802010-12-06 10:55:11 -0800203ssize_t syncsocket_read_line(SyncSocket* ssocket,
204 char* buffer,
205 size_t size,
206 int timeout);
Vladimir Chtchetkinea8fc4912010-11-30 09:32:55 -0800207
208#endif // ANDROID_SYNC_UTILS_H