| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 1 | /* | 
 | 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 Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 20 | #include <sys/types.h> | 
 | 21 |  | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 22 | #include <string> | 
| Josh Gao | 342b661 | 2019-02-20 17:16:37 -0800 | [diff] [blame] | 23 | #include <string_view> | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 24 |  | 
| Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 25 | #include "adb_unique_fd.h" | 
 | 26 |  | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 27 | // Sends the protocol "OKAY" message. | 
| Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 28 | bool SendOkay(borrowed_fd fd); | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 29 |  | 
 | 30 | // Sends the protocol "FAIL" message, with the given failure reason. | 
| Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 31 | bool SendFail(borrowed_fd fd, std::string_view reason); | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 32 |  | 
 | 33 | // Writes a protocol-format string; a four hex digit length followed by the string data. | 
| Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 34 | bool SendProtocolString(borrowed_fd fd, std::string_view s); | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 35 |  | 
| Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 36 | // Reads a protocol-format string; a four hex digit length followed by the string data. | 
| Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 37 | bool ReadProtocolString(borrowed_fd fd, std::string* s, std::string* error); | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 38 |  | 
| Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 39 | // Reads exactly len bytes from fd into buf. | 
 | 40 | // | 
 | 41 | // Returns false if there is an error or if EOF was reached before len bytes | 
 | 42 | // were read. If EOF was found, errno will be set to 0. | 
 | 43 | // | 
 | 44 | // If this function fails, the contents of buf are undefined. | 
| Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 45 | bool ReadFdExactly(borrowed_fd fd, void* buf, size_t len); | 
| Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 46 |  | 
| Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 47 | // Given a client socket, wait for orderly/graceful shutdown. Call this: | 
 | 48 | // | 
 | 49 | // * Before closing a client socket. | 
 | 50 | // * Only when no more data is expected to come in. | 
 | 51 | // * Only when the server is not waiting for data from the client (because then | 
 | 52 | //   the client and server will deadlock waiting for each other). | 
 | 53 | // * Only when the server is expected to close its socket right now. | 
 | 54 | // * Don't call shutdown(SHUT_WR) before calling this because that will shutdown | 
 | 55 | //   the client socket early, defeating the purpose of calling this. | 
 | 56 | // | 
 | 57 | // Waiting for orderly/graceful shutdown of the server socket will cause the | 
 | 58 | // server socket to close before the client socket. That prevents the client | 
 | 59 | // socket from staying in TIME_WAIT which eventually causes subsequent | 
 | 60 | // connect()s from the client to fail with WSAEADDRINUSE on Windows. | 
 | 61 | // Returns true if it is sure that orderly/graceful shutdown has occurred with | 
 | 62 | // no additional data read from the server. | 
| Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 63 | bool ReadOrderlyShutdown(borrowed_fd fd); | 
| Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 64 |  | 
| Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 65 | // Writes exactly len bytes from buf to fd. | 
 | 66 | // | 
 | 67 | // Returns false if there is an error or if the fd was closed before the write | 
 | 68 | // completed. If the other end of the fd (such as in a socket, pipe, or fifo), | 
 | 69 | // is closed, errno will be set to 0. | 
| Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 70 | bool WriteFdExactly(borrowed_fd fd, const void* buf, size_t len); | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 71 |  | 
| Elliott Hughes | ab52c18 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 72 | // Same as above, but for strings. | 
| Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 73 | bool WriteFdExactly(borrowed_fd fd, const char* s); | 
 | 74 | bool WriteFdExactly(borrowed_fd fd, const std::string& s); | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 75 |  | 
| Elliott Hughes | ab52c18 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 76 | // Same as above, but formats the string to send. | 
| Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 77 | bool WriteFdFmt(borrowed_fd fd, const char* fmt, ...) __attribute__((__format__(__printf__, 2, 3))); | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 78 | #endif /* ADB_IO_H */ |