@@ -36,7 +36,7 @@ typedef struct conn_impl {
3636	conn_state_t  state ;
3737	socket_t  udp_sock ;
3838	socket_t  tcp_sock ;
39- 	void  (* tcp_sock_connected )(juice_agent_t * );
39+ 	void  (* tcp_sock_callback )(juice_agent_t * ,  bool );
4040	addr_record_t  tcp_dst ;
4141	uint16_t  ice_tcp_len ;
4242	mutex_t  send_mutex ;
@@ -200,7 +200,7 @@ int conn_poll_prepare(conn_registry_t *registry, pfds_record_t *pfds, timestamp_
200200		if  (conn_impl -> tcp_sock  !=  INVALID_SOCKET ) {
201201			pfds -> pfds [i ].fd  =  conn_impl -> tcp_sock ;
202202			pfds -> pfds [i ].events  =  POLLIN ;
203- 			if  (conn_impl -> tcp_sock_connected  !=  NULL ) {
203+ 			if  (conn_impl -> tcp_sock_callback  !=  NULL ) {
204204				pfds -> pfds [i ].events  |= POLLOUT ;
205205			}
206206			i ++ ;
@@ -284,21 +284,28 @@ void conn_poll_process_udp(juice_agent_t *agent, conn_impl_t *conn_impl, struct
284284
285285}
286286
287+ void  conn_poll_call_tcp_callback (juice_agent_t  * agent , conn_impl_t  * conn_impl , bool  status ) {
288+ 	if  (conn_impl -> tcp_sock_callback ) {
289+ 		conn_impl -> tcp_sock_callback (agent , status );
290+ 		conn_impl -> tcp_sock_callback  =  NULL ;
291+ 	}
292+ }
293+ 
287294void  conn_poll_process_tcp (juice_agent_t  * agent , conn_impl_t  * conn_impl , struct  pollfd  * pfd ) {
288295	if  (pfd -> revents  &  POLLNVAL ) {
289296		JLOG_WARN ("Error when polling socket" );
290297		return ;
291298	}
292299
293300	if  (pfd -> revents  &  POLLHUP  ||  pfd -> revents  &  POLLERR ) {
294- 		agent_conn_fail (agent );
295- 		conn_impl -> state  =  CONN_STATE_FINISHED ;
301+ 		JLOG_WARN ("ice-tcp poll returned error, closing socket" );
302+ 		closesocket (conn_impl -> tcp_sock );
303+ 		conn_impl -> tcp_sock  =  INVALID_SOCKET ;
296304		return ;
297305	}
298306
299- 	if  (pfd -> revents  &  POLLOUT  &&  conn_impl -> tcp_sock_connected ) {
300- 		conn_impl -> tcp_sock_connected (agent );
301- 		conn_impl -> tcp_sock_connected  =  NULL ;
307+ 	if  (pfd -> revents  &  POLLOUT  &&  conn_impl -> tcp_sock_callback ) {
308+ 		conn_poll_call_tcp_callback (agent , conn_impl , true);
302309	}
303310
304311	if  (pfd -> revents  &  POLLIN ) {
@@ -321,8 +328,9 @@ void conn_poll_process_tcp(juice_agent_t *agent, conn_impl_t *conn_impl, struct
321328			return ;
322329
323330		if  (ret  <  0 ) {
324- 			agent_conn_fail (agent );
325- 			conn_impl -> state  =  CONN_STATE_FINISHED ;
331+ 			JLOG_WARN ("ice-tcp read returned error, closing socket" );
332+ 			closesocket (conn_impl -> tcp_sock );
333+ 			conn_impl -> tcp_sock  =  INVALID_SOCKET ;
326334			return ;
327335		}
328336
@@ -447,7 +455,7 @@ int conn_poll_init(juice_agent_t *agent, conn_registry_t *registry, udp_socket_c
447455	mutex_init (& conn_impl -> send_mutex , 0 );
448456	conn_impl -> registry  =  registry ;
449457	conn_impl -> tcp_sock  =  INVALID_SOCKET ;
450- 	conn_impl -> tcp_sock_connected  =  NULL ;
458+ 	conn_impl -> tcp_sock_callback  =  NULL ;
451459
452460	agent -> conn_impl  =  conn_impl ;
453461	return  0 ;
@@ -542,15 +550,15 @@ int conn_poll_send(juice_agent_t *agent, const addr_record_t *dst, const char *d
542550	return  ret ;
543551}
544552
545- void  conn_poll_tcp_connect_func (juice_agent_t  * agent , const  addr_record_t  * dst , void  (* callback )(juice_agent_t  * )) {
553+ void  conn_poll_tcp_connect_func (juice_agent_t  * agent , const  addr_record_t  * dst , void  (* callback )(juice_agent_t  * ,  bool )) {
546554	conn_impl_t  * conn_impl  =  agent -> conn_impl ;
547555
548556	mutex_lock (& conn_impl -> registry -> mutex );
549557	mutex_lock (& conn_impl -> send_mutex );
550- 	if  (conn_impl -> tcp_sock  ==  INVALID_SOCKET ) {
558+ 	if  (conn_impl -> tcp_sock  ==  INVALID_SOCKET   &&   conn_impl -> tcp_sock_callback   ==   NULL ) {
551559		conn_impl -> tcp_sock  =  tcp_create_socket (dst );
552560		memcpy (& conn_impl -> tcp_dst , dst , sizeof (conn_impl -> tcp_dst ));
553- 		conn_impl -> tcp_sock_connected  =  callback ;
561+ 		conn_impl -> tcp_sock_callback  =  callback ;
554562	}
555563	mutex_unlock (& conn_impl -> send_mutex );
556564	mutex_unlock (& conn_impl -> registry -> mutex );
0 commit comments