Skip to content

Commit cf814b2

Browse files
author
Daniel Campora
committed
cc3200: Refactor and clean-up socket closing code.
1 parent ecb7f9f commit cf814b2

4 files changed

Lines changed: 15 additions & 49 deletions

File tree

cc3200/mods/modnetwork.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,17 @@ typedef struct _mod_network_nic_type_t {
4343
typedef struct _mod_network_socket_base_t {
4444
union {
4545
struct {
46+
// this order is important so that fileno gets > 0 once
47+
// the socket descriptor is assigned after being created.
4648
uint8_t domain;
49+
int8_t fileno;
4750
uint8_t type;
4851
uint8_t proto;
49-
int8_t fileno;
5052
} u_param;
5153
int16_t sd;
5254
};
5355
bool has_timeout;
5456
bool cert_req;
55-
bool closed;
5657
} mod_network_socket_base_t;
5758

5859
typedef struct _mod_network_socket_obj_t {

cc3200/mods/modusocket.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ STATIC mp_obj_t socket_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_
138138
s->sock_base.u_param.fileno = -1;
139139
s->sock_base.has_timeout = false;
140140
s->sock_base.cert_req = false;
141-
s->sock_base.closed = false;
142141

143142
if (n_args > 0) {
144143
s->sock_base.u_param.domain = mp_obj_get_int(args[0]);
@@ -158,7 +157,6 @@ STATIC mp_obj_t socket_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_
158157
if (wlan_socket_socket(s, &_errno) != 0) {
159158
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(-_errno)));
160159
}
161-
162160
return s;
163161
}
164162

cc3200/mods/modwlan.c

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,36 +1151,30 @@ STATIC const mp_cb_methods_t wlan_cb_methods = {
11511151
int wlan_gethostbyname(const char *name, mp_uint_t len, uint8_t *out_ip, uint8_t family) {
11521152
uint32_t ip;
11531153
int result = sl_NetAppDnsGetHostByName((_i8 *)name, (_u16)len, (_u32*)&ip, (_u8)family);
1154-
11551154
out_ip[0] = ip;
11561155
out_ip[1] = ip >> 8;
11571156
out_ip[2] = ip >> 16;
11581157
out_ip[3] = ip >> 24;
1159-
11601158
return result;
11611159
}
11621160

11631161
int wlan_socket_socket(mod_network_socket_obj_t *s, int *_errno) {
1164-
// open the socket
11651162
int16_t sd = sl_Socket(s->sock_base.u_param.domain, s->sock_base.u_param.type, s->sock_base.u_param.proto);
1166-
// save the socket descriptor
1167-
s->sock_base.sd = sd;
11681163
if (sd < 0) {
11691164
*_errno = sd;
11701165
return -1;
11711166
}
1172-
1167+
s->sock_base.sd = sd;
11731168
return 0;
11741169
}
11751170

11761171
void wlan_socket_close(mod_network_socket_obj_t *s) {
11771172
// this is to prevent the finalizer to close a socket that failed when being created
11781173
if (s->sock_base.sd >= 0) {
11791174
modusocket_socket_delete(s->sock_base.sd);
1180-
// TODO check return value and raise an exception if applicable
11811175
sl_Close(s->sock_base.sd);
1176+
s->sock_base.sd = -1;
11821177
}
1183-
s->sock_base.closed = true;
11841178
}
11851179

11861180
int wlan_socket_bind(mod_network_socket_obj_t *s, byte *ip, mp_uint_t port, int *_errno) {
@@ -1218,7 +1212,6 @@ int wlan_socket_accept(mod_network_socket_obj_t *s, mod_network_socket_obj_t *s2
12181212

12191213
// return ip and port
12201214
UNPACK_SOCKADDR(addr, ip, *port);
1221-
12221215
return 0;
12231216
}
12241217

@@ -1241,38 +1234,15 @@ int wlan_socket_send(mod_network_socket_obj_t *s, const byte *buf, mp_uint_t len
12411234
*_errno = bytes;
12421235
return -1;
12431236
}
1244-
12451237
return bytes;
12461238
}
12471239

12481240
int wlan_socket_recv(mod_network_socket_obj_t *s, byte *buf, mp_uint_t len, int *_errno) {
1249-
// check if the socket is open
1250-
if (s->sock_base.closed) {
1251-
// socket is closed, but the there might be data remaining in the buffer, so check
1252-
fd_set rfds;
1253-
FD_ZERO(&rfds);
1254-
FD_SET(s->sock_base.sd, &rfds);
1255-
timeval tv;
1256-
tv.tv_sec = 0;
1257-
tv.tv_usec = 2;
1258-
int nfds = sl_Select(s->sock_base.sd + 1, &rfds, NULL, NULL, &tv);
1259-
if (nfds == -1 || !FD_ISSET(s->sock_base.sd, &rfds)) {
1260-
// no data waiting, so close the socket and return 0 data
1261-
wlan_socket_close(s);
1262-
return 0;
1263-
}
1264-
}
1265-
1266-
// cap length at WLAN_MAX_RX_SIZE
1267-
len = MIN(len, WLAN_MAX_RX_SIZE);
1268-
1269-
// do the recv
1270-
int ret = sl_Recv(s->sock_base.sd, buf, len, 0);
1241+
int ret = sl_Recv(s->sock_base.sd, buf, MIN(len, WLAN_MAX_RX_SIZE), 0);
12711242
if (ret < 0) {
12721243
*_errno = ret;
12731244
return -1;
12741245
}
1275-
12761246
return ret;
12771247
}
12781248

@@ -1289,7 +1259,7 @@ int wlan_socket_sendto( mod_network_socket_obj_t *s, const byte *buf, mp_uint_t
12891259
int wlan_socket_recvfrom(mod_network_socket_obj_t *s, byte *buf, mp_uint_t len, byte *ip, mp_uint_t *port, int *_errno) {
12901260
sockaddr addr;
12911261
socklen_t addr_len = sizeof(addr);
1292-
mp_int_t ret = sl_RecvFrom(s->sock_base.sd, buf, len, 0, &addr, &addr_len);
1262+
mp_int_t ret = sl_RecvFrom(s->sock_base.sd, buf, MIN(len, WLAN_MAX_RX_SIZE), 0, &addr, &addr_len);
12931263
if (ret < 0) {
12941264
*_errno = ret;
12951265
return -1;
@@ -1355,12 +1325,6 @@ int wlan_socket_ioctl (mod_network_socket_obj_t *s, mp_uint_t request, mp_uint_t
13551325
// set fds if needed
13561326
if (flags & MP_IOCTL_POLL_RD) {
13571327
FD_SET(sd, &rfds);
1358-
1359-
// A socked that just closed is available for reading. A call to
1360-
// recv() returns 0 which is consistent with BSD.
1361-
if (s->sock_base.closed) {
1362-
ret |= MP_IOCTL_POLL_RD;
1363-
}
13641328
}
13651329
if (flags & MP_IOCTL_POLL_WR) {
13661330
FD_SET(sd, &wfds);

cc3200/serverstask.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,15 @@ void TASK_Servers (void *pvParameters) {
107107
servers_data.do_disable = false;
108108
servers_data.enabled = false;
109109
}
110-
else if (servers_data.do_reset && servers_data.enabled) {
111-
telnet_reset();
112-
ftp_reset();
110+
else if (servers_data.do_reset) {
111+
// resetting the servers is needed to prevent half-open sockets
113112
servers_data.do_reset = false;
114-
// resetting the servers is needed to preven half-open sockets
115-
// and we should also close all user sockets
113+
if (servers_data.enabled) {
114+
telnet_reset();
115+
ftp_reset();
116+
}
117+
// and we should also close all user sockets. We do it here
118+
// for convinience and to save on code size.
116119
modusocket_close_all_user_sockets();
117120
}
118121

0 commit comments

Comments
 (0)