Commit 722fb2d2 authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

extmod/modlwip: User proper field name and value names for socket state.

parent 43fecb0a
......@@ -199,8 +199,12 @@ typedef struct _lwip_socket_obj_t {
uint8_t domain;
uint8_t type;
// 0 = unconnected, 1 = connecting, 2 = connected, 3 = other side closed
int8_t connected;
#define STATE_NEW 0
#define STATE_CONNECTING 1
#define STATE_CONNECTED 2
#define STATE_PEER_CLOSED 3
// Negative value is lwIP error
int8_t state;
} lwip_socket_obj_t;
static inline void poll_sockets(void) {
......@@ -231,7 +235,7 @@ STATIC void _lwip_tcp_error(void *arg, err_t err) {
lwip_socket_obj_t *socket = (lwip_socket_obj_t*)arg;
// Pass the error code back via the connection variable.
socket->connected = err;
socket->state = err;
// If we got here, the lwIP stack either has deallocated or will deallocate the pcb.
socket->pcb.tcp = NULL;
}
......@@ -240,7 +244,7 @@ STATIC void _lwip_tcp_error(void *arg, err_t err) {
STATIC err_t _lwip_tcp_connected(void *arg, struct tcp_pcb *tpcb, err_t err) {
lwip_socket_obj_t *socket = (lwip_socket_obj_t*)arg;
socket->connected = 2;
socket->state = STATE_CONNECTED;
return ERR_OK;
}
......@@ -264,7 +268,7 @@ STATIC err_t _lwip_tcp_recv(void *arg, struct tcp_pcb *tcpb, struct pbuf *p, err
if (p == NULL) {
// Other side has closed connection.
socket->connected = 3;
socket->state = STATE_PEER_CLOSED;
return ERR_OK;
} else if (socket->incoming.pbuf != NULL) {
// No room in the inn, let LWIP know it's still responsible for delivery later
......@@ -378,7 +382,7 @@ STATIC mp_uint_t lwip_tcp_send(lwip_socket_obj_t *socket, const byte *buf, mp_ui
// Helper function for recv/recvfrom to handle TCP packets
STATIC mp_uint_t lwip_tcp_receive(lwip_socket_obj_t *socket, byte *buf, mp_uint_t len, int *_errno) {
if (socket->connected == 3) {
if (socket->state == STATE_PEER_CLOSED) {
return 0;
}
......@@ -463,7 +467,7 @@ STATIC mp_obj_t lwip_socket_make_new(mp_obj_t type_in, mp_uint_t n_args,
socket->incoming.pbuf = NULL;
socket->timeout = -1;
socket->connected = 0;
socket->state = STATE_NEW;
socket->leftover_count = 0;
return socket;
}
......@@ -489,7 +493,7 @@ STATIC mp_obj_t lwip_socket_close(mp_obj_t self_in) {
//case MOD_NETWORK_SOCK_RAW: raw_remove(socket->pcb.raw); break;
}
socket->pcb.tcp = NULL;
socket->connected = -16; // EBADF
socket->state = -16; // EBADF
if (socket->incoming.pbuf != NULL) {
if (!socket_is_listener) {
pbuf_free(socket->incoming.pbuf);
......@@ -599,7 +603,7 @@ STATIC mp_obj_t lwip_socket_accept(mp_obj_t self_in) {
socket2->type = MOD_NETWORK_SOCK_STREAM;
socket2->incoming.pbuf = NULL;
socket2->timeout = socket->timeout;
socket2->connected = 2;
socket2->state = STATE_CONNECTED;
socket2->leftover_count = 0;
tcp_arg(socket2->pcb.tcp, (void*)socket2);
tcp_err(socket2->pcb.tcp, _lwip_tcp_error);
......@@ -636,8 +640,8 @@ STATIC mp_obj_t lwip_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
err_t err = ERR_ARG;
switch (socket->type) {
case MOD_NETWORK_SOCK_STREAM: {
if (socket->connected != 0) {
if (socket->connected == 2) {
if (socket->state != STATE_NEW) {
if (socket->state == STATE_CONNECTED) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(EALREADY)));
} else {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(EINPROGRESS)));
......@@ -645,11 +649,10 @@ STATIC mp_obj_t lwip_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
}
// Register our recieve callback.
tcp_recv(socket->pcb.tcp, _lwip_tcp_recv);
// Mark us as "connecting"
socket->connected = 1;
socket->state = STATE_CONNECTING;
err = tcp_connect(socket->pcb.tcp, &dest, port, _lwip_tcp_connected);
if (err != ERR_OK) {
socket->connected = 0;
socket->state = STATE_NEW;
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error_lookup_table[-err])));
}
socket->peer_port = (mp_uint_t)port;
......@@ -658,20 +661,20 @@ STATIC mp_obj_t lwip_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
if (socket->timeout != -1) {
for (mp_uint_t retries = socket->timeout / 100; retries--;) {
mp_hal_delay_ms(100);
if (socket->connected != 1) break;
if (socket->state != STATE_CONNECTING) break;
}
if (socket->connected == 1) {
if (socket->state == STATE_CONNECTING) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ETIMEDOUT)));
}
} else {
while (socket->connected == 1) {
while (socket->state == STATE_CONNECTING) {
mp_hal_delay_ms(100);
}
}
if (socket->connected == 2) {
if (socket->state == STATE_CONNECTED) {
err = ERR_OK;
} else {
err = socket->connected;
err = socket->state;
}
break;
}
......@@ -695,8 +698,8 @@ STATIC mp_obj_t lwip_socket_send(mp_obj_t self_in, mp_obj_t buf_in) {
if (socket->pcb.tcp == NULL) {
// not connected
_errno = error_lookup_table[-(socket->connected)];
socket->connected = -16;
_errno = error_lookup_table[-(socket->state)];
socket->state = -16;
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
}
......@@ -728,8 +731,8 @@ STATIC mp_obj_t lwip_socket_recv(mp_obj_t self_in, mp_obj_t len_in) {
if (socket->pcb.tcp == NULL) {
// not connected
_errno = error_lookup_table[-(socket->connected)];
socket->connected = -16;
_errno = error_lookup_table[-(socket->state)];
socket->state = -16;
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
}
......@@ -766,8 +769,8 @@ STATIC mp_obj_t lwip_socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_obj_t
if (socket->pcb.tcp == NULL) {
// not connected
_errno = error_lookup_table[-(socket->connected)];
socket->connected = -16;
_errno = error_lookup_table[-(socket->state)];
socket->state = -16;
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
}
......@@ -802,8 +805,8 @@ STATIC mp_obj_t lwip_socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in) {
if (socket->pcb.tcp == NULL) {
// not connected
_errno = error_lookup_table[-(socket->connected)];
socket->connected = -16;
_errno = error_lookup_table[-(socket->state)];
socket->state = -16;
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno)));
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment