net: tcp: Make the connect callback on success, not transmission

The net_context_connect() callback was being invoked synchronously
with the transmission of the SYN packet.  That's not very useful, as
it doesn't tell the user anything they can't already figure out from
the return code.  Move it to the receipt of the SYNACK instead, so the
app can know that it's time to start transmitting.  This matches the
Unix semantics more closely, where connect(2) is a blocking call that
wakes up only when the connection is live.

Change-Id: I11e3cca8572d51bee215274e82667e0917587a0f
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
This commit is contained in:
Andy Ross 2017-01-13 12:29:04 -08:00 committed by Jukka Rissanen
commit 7b27d4be0a
2 changed files with 27 additions and 19 deletions

View file

@ -112,6 +112,22 @@ typedef void (*net_tcp_accept_cb_t)(struct net_context *new_context,
int status,
void *user_data);
/**
* @brief Connection callback.
*
* @details The connect callback is called after a connection is being
* established.
*
* @param context The context to use.
* @param status Status of the connection establishment. This is 0
* if the connection was established successfully, <0 if there was an
* error.
* @param user_data The user data given in net_context_connect() call.
*/
typedef void (*net_context_connect_cb_t)(struct net_context *context,
int status,
void *user_data);
struct net_tcp;
struct net_conn_handle;
@ -144,6 +160,11 @@ struct net_context {
*/
net_context_send_cb_t send_cb;
/** Connect callback to be called when a connection has been
* established.
*/
net_context_connect_cb_t connect_cb;
/** User data.
*/
void *user_data;
@ -439,22 +460,6 @@ int net_context_bind(struct net_context *context,
int net_context_listen(struct net_context *context,
int backlog);
/**
* @brief Connection callback.
*
* @details The connect callback is called after a connection is being
* established.
*
* @param context The context to use.
* @param status Status of the connection establishment. This is 0
* if the connection was established successfully, <0 if there was an
* error.
* @param user_data The user data given in net_context_connect() call.
*/
typedef void (*net_context_connect_cb_t)(struct net_context *context,
int status,
void *user_data);
/**
* @brief Create a network connection.
*

View file

@ -927,6 +927,10 @@ static enum net_verdict tcp_synack_received(struct net_conn *conn,
net_tcp_change_state(context->tcp, NET_TCP_ESTABLISHED);
net_context_set_state(context, NET_CONTEXT_CONNECTED);
if (context->connect_cb) {
context->connect_cb(context, 0, context->user_data);
}
send_ack(context, raddr);
k_sem_give(&context->tcp->connect_wait);
@ -1111,9 +1115,8 @@ int net_context_connect(struct net_context *context,
send_syn(context, addr);
if (cb) {
cb(context, 0, user_data);
}
context->connect_cb = cb;
context->user_data = user_data;
/* in tcp_synack_received() we give back this semaphore */
if (k_sem_take(&context->tcp->connect_wait, timeout)) {