Exemple #1
0
void start_Protocol(int *sock_fd,char* filename, unsigned long long int bytesToTransfer)
{
	unsigned long long int transferred = 0;
	unsigned long long int window_size = 1;
	unsigned long long int packet_size = 10000;																				//840
	unsigned long long int current_seq=1;
	unsigned long long int last_ack;
	unsigned long long int last_packet_size;
	unsigned long long int total_ack;
	unsigned long long int count_receive=0;
	unsigned long long int window_id=0;
	unsigned long long int start_seq=0;
	unsigned long long int end_seq=0;
	unsigned long long int start[5000],end[5000];
	unsigned long long int offset[5000];
	unsigned long long int current_packet_size[5000];
	unsigned long long int total_message_size[5000];
	char * mssg_pointer[5000];
	int change_window=0;
	int all_delivered=0;
	int max_size;
	while(transferred<bytesToTransfer)
	{
		if(packet_size>(bytesToTransfer-transferred))// only one packet needs to be send
		{
			window_size=1;
			packet_size=bytesToTransfer-transferred;
			//fprintf(stderr,"Last packet \n");
		}
		int i;
		
		
		start[0]=transferred;
        end[0]=transferred+packet_size;
        window_id=0;
        change_window++;
		for(i=1;i<window_size;i++)
        {
        	start[i]=end[i-1];
        	if(start[i]+packet_size>=bytesToTransfer)
     		{
				packet_size=bytesToTransfer-start[i]; //this is the last packet
				last_packet_size=packet_size;				
				window_size=i+1;	//this will be the new max size of last window
				end[i]=start[i]+packet_size;
				//fprintf(stderr,"Last packet \n");
				break;			
			}		        		
			end[i]=start[i]+packet_size;
        	//fprintf(stderr,"transferred =%d start %d end %d\n",transferred,start[i],end[i]);
        }
        
        start_seq=current_seq;
        max_size=window_size;
		for(i=0;i<window_size;i++)
        {
			offset[i]=start[i];
        	current_packet_size[i]=end[i]-start[i];
        	if(offset[i]+current_packet_size[i]==bytesToTransfer)
        	{
				total_ack=current_seq-1;        			
				//fprintf(stderr,"Last packet \n");
        	 	end_seq=current_seq;
        	 	current_seq=0;
				all_delivered=1;
			}
        	mssg_pointer[i]=create_message(current_packet_size[i],offset[i],current_seq,window_size,window_id,change_window);
        	total_message_size[i]=total_length;
        	//current_seq!=0 && current_seq!=1217 && current_seq!=2100 && current_seq!=3500 && current_seq!=5000
        	//if(current_seq%4!=0)//drop every odd seq packet
        	//{	
				if(transmit(mssg_pointer[i],sock_fd,total_message_size[i])==1)
        		{	
        			if(current_seq!=0)
        				current_seq++;
        			transferred+=current_packet_size[i];
        		}
        		else// something bad happened , go for retransmission
        		{
        			fprintf(stderr,"Window retransmit unexpected behavior \n");
        	 		transferred-=current_packet_size[i];
        	 		i--;
        	 		//window_id/2;
        		}
        		window_id++;
        	//}
        	//else
        	//{ 
        	//	transferred+=current_packet_size[i];
        	//	if(current_seq!=0)
        	//		current_seq++;	//just for simulating forced drop
			//	window_id++;	//just for simulating forced drop
			//	fprintf(stderr,"droped %d\n",current_seq-1);
			//}	        	
		}
		
		//fprintf(stderr," round over window size was %d \n",window_size);
		/******************************Window has been sent look for ACK******************************************/
		//change_window=0;
		end_seq=start_seq;
		last_ack=receive_protocol(sock_fd);
		//fprintf(stderr,"last ack %lld  wanted %lld \n", last_ack,current_seq);
		if(last_ack!=current_seq || current_seq==0)//all packets did not got delivered
		{
			//fprintf(stderr,"congestion last ack %lld  wanted %lld \n", last_ack,current_seq);
			while(last_ack!=current_seq || current_seq==0)
			{
				for(i=0;i<window_size;i++)
        			{
        				//fprintf(stderr,"window_id  %s\n",mssg_pointer[i]);
        				//fprintf(stderr,"rt ");
        				if(offset[i]+current_packet_size[i]==bytesToTransfer)//last packet rt
							start_seq=0;
        				transmit(mssg_pointer[i],sock_fd,total_message_size[i]);
        				
             			}
             			//fprintf(stderr,"\n");
        			last_ack=receive_protocol(sock_fd);
        			start_seq=end_seq;
        			
			}
			
			if(window_size!=1)
			{
				window_size = window_size/2;
	
			}
			else
			{	
				window_size+=1;
			}
		}
		else 
		{	
			if(window_size<53)																//window_size
				window_size+=1;
			
		}
		//fprintf(stderr,"round over window_id %lld  %lld\n",window_size,change_window);	
		for(i=0;i<max_size;i++)
			free(mssg_pointer[i]);	
	}
}	
Exemple #2
0
void establish_connection_reception()
{
	if(get_role() == SEARCHING || get_role() == CONNECTING)
	{
		message_type_t received = 0;
		signal_type_t received_signal_type;
		size_t received_size = 0;
		if(receive_protocol(&received,&received_signal_type, &received_size) == 0)
		{
			if(received == MES_HELLO && get_role()== SEARCHING)
			{
				transmit_message(MES_ACK);
				set_role(CONNECTING);
			}
			else if(received == MES_ACK && (get_role() == CONNECTING||get_role() == SEARCHING))
			{
				transmit_message(MES_ACK);
				set_role(CONNECTED);
				transmit_protocol(get_program_playername(), TYPE_STRING, strlen(get_program_playername()));
				transmit_protocol(get_programinitialtime_ptr(), TYPE_INT, sizeof(int));
			}
		}

	}
	else if(get_role() == CONNECTED)
	{
		message_type_t received = 0;
		signal_type_t received_signal_type;
		size_t received_size = 0;

		if(receive_protocol(get_program_playername2(),&received_signal_type, &received_size) == 0)
		{
			if(received_signal_type == TYPE_STRING)
			{
				strcat(opponentmessage, get_program_playername2());
				if(strcmp(get_program_playername(), get_program_playername2()) == 0)
					set_role(FAILED);
				else if(strcmp(get_program_playername(), get_program_playername2()) < 0)
					{
						set_role(SERVER);
					}
				else set_role(CLIENT);
			}
		}
	}
	else {
		char received[100];
		memset(received, 0, 100);
		signal_type_t received_signal_type;
		size_t received_size = 0;
		if(receive_protocol(received,&received_signal_type, &received_size) == 0)
		{
			if(*(unsigned long*)received == MES_BYE)
			{
				set_role(SEARCHING);
				memset(get_program_playername2(),0,sizeof(char)*MAX_PLAYER_NAME_LENGTH+1);
				if(im_message_log != NULL)
				{

					size_t del_i;
					for(del_i = 0 ; del_i < size(im_message_log); del_i++)
					{
						free(*(char**)at(im_message_log, del_i));
					}

					clear(im_message_log);
				}
			}
			else if(received_signal_type == TYPE_INT)
			{
				set_player2time( *(unsigned long*)received);
				if(get_role() == SERVER)
					srand(*get_programinitialtime_ptr());
				else srand(get_player2time());
			}
			else if(*(unsigned long*)received == MES_READY)
			{
				if(lobby_state == NOT_READY)
					lobby_state = PLAYER2_READY;
				else if(lobby_state == PLAYER1_READY)
				{
					lobby_state = BOTH_READY;
					fade_condition = true;
				}
			}
			else if(*(unsigned long*)received == MES_UNREADY)
			{
				if(lobby_state == PLAYER2_READY)
				{
					lobby_state = NOT_READY;
				}
				else if(lobby_state == BOTH_READY)
				{
					lobby_state = PLAYER1_READY;
					fade_condition = false;
				}
			}
			else if(received_signal_type == TYPE_STRING)
			{
				char* temp = (char*) calloc(IM_MESSAGE_MAX + 5,1);
				strcpy(temp, "you: ");
				strcat(temp,received);
				push_back(im_message_log, &temp);
			}
		}
	}
}