OSDN Git Service

Introduce a socket class
authorSharvil Nanavati <sharvil@google.com>
Fri, 25 Jul 2014 22:50:17 +0000 (15:50 -0700)
committerAndre Eisenbach <eisenbach@google.com>
Mon, 16 Mar 2015 23:51:27 +0000 (16:51 -0700)
This socket implementation is limited to TCP server sockets and
provides no mechanism to connect to a remote host. It's primarily
intended to be used to interface with debugging tools. It's designed
to work seamlessly with the reactor (event-driven) model.

osi/Android.mk
osi/include/socket.h [new file with mode: 0644]
osi/src/socket.c [new file with mode: 0644]

index 1f288ec..9750d73 100644 (file)
@@ -31,6 +31,7 @@ LOCAL_SRC_FILES := \
     ./src/list.c \
     ./src/reactor.c \
     ./src/semaphore.c \
+    ./src/socket.c \
     ./src/thread.c
 
 LOCAL_CFLAGS := -std=c99 -Wall -Werror
diff --git a/osi/include/socket.h b/osi/include/socket.h
new file mode 100644 (file)
index 0000000..ba36291
--- /dev/null
@@ -0,0 +1,78 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2014 Google, Inc.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at:
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ ******************************************************************************/
+
+#pragma once
+
+#include <stddef.h>
+#include <stdint.h>
+
+typedef struct reactor_t reactor_t;
+typedef struct socket_t socket_t;
+typedef uint16_t port_t;
+
+typedef void (*socket_cb)(socket_t *socket, void *context);
+
+// Returns a new socket object. The socket is in an idle, disconnected state when
+// it is returned by this function. The returned object must be freed by calling
+// |socket_free|. Returns NULL on failure.
+socket_t *socket_new(void);
+
+// Frees a socket object created by |socket_new| or |socket_accept|. |socket| may
+// be NULL. If the socket was connected, it will be disconnected.
+void socket_free(socket_t *socket);
+
+// Puts |socket| in listening mode for incoming TCP connections on the specified
+// |port|. Returns true on success, false on failure (e.g. |port| is bound by
+// another socket). |socket| may not be NULL.
+bool socket_listen(const socket_t *socket, port_t port);
+
+// Blocks on a listening socket, |socket|, until a client connects to it. Returns
+// a connected socket on success, NULL on failure. The returned object must be
+// freed by calling |socket_free|. |socket| may not be NULL.
+socket_t *socket_accept(const socket_t *socket);
+
+// Reads up to |count| bytes from |socket| into |buf|. This function will not
+// block. This function returns a positive integer representing the number
+// of bytes copied into |buf| on success, 0 if the socket has disconnected,
+// and -1 on error. This function may return a value less than |count| if not
+// enough data is currently available. If this function returns -1, errno will also
+// be set (see recv(2) for possible errno values). If there were no bytes available
+// to be read, this function returns -1 and sets errno to EWOULDBLOCK. Neither
+// |socket| nor |buf| may be NULL.
+ssize_t socket_read(const socket_t *socket, void *buf, size_t count);
+
+// Writes up to |count| bytes from |buf| into |socket|. This function will not
+// block. Returns a positive integer representing the number of bytes written
+// to |socket| on success, 0 if the socket has disconnected, and -1 on error. This
+// function may return a value less than |count| if writing more bytes would result
+// in blocking. If this function returns -1, errno will also be set (see send(2) for
+// possible errno values). If no bytes could be written without blocking, this
+// function will return -1 and set errno to EWOULDBLOCK. Neither |socket| nor |buf|
+// may be NULL.
+ssize_t socket_write(const socket_t *socket, const void *buf, size_t count);
+
+// Registers |socket| with the |reactor|. When the socket becomes readable, |read_cb|
+// will be called. When the socket becomes writeable, |write_cb| will be called. The
+// |context| parameter is passed, untouched, to each of the callback routines. Neither
+// |socket| nor |reactor| may be NULL. |read_cb| or |write_cb|, but not both, may be NULL.
+// |context| may be NULL.
+void socket_register(socket_t *socket, reactor_t *reactor, socket_cb read_cb, socket_cb write_cb, void *context);
+
+// Unregisters |socket| from whichever reactor it is registered with, if any. This
+// function is idempotent.
+void socket_unregister(socket_t *socket);
diff --git a/osi/src/socket.c b/osi/src/socket.c
new file mode 100644 (file)
index 0000000..83f4a5a
--- /dev/null
@@ -0,0 +1,180 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2014 Google, Inc.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at:
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ ******************************************************************************/
+
+#define LOG_TAG "bt_osi_socket"
+
+#include <assert.h>
+#include <errno.h>
+#include <netinet/in.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <utils/Log.h>
+
+#include "reactor.h"
+#include "socket.h"
+
+struct socket_t {
+  reactor_t *reactor;
+  reactor_object_t socket_object;
+  socket_cb read_ready;
+  socket_cb write_ready;
+  void *context;
+};
+
+static void internal_read_ready(void *context);
+static void internal_write_ready(void *context);
+
+socket_t *socket_new(void) {
+  socket_t *ret = (socket_t *)calloc(1, sizeof(socket_t));
+  if (!ret) {
+    ALOGE("%s unable to allocate memory for socket.", __func__);
+    goto error;
+  }
+
+  ret->socket_object.fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+  if (ret->socket_object.fd == -1) {
+    ALOGE("%s unable to create socket: %s", __func__, strerror(errno));
+    goto error;
+  }
+
+  int enable = 1;
+  if (setsockopt(ret->socket_object.fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) == -1) {
+    ALOGE("%s unable to set SO_REUSEADDR: %s", __func__, strerror(errno));
+    goto error;
+  }
+
+  return ret;
+
+error:;
+  if (ret)
+    close(ret->socket_object.fd);
+  free(ret);
+  return NULL;
+}
+
+void socket_free(socket_t *socket) {
+  if (!socket)
+    return;
+
+  socket_unregister(socket);
+  close(socket->socket_object.fd);
+  free(socket);
+}
+
+bool socket_listen(const socket_t *socket, port_t port) {
+  assert(socket != NULL);
+
+  struct sockaddr_in addr;
+  addr.sin_family = AF_INET;
+  addr.sin_addr.s_addr = 0;
+  addr.sin_port = htons(port);
+  if (bind(socket->socket_object.fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
+    ALOGE("%s unable to bind socket to port %u: %s", __func__, port, strerror(errno));
+    return false;
+  }
+
+  if (listen(socket->socket_object.fd, 10) == -1) {
+    ALOGE("%s unable to listen on port %u: %s", __func__, port, strerror(errno));
+    return false;
+  }
+
+  return true;
+}
+
+socket_t *socket_accept(const socket_t *socket) {
+  assert(socket != NULL);
+
+  int fd = accept(socket->socket_object.fd, NULL, NULL);
+  if (fd == -1) {
+    ALOGE("%s unable to accept socket: %s", __func__, strerror(errno));
+    return NULL;
+  }
+
+  socket_t *ret = (socket_t *)calloc(1, sizeof(socket_t));
+  if (!ret) {
+    close(fd);
+    ALOGE("%s unable to allocate memory for socket.", __func__);
+    return NULL;
+  }
+
+  ret->socket_object.fd = fd;
+  return ret;
+}
+
+ssize_t socket_read(const socket_t *socket, void *buf, size_t count) {
+  assert(socket != NULL);
+  assert(buf != NULL);
+
+  return recv(socket->socket_object.fd, buf, count, MSG_DONTWAIT);
+}
+
+ssize_t socket_write(const socket_t *socket, const void *buf, size_t count) {
+  assert(socket != NULL);
+  assert(buf != NULL);
+
+  return send(socket->socket_object.fd, buf, count, MSG_DONTWAIT);
+}
+
+void socket_register(socket_t *socket, reactor_t *reactor, socket_cb read_cb, socket_cb write_cb, void *context) {
+  assert(socket != NULL);
+  assert(reactor != NULL);
+  assert(read_cb || write_cb);
+
+  // Make sure the socket isn't currently registered.
+  socket_unregister(socket);
+
+  socket->reactor = reactor;
+  socket->read_ready = read_cb;
+  socket->write_ready = write_cb;
+  socket->context = context;
+
+  socket->socket_object.read_ready = internal_read_ready;
+  socket->socket_object.write_ready = internal_write_ready;
+  socket->socket_object.context = socket;
+  if (read_cb && write_cb)
+    socket->socket_object.interest = REACTOR_INTEREST_READ_WRITE;
+  else if (read_cb)
+    socket->socket_object.interest = REACTOR_INTEREST_READ;
+  else if (write_cb)
+    socket->socket_object.interest = REACTOR_INTEREST_WRITE;
+
+  reactor_register(reactor, &socket->socket_object);
+}
+
+void socket_unregister(socket_t *socket) {
+  assert(socket != NULL);
+
+  if (socket->reactor)
+    reactor_unregister(socket->reactor, &socket->socket_object);
+}
+
+static void internal_read_ready(void *context) {
+  assert(context != NULL);
+
+  socket_t *socket = (void *)context;
+  socket->read_ready(socket, socket->context);
+}
+
+static void internal_write_ready(void *context) {
+  assert(context != NULL);
+
+  socket_t *socket = (void *)context;
+  socket->write_ready(socket, socket->context);
+}