Ejemplo n.º 1
0
/**
clientMove - struct containing msgTxt and reciver
index - ClientsQueue index of sender
*/
void handleMsg(struct clientMsg clientMove, int index){
	struct gameData data;
	//int i;
	char buf[MSGTXT_SIZE];
	int msg_size = MSGTXT_SIZE;

	data.valid = 1;
	data.msg = 1;
	strcpy(data.msgTxt, clientMove.msgTxt);
	createClientMsgBuff(clientMove, buf);
	sendAll(ClientsQueue[(index + 1) % 2].fd, buf, &msg_size);
	//if (clientMove.recp == -1){
	//	// send to all except the sender
	//	for (i = 0; i< conPlayers + conViewers; i++){
	//		if (i != index){
	//			sendAll(ClientsQueue[i].fd, buf, &MSGTXT_SIZE);
	//		}
	//	}
	//}
	//else{
	//	// send only to a specific client number
	//	for (i = 0; i< conPlayers + conViewers; i++){
	//		if (ClientsQueue[i].clientNum == clientMove.recp){
	//			sendAll(ClientsQueue[i].fd, buf, &MSGTXT_SIZE);
	//		}
	//	}
	//}
}
Ejemplo n.º 2
0
/**
	clientMove - struct containing msgTxt and reciver
	index - ClientsQueue index of sender
*/
void handleMsg(struct clientMsg clientMove,int index){
	struct gameData data;
	int i;
	char buf[MSG_SIZE];

	data.valid = 1;
	data.msg = index;
	strcpy(data.msgTxt, clientMove.msgTxt);
	createClientMsgBuff(clientMove, buf);

	if(clientMove.recp == -1){
		// send to all except the sender
		for (i=0; i< conPlayers + conViewers ; i++){
			if(i != index){
				sendAll(ClientsQueue[i].fd, buf, &msg_SIZE);
			}
		}
	}
	else{
		// send only to a specific client number
		for (i=0; i< conPlayers + conViewers ; i++){
			if(ClientsQueue[i].clientNum == clientMove.recp){
				sendAll(ClientsQueue[i].fd, buf, &msg_SIZE);
			}
		}
	}
}
Ejemplo n.º 3
0
int main(int argc, char const *argv[]){
	char port[20];
	char address[50];
	int i, j;

	if (argc<1 || argc>3){
		printf(ILLEGAL_ARGS);
		exit(1);
	}
	if (argc == 1 || argc == 2){
		strcpy(port, DEFAULT_PORT);
		if (argc == 1) strcpy(address, DEFAULT_HOST);
	}
	if (argc == 2 || argc == 3){
		strcpy(address, argv[1]);
		if (argc == 3){
			strcpy(port, argv[2]);
		}
	}
	int sock = socket(AF_INET, SOCK_STREAM, 0); // Get socket
	if (sock == -1){
		printf(SOCKET_ERROR, strerror(errno));
		return errno;
	}
	// Connect to server
	sock = connectToServer(sock, address, port);
	// Get initial data
	char readBuf[BUF_SIZE];
	int bufSize = BUF_SIZE;
	receive_all(sock, readBuf, &bufSize, 1);
	// Get the initial data from the server
	if (game.valid == 0){
		printf(CONNECTION_REJECTION);
		return 0;
	}

	playerId = game.myPlayerId;
	myTurn = game.isMyTurn;
	printf("You are client %d\n", playerId + 1);
	if (playerId == 0){
		printf("Waiting to client 2 to connect.\n");
		receive_all(sock, readBuf, &bufSize, 1); //wait until second player connects
	}

	printGameState(game);
	int addReadyForSend = 0;
	fd_set fdSetRead, fdSetWrite;
	struct clientMsg cm;

	if (myTurn == 1){
		printf(YOUR_TURN);
	}
	while (game.win == -1){
		int maxClientFd = sock;

		FD_ZERO(&fdSetRead);
		FD_ZERO(&fdSetWrite);
		FD_SET(STDIN, &fdSetRead);
		FD_SET(sock, &fdSetRead);

		if (addReadyForSend == 1){
			FD_SET(sock, &fdSetWrite);
		}

		int fdReady = select(maxClientFd + 1, &fdSetRead, &fdSetWrite, NULL, NULL);
		if (fdReady == 0){ //chicken check.
			continue;
		}

		if (FD_ISSET(sock, &fdSetWrite) && addReadyForSend == 1){//packets are ready for send
			int buf = BUF_SIZE;
			char cmBuffer[BUF_SIZE];
			i = 0;
			while (i<cmQueueLength){ //send as fifo
				createClientMsgBuff(cmQueue[i], cmBuffer);
				if (send_all(sock, cmBuffer, &buf) == -1){
					break;
				}
				i++;
			}
			j = -1;
			while (i<cmQueueLength){ //reorganize cmQueue
				j++;
				cmQueue[j] = cmQueue[i];
				i++;
			}
			cmQueueLength = j + 1;
			if (cmQueueLength == 0){ addReadyForSend = 0; };
		}
		if (FD_ISSET(STDIN, &fdSetRead)){// there is input from cmd
			int rSize = BUF_SIZE;
			fgets(readBuf, rSize, stdin);
			cm = getMoveFromInput(sock, readBuf);
			if (cm.msg == 1){ //it's a message! send it right away!
				cmQueue[cmQueueLength] = cm;
				cmQueueLength++;
				addReadyForSend = 1;
			}
			else{//it's a turn.
				if (myTurn != 1){// not my turn!
					printf(MOVE_REJECTED);
				}
				else{
					cmQueue[cmQueueLength] = cm;
					cmQueueLength++;
					addReadyForSend = 1;
				}
			}
		}
		if (FD_ISSET(sock, &fdSetRead)){
			char rBuf[BUF_SIZE];
			int rSize = BUF_SIZE;
			receive_all(sock, rBuf, &rSize, 0);
		}
	}
	printWinner(game);
	return 0;
}