mirror of https://github.com/ARMmbed/mbed-os.git
Add open call as alternative to passing NetworkInterface at construction
Pros - Allows memory to be statically allocated - Avoids issues with Thread creation before entering main - Matches existing APIs such as FunctionPointer and Ticker Cons - Does not enforce passing a NetworkInterfacepull/2216/head^2
parent
4c7992cb24
commit
d38ccb70a6
44
Socket.cpp
44
Socket.cpp
|
@ -16,12 +16,12 @@
|
||||||
|
|
||||||
#include "Socket.h"
|
#include "Socket.h"
|
||||||
|
|
||||||
Socket::Socket(NetworkInterface *iface, nsapi_protocol_t proto)
|
Socket::Socket()
|
||||||
: _iface(iface)
|
: _iface(0)
|
||||||
|
, _socket(0)
|
||||||
, _blocking(true)
|
, _blocking(true)
|
||||||
, _timeout(0)
|
, _timeout(0)
|
||||||
{
|
{
|
||||||
_socket = _iface->socket_create(proto);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Socket::~Socket()
|
Socket::~Socket()
|
||||||
|
@ -31,6 +31,28 @@ Socket::~Socket()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Socket::open(NetworkInterface *iface, nsapi_protocol_t proto)
|
||||||
|
{
|
||||||
|
_iface = iface;
|
||||||
|
_socket = _iface->socket_create(proto);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Socket::close(bool shutdown)
|
||||||
|
{
|
||||||
|
if (!_socket) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int err = _iface->socket_close(_socket, shutdown);
|
||||||
|
if (!err) {
|
||||||
|
void *socket = _socket;
|
||||||
|
_socket = 0;
|
||||||
|
_iface->socket_destroy(socket);
|
||||||
|
}
|
||||||
|
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
void Socket::set_blocking(bool blocking)
|
void Socket::set_blocking(bool blocking)
|
||||||
{
|
{
|
||||||
_blocking = blocking;
|
_blocking = blocking;
|
||||||
|
@ -59,22 +81,6 @@ int Socket::get_option(int optname, void *optval, unsigned int *optlen)
|
||||||
return _iface->socket_get_option(_socket, optname, optval, optlen);
|
return _iface->socket_get_option(_socket, optname, optval, optlen);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Socket::close(bool shutdown)
|
|
||||||
{
|
|
||||||
if (!_socket) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int err = _iface->socket_close(_socket, shutdown);
|
|
||||||
if (!err) {
|
|
||||||
void *socket = _socket;
|
|
||||||
_socket = 0;
|
|
||||||
_iface->socket_destroy(socket);
|
|
||||||
}
|
|
||||||
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Socket::thunk(void *p)
|
void Socket::thunk(void *p)
|
||||||
{
|
{
|
||||||
FunctionPointer *fptr = (FunctionPointer *)p;
|
FunctionPointer *fptr = (FunctionPointer *)p;
|
||||||
|
|
8
Socket.h
8
Socket.h
|
@ -27,6 +27,11 @@ public:
|
||||||
/** Socket lifetime
|
/** Socket lifetime
|
||||||
*/
|
*/
|
||||||
virtual ~Socket();
|
virtual ~Socket();
|
||||||
|
|
||||||
|
/** Open the socket
|
||||||
|
* @param iface Interface to open socket on
|
||||||
|
*/
|
||||||
|
virtual int open(NetworkInterface *iface) = 0;
|
||||||
|
|
||||||
/** Set blocking or non-blocking mode of the socket
|
/** Set blocking or non-blocking mode of the socket
|
||||||
* @param blocking true for blocking mode, false for non-blocking mode.
|
* @param blocking true for blocking mode, false for non-blocking mode.
|
||||||
|
@ -60,7 +65,8 @@ public:
|
||||||
int close(bool shutdown=true);
|
int close(bool shutdown=true);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Socket(NetworkInterface *iface, nsapi_protocol_t proto);
|
Socket();
|
||||||
|
int open(NetworkInterface *iface, nsapi_protocol_t proto);
|
||||||
|
|
||||||
static void thunk(void *);
|
static void thunk(void *);
|
||||||
|
|
||||||
|
|
|
@ -17,11 +17,20 @@
|
||||||
#include "TCPServer.h"
|
#include "TCPServer.h"
|
||||||
#include "Timer.h"
|
#include "Timer.h"
|
||||||
|
|
||||||
TCPServer::TCPServer(NetworkInterface *iface)
|
TCPServer::TCPServer()
|
||||||
: Socket(iface, NSAPI_TCP)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TCPServer::TCPServer(NetworkInterface *iface)
|
||||||
|
{
|
||||||
|
open(iface);
|
||||||
|
}
|
||||||
|
|
||||||
|
int TCPServer::open(NetworkInterface *iface)
|
||||||
|
{
|
||||||
|
return Socket::open(iface, NSAPI_TCP);
|
||||||
|
}
|
||||||
|
|
||||||
int TCPServer::bind(uint16_t port)
|
int TCPServer::bind(uint16_t port)
|
||||||
{
|
{
|
||||||
if (!_socket) {
|
if (!_socket) {
|
||||||
|
@ -45,15 +54,16 @@ int TCPServer::accept(TCPSocket *connection)
|
||||||
mbed::Timer timer;
|
mbed::Timer timer;
|
||||||
timer.start();
|
timer.start();
|
||||||
|
|
||||||
void *socket = connection->_socket;
|
if (connection->_socket) {
|
||||||
connection->_socket = 0;
|
connection->close();
|
||||||
_iface->socket_destroy(socket);
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
if (!_socket) {
|
if (!_socket) {
|
||||||
return NSAPI_ERROR_NO_SOCKET;
|
return NSAPI_ERROR_NO_SOCKET;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *socket;
|
||||||
int err = _iface->socket_accept(_socket, &socket);
|
int err = _iface->socket_accept(_socket, &socket);
|
||||||
|
|
||||||
if (err > 0) {
|
if (err > 0) {
|
||||||
|
|
|
@ -27,8 +27,14 @@ class TCPServer : public Socket {
|
||||||
public:
|
public:
|
||||||
/** TCP Server lifetime
|
/** TCP Server lifetime
|
||||||
*/
|
*/
|
||||||
|
TCPServer();
|
||||||
TCPServer(NetworkInterface *iface);
|
TCPServer(NetworkInterface *iface);
|
||||||
virtual ~TCPServer();
|
virtual ~TCPServer();
|
||||||
|
|
||||||
|
/** Open the socket
|
||||||
|
* @param iface Interface to open socket on
|
||||||
|
*/
|
||||||
|
virtual int open(NetworkInterface *iface);
|
||||||
|
|
||||||
/** Bind a socket to a specific port
|
/** Bind a socket to a specific port
|
||||||
* @param port The port to listen for incoming connections on
|
* @param port The port to listen for incoming connections on
|
||||||
|
|
|
@ -17,11 +17,20 @@
|
||||||
#include "TCPSocket.h"
|
#include "TCPSocket.h"
|
||||||
#include "Timer.h"
|
#include "Timer.h"
|
||||||
|
|
||||||
TCPSocket::TCPSocket(NetworkInterface *iface)
|
TCPSocket::TCPSocket()
|
||||||
: Socket(iface, NSAPI_TCP)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TCPSocket::TCPSocket(NetworkInterface *iface)
|
||||||
|
{
|
||||||
|
open(iface);
|
||||||
|
}
|
||||||
|
|
||||||
|
int TCPSocket::open(NetworkInterface *iface)
|
||||||
|
{
|
||||||
|
return Socket::open(iface, NSAPI_TCP);
|
||||||
|
}
|
||||||
|
|
||||||
int TCPSocket::connect(const SocketAddress &addr)
|
int TCPSocket::connect(const SocketAddress &addr)
|
||||||
{
|
{
|
||||||
if (!_socket) {
|
if (!_socket) {
|
||||||
|
|
|
@ -26,8 +26,14 @@ class TCPSocket : public Socket {
|
||||||
public:
|
public:
|
||||||
/** TCP socket lifetime
|
/** TCP socket lifetime
|
||||||
*/
|
*/
|
||||||
|
TCPSocket();
|
||||||
TCPSocket(NetworkInterface *iface);
|
TCPSocket(NetworkInterface *iface);
|
||||||
virtual ~TCPSocket();
|
virtual ~TCPSocket();
|
||||||
|
|
||||||
|
/** Open the socket
|
||||||
|
* @param iface Interface to open socket on
|
||||||
|
*/
|
||||||
|
virtual int open(NetworkInterface *iface);
|
||||||
|
|
||||||
/** Connects this TCP socket to the server
|
/** Connects this TCP socket to the server
|
||||||
* @param host The host to connect to. It can either be an IP Address
|
* @param host The host to connect to. It can either be an IP Address
|
||||||
|
|
|
@ -17,11 +17,20 @@
|
||||||
#include "UDPSocket.h"
|
#include "UDPSocket.h"
|
||||||
#include "Timer.h"
|
#include "Timer.h"
|
||||||
|
|
||||||
UDPSocket::UDPSocket(NetworkInterface *iface)
|
UDPSocket::UDPSocket()
|
||||||
: Socket(iface, NSAPI_UDP)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UDPSocket::UDPSocket(NetworkInterface *iface)
|
||||||
|
{
|
||||||
|
open(iface);
|
||||||
|
}
|
||||||
|
|
||||||
|
int UDPSocket::open(NetworkInterface *iface)
|
||||||
|
{
|
||||||
|
return Socket::open(iface, NSAPI_UDP);
|
||||||
|
}
|
||||||
|
|
||||||
int UDPSocket::bind(uint16_t port)
|
int UDPSocket::bind(uint16_t port)
|
||||||
{
|
{
|
||||||
if (!_socket) {
|
if (!_socket) {
|
||||||
|
|
|
@ -26,8 +26,14 @@ class UDPSocket : public Socket {
|
||||||
public:
|
public:
|
||||||
/** UDPSocket lifetime
|
/** UDPSocket lifetime
|
||||||
*/
|
*/
|
||||||
|
UDPSocket();
|
||||||
UDPSocket(NetworkInterface *iface);
|
UDPSocket(NetworkInterface *iface);
|
||||||
virtual ~UDPSocket();
|
virtual ~UDPSocket();
|
||||||
|
|
||||||
|
/** Open the socket
|
||||||
|
* @param iface Interface to open socket on
|
||||||
|
*/
|
||||||
|
virtual int open(NetworkInterface *iface);
|
||||||
|
|
||||||
/** Bind a UDP Server Socket to a specific port
|
/** Bind a UDP Server Socket to a specific port
|
||||||
* @param port The port to listen for incoming connections on
|
* @param port The port to listen for incoming connections on
|
||||||
|
|
Loading…
Reference in New Issue