/******************** v_cose() ******************************** transmit all the packets not yet transmitted, then close the connection ***************************************************************/ int v_close(int socket) { socket_t *so = fd_lookup(socket); if(so == NULL) { printf(_RED_"v_close: socket %d does not exist"_NORMAL_"\n", socket); return -EBADF; } #ifdef DEBUG printf(_BLUE_"v_close: socket %d "_NORMAL_"\n", socket); #endif //while(!CB_EMPTY(socket->sendw->buf)){ // v_shutdown(socket, SHUTDOWN_READ); //} if(so->state == LISTENING) return 0; //this hack doesn't work sometimes //for example, retransmission queue being empty doesn't always mean //that everything has been sent (sent packet might have been in the process of //being queued for retransmission) seg_t *el; int count; DL_COUNT(so->sendw->retrans_q_head,el,count); int sum = count + CB_SIZE(so->sendw->buf) + CB_SIZE(so->recvw->buf) + so->recvw->oor_q_size; int newsum = sum; //TODO shut down sending window while(!CB_EMPTY(so->sendw->buf) || !CB_EMPTY(so->recvw->buf) || count || so->recvw->oor_q_size){ #ifdef DEBUG if(sum != newsum){ printf(_BRED_"V_CLOSED STALLED-----------\n[retransq %d], [sendwindow %d], [recvwindow %d], [oorq %d]\n"_NORMAL_, count, CB_SIZE(so->sendw->buf), CB_SIZE(so->recvw->buf), so->recvw->oor_q_size); sum = newsum; } #endif newsum = count + CB_SIZE(so->sendw->buf) + CB_SIZE(so->recvw->buf) + so->recvw->oor_q_size; DL_COUNT(so->sendw->retrans_q_head,el,count); } //TODO memory stuff printf(_BRED_"---CANCELING THREAD, DELETING SOCKET---\n"_NORMAL_); v_shutdown(socket, SHUTDOWN_BOTH); pthread_cancel(so->th); HASH_DELETE(hh1, fd_list, so); return 0; }
int32_t queue_size(queue_t *queue) { int32_t count = 0; struct queueitem *tmp; lock_queue(queue); DL_COUNT(queue->list,tmp,count); portable_mutex_unlock(&queue->mutex); return count; }