int send_all(serverinfo_t *si, chisocketentry_t *entry, tcp_data_t *data){ int sent = 0; int to_send = 0; int num_allowed = 0; tcp_packet_t *new_pack; tcphdr_t *ACK; uint8_t *dst; num_allowed = data->SND_UNA + data->SND_WND - data->SND_NXT; to_send = MIN(num_allowed, MIN(MIN(TCP_MSS, data->SND_WND), circular_buffer_count(&data->send))); while (circular_buffer_count(&data->send) && (to_send > 0)){ new_pack = (tcp_packet_t*)malloc(sizeof(tcp_packet_t) + to_send); dst = (uint8_t*)malloc(sizeof(uint8_t) * to_send); sent = circular_buffer_read(&(data->send), dst, to_send, BUFFER_BLOCKING); chitcpd_tcp_packet_create(entry, new_pack, dst, sent); ACK = TCP_PACKET_HEADER(new_pack); ACK->seq = chitcp_htonl(data->SND_NXT); ACK->ack_seq = chitcp_htonl(data->RCV_NXT); ACK->ack = 1; ACK->win = chitcp_htons(data->RCV_WND); data->SND_NXT += sent; data->SND_WND -= sent; chilog_tcp(CRITICAL,new_pack,LOG_OUTBOUND); chitcpd_send_tcp_packet(si,entry,new_pack); to_send = MIN(num_allowed, MIN(MIN(TCP_MSS, data->SND_WND), circular_buffer_count(&data->send))); chitcp_tcp_packet_free(new_pack); free(dst); } return CHITCP_OK; }
/* function name: draw_contour * Input Parameter * maze : maze array wich has maze information * map : array to draw contour map * type : maze type to run mouse * pos : current mouse location in the maze */ void draw_contour(unsigned char *maze, unsigned char *map, unsigned int type, unsigned char pos) { int i, contour_lvl = 1; int index; unsigned int item; char found_mouse = 0; struct circular_buffer *cb, contour_buffer; cb = &contour_buffer; circular_buffer_init(cb, contour_cb_buffer, CONTOUR_CB_BUFFER_MAX); /* Uninitialized contour map */ memset(map, 0, MAZEMAX); /* Seed value 1 as a goal */ switch (type) { case TO_GOAL_4X4: map[get_index(3, 3)] = contour_lvl; circular_buffer_write(cb, gen_contour_pos(contour_lvl, 0x33)); break; case TO_GOAL_8X8: map[get_index(7, 7)] = contour_lvl; circular_buffer_write(cb, gen_contour_pos(contour_lvl, 0x77)); break; case TO_GOAL_16X16: map[get_index(7, 7)] = contour_lvl; map[get_index(8, 7)] = contour_lvl; map[get_index(7, 8)] = contour_lvl; map[get_index(8, 8)] = contour_lvl; /* Add list of same level contour value */ circular_buffer_write(cb, gen_contour_pos(contour_lvl, 0x77)); circular_buffer_write(cb, gen_contour_pos(contour_lvl, 0x78)); circular_buffer_write(cb, gen_contour_pos(contour_lvl, 0x87)); circular_buffer_write(cb, gen_contour_pos(contour_lvl, 0x88)); break; case TO_START_4X4: case TO_START_8X8: case TO_START_16X16: map[get_index(0, 0)] = contour_lvl; circular_buffer_write(cb, gen_contour_pos(contour_lvl, 0x00)); break; default: if (type < MAZEMAX) { map[type] = contour_lvl; circular_buffer_write(cb, gen_contour_pos(contour_lvl, type)); } else { print_exit("Invalid target goal index!\n"); } break; } /* Get one contour number from circular buffer. * Put next higher value in next block if there is * no wall to there and save it inti circular buffer. * If contour map reaches to current mouse or * circular buffer is empty then it's done. */ while (!circular_buffer_empty(cb) && !found_mouse) { circular_buffer_read(cb, &item); index = get_contour_index(item); contour_lvl = get_contour_lvl(item) + 1; /* Calculate contour lvl around current cube */ for (i = NI; i <= WI; i++) { if (!(maze[index] & wall_bit(i)) && (map[index + maze_dxy[i]] == 0)) { map[index + maze_dxy[i]] = contour_lvl; circular_buffer_write(cb, gen_contour_pos(contour_lvl, index + maze_dxy[i])); if (index + maze_dxy[i] == pos) found_mouse = 1; } } #ifdef DEBUG if (debug_flag & DEBUG_CONTOUR) { print_map(map); usleep(20000); } #endif } if (!found_mouse) { /* Mouse alorighm should never hit this location */ print_map(map); print_exit("%s couldn't find mouse location\n", __func__); } }