Ensure connection is initiated with no input

This commit is contained in:
Jeremy Lakeman 2013-12-20 11:00:20 +10:30
parent dbb65228e6
commit 8d9447ba6b

View File

@ -86,10 +86,12 @@ void msp_debug()
struct msp_sock *p=root; struct msp_sock *p=root;
DEBUGF("Msp sockets;"); DEBUGF("Msp sockets;");
while(p){ while(p){
DEBUGF("State %d, from %s:%d to %s:%d, timeout in %"PRId64"ms", DEBUGF("State %d, from %s:%d to %s:%d, next %"PRId64"ms, ack %"PRId64"ms timeout %"PRId64"ms",
p->state, p->state,
alloca_tohex_sid_t(p->header.local.sid), p->header.local.port, alloca_tohex_sid_t(p->header.local.sid), p->header.local.port,
alloca_tohex_sid_t(p->header.remote.sid), p->header.remote.port, alloca_tohex_sid_t(p->header.remote.sid), p->header.remote.port,
(p->next_action - now),
(p->next_ack - now),
(p->timeout - now)); (p->timeout - now));
p=p->_next; p=p->_next;
} }
@ -205,7 +207,8 @@ int msp_set_remote(struct msp_sock *sock, struct mdp_sockaddr remote)
sock->header.remote = remote; sock->header.remote = remote;
sock->state|=MSP_STATE_DATAOUT; sock->state|=MSP_STATE_DATAOUT;
// make sure we send a packet soon // make sure we send a packet soon
sock->next_action = gettime_ms()+10; sock->next_ack = gettime_ms()+10;
sock->next_action = sock->next_ack;
return 0; return 0;
} }
@ -497,6 +500,12 @@ static int process_sock(struct msp_sock *sock)
// should we send an ack now without sending a payload? // should we send an ack now without sending a payload?
if (now > sock->next_ack){ if (now > sock->next_ack){
if (!sock->header.local.port){
if (sock->header.flags & MDP_FLAG_BIND)
// wait until we have heard back from the daemon with our port number before sending another packet.
return 0;
sock->header.flags |= MDP_FLAG_BIND;
}
int r = send_ack(sock); int r = send_ack(sock);
if (r==-1) if (r==-1)
return -1; return -1;
@ -531,7 +540,8 @@ int msp_processing(time_ms_t *next_action)
if (sock->next_action < *next_action) if (sock->next_action < *next_action)
*next_action=sock->next_action; *next_action=sock->next_action;
} }
} }else if (sock->next_action < *next_action)
*next_action=sock->next_action;
if (sock->state & MSP_STATE_CLOSED){ if (sock->state & MSP_STATE_CLOSED){
struct msp_sock *s = sock->_next; struct msp_sock *s = sock->_next;
msp_free(sock); msp_free(sock);
@ -547,6 +557,7 @@ static int process_packet(int mdp_sock, struct mdp_header *header, const uint8_t
{ {
// any kind of error reported by the daemon, close all related msp connections // any kind of error reported by the daemon, close all related msp connections
if (header->flags & MDP_FLAG_ERROR){ if (header->flags & MDP_FLAG_ERROR){
WHY("Error returned from daemon");
msp_close_all(mdp_sock); msp_close_all(mdp_sock);
return -1; return -1;
} }