Sharvil Nanavati | c2031c4 | 2014-07-25 15:50:17 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 2014 Google, Inc. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | ******************************************************************************/ |
| 18 | |
| 19 | #pragma once |
| 20 | |
Sharvil Nanavati | 98bf85f | 2014-08-27 19:05:40 -0700 | [diff] [blame] | 21 | #include <stdbool.h> |
Sharvil Nanavati | c2031c4 | 2014-07-25 15:50:17 -0700 | [diff] [blame] | 22 | #include <stddef.h> |
| 23 | #include <stdint.h> |
Sharvil Nanavati | 98bf85f | 2014-08-27 19:05:40 -0700 | [diff] [blame] | 24 | #include <sys/types.h> |
Sharvil Nanavati | c2031c4 | 2014-07-25 15:50:17 -0700 | [diff] [blame] | 25 | |
Jakub Pawlowski | 713993d | 2016-04-21 13:16:45 -0700 | [diff] [blame^] | 26 | #ifdef __cplusplus |
| 27 | extern "C" { |
| 28 | #endif |
| 29 | |
Sharvil Nanavati | c2031c4 | 2014-07-25 15:50:17 -0700 | [diff] [blame] | 30 | typedef struct reactor_t reactor_t; |
| 31 | typedef struct socket_t socket_t; |
| 32 | typedef uint16_t port_t; |
| 33 | |
| 34 | typedef void (*socket_cb)(socket_t *socket, void *context); |
| 35 | |
| 36 | // Returns a new socket object. The socket is in an idle, disconnected state when |
| 37 | // it is returned by this function. The returned object must be freed by calling |
| 38 | // |socket_free|. Returns NULL on failure. |
| 39 | socket_t *socket_new(void); |
| 40 | |
Sharvil Nanavati | ad3067b | 2014-08-21 18:13:46 -0700 | [diff] [blame] | 41 | // Returns a new socket object backed by |fd|. The socket object is in whichever |
| 42 | // state |fd| is in when it was passed to this function. The returned object must |
| 43 | // be freed by calling |socket_free|. Returns NULL on failure. If this function |
| 44 | // is successful, ownership of |fd| is transferred and the caller must not close it. |
| 45 | socket_t *socket_new_from_fd(int fd); |
| 46 | |
Sharvil Nanavati | c2031c4 | 2014-07-25 15:50:17 -0700 | [diff] [blame] | 47 | // Frees a socket object created by |socket_new| or |socket_accept|. |socket| may |
| 48 | // be NULL. If the socket was connected, it will be disconnected. |
| 49 | void socket_free(socket_t *socket); |
| 50 | |
| 51 | // Puts |socket| in listening mode for incoming TCP connections on the specified |
Pavlin Radoslavov | d2199cb | 2015-08-17 18:54:22 -0700 | [diff] [blame] | 52 | // |port| and the loopback IPv4 address. Returns true on success, false on |
| 53 | // failure (e.g. |port| is bound by another socket). |socket| may not be NULL. |
Sharvil Nanavati | c2031c4 | 2014-07-25 15:50:17 -0700 | [diff] [blame] | 54 | bool socket_listen(const socket_t *socket, port_t port); |
| 55 | |
| 56 | // Blocks on a listening socket, |socket|, until a client connects to it. Returns |
| 57 | // a connected socket on success, NULL on failure. The returned object must be |
| 58 | // freed by calling |socket_free|. |socket| may not be NULL. |
| 59 | socket_t *socket_accept(const socket_t *socket); |
| 60 | |
| 61 | // Reads up to |count| bytes from |socket| into |buf|. This function will not |
| 62 | // block. This function returns a positive integer representing the number |
| 63 | // of bytes copied into |buf| on success, 0 if the socket has disconnected, |
| 64 | // and -1 on error. This function may return a value less than |count| if not |
| 65 | // enough data is currently available. If this function returns -1, errno will also |
| 66 | // be set (see recv(2) for possible errno values). If there were no bytes available |
| 67 | // to be read, this function returns -1 and sets errno to EWOULDBLOCK. Neither |
| 68 | // |socket| nor |buf| may be NULL. |
| 69 | ssize_t socket_read(const socket_t *socket, void *buf, size_t count); |
| 70 | |
| 71 | // Writes up to |count| bytes from |buf| into |socket|. This function will not |
| 72 | // block. Returns a positive integer representing the number of bytes written |
| 73 | // to |socket| on success, 0 if the socket has disconnected, and -1 on error. This |
| 74 | // function may return a value less than |count| if writing more bytes would result |
| 75 | // in blocking. If this function returns -1, errno will also be set (see send(2) for |
| 76 | // possible errno values). If no bytes could be written without blocking, this |
| 77 | // function will return -1 and set errno to EWOULDBLOCK. Neither |socket| nor |buf| |
| 78 | // may be NULL. |
| 79 | ssize_t socket_write(const socket_t *socket, const void *buf, size_t count); |
| 80 | |
Sharvil Nanavati | ad3067b | 2014-08-21 18:13:46 -0700 | [diff] [blame] | 81 | // This function performs the same write operation as |socket_write| and also |
| 82 | // sends the file descriptor |fd| over the socket to a remote process. Ownership |
| 83 | // of |fd| transfers to this function and the descriptor must not be used any longer. |
| 84 | // If |fd| is INVALID_FD, this function behaves the same as |socket_write|. |
| 85 | ssize_t socket_write_and_transfer_fd(const socket_t *socket, const void *buf, size_t count, int fd); |
| 86 | |
Sharvil Nanavati | 98bf85f | 2014-08-27 19:05:40 -0700 | [diff] [blame] | 87 | // Returns the number of bytes that can be read from |socket| without blocking. On error, |
| 88 | // this function returns -1. |socket| may not be NULL. |
| 89 | // |
| 90 | // Note: this function should not be part of the socket interface. It is only provided as |
| 91 | // a stop-gap until we can refactor away code that depends on a priori knowledge of |
| 92 | // the byte count. Do not use this function unless you need it while refactoring |
| 93 | // legacy bluedroid code. |
| 94 | ssize_t socket_bytes_available(const socket_t *socket); |
| 95 | |
Sharvil Nanavati | fbf8908 | 2014-08-13 00:40:49 -0700 | [diff] [blame] | 96 | // Registers |socket| with the |reactor|. When the socket becomes readable, |read_cb| |
Sharvil Nanavati | c2031c4 | 2014-07-25 15:50:17 -0700 | [diff] [blame] | 97 | // will be called. When the socket becomes writeable, |write_cb| will be called. The |
| 98 | // |context| parameter is passed, untouched, to each of the callback routines. Neither |
Sharvil Nanavati | 98bf85f | 2014-08-27 19:05:40 -0700 | [diff] [blame] | 99 | // |socket| nor |reactor| may be NULL. |read_cb|, |write_cb|, and |context| may be NULL. |
Sharvil Nanavati | fbf8908 | 2014-08-13 00:40:49 -0700 | [diff] [blame] | 100 | void socket_register(socket_t *socket, reactor_t *reactor, void *context, socket_cb read_cb, socket_cb write_cb); |
Sharvil Nanavati | c2031c4 | 2014-07-25 15:50:17 -0700 | [diff] [blame] | 101 | |
Sharvil Nanavati | fbf8908 | 2014-08-13 00:40:49 -0700 | [diff] [blame] | 102 | // Unregisters |socket| from whichever reactor it is registered with, if any. This |
Sharvil Nanavati | c2031c4 | 2014-07-25 15:50:17 -0700 | [diff] [blame] | 103 | // function is idempotent. |
| 104 | void socket_unregister(socket_t *socket); |
Jakub Pawlowski | 713993d | 2016-04-21 13:16:45 -0700 | [diff] [blame^] | 105 | |
| 106 | #ifdef __cplusplus |
| 107 | } |
| 108 | #endif |