示例#1
0
void sendPacketToPlayers(MW244BPacket p)
{

	ConvertOutgoing(&p);

	if (sendto(M->theSocket(), &p, sizeof(MW244BPacket), 0, (struct sockaddr *)&groupAddr, sizeof(Sockaddr)) < 0)
		MWError("Sample error"); 

}
示例#2
0
/* This will presumably be modified by you.
   It is here to provide an example of how to open a UDP port.
   You might choose to use a different strategy
 */
void
netInit()
{
	bool		joinGame();
	Sockaddr	nullAddr;
	Sockaddr	*thisHost;
	char		buf[128];
	int		reuse;
	u_char          ttl;
	struct ip_mreq  mreq;

	/* MAZEPORT will be assigned by the TA to each team */
	M->mazePortIs(htons(MAZEPORT));

	gethostname(buf, sizeof(buf));
	if ((thisHost = resolveHost(buf)) == (Sockaddr *) NULL)
	  MWError("who am I?");
	bcopy((caddr_t) thisHost, (caddr_t) (M->myAddr()), sizeof(Sockaddr));

	M->theSocketIs(socket(AF_INET, SOCK_DGRAM, 0));
	if (M->theSocket() < 0)
	  MWError("can't get socket");

	/* SO_REUSEADDR allows more than one binding to the same
	   socket - you cannot have more than one player on one
	   machine without this */
	reuse = 1;
	if (setsockopt(M->theSocket(), SOL_SOCKET, SO_REUSEADDR, &reuse,
		   sizeof(reuse)) < 0) {
		MWError("setsockopt failed (SO_REUSEADDR)");
	}

	nullAddr.sin_family = AF_INET;
	nullAddr.sin_addr.s_addr = htonl(INADDR_ANY);
	nullAddr.sin_port = M->mazePort();
	if (bind(M->theSocket(), (struct sockaddr *)&nullAddr,
		 sizeof(nullAddr)) < 0)
	  MWError("netInit binding");

	/* Multicast TTL:
	   0 restricted to the same host
	   1 restricted to the same subnet
	   32 restricted to the same site
	   64 restricted to the same region
	   128 restricted to the same continent
	   255 unrestricted

	   DO NOT use a value > 32. If possible, use a value of 1 when
	   testing.
	*/

	ttl = 32;
	if (setsockopt(M->theSocket(), IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
		   sizeof(ttl)) < 0) {
		MWError("setsockopt failed (IP_MULTICAST_TTL)");
	}

	/* uncomment the following if you do not want to receive messages that
	   you sent out - of course, you cannot have multiple players on the
	   same machine if you do this */
#if 0
	{
	  u_char loop = 0;
	  if (setsockopt(M->theSocket(), IPPROTO_IP, IP_MULTICAST_LOOP, &loop,
	             sizeof(loop)) < 0) {
		MWError("setsockopt failed (IP_MULTICAST_LOOP)");
	  }
	}
#endif

	/* join the multicast group */
	mreq.imr_multiaddr.s_addr = htonl(MAZEGROUP);
	mreq.imr_interface.s_addr = htonl(INADDR_ANY);
	if (setsockopt(M->theSocket(), IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)
		   &mreq, sizeof(mreq)) < 0) {
		MWError("setsockopt failed (IP_ADD_MEMBERSHIP)");
	}

	/*
	 * Now we can try to find a game to join; if none, start one.
	 */

	/* I haven't implemented joinGame or startGame in this sample */
	/*
	if (!joinGame())
		startGame();
        */
	printf("\n");

	/* set up some stuff strictly for this local sample */
	M->myRatIdIs(0);
	M->scoreIs(0);
	SetMyRatIndexType(0);

	/* Get the multi-cast address ready to use in SendData()
           calls. */
	memcpy(&groupAddr, &nullAddr, sizeof(Sockaddr));
	groupAddr.sin_addr.s_addr = htonl(MAZEGROUP);

	mws_set_addr(M->state, (struct sockaddr *)&groupAddr, M->theSocket());
}
示例#3
0
void
play(void)
{
	MWEvent		event;
	MW244BPacket	incoming;

	event.eventDetail = &incoming;

	while (TRUE) {
		NextEvent(&event, M->theSocket());
		if (!M->peeking())
			switch(event.eventType) {
			case EVENT_A:
				aboutFace();
				break;

			case EVENT_S:
				leftTurn();
				break;

			case EVENT_D:
				forward();
				break;

			case EVENT_F:
				rightTurn();
				break;

			case EVENT_BAR:
				backward();
				break;

			case EVENT_LEFT_D:
				peekLeft();
				break;

			case EVENT_MIDDLE_D:
				shoot();
				break;

			case EVENT_RIGHT_D:
				peekRight();
				break;

			case EVENT_NETWORK:
				processPacket(&event);
				break;

			case EVENT_INT:
				quit(0);
				break;

			}
		else
			switch (event.eventType) {
			case EVENT_RIGHT_U:
			case EVENT_LEFT_U:
				peekStop();
				break;

			case EVENT_NETWORK:
				processPacket(&event);
				break;
			}

		ratStates();		/* clean house */

		manageMissiles();

		DoViewUpdate();

		mws_update(M->state);

		/* Any info to send over network? */

	}
}
示例#4
0
/* ----------------------------------------------------------------------- */
void play(void)
{	// initialisation
	cout << "Starting Play" << endl;
	//SetRatPosition(7,1,5,0);
	proj.present = false;
	proj.prev_x = proj.prev_y = proj.x = proj.y = 1;
	int turn = 0;
	
	updateSeqNo = true;
	join = false;
	
	for (int k = 0; k < 8; k++)
		participants[k] = prevseq_a[k] = 0;
	for (int k = 0; k < 8; k++)
		expected_seqno[k] = k+1;
	GLOBAL_ID = 0;
	checking = 1;
	checkingzero=1;
	MWEvent		event;
	MW244BPacket	incoming;

	event.eventDetail = &incoming;

	while (TRUE) {
		turn ++;
		NextEvent(&event, M->theSocket());
		if (!M->peeking())
			switch(event.eventType) {
				case EVENT_A:
					aboutFace();
					break;

				case EVENT_S:
					leftTurn();
					break;

				case EVENT_D:
					forward();
					break;

				case EVENT_F:
					rightTurn();
					break;


				case EVENT_LEFT_D:
					peekLeft();
					break;

				case EVENT_BAR:
					shoot();
					break;

				case EVENT_RIGHT_D:
					peekRight();
					break;

				case EVENT_NETWORK:
					processPacket(&event);
					break;

				case EVENT_TIMEOUT:

					checking = (++checking) % 25;					
					if (checking == 0 && !join){
						cout << "Setting ID to zero" << endl;
						join = true;
						setMapping();
					}		
					else {cout << "TIMEOUT"  << endl;
						participation = (++participation) % 30;
						manageMissiles();
						if (participation == 29)
						{ ratStates(); /* clean house */
							for (int i = 0 ; i < 8 ; i++)
								participants[i] = 0;
						}
					}
					break;

				case EVENT_INT:
					quit(0);
					break;

			}
		else
			switch (event.eventType) {
				case EVENT_RIGHT_U:
				case EVENT_LEFT_U:
					peekStop();
					break;
				case EVENT_NETWORK:
					processPacket(&event);
					break;
			}
		DoViewUpdate();
		checking ++;

		manageMissiles();
		if (join && checkingzero == 0)
		{	
			MW244BPacket p;
			makePacket(&p,'a',kills, updateSeqNo);
			sendPacketToPlayers(p);
		}

		else if (join && turn % 2 == 0)
		{
			MW244BPacket p;
			makePacket(&p,'t',-1, updateSeqNo);
			sendPacketToPlayers(p);
		}

	}
}
示例#5
0
	void
netInit()
{
	Sockaddr		nullAddr;
	Sockaddr		*thisHost;
	char			buf[128];
	int				reuse;
	u_char          ttl;
	struct ip_mreq  mreq;

	/* MAZEPORT will be assigned by the TA to each team */
	M->mazePortIs(htons(MAZEPORT));

	gethostname(buf, sizeof(buf));
	if ((thisHost = resolveHost(buf)) == (Sockaddr *) NULL)
		MWError("who am I?");
	bcopy((caddr_t) thisHost, (caddr_t) (M->myAddr()), sizeof(Sockaddr));

	M->theSocketIs(socket(AF_INET, SOCK_DGRAM, 0));
	if (M->theSocket() < 0)
		MWError("can't get socket");

	/* SO_REUSEADDR allows more than one binding to the same
	   socket - you cannot have more than one player on one
	   machine without this */
	reuse = 1;
	if (setsockopt(M->theSocket(), SOL_SOCKET, SO_REUSEADDR, &reuse,
				sizeof(reuse)) < 0) {
		MWError("setsockopt failed (SO_REUSEADDR)");
	}

	nullAddr.sin_family = AF_INET;
	nullAddr.sin_addr.s_addr = htonl(INADDR_ANY);
	nullAddr.sin_port = M->mazePort();
	if (bind(M->theSocket(), (struct sockaddr *)&nullAddr,
				sizeof(nullAddr)) < 0)
		MWError("netInit binding");

	/* Multicast TTL:
	   0 restricted to the same host
	   1 restricted to the same subnet
	   32 restricted to the same site
	   64 restricted to the same region
	   128 restricted to the same continent
	   255 unrestricted

	   DO NOT use a value > 32. If possible, use a value of 1 when
	   testing.
	 */

	ttl = 1;
	if (setsockopt(M->theSocket(), IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
				sizeof(ttl)) < 0) {
		MWError("setsockopt failed (IP_MULTICAST_TTL)");
	}

	/* join the multicast group */
	mreq.imr_multiaddr.s_addr = htonl(MAZEGROUP);
	mreq.imr_interface.s_addr = htonl(INADDR_ANY);
	if (setsockopt(M->theSocket(), IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)
				&mreq, sizeof(mreq)) < 0) {
		MWError("setsockopt failed (IP_ADD_MEMBERSHIP)");
	}

	/*
	 * Now we can try to find a game to join; if none, start one.
	 */

	printf("\n");

	/* set up some stuff strictly for this local sample */
	//int  temp = rand()%8;/*FIXME- REALLY REALLY RANDOM*/
	M->myRatIdIs(0); // FIXME
	//M->rat(M->myRatId().value()).playing=true;
	//cout <<  M->myRatId().value() << endl;
	//cout <<  temp << endl;
	M->scoreIs(0);
	SetMyRatIndexType(0);

	/* Get the multi-cast address ready to use in SendData()
	   calls. */
	memcpy(&groupAddr, &nullAddr, sizeof(Sockaddr));
	groupAddr.sin_addr.s_addr = htonl(MAZEGROUP);

}