Skip to content

Commit 5168bbd

Browse files
committed
Add InternetDatagram Base Class
1 parent ab88335 commit 5168bbd

File tree

7 files changed

+365
-273
lines changed

7 files changed

+365
-273
lines changed

features/netsocket/ICMPSocket.cpp

Lines changed: 0 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -31,166 +31,3 @@ nsapi_protocol_t ICMPSocket::get_proto()
3131
{
3232
return NSAPI_ICMP;
3333
}
34-
35-
nsapi_error_t ICMPSocket::connect(const SocketAddress &address)
36-
{
37-
_remote_peer = address;
38-
_socket_stats.stats_update_peer(this, _remote_peer);
39-
_socket_stats.stats_update_socket_state(this, SOCK_CONNECTED);
40-
return NSAPI_ERROR_OK;
41-
}
42-
43-
nsapi_size_or_error_t ICMPSocket::sendto(const char *host, uint16_t port, const void *data, nsapi_size_t size)
44-
{
45-
SocketAddress address;
46-
nsapi_size_or_error_t err;
47-
48-
if (!strcmp(_interface_name, "")) {
49-
err = _stack->gethostbyname(host, &address);
50-
} else {
51-
err = _stack->gethostbyname(host, &address, NSAPI_UNSPEC, _interface_name);
52-
}
53-
54-
if (err) {
55-
return NSAPI_ERROR_DNS_FAILURE;
56-
}
57-
58-
address.set_port(port);
59-
60-
// sendto is thread safe
61-
return sendto(address, data, size);
62-
}
63-
64-
nsapi_size_or_error_t ICMPSocket::sendto(const SocketAddress &address, const void *data, nsapi_size_t size)
65-
{
66-
_lock.lock();
67-
nsapi_size_or_error_t ret;
68-
69-
_writers++;
70-
if (_socket) {
71-
_socket_stats.stats_update_socket_state(this, SOCK_OPEN);
72-
_socket_stats.stats_update_peer(this, address);
73-
}
74-
while (true) {
75-
if (!_socket) {
76-
ret = NSAPI_ERROR_NO_SOCKET;
77-
break;
78-
}
79-
80-
core_util_atomic_flag_clear(&_pending);
81-
nsapi_size_or_error_t sent = _stack->socket_sendto(_socket, address, data, size);
82-
if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != sent)) {
83-
_socket_stats.stats_update_sent_bytes(this, sent);
84-
ret = sent;
85-
break;
86-
} else {
87-
uint32_t flag;
88-
89-
// Release lock before blocking so other threads
90-
// accessing this object aren't blocked
91-
_lock.unlock();
92-
flag = _event_flag.wait_any(WRITE_FLAG, _timeout);
93-
_lock.lock();
94-
95-
if (flag & osFlagsError) {
96-
// Timeout break
97-
ret = NSAPI_ERROR_WOULD_BLOCK;
98-
break;
99-
}
100-
}
101-
}
102-
103-
_writers--;
104-
if (!_socket || !_writers) {
105-
_event_flag.set(FINISHED_FLAG);
106-
}
107-
_lock.unlock();
108-
return ret;
109-
}
110-
111-
nsapi_size_or_error_t ICMPSocket::send(const void *data, nsapi_size_t size)
112-
{
113-
if (!_remote_peer) {
114-
return NSAPI_ERROR_NO_ADDRESS;
115-
}
116-
return sendto(_remote_peer, data, size);
117-
}
118-
119-
nsapi_size_or_error_t ICMPSocket::recvfrom(SocketAddress *address, void *buffer, nsapi_size_t size)
120-
{
121-
_lock.lock();
122-
nsapi_size_or_error_t ret;
123-
SocketAddress ignored;
124-
125-
if (!address) {
126-
address = &ignored;
127-
}
128-
129-
_readers++;
130-
131-
if (_socket) {
132-
_socket_stats.stats_update_socket_state(this, SOCK_OPEN);
133-
}
134-
while (true) {
135-
if (!_socket) {
136-
ret = NSAPI_ERROR_NO_SOCKET;
137-
break;
138-
}
139-
140-
core_util_atomic_flag_clear(&_pending);
141-
nsapi_size_or_error_t recv = _stack->socket_recvfrom(_socket, address, buffer, size);
142-
143-
// Filter incomming packets using connected peer address
144-
if (recv >= 0 && _remote_peer && _remote_peer != *address) {
145-
continue;
146-
}
147-
148-
_socket_stats.stats_update_peer(this, _remote_peer);
149-
// Non-blocking sockets always return. Blocking only returns when success or errors other than WOULD_BLOCK
150-
if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != recv)) {
151-
ret = recv;
152-
_socket_stats.stats_update_recv_bytes(this, recv);
153-
break;
154-
} else {
155-
uint32_t flag;
156-
157-
// Release lock before blocking so other threads
158-
// accessing this object aren't blocked
159-
_lock.unlock();
160-
flag = _event_flag.wait_any(READ_FLAG, _timeout);
161-
_lock.lock();
162-
163-
if (flag & osFlagsError) {
164-
// Timeout break
165-
ret = NSAPI_ERROR_WOULD_BLOCK;
166-
break;
167-
}
168-
}
169-
}
170-
171-
_readers--;
172-
if (!_socket || !_readers) {
173-
_event_flag.set(FINISHED_FLAG);
174-
}
175-
176-
_lock.unlock();
177-
return ret;
178-
}
179-
180-
nsapi_size_or_error_t ICMPSocket::recv(void *buffer, nsapi_size_t size)
181-
{
182-
return recvfrom(NULL, buffer, size);
183-
}
184-
185-
Socket *ICMPSocket::accept(nsapi_error_t *error)
186-
{
187-
if (error) {
188-
*error = NSAPI_ERROR_UNSUPPORTED;
189-
}
190-
return NULL;
191-
}
192-
193-
nsapi_error_t ICMPSocket::listen(int)
194-
{
195-
return NSAPI_ERROR_UNSUPPORTED;
196-
}

features/netsocket/ICMPSocket.h

Lines changed: 3 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
#define ICMPSOCKET_H
2121

2222
#include "netsocket/InternetSocket.h"
23+
#include "netsocket/InternetDatagram.h"
2324
#include "netsocket/NetworkStack.h"
2425
#include "netsocket/NetworkInterface.h"
2526
#include "rtos/EventFlags.h"
2627

2728

28-
/** RAW socket implementation.
29+
/** ICMP socket implementation.
2930
*/
30-
class ICMPSocket : public InternetSocket {
31+
class ICMPSocket : public InternetDatagram {
3132
public:
3233
/** Create an uninitialized socket.
3334
*
@@ -41,113 +42,6 @@ class ICMPSocket : public InternetSocket {
4142
*/
4243
virtual ~ICMPSocket();
4344

44-
/** Send data to the specified host and port.
45-
*
46-
* By default, sendto blocks until data is sent. If socket is set to
47-
* nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
48-
* immediately.
49-
*
50-
* @param host Domain name of the remote host or a dotted notation IP address.
51-
* @param port Port of the remote host.
52-
* @param data Buffer of data to send to the host.
53-
* @param size Size of the buffer in bytes.
54-
* @return Number of sent bytes on success, negative error
55-
* code on failure.
56-
*/
57-
virtual nsapi_size_or_error_t sendto(const char *host, uint16_t port,
58-
const void *data, nsapi_size_t size);
59-
60-
/** Send data to the specified address.
61-
*
62-
* By default, sendto blocks until data is sent. If socket is set to
63-
* nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
64-
* immediately.
65-
*
66-
* @param address The SocketAddress of the remote host.
67-
* @param data Buffer of data to send to the host.
68-
* @param size Size of the buffer in bytes.
69-
* @return Number of sent bytes on success, negative error
70-
* code on failure.
71-
*/
72-
virtual nsapi_size_or_error_t sendto(const SocketAddress &address,
73-
const void *data, nsapi_size_t size);
74-
75-
/** Receive a datagram and store the source address in address if it's not NULL.
76-
*
77-
* By default, recvfrom blocks until a datagram is received. If socket is set to
78-
* nonblocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK
79-
* is returned.
80-
*
81-
* @note If the datagram is larger than the buffer, the excess data is silently discarded.
82-
*
83-
* @note If socket is connected, only packets coming from connected peer address
84-
* are accepted.
85-
*
86-
* @note recvfrom() is allowed write to address and data buffers even if error occurs.
87-
*
88-
* @param address Destination for the source address or NULL.
89-
* @param data Destination buffer for RAW data to be received from the host.
90-
* @param size Size of the buffer in bytes.
91-
* @return Number of received bytes on success, negative error
92-
* code on failure.
93-
*/
94-
virtual nsapi_size_or_error_t recvfrom(SocketAddress *address,
95-
void *data, nsapi_size_t size);
96-
97-
/** Set the remote address for next send() call and filtering
98-
* of incoming packets. To reset the address, zero initialized
99-
* SocketAddress must be in the address parameter.
100-
*
101-
* @param address The SocketAddress of the remote host.
102-
* @return 0 on success, negative error code on failure.
103-
*/
104-
virtual nsapi_error_t connect(const SocketAddress &address);
105-
106-
/** Send a raw data to connected remote address.
107-
*
108-
* By default, send blocks until all data is sent. If socket is set to
109-
* nonblocking or times out, a partial amount can be written.
110-
* NSAPI_ERROR_WOULD_BLOCK is returned if no data was written.
111-
*
112-
* @note The socket must be connected to a remote host before send() call.
113-
*
114-
* @param data Buffer of data to send to the host.
115-
* @param size Size of the buffer in bytes.
116-
* @return Number of sent bytes on success, negative error
117-
* code on failure.
118-
*/
119-
virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size);
120-
121-
/** Receive data from a socket.
122-
*
123-
* This is equivalent to calling recvfrom(NULL, data, size).
124-
*
125-
* By default, recv blocks until some data is received. If socket is set to
126-
* nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to
127-
* indicate no data.
128-
*
129-
* @note recv() is allowed write to data buffer even if error occurs.
130-
*
131-
* @param data Pointer to buffer for data received from the host.
132-
* @param size Size of the buffer in bytes.
133-
* @return Number of received bytes on success, negative error
134-
* code on failure.
135-
*/
136-
virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size);
137-
138-
/** Not implemented for ICMPSocket.
139-
*
140-
* @param error Not used.
141-
* @return NSAPI_ERROR_UNSUPPORTED
142-
*/
143-
virtual Socket *accept(nsapi_error_t *error = NULL);
144-
145-
/** Not implemented for ICMPSocket.
146-
*
147-
* @param backlog Not used.
148-
* @return NSAPI_ERROR_UNSUPPORTED
149-
*/
150-
virtual nsapi_error_t listen(int backlog = 1);
15145
#if !defined(DOXYGEN_ONLY)
15246

15347
protected:

0 commit comments

Comments
 (0)