int get_from_input(char **str) { int read_bytes = 0; int len = 0; char *temp_str = *str; while(1) { if (processed_ind != 0) { read_ind -= processed_ind; memmove(buffer, buffer + processed_ind, read_ind); processed_ind = 0; } temp_str = get_from_buf(&len); if (temp_str != NULL) { *str = temp_str; return len; } else { if (BUFSIZE == read_ind) { while(temp_str == NULL) { processed_ind = 0; read_ind = 0; read_bytes = read(0, buffer, BUFSIZE); if (read_bytes == 0) { *str = NULL; return 0; } else if (read_bytes < 0){ exit(1); } else { read_ind += read_bytes; } temp_str = get_from_buf(&len); } } else { read_bytes = read(0, buffer + read_ind, BUFSIZE - read_ind); if (read_bytes == 0) { *str = NULL; return 0; } else if (read_bytes < 0) { exit(1); } else { read_ind += read_bytes; } } } } }
unsigned int kbd_read() { if (buf_read != buf_write) { return get_from_buf(); } else { return 0; } }
int mn_recv(int sock, char *client_message, int *mn, struct mes_buf mesbuf[100], struct timeval *timeout, fd_set *readset){ char msg_with_n[BUFLEN], tmp[BUFLEN]; int read_size, new_mn, buf_read_size, reason; while(reason = select(sock+1, readset, NULL, NULL, timeout) > 0){ if( (read_size = recv(sock , msg_with_n, BUFLEN , 0)) > 0){ if(strncmp(msg_with_n,"BEY",3) == 0){ return -1; } new_mn = get_mn(msg_with_n); if( (buf_read_size = get_from_buf(*mn+1, mesbuf, tmp)) > 0){ *mn = *mn + 1; strcpy(client_message, tmp); return buf_read_size; } else{ strcpy(tmp, msg_with_n+4); if( new_mn == *mn + 1){ *mn = *mn + 1; strcpy(client_message, tmp); return strlen(client_message); } else{ if(new_mn > *mn + 1){ add_to_buf(new_mn, tmp, &mesbuf[100]); } if(new_mn < *mn + 1) puts("duplicated message"); } } } } if( (buf_read_size = get_from_buf(*mn+1, mesbuf, tmp)) > 0){ *mn = *mn + 1; return buf_read_size; } return reason; }