Skip to content

Commit 7f12aa5

Browse files
committed
Refactor Sockets to be FileHandle
This allows sockets to be used with standard C library: TCPSocket sock; sock.open(interface); sock.connect("echo.server.foo", 7); FILE *stream = fdopen(&sock, "r+"); fprintf(stream, "Hello World\n");
1 parent b4dc325 commit 7f12aa5

File tree

5 files changed

+50
-24
lines changed

5 files changed

+50
-24
lines changed

features/netsocket/Socket.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,11 @@ nsapi_error_t Socket::bind(const SocketAddress &address)
121121
return ret;
122122
}
123123

124-
void Socket::set_blocking(bool blocking)
124+
int Socket::set_blocking(bool blocking)
125125
{
126126
// Socket::set_timeout is thread safe
127127
set_timeout(blocking ? -1 : 0);
128+
return 0;
128129
}
129130

130131
void Socket::set_timeout(int timeout)

features/netsocket/Socket.h

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525
#include "rtos/Mutex.h"
2626
#include "Callback.h"
2727
#include "mbed_toolchain.h"
28+
#include "platform/FileHandle.h"
2829

2930

3031
/** Abstract socket class
3132
*/
32-
class Socket {
33+
class Socket : public mbed::FileHandle {
3334
public:
3435
/** Destroy a socket
3536
*
@@ -53,15 +54,6 @@ class Socket {
5354
return open(nsapi_create_stack(stack));
5455
}
5556

56-
/** Close the socket
57-
*
58-
* Closes any open connection and deallocates any memory associated
59-
* with the socket. Called from destructor if socket is not closed.
60-
*
61-
* @return 0 on success, negative error code on failure
62-
*/
63-
nsapi_error_t close();
64-
6557
/** Subscribes to an IP multicast group
6658
*
6759
* @param address Multicast group IP address
@@ -107,19 +99,6 @@ class Socket {
10799
*/
108100
nsapi_error_t bind(const SocketAddress &address);
109101

110-
/** Set blocking or non-blocking mode of the socket
111-
*
112-
* Initially all sockets are in blocking mode. In non-blocking mode
113-
* blocking operations such as send/recv/accept return
114-
* NSAPI_ERROR_WOULD_BLOCK if they can not continue.
115-
*
116-
* set_blocking(false) is equivalent to set_timeout(-1)
117-
* set_blocking(true) is equivalent to set_timeout(0)
118-
*
119-
* @param blocking true for blocking mode, false for non-blocking mode.
120-
*/
121-
void set_blocking(bool blocking);
122-
123102
/** Set timeout on blocking socket operations
124103
*
125104
* Initially all sockets have unbounded timeouts. NSAPI_ERROR_WOULD_BLOCK
@@ -213,6 +192,38 @@ class Socket {
213192
attach(mbed::callback(obj, method));
214193
}
215194

195+
/* ------Inherited from FileHandle------- */
196+
197+
/** Close the socket
198+
*
199+
* Closes any open connection and deallocates any memory associated
200+
* with the socket. Called from destructor if socket is not closed.
201+
*
202+
* Inherited from FileHandle
203+
*
204+
* @return 0 on success, negative error code on failure
205+
*/
206+
virtual nsapi_error_t close();
207+
208+
/** Set blocking or non-blocking mode of the socket
209+
*
210+
* Initially all sockets are in blocking mode. In non-blocking mode
211+
* blocking operations such as send/recv/accept return
212+
* NSAPI_ERROR_WOULD_BLOCK if they can not continue.
213+
*
214+
* set_blocking(false) is equivalent to set_timeout(-1)
215+
* set_blocking(true) is equivalent to set_timeout(0)
216+
*
217+
* @param blocking true for blocking mode, false for non-blocking mode.
218+
*/
219+
virtual int set_blocking(bool blocking);
220+
221+
virtual int sync() { return -1; }
222+
virtual int isatty() { return true; }
223+
virtual off_t seek(off_t offset, int whence = SEEK_SET) { return -1; }
224+
virtual off_t size() { return -1; }
225+
virtual short poll(short events) { return 0; }
226+
216227
protected:
217228
Socket();
218229
virtual nsapi_protocol_t get_proto() = 0;

features/netsocket/TCPServer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ class TCPServer : public Socket {
8989

9090
volatile unsigned _pending;
9191
rtos::Semaphore _accept_sem;
92+
93+
private:
94+
/* ------Inherited from FileHandle------- */
95+
/* Server sockets cannot really be FileHandle's so moved into private */
96+
virtual ssize_t read(void *buffer, size_t size) { return NSAPI_ERROR_UNSUPPORTED; }
97+
virtual ssize_t write(const void *buffer, size_t size) { return NSAPI_ERROR_UNSUPPORTED; }
9298
};
9399

94100

features/netsocket/TCPSocket.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ class TCPSocket : public Socket {
115115
*/
116116
nsapi_size_or_error_t recv(void *data, nsapi_size_t size);
117117

118+
/* ------Inherited from FileHandle------- */
119+
virtual ssize_t read(void *buffer, size_t size) { return recv(buffer, size); }
120+
virtual ssize_t write(const void *buffer, size_t size) { return send(buffer, size); }
121+
118122
protected:
119123
friend class TCPServer;
120124

features/netsocket/UDPSocket.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ class UDPSocket : public Socket {
112112
nsapi_size_or_error_t recvfrom(SocketAddress *address,
113113
void *data, nsapi_size_t size);
114114

115+
/* ------Inherited from FileHandle------- */
116+
virtual ssize_t read(void *buffer, size_t size) { return recvfrom(NULL, buffer, size); }
117+
virtual ssize_t write(const void *buffer, size_t size) { return NSAPI_ERROR_UNSUPPORTED; }
118+
115119
protected:
116120
virtual nsapi_protocol_t get_proto();
117121
virtual void event();

0 commit comments

Comments
 (0)