Exemple #1
0
int main(int argc, char *argv[]){
	//check arguments	
	if(argc != 6){
		printf("usage: ./%s server_ip server_port my_ip, my_port my_id\n",argv[0]);
		return -1;
	}

	//get arguments
	in_addr_t server_ip = inet_addr(argv[1]);
	in_port_t server_port = htons(atoi(argv[2]));
	in_addr_t my_ip = inet_addr(argv[3]);
	in_port_t my_port = htons(atoi(argv[4]));
	int my_id = atoi(argv[5]);

	//construct a socket
	int my_socket = socket(AF_INET, SOCK_STREAM, 0);
	if(my_socket < 0){
		printf("initialize socket failed!\n");
		return -1;
	}

	//set socket option for reuse address
	int reuse_addr = 1;
	setsockopt(my_socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse_addr, sizeof(reuse_addr));

	//bind ip and port to socket
	struct sockaddr_in my_addr;
	bzero(&my_addr, sizeof(my_addr));
	my_addr.sin_family = AF_INET;
	my_addr.sin_addr.s_addr = my_ip;
	my_addr.sin_port = my_port;
	if(bind(my_socket, (struct sockaddr*)&my_addr, sizeof(my_addr))){
		printf("bind address to socket failed!\n");
		return -1;
	}

	//connect to server
	struct sockaddr_in server_addr;
	bzero(&server_addr, sizeof(server_addr));
	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = server_ip;
	server_addr.sin_port = server_port;
	while(connect(my_socket, (struct sockaddr*)&server_addr, sizeof(server_addr))<0){
		usleep(100*1000); //sleep 100ms and try again
	}
	printf("connected to gameserver\n");

	//register to gameserver
	char reg_msg[50] = {'\0'};
	snprintf(reg_msg, sizeof(reg_msg)-1, "reg: %d %s need_notify \n", my_id, "dusheng");
	send(my_socket, reg_msg, strlen(reg_msg)+1, 0);
	printf("register in game\n");

	//receive gameserver's msg and starting game
	int mycard[7]={0}, money[8]={0}, jetton[8]={0};
	char buf[4096] = {'\0'};
	int len = 0, ii = 0, tt = 0, sta = 0, end = 0;
	int game_num = 0, b_id, bb_id, sb_id, b_jetton, bb_jetton, sb_jetton, b_money, bb_money, sb_money;

	char id_buf[10], jetton_buf[10], money_buf[10];
	int player_num=0;
	// play game start
	while(1){
		if (buf[0]=='\0')
			len = recv(my_socket, buf, sizeof(buf)-1, 0);buf[len]='\0';

		if(strncmp(buf, "game-over ", 10) == 0)
			break;
		game_num ++;	printf("start play game: %d\n",game_num);
		printf("0\n");printf("%s", buf);
		//handle seat-info-msg
			//button
		sta = strpchr(buf, len, '\n', 0) + 1;
		sta = strpchr(buf, len, ':', sta) + 2;
                end = strpchr(buf, len, ' ', sta);
		b_id = atoi(substr(id_buf, buf, len ,sta, end-sta));
		sta = end + 1;
		end = strpchr(buf, len, ' ', sta);
		b_jetton = atoi(substr(jetton_buf, buf, len ,sta, end-sta));
		sta = end + 1;
		end = strpchr(buf, len, ' ', sta);
		b_money = atoi(substr(money_buf, buf, len ,sta, end-sta));
		//small blind
		sta = end + 2;
		sta = strpchr(buf, len, ':', sta) + 2;
		end = strpchr(buf, len, ' ', sta);
                sb_id = atoi(substr(id_buf, buf, len ,sta, end-sta));
                sta = end + 1;
                end = strpchr(buf, len, ' ', sta);
                sb_jetton = atoi(substr(jetton_buf, buf, len ,sta, end-sta));
                sta = end + 1;
                end = strpchr(buf, len, ' ', sta);
                sb_money = atoi(substr(money_buf, buf, len ,sta, end-sta));
		if (buf[end+2]!='/')
		{
			
		}
		else player_num = 2;

		//big blind
		//
		end = strpchr(buf, len, '/', 5)+6;//to read out entire the seat message
		sta = end + 1;
		strnmv(buf, len, sta);
		len = len - sta;

		//must be blind-msg
		if (buf[0]=='\0')
			len = recv(my_socket, buf,sizeof(buf)-1, 0);buf[len]='\0';
		printf("1\n");printf("%s\n", buf);

		sta = strpchr(buf, len, '\n', 0) + 1;
		end = strpchr(buf, len, ':', sta);
		char pid_buf[10];
		int sb_pid = atoi(substr(pid_buf, buf, len, sta, end-sta));
		sta = end + 2;
		end = strpchr(buf, len, ' ', sta);
		char bet_buf[20];
		int sb_bet = atoi(substr(bet_buf, buf, len, sta, end-sta));
		printf("small blind: %d, %d\n", sb_pid, sb_bet);

		end = strpchr(buf, len, '/', 6)+7;// to read out the entire blind message
                sta = end + 1;
                strnmv(buf, len, sta);
                len = len - sta;

		//must be hold-cards-msg
		if (buf[0]=='\0')
			len = recv(my_socket, buf,sizeof(buf)-1, 0);buf[len]='\0';
		printf("2\n");printf("%s\n", buf);
		sta = strpchr(buf, len, '\n', 0) + 1;
		end = strpchr(buf, len, ' ', sta);
		mycard[0]=card2num(buf[sta], buf[end+1]);//color and point stored by int
		//char col_1[10];
		//struct Card card_1 = {substr(col_1, buf, len, sta, end-sta), buf[end+1]};
		sta = strpchr(buf, len, '\n', end) + 1;
		end = strpchr(buf, len, ' ', sta);
		mycard[1]=card2num(buf[sta], buf[end+1]);
		//char col_2[10];
		//struct Card card_2 = {substr(col_2, buf, len, sta, end-sta), buf[end+1]};
		//printf("card_1: %d, card_2: %d\n", mycard[0], mycard[1]);
		int b_go_on = check_hold_cards(mycard[0], mycard[1]);
		printf("card1: %d, card2: %d, go on: %d\n", mycard[0], mycard[1], b_go_on);
		
		end = strpchr(buf, len, '/', 5)+6;
                sta = end + 1;
                strnmv(buf, len, sta);
                len = len - sta;

		//inquire-msg for hold round bet
		while(1){
			printf("3\n");
			if (buf[0]=='\0')
				len = recv(my_socket, buf,sizeof(buf)-1, 0);buf[len]='\0';
			
			if (strncmp(buf,"inquire",7)==0) // inquire 
			{
				printf("%s\n", buf);
				char act_msg[20];
				if(b_go_on == 1 || player_num == 2)
					snprintf(act_msg, sizeof(act_msg)-1, "check \n");
				else
					snprintf(act_msg, sizeof(act_msg)-1, "fold \n");
				send(my_socket, act_msg, strlen(act_msg)+1, 0);

				end = strpchr(buf, len, '/', 8)+9;
                		sta = end + 1;
                		strnmv(buf, len, sta);
				len = len - sta;
			}
			else if ( strncmp(buf,"notify",6)==0)// I have take fold or all_in
			{
				printf("%s\n", buf);
				end = strpchr(buf, len, '/', 7)+8;
                                sta = end + 1;
                                strnmv(buf, len, sta);
                                len = len - sta;
			}
			else// end round bet
			{
				break;
			}
			
		}
		
		//flop-msg
		if (strncmp(buf,"pot-win",7)!=0)//2 or more players alive
		{
			printf("4\n");printf("%s\n", buf);
			sta = strpchr(buf, len, '\n', 0) + 1;
			end = strpchr(buf, len, ' ', sta);
			mycard[2]=card2num(buf[sta], buf[end+1]);

			sta = strpchr(buf, len, '\n', end) + 1;
			end = strpchr(buf, len, ' ', sta);
			mycard[3]=card2num(buf[sta], buf[end+1]);

			sta = strpchr(buf, len, '\n', end) + 1;
			end = strpchr(buf, len, ' ', sta);
			mycard[4]=card2num(buf[sta], buf[end+1]);

			b_go_on = check_cards(mycard, 5);
			printf("go on: %d\n", b_go_on);

			end = strpchr(buf, len, '/', 5)+6;
			sta = end + 1;
			strnmv(buf, len, sta);
			len = len - sta;

			//inquire-msg for flop round bet
			while(1){
				printf("5\n");
				if (buf[0]=='\0')
						len = recv(my_socket, buf,sizeof(buf)-1, 0);buf[len]='\0';
                		if (strncmp(buf,"inquire",7)==0)
	                        {
	                                printf("%s\n", buf);
					if(b_go_on<=1)
	                                	send(my_socket, "check \n", 8, 0);
					else if(b_go_on==2)
		                               	send(my_socket, "raise 1000 \n", 13, 0);
					else 
	                                	send(my_socket, "all_in \n", 9, 0);
	
	                                end = strpchr(buf, len, '/', 8)+9;
	                                sta = end + 1;
	                                strnmv(buf, len, sta);
	                                len = len - sta;
	                        }
	                        else if ( strncmp(buf,"notify",6)==0)
	                        {
	                                printf("%s\n", buf);
	                                end = strpchr(buf, len, '/', 7)+8;
	                                sta = end + 1;
	                                strnmv(buf, len, sta);
	                                len = len - sta;
	                        }
        	                else// round bet end
        	                {
        	                        break;
        	                }
			}

			//turn-msg
			if (strncmp(buf,"pot-win",7)!=0)//2 or more players alive
			{
				printf("6\n");printf("%s\n", buf);
                		sta = strpchr(buf, len, '\n', 0) + 1;
                		end = strpchr(buf, len, ' ', sta);
                		mycard[5]=card2num(buf[sta], buf[end+1]);

					b_go_on = check_cards(mycard, 6);
					printf("go on: %d\n", b_go_on);

				end = strpchr(buf, len, '/', 5)+6;
                		sta = end + 1;
                		strnmv(buf, len, sta);
                		len = len - sta;

				//inquire-msg for turn round bet
                		while(1){
					printf("7\n");
					if (buf[0]=='\0')
                 			       len = recv(my_socket, buf,sizeof(buf)-1, 0);buf[len]='\0';

					if (strncmp(buf,"inquire",7)==0)
		                        {
        		                        printf("%s\n", buf);
									if(b_go_on==1)
	                                	send(my_socket, "check \n", 8, 0);
									else if(b_go_on==2)
	                                	send(my_socket, "raise 1000 \n", 13, 0);
									else if(b_go_on==3)
	                                	send(my_socket, "all_in \n", 9, 0);
									else
	                                	send(my_socket, "fold \n", 7, 0);

                        		        end = strpchr(buf, len, '/', 8)+9;
                        		        sta = end + 1;
						strnmv(buf, len, sta);
                                		len = len - sta;
                        		}
                        		else if ( strncmp(buf,"notify",6)==0)
                        		{
                                		printf("%s\n", buf);
                                		end = strpchr(buf, len, '/', 7)+8;
                                		sta = end + 1;
                                		strnmv(buf, len, sta);
                                		len = len - sta;
                        		}
                        		else// round bet end
                        		{
                                		break;
                        		}
                		}

                		//river-msg
				if (strncmp(buf,"pot-win",7)!=0)//2 or more players alive
				{
					printf("8\n");printf("%s\n", buf);
        		        	sta = strpchr(buf, len, '\n', 0) + 1;
        		        	end = strpchr(buf, len, ' ', sta);
        		        	mycard[6]=card2num(buf[sta], buf[end+1]);

							b_go_on = check_cards(mycard, 7);
							printf("go on: %d\n", b_go_on);

					end = strpchr(buf, len, '/', 6)+7;
                			sta = end + 1;
                			strnmv(buf, len, sta);
                			len = len - sta;

					//inquire-msg river round bet
                			while(1){
                        			printf("9\n");
						if (buf[0]=='\0')
							len = recv(my_socket, buf,sizeof(buf)-1, 0);buf[len]='\0';
						if (strncmp(buf,"inquire",7)==0)
			                        {
                			                printf("%s\n", buf);
											if(b_go_on==1)
													send(my_socket, "check \n", 8, 0);
											else if(b_go_on==2)
													send(my_socket, "raise 1000 \n", 13, 0);
											else if(b_go_on==3)
													send(my_socket, "all_in \n", 9, 0);
											else
													send(my_socket, "fold \n", 7, 0);
                                			end = strpchr(buf, len, '/', 8)+9;
                                			sta = end + 1;
                                			strnmv(buf, len, sta);
                                			len = len - sta;
                        			}
                        			else if ( strncmp(buf,"notify",6)==0)
                        			{
                                			printf("%s\n", buf);
                                			end = strpchr(buf, len, '/', 7)+8;
                                			sta = end + 1;
                                			strnmv(buf, len, sta);
                                			len = len - sta;
                        			}
                        			else
                        			{
                                			break;
                        			}

					}

					//showdown-msg
					if (strncmp(buf,"pot-win",7)!=0)//two or more players alive
					{
						printf("10\n");printf("%s\n", buf);
						sta = 0;
						for (ii = 0;ii < 8; ii++)//the common card is invalid
                					sta = strpchr(buf, len, '\n', sta) + 1;
		
		
						end = strpchr(buf, len, '/', sta)+10;
                				sta = end + 1;
                				strnmv(buf, len, sta);
               					len = len - sta;
					}

				}
			}
		}
		//pot-win-msg
		if (buf[0]=='\0')
			len = recv(my_socket, buf,sizeof(buf)-1, 0);buf[len]='\0';
		
		printf("11\n");printf("%s\n", buf);
                sta = strpchr(buf, len, '\n', 0) + 1;
                end = strpchr(buf, len, ' ', sta);//pot-win 0~8 possible records

                //sta = strpchr(buf, len, '\n', end) + 1;
                //end = strpchr(buf, len, ' ', sta);
		
		end = strpchr(buf, len, '/', 8)+9;
                sta = end + 1;
                strnmv(buf, len, sta);
                len = len - sta;

		//if (game_num>2) break;

	}
	close(my_socket);
	return 0;
}
Exemple #2
0
static int ParseHttpHeaderLine(struct HttpEnv *e )
{
	if( e->req_header.METHOD == NULL )
	{
		char	*pc = NULL ;
		
		for( pc = e->req_header.line_begin ; *(pc) == ' ' ; pc++ )
		{
			if( pc > e->req_header.line_end )
				return HTTPARSER_ERROR_HTTP_HEADERLINE_INVALID;
		}
		e->req_header.METHOD = pc ;
		for( pc++ ; *(pc) != ' ' ; pc++ )
		{
			if( pc > e->req_header.line_end )
				return HTTPARSER_ERROR_HTTP_HEADERLINE_INVALID;
		}
		*(pc) = '\0' ;
		
		for( pc++ ; *(pc) == ' ' ; pc++ )
		{
			if( pc > e->req_header.line_end )
				return HTTPARSER_ERROR_HTTP_HEADERLINE_INVALID;
		}
		e->req_header.URL = pc ;
		for( pc++ ; *(pc) != ' ' ; pc++ )
		{
			if( pc > e->req_header.line_end )
				return HTTPARSER_ERROR_HTTP_HEADERLINE_INVALID;
		}
		*(pc) = '\0' ;
		
		for( pc++ ; *(pc) == ' ' ; pc++ )
		{
			if( pc > e->req_header.line_end )
				return HTTPARSER_ERROR_HTTP_HEADERLINE_INVALID;
		}
		e->req_header.VERSION = pc ;
		for( pc++ ; *(pc) != ' ' ; pc++ )
		{
			if( pc > e->req_header.line_end )
				break;
		}
	}
	else
	{
		char	*line_middle = NULL ;
		
		line_middle = strpchr( e->req_header.line_begin , e->req_header.line_end , ':' ) ;
		if( line_middle == NULL )
		{
			return HTTPARSER_ERROR_HTTP_HEADERLINE_INVALID;
		}
		
		*(line_middle) = '\0' ;
		
		for( line_middle++ ; line_middle < e->req_header.line_end ; line_middle++ )
		{
			if( *(line_middle) != ' ' )
				break;
		}
		
		if( STRICMP( e->req_header.line_begin , == , "Content-Length" ) )
		{
			e->req_header.CONTENT_LENGTH = line_middle ;
			e->req_body.body_total_len = atol(line_middle) ;
		}
		else if( STRICMP( e->req_header.line_begin , == , "Cookie" ) )
		{
			e->req_header.COOKIE = line_middle ;
		}
		else if( STRICMP( e->req_header.line_begin , == , "User-Agent" ) )