blob: aa550af0ef86d36c56540f21f88df10db9f16684 [file] [log] [blame]
Dan Albertcc731cc2015-02-24 21:26:58 -08001/*
2 * Copyright (C) 2015 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#ifndef ADB_IO_H
18#define ADB_IO_H
19
Dan Albertcc731cc2015-02-24 21:26:58 -080020#include <sys/types.h>
21
Elliott Hughese67f1f82015-04-30 17:32:03 -070022#include <string>
23
24// Sends the protocol "OKAY" message.
25bool SendOkay(int fd);
26
27// Sends the protocol "FAIL" message, with the given failure reason.
28bool SendFail(int fd, const std::string& reason);
29
30// Writes a protocol-format string; a four hex digit length followed by the string data.
31bool SendProtocolString(int fd, const std::string& s);
32
Elliott Hughesaa245492015-08-03 10:38:08 -070033// Reads a protocol-format string; a four hex digit length followed by the string data.
34bool ReadProtocolString(int fd, std::string* s, std::string* error);
Dan Albertcc731cc2015-02-24 21:26:58 -080035
Elliott Hughesaa245492015-08-03 10:38:08 -070036// Reads exactly len bytes from fd into buf.
37//
38// Returns false if there is an error or if EOF was reached before len bytes
39// were read. If EOF was found, errno will be set to 0.
40//
41// If this function fails, the contents of buf are undefined.
42bool ReadFdExactly(int fd, void* buf, size_t len);
43
Spencer Low351ecd12015-10-14 17:32:44 -070044// Given a client socket, wait for orderly/graceful shutdown. Call this:
45//
46// * Before closing a client socket.
47// * Only when no more data is expected to come in.
48// * Only when the server is not waiting for data from the client (because then
49// the client and server will deadlock waiting for each other).
50// * Only when the server is expected to close its socket right now.
51// * Don't call shutdown(SHUT_WR) before calling this because that will shutdown
52// the client socket early, defeating the purpose of calling this.
53//
54// Waiting for orderly/graceful shutdown of the server socket will cause the
55// server socket to close before the client socket. That prevents the client
56// socket from staying in TIME_WAIT which eventually causes subsequent
57// connect()s from the client to fail with WSAEADDRINUSE on Windows.
58// Returns true if it is sure that orderly/graceful shutdown has occurred with
59// no additional data read from the server.
60bool ReadOrderlyShutdown(int fd);
61
Elliott Hughesaa245492015-08-03 10:38:08 -070062// Writes exactly len bytes from buf to fd.
63//
64// Returns false if there is an error or if the fd was closed before the write
65// completed. If the other end of the fd (such as in a socket, pipe, or fifo),
66// is closed, errno will be set to 0.
Elliott Hughese67f1f82015-04-30 17:32:03 -070067bool WriteFdExactly(int fd, const void* buf, size_t len);
Dan Albertcc731cc2015-02-24 21:26:58 -080068
Elliott Hughesab52c182015-05-01 17:04:38 -070069// Same as above, but for strings.
Elliott Hughese67f1f82015-04-30 17:32:03 -070070bool WriteFdExactly(int fd, const char* s);
71bool WriteFdExactly(int fd, const std::string& s);
72
Elliott Hughesab52c182015-05-01 17:04:38 -070073// Same as above, but formats the string to send.
Dan Albert286bb6d2015-07-09 20:35:09 +000074bool WriteFdFmt(int fd, const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3)));
Dan Albertcc731cc2015-02-24 21:26:58 -080075
Dan Albertcc731cc2015-02-24 21:26:58 -080076#endif /* ADB_IO_H */