BufferInfo* Renderer::getAvailableBuffer(GLuint neededSize){

	//Find the buffer info with enough room
	//if one is not found, make one and allocate 1 meg of graphics card RAM

	BufferInfo* ret = findBufferWithSpace(neededSize);

	if(ret != 0)
		return ret;
	return allocateNewBuffer();
}
Exemple #2
0
int main(int argc, char** argv)
{
   struct sockaddr_in serverAddr; /* Local address */
   struct sockaddr_in clientAddr;   /* Client address */
   unsigned int cliAddrLen;         /* Length of incoming message */
   int recvMsgSize;                 /* Size of received message */
   struct sigaction myAction;       /* For setting signal handler */

   float lossRate;                  /* Rate of packet loss for testing. */
   unsigned short serverPort;       /* Port to open server. */
   unsigned int chunkSize;          /* Chuck size to handle packet data. */

   Sender_Message rcvdMessage;
   Receiver_Message ackMessage;

   unsigned int maxMessageSize = sizeof(rcvdMessage);
   unsigned int recvMessageSize;

   unsigned int packet_rcvd = -1;

   

   /* Seed our generator for packet loss. */
   srand48(DEFAULT_RAND_SEED);

   if(argc < 3)
   {
      fprintf(stderr, "Usage:    %s port_no chunk_size [loss_rate]", argv[0]);
      exit(1);
   }

   /* Get the args. */
   serverPort = atoi(argv[1]);
   chunkSize = atoi(argv[2]);

   /* Get the loss rate if there. */
   if(argc >= 4)
      lossRate = atof(argv[3]);
   else
      lossRate = DEFAULT_LOSS_RATE;

   printf("Loss Rate: %f\n", lossRate);

   allocateNewBuffer(DEFAULT_BUFFER_SIZE);

   /* Set signal handler for alarm signal */
   myAction.sa_handler = CatchAlarm;
   if (sigfillset(&myAction.sa_mask) < 0) /* block everything in handler */
      DieWithError("sigfillset() failed");
   myAction.sa_flags = 0;

   if (sigaction(SIGALRM, &myAction, 0) < 0)
      DieWithError("sigaction() failed for SIGALRM");

   /* Create socket for sending/receiving datagrams */
   if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
      DieWithError("socket() failed");

   /* Construct local address structure */
   memset(&serverAddr, 0, sizeof(serverAddr));     /* Zero out structure */
   serverAddr.sin_family = AF_INET;                /* Internet address family */
   serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
   serverAddr.sin_port = htons(serverPort);        /* Local port */

   /* Bind to the local address */
   if (bind(sock, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0)
      DieWithError("bind() failed");

   for(;;) /* Run forever */
   {
      /* Set the size of the in-out parameter */
      cliAddrLen = sizeof(clientAddr);

      /* Drop packets at the given loss rate. */
      while(is_lost(lossRate))
      {
         if((recvMessageSize = recvfrom(sock, (char *)&rcvdMessage, maxMessageSize, 0, (struct sockaddr *) &clientAddr, &cliAddrLen)) < 0)
            DieWithError("recvfrom() failed");
         printf("Packet received. type: %d. seq_no: %d. length: %d.\n", rcvdMessage.type, rcvdMessage.seq_no, rcvdMessage.length);

         printf("Packet Tossed.\n");
      }

      /* Now actually grab the proper packet. */
      if((recvMessageSize = recvfrom(sock, (char *)&rcvdMessage, maxMessageSize, 0, (struct sockaddr *) &clientAddr, &cliAddrLen)) < 0)
         DieWithError("recvfrom() failed");

      if(rcvdMessage.type == PACKET_TYPE_TEAR_DOWN)
      {
         ackMessage.ack_no = 0;
         ackMessage.type = PACKET_TYPE_TEAR_DOWN_ACK;

         printf("Breaking\n");
         if(sendto(sock, (char *)&ackMessage, sizeof(ackMessage), 0, (struct sockaddr *)&clientAddr, sizeof(clientAddr)) < 0)
            DieWithError("Send Failed");

         alarm(7);

         break;
      }

      ackMessage.type = PACKET_TYPE_ACK;

      if(rcvdMessage.seq_no == packet_rcvd+1)
      {
         packet_rcvd++;

         /* We want this packet. Copy the data. */
         while(dataInBuffer + rcvdMessage.length > currentBufferSize)
            allocateNewBuffer(currentBufferSize + DEFAULT_BUFFER_SIZE);

         memcpy(&(buffer[dataInBuffer]), rcvdMessage.data, rcvdMessage.length);
         dataInBuffer += rcvdMessage.length;
      }

      ackMessage.ack_no = packet_rcvd;

      printf("Packet received. type: %d. seq_no: %d. length: %d.\n", rcvdMessage.type, rcvdMessage.seq_no, rcvdMessage.length);

      if(sendto(sock, (char *)&ackMessage, sizeof(ackMessage), 0, (struct sockaddr *)&clientAddr, sizeof(clientAddr)) < 0)
         DieWithError("Send Failed");

      printf("Ack num %d sent\n", ackMessage.ack_no);

   }

   /* We broke because we got a tear down. Now send tear down to any messaged. */
   for(;;)
   {
      /* Now actually grab the proper packet. */
      if((recvMessageSize = recvfrom(sock, (char *)&rcvdMessage, maxMessageSize, 0, (struct sockaddr *) &clientAddr, &cliAddrLen)) < 0)
         DieWithError("recvfrom() failed");

      ackMessage.ack_no = 0;
      ackMessage.type = PACKET_TYPE_TEAR_DOWN_ACK;

      printf("Tear down responded\n");
      if(sendto(sock, (char *)&ackMessage, sizeof(ackMessage), 0, (struct sockaddr *)&clientAddr, sizeof(clientAddr)) < 0)
         DieWithError("Send Failed");
   }

   return 0;
}