socket.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /******************************************************************************
  2. *
  3. * Copyright 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. #pragma once
  19. #include <stdbool.h>
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. #include <sys/types.h>
  23. typedef struct reactor_t reactor_t;
  24. typedef struct socket_t socket_t;
  25. typedef uint16_t port_t;
  26. typedef void (*socket_cb)(socket_t* socket, void* context);
  27. // Returns a new socket object. The socket is in an idle, disconnected state
  28. // when it is returned by this function. The returned object must be freed by
  29. // calling |socket_free|. Returns NULL on failure.
  30. socket_t* socket_new(void);
  31. // Returns a new socket object backed by |fd|. The socket object is in whichever
  32. // state |fd| is in when it was passed to this function. The returned object
  33. // must be freed by calling |socket_free|. Returns NULL on failure. If this
  34. // function is successful, ownership of |fd| is transferred and the caller must
  35. // not close it.
  36. socket_t* socket_new_from_fd(int fd);
  37. // Frees a socket object created by |socket_new| or |socket_accept|. |socket|
  38. // may be NULL. If the socket was connected, it will be disconnected.
  39. void socket_free(socket_t* socket);
  40. // Puts |socket| in listening mode for incoming TCP connections on the specified
  41. // |port| and the loopback IPv4 address. Returns true on success, false on
  42. // failure (e.g. |port| is bound by another socket). |socket| may not be NULL.
  43. bool socket_listen(const socket_t* socket, port_t port);
  44. // Blocks on a listening socket, |socket|, until a client connects to it.
  45. // Returns a connected socket on success, NULL on failure. The returned object
  46. // must be freed by calling |socket_free|. |socket| may not be NULL.
  47. socket_t* socket_accept(const socket_t* socket);
  48. // Reads up to |count| bytes from |socket| into |buf|. This function will not
  49. // block. This function returns a positive integer representing the number
  50. // of bytes copied into |buf| on success, 0 if the socket has disconnected,
  51. // and -1 on error. This function may return a value less than |count| if not
  52. // enough data is currently available. If this function returns -1, errno will
  53. // also be set (see recv(2) for possible errno values). However, if the reading
  54. // system call was interrupted with errno of EINTR, the read operation is
  55. // restarted internally without propagating EINTR back to the caller. If there
  56. // were no bytes available to be read, this function returns -1 and sets errno
  57. // to EWOULDBLOCK. Neither |socket| nor |buf| may be NULL.
  58. ssize_t socket_read(const socket_t* socket, void* buf, size_t count);
  59. // Writes up to |count| bytes from |buf| into |socket|. This function will not
  60. // block. Returns a positive integer representing the number of bytes written
  61. // to |socket| on success, 0 if the socket has disconnected, and -1 on error.
  62. // This function may return a value less than |count| if writing more bytes
  63. // would result in blocking. If this function returns -1, errno will also be
  64. // set (see send(2) for possible errno values). However, if the writing system
  65. // call was interrupted with errno of EINTR, the write operation is restarted
  66. // internally without propagating EINTR back to the caller. If no bytes could
  67. // be written without blocking, this function will return -1 and set errno to
  68. // EWOULDBLOCK. Neither |socket| nor |buf| may be NULL.
  69. ssize_t socket_write(const socket_t* socket, const void* buf, size_t count);
  70. // This function performs the same write operation as |socket_write| and also
  71. // sends the file descriptor |fd| over the socket to a remote process. Ownership
  72. // of |fd| transfers to this function and the descriptor must not be used any
  73. // longer. If |fd| is INVALID_FD, this function behaves the same as
  74. // |socket_write|.
  75. ssize_t socket_write_and_transfer_fd(const socket_t* socket, const void* buf,
  76. size_t count, int fd);
  77. // Returns the number of bytes that can be read from |socket| without blocking.
  78. // On error, this function returns -1. |socket| may not be NULL.
  79. //
  80. // Note: this function should not be part of the socket interface. It is only
  81. // provided as a stop-gap until we can refactor away code that depends on
  82. // a priori knowledge of the byte count. Do not use this function unless
  83. // you need it while refactoring legacy bluedroid code.
  84. ssize_t socket_bytes_available(const socket_t* socket);
  85. // Registers |socket| with the |reactor|. When the socket becomes readable,
  86. // |read_cb| will be called. When the socket becomes writeable, |write_cb| will
  87. // be called. The |context| parameter is passed, untouched, to each of the
  88. // callback routines. Neither |socket| nor |reactor| may be NULL. |read_cb|,
  89. // |write_cb|, and |context| may be NULL.
  90. void socket_register(socket_t* socket, reactor_t* reactor, void* context,
  91. socket_cb read_cb, socket_cb write_cb);
  92. // Unregisters |socket| from whichever reactor it is registered with, if any.
  93. // This function is idempotent.
  94. void socket_unregister(socket_t* socket);