NetworkSocketAPI

Dependencies:   DnsQuery

Dependents:   HelloWizFi250Interface

Fork of NetworkSocketAPI by NetworkSocketAPI

Files at this revision

API Documentation at this revision

Comitter:
Christopher Haster
Date:
Thu Feb 18 06:01:55 2016 -0600
Branch:
api-changes
Parent:
24:a5e959bdd2dd
Child:
26:9774a2edad71
Commit message:
Added abstract base case for Sockets

Changed in this revision

NetworkInterface.h Show annotated file Show diff for this revision Revisions of this file
Socket.cpp Show annotated file Show diff for this revision Revisions of this file
Socket.h Show annotated file Show diff for this revision Revisions of this file
TCPSocket.cpp Show annotated file Show diff for this revision Revisions of this file
TCPSocket.h Show annotated file Show diff for this revision Revisions of this file
UDPSocket.cpp Show annotated file Show diff for this revision Revisions of this file
UDPSocket.h Show annotated file Show diff for this revision Revisions of this file
--- a/NetworkInterface.h	Thu Feb 18 04:09:00 2016 -0600
+++ b/NetworkInterface.h	Thu Feb 18 06:01:55 2016 -0600
@@ -80,8 +80,7 @@
 
 
 private:
-    friend class TCPSocket;
-    friend class UDPSocket;
+    friend class Socket;
 
     /** Internally create a socket
      *  @param proto The type of socket to open, SOCK_TCP or SOCK_UDP
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket.cpp	Thu Feb 18 06:01:55 2016 -0600
@@ -0,0 +1,81 @@
+/* Socket
+ * Copyright (c) 2015 ARM Limited
+ *
+ * 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.
+ */
+
+#include "Socket.h"
+
+Socket::Socket(
+        NetworkInterface *iface,
+        socket_protocol_t proto, 
+        const char *url,
+        uint16_t port)
+  : _iface(iface)
+  , _proto(proto)
+  , _socket(0) {
+    if (url)  setURL(url);
+    if (port) setPort(port);
+}
+
+Socket::~Socket() {
+    if (_socket) _iface->destroySocket(_socket);
+}
+
+SocketInterface *Socket::_get_socket() {
+    if (!_socket) _socket = _iface->createSocket(_proto);
+    return _socket;
+}
+
+void Socket::setURL(const char *url) {
+    SocketInterface *s = _get_socket();
+    if (!s) return;
+    s->setURL(url);
+}
+
+void Socket::setIPAddress(const char *ip) {
+    SocketInterface *s = _get_socket();
+    if (!s) return;
+    s->setIPAddress(ip);
+}
+
+void Socket::setPort(uint16_t port) {
+    SocketInterface *s = _get_socket();
+	if (!s) return;
+	s->setPort(port);
+}
+
+const char *Socket::getIPAddress() {
+    SocketInterface *s = _get_socket();
+	if (!s) return 0;
+	return s->getIPAddress();
+}
+
+uint16_t Socket::getPort() {
+    SocketInterface *s = _get_socket();
+	if (!s) return 0;
+	return s->getPort();
+}
+
+int32_t Socket::send(const void *data, uint32_t len, uint32_t timeout_ms) {
+    SocketInterface *s = _get_socket();
+	if (!s) return -2;
+	return s->send(data, len, timeout_ms);
+}
+
+int32_t Socket::recv(void *data, uint32_t len, uint32_t timeout_ms) {
+    SocketInterface *s = _get_socket();
+	if (!s) return -2;
+	return s->recv(data, len, timeout_ms);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket.h	Thu Feb 18 06:01:55 2016 -0600
@@ -0,0 +1,88 @@
+/* Socket
+ * Copyright (c) 2015 ARM Limited
+ *
+ * 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.
+ */
+
+#ifndef SOCKET_H
+#define SOCKET_H
+
+#include "NetworkInterface.h"
+
+/** Abstract socket class
+ *  API for handling general sockets. Supports IP address operations
+ *  and sending/recieving data.
+ */
+class Socket
+{
+public:
+    /** Set the URL of the socket
+     *  Performs DNS lookup if necessary
+     *  @param url URL to connect to
+     */
+    void setURL(const char *url);
+
+    /** Set the IP address of the socket
+     *  @param ip IP address to connect to, copied internally
+     */
+    void setIPAddress(const char *ip);
+
+    /** Set the port of the socket
+     *  @param port Port to connect to
+     */
+    void setPort(uint16_t port);
+
+    /** Gets the IP address
+     *  @return IP address to connect to
+     */
+    const char *getIPAddress();
+
+    /** Gets the port
+     *  @return Port to connect to
+     */
+    uint16_t getPort();
+
+
+    /** Send data over the socket
+     *  @param data Buffer of data to send
+     *  @param len Size of data to send
+     *  @param timeout_ms Maximum amount of time to wait
+     *  @return 0 on success
+     */
+    int32_t send(const void *data, uint32_t len, uint32_t timeout_ms = 15000);
+
+    /** Recieve data over the socket
+     *  @param data Buffer to store recieved data
+     *  @param len Size of provided buffer
+     *  @param timeout_ms Maximum amount of time to wait
+     *  @return Number of bytes sent or a negative value on failure
+     */
+    int32_t recv(void *data, uint32_t len, uint32_t timeout_ms = 15000);
+
+
+protected:
+    Socket( NetworkInterface *iface,
+            socket_protocol_t proto, 
+            const char *url = 0,
+            uint16_t port = 0);
+    ~Socket();
+
+    SocketInterface *_get_socket();
+
+private:
+    NetworkInterface *_iface;
+    socket_protocol_t _proto;
+    SocketInterface *_socket;
+};
+
+#endif
--- a/TCPSocket.cpp	Thu Feb 18 04:09:00 2016 -0600
+++ b/TCPSocket.cpp	Thu Feb 18 06:01:55 2016 -0600
@@ -16,51 +16,19 @@
 
 #include "TCPSocket.h"
 
-TCPSocket::TCPSocket(NetworkInterface *iface, const char *ip, uint16_t port)
-  : _iface(iface) {
-    _socket = _iface->createSocket(SOCK_TCP);
-
-    if (ip)   setIPAddress(ip);
-    if (port) setPort(port);
-}
-
-TCPSocket::~TCPSocket() {
-    _iface->destroySocket(_socket);
-}
-
-void TCPSocket::setURL(const char *url) {
-    return _socket->setURL(url);
-}
-
-void TCPSocket::setIPAddress(const char *ip) {
-    return _socket->setIPAddress(ip);
-}
-
-void TCPSocket::setPort(uint16_t port) {
-    return _socket->setPort(port);
-}
-
-const char *TCPSocket::getIPAddress() {
-    return _socket->getIPAddress();
-}
-
-uint16_t TCPSocket::getPort() {
-    return _socket->getPort();
+TCPSocket::TCPSocket(NetworkInterface *iface, const char *url, uint16_t port)
+  : Socket(iface, SOCK_TCP, url, port) {
 }
 
 int32_t TCPSocket::open() {
-    return _socket->open();
+    SocketInterface *s = _get_socket();
+    if (!s) return -2;
+    return s->close();
 }
 
 int32_t TCPSocket::close() {
-    return _socket->close();
+    SocketInterface *s = _get_socket();
+    if (!s) return -2;
+    return s->close();
 }
 
-int32_t TCPSocket::send(const void *data, uint32_t len, uint32_t timeout_ms) {
-    return _socket->send(data, len, timeout_ms);
-}
-
-int32_t TCPSocket::recv(void *data, uint32_t len, uint32_t timeout_ms) {
-    return _socket->recv(data, len, timeout_ms);
-}
-
--- a/TCPSocket.h	Thu Feb 18 04:09:00 2016 -0600
+++ b/TCPSocket.h	Thu Feb 18 06:01:55 2016 -0600
@@ -17,13 +17,13 @@
 #ifndef TCP_SOCKET_H
 #define TCP_SOCKET_H
 
-#include "NetworkInterface.h"
+#include "Socket.h"
 
 /** TCPSocket class
  *  API for handling TCP sockets. The implementation is determined
  *  by the interface passed during construction.
  */
-class TCPSocket
+class TCPSocket : public Socket
 {
 public:
     /** Create a socket using the specified network interface
@@ -33,37 +33,6 @@
      */
     TCPSocket(NetworkInterface *iface, const char *url = 0, uint16_t port = 0);
 
-    /** Closes and destroys the socket
-     */
-    ~TCPSocket();
-
-
-    /** Set the URL of the socket
-     *  Performs DNS lookup if necessary
-     *  @param url URL to connect to
-     */
-    void setURL(const char *url);
-
-    /** Set the IP address of the socket
-     *  @param ip IP address to connect to, copied internally
-     */
-    void setIPAddress(const char *ip);
-
-    /** Set the port of the socket
-     *  @param port Port to connect to
-     */
-    void setPort(uint16_t port);
-
-    /** Gets the IP address
-     *  @return IP address to connect to
-     */
-    const char *getIPAddress();
-
-    /** Gets the port
-     *  @return Port to connect to
-     */
-    uint16_t getPort();
-
 
     /** Open a connection to the underlying address
      *  @return 0 on success
@@ -74,26 +43,6 @@
      *  @return 0 on success
      */
     int32_t close();
-
-    /** Send data over TCP 
-     *  @param data Buffer of data to send
-     *  @param len Size of data to send
-     *  @param timeout_ms Maximum amount of time to wait
-     *  @return 0 on success
-     */
-    int32_t send(const void *data, uint32_t len, uint32_t timeout_ms = 15000);
-
-    /** Recieve data over TCP
-     *  @param data Buffer to store recieved data
-     *  @param len Size of provided buffer
-     *  @param timeout_ms Maximum amount of time to wait
-     *  @return Number of bytes sent or a negative value on failure
-     */
-    int32_t recv(void *data, uint32_t len, uint32_t timeout_ms = 15000);
-
-private:
-    NetworkInterface *_iface;
-    SocketInterface *_socket;
 };
 
 #endif
--- a/UDPSocket.cpp	Thu Feb 18 04:09:00 2016 -0600
+++ b/UDPSocket.cpp	Thu Feb 18 06:01:55 2016 -0600
@@ -17,42 +17,6 @@
 #include "UDPSocket.h"
 
 UDPSocket::UDPSocket(NetworkInterface *iface, const char *url, uint16_t port)
-  : _iface(iface) {
-    _socket = _iface->createSocket(SOCK_UDP);
-
-    if (url)  setURL(url);
-    if (port) setPort(port);
-}
-
-UDPSocket::~UDPSocket() {
-    _iface->destroySocket(_socket);
-}
-
-void UDPSocket::setURL(const char *url) {
-    return _socket->setURL(url);
-}
-
-void UDPSocket::setIPAddress(const char *ip) {
-    return _socket->setIPAddress(ip);
+  : Socket(iface, SOCK_UDP, url, port) {
 }
 
-void UDPSocket::setPort(uint16_t port) {
-    return _socket->setPort(port);
-}
-
-const char *UDPSocket::getIPAddress() {
-    return _socket->getIPAddress();
-}
-
-uint16_t UDPSocket::getPort() {
-    return _socket->getPort();
-}
-
-int32_t UDPSocket::send(const void *data, uint32_t len, uint32_t timeout_ms) {
-    return _socket->send(data, len, timeout_ms);
-}
-
-int32_t UDPSocket::recv(void *data, uint32_t len, uint32_t timeout_ms) {
-    return _socket->recv(data, len, timeout_ms);
-}
-
--- a/UDPSocket.h	Thu Feb 18 04:09:00 2016 -0600
+++ b/UDPSocket.h	Thu Feb 18 06:01:55 2016 -0600
@@ -17,13 +17,13 @@
 #ifndef UDP_SOCKET_H
 #define UDP_SOCKET_H
 
-#include "NetworkInterface.h"
+#include "Socket.h"
 
 /** UDPSocket class
  *  API for handling UDP sockets. The implementation is determined
  *  by the interface passed during construction.
  */
-class UDPSocket
+class UDPSocket : public Socket
 {
 public:
     /** Create a socket using the specified network interface
@@ -32,58 +32,6 @@
      *  @param port Optional port to connect to
      */
     UDPSocket(NetworkInterface *iface, const char *url = 0, uint16_t port = 0);
-
-    /** Closes and destroys the socket
-     */
-    ~UDPSocket();
-
-
-    /** Set the URL of the socket
-     *  Performs DNS lookup if necessary
-     *  @param url URL to connect to
-     */
-    void setURL(const char *url);
-
-    /** Set the IP address of the socket
-     *  @param ip IP address to connect to, copied internally
-     */
-    void setIPAddress(const char *ip);
-
-    /** Set the port of the socket
-     *  @param port Port to connect to
-     */
-    void setPort(uint16_t port);
-
-    /** Gets the IP address
-     *  @return IP address to connect to
-     */
-    const char *getIPAddress();
-
-    /** Gets the port
-     *  @return Port to connect to
-     */
-    uint16_t getPort();
-
-
-    /** Send a UDP packet
-     *  @param data Buffer of data to send
-     *  @param len Size of data to send
-     *  @param timeout_ms Maximum amount of time to wait
-     *  @return 0 on success
-     */
-    int32_t send(const void *data, uint32_t len, uint32_t timeout_ms = 15000);
-
-    /** Recieve a UDP packet
-     *  @param data Buffer to store recieved data
-     *  @param len Size of provided buffer
-     *  @param timeout_ms Maximum amount of time to wait
-     *  @return Number of bytes sent or a negative value on failure
-     */
-    int32_t recv(void *data, uint32_t len, uint32_t timeout_ms = 15000);
-
-private:
-    NetworkInterface *_iface;
-    SocketInterface *_socket;
 };
 
 #endif