Skip to content

Create abstract Socket interface #7192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Socket
/* InternetSocket
* Copyright (c) 2015 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -14,17 +14,19 @@
* limitations under the License.
*/

#include "Socket.h"
#include "mbed.h"
#include "InternetSocket.h"
#include "platform/Callback.h"

Socket::Socket()
: _stack(0)
, _socket(0)
, _timeout(osWaitForever)
using namespace mbed;

InternetSocket::InternetSocket()
: _stack(0), _socket(0), _timeout(osWaitForever),
_readers(0), _writers(0), _factory_allocated(false),
_pending(0)
{
}

nsapi_error_t Socket::open(NetworkStack *stack)
nsapi_error_t InternetSocket::open(NetworkStack *stack)
{
_lock.lock();

Expand All @@ -42,14 +44,14 @@ nsapi_error_t Socket::open(NetworkStack *stack)
}

_socket = socket;
_event = callback(this, &Socket::event);
_event = callback(this, &InternetSocket::event);
_stack->socket_attach(_socket, Callback<void()>::thunk, &_event);

_lock.unlock();
return NSAPI_ERROR_OK;
}

nsapi_error_t Socket::close()
nsapi_error_t InternetSocket::close()
{
_lock.lock();

Expand All @@ -66,11 +68,23 @@ nsapi_error_t Socket::close()
// on this socket
event();

// Wait until all readers and writers are gone
while (_readers || _writers) {
_lock.unlock();
_event_flag.wait_any(FINISHED_FLAG, osWaitForever);
_lock.lock();
}

_lock.unlock();

// When allocated by accept() call, will self desctruct on close();
if (_factory_allocated) {
delete this;
}
return ret;
}

int Socket::modify_multicast_group(const SocketAddress &address, nsapi_socket_option_t socketopt)
int InternetSocket::modify_multicast_group(const SocketAddress &address, nsapi_socket_option_t socketopt)
{
nsapi_ip_mreq_t mreq;

Expand All @@ -81,32 +95,32 @@ int Socket::modify_multicast_group(const SocketAddress &address, nsapi_socket_op
return this->setsockopt(NSAPI_SOCKET, socketopt, &mreq, sizeof(mreq));
}

int Socket::join_multicast_group(const SocketAddress &address)
int InternetSocket::join_multicast_group(const SocketAddress &address)
{
return modify_multicast_group(address, NSAPI_ADD_MEMBERSHIP);
}

int Socket::leave_multicast_group(const SocketAddress &address)
int InternetSocket::leave_multicast_group(const SocketAddress &address)
{
return modify_multicast_group(address, NSAPI_DROP_MEMBERSHIP);
}


nsapi_error_t Socket::bind(uint16_t port)
nsapi_error_t InternetSocket::bind(uint16_t port)
{
// Underlying bind is thread safe
SocketAddress addr(0, port);
return bind(addr);
}

nsapi_error_t Socket::bind(const char *address, uint16_t port)
nsapi_error_t InternetSocket::bind(const char *address, uint16_t port)
{
// Underlying bind is thread safe
SocketAddress addr(address, port);
return bind(addr);
}

nsapi_error_t Socket::bind(const SocketAddress &address)
nsapi_error_t InternetSocket::bind(const SocketAddress &address)
{
_lock.lock();
nsapi_error_t ret;
Expand All @@ -121,13 +135,13 @@ nsapi_error_t Socket::bind(const SocketAddress &address)
return ret;
}

void Socket::set_blocking(bool blocking)
void InternetSocket::set_blocking(bool blocking)
{
// Socket::set_timeout is thread safe
// InternetSocket::set_timeout is thread safe
set_timeout(blocking ? -1 : 0);
}

void Socket::set_timeout(int timeout)
void InternetSocket::set_timeout(int timeout)
{
_lock.lock();

Expand All @@ -140,7 +154,7 @@ void Socket::set_timeout(int timeout)
_lock.unlock();
}

nsapi_error_t Socket::setsockopt(int level, int optname, const void *optval, unsigned optlen)
nsapi_error_t InternetSocket::setsockopt(int level, int optname, const void *optval, unsigned optlen)
{
_lock.lock();
nsapi_error_t ret;
Expand All @@ -155,7 +169,7 @@ nsapi_error_t Socket::setsockopt(int level, int optname, const void *optval, uns
return ret;
}

nsapi_error_t Socket::getsockopt(int level, int optname, void *optval, unsigned *optlen)
nsapi_error_t InternetSocket::getsockopt(int level, int optname, void *optval, unsigned *optlen)
{
_lock.lock();
nsapi_error_t ret;
Expand All @@ -170,15 +184,24 @@ nsapi_error_t Socket::getsockopt(int level, int optname, void *optval, unsigned
return ret;

}
void InternetSocket::event()
{
_event_flag.set(READ_FLAG|WRITE_FLAG);

_pending += 1;
if (_callback && _pending == 1) {
_callback();
}
}

void Socket::sigio(Callback<void()> callback)
void InternetSocket::sigio(Callback<void()> callback)
{
_lock.lock();
_callback = callback;
_lock.unlock();
}

void Socket::attach(Callback<void()> callback)
void InternetSocket::attach(Callback<void()> callback)
{
sigio(callback);
}
Loading