David Pursell | 815c7be | 2015-12-09 17:09:54 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | // This file provides a class interface for cross-platform UDP functionality. The main fastboot |
| 30 | // engine should not be using this interface directly, but instead should use a higher-level |
| 31 | // interface that enforces the fastboot UDP protocol. |
| 32 | |
| 33 | #ifndef SOCKET_H_ |
| 34 | #define SOCKET_H_ |
| 35 | |
| 36 | #include "android-base/macros.h" |
| 37 | |
| 38 | #include <memory> |
| 39 | #include <string> |
| 40 | |
| 41 | // UdpSocket interface to be implemented for each platform. |
| 42 | class UdpSocket { |
| 43 | public: |
| 44 | // Creates a new client connection. Clients are connected to a specific hostname/port and can |
| 45 | // only send to that destination. |
| 46 | // On failure, |error| is filled (if non-null) and nullptr is returned. |
| 47 | static std::unique_ptr<UdpSocket> NewUdpClient(const std::string& hostname, int port, |
| 48 | std::string* error); |
| 49 | |
| 50 | // Creates a new server bound to local |port|. This is only meant for testing, during normal |
| 51 | // fastboot operation the device acts as the server. |
| 52 | // The server saves sender addresses in Receive(), and uses the most recent address during |
| 53 | // calls to Send(). |
| 54 | static std::unique_ptr<UdpSocket> NewUdpServer(int port); |
| 55 | |
| 56 | virtual ~UdpSocket() = default; |
| 57 | |
| 58 | // Sends |length| bytes of |data|. Returns the number of bytes actually sent or -1 on error. |
| 59 | virtual ssize_t Send(const void* data, size_t length) = 0; |
| 60 | |
| 61 | // Waits up to |timeout_ms| to receive up to |length| bytes of data. |timout_ms| of 0 will |
| 62 | // block forever. Returns the number of bytes received or -1 on error/timeout. On timeout |
| 63 | // errno will be set to EAGAIN or EWOULDBLOCK. |
| 64 | virtual ssize_t Receive(void* data, size_t length, int timeout_ms) = 0; |
| 65 | |
| 66 | // Closes the socket. Returns 0 on success, -1 on error. |
| 67 | virtual int Close() = 0; |
| 68 | |
| 69 | protected: |
| 70 | // Protected constructor to force factory function use. |
| 71 | UdpSocket() = default; |
| 72 | |
| 73 | DISALLOW_COPY_AND_ASSIGN(UdpSocket); |
| 74 | }; |
| 75 | |
| 76 | #endif // SOCKET_H_ |