Esempio n. 1
0
bool AdResponseBox::listen(BaseScriptHolder *param1, uint32 param2) {
    UIObject *obj = (UIObject *)param1;

    switch (obj->_type) {
    case UI_BUTTON:
        if (scumm_stricmp(obj->getName(), "prev") == 0) {
            _scrollOffset--;
        } else if (scumm_stricmp(obj->getName(), "next") == 0) {
            _scrollOffset++;
        } else if (scumm_stricmp(obj->getName(), "response") == 0) {
            if (_waitingScript) {
                _waitingScript->_stack->pushInt(_responses[param2]->getID());
            }
            handleResponse(_responses[param2]);
            _waitingScript = nullptr;
            _gameRef->_state = GAME_RUNNING;
            ((AdGame *)_gameRef)->_stateEx = GAME_NORMAL;
            _ready = true;
            invalidateButtons();
            clearResponses();
        } else {
            return BaseObject::listen(param1, param2);
        }
        break;
    default:
        error("AdResponseBox::Listen - Unhandled enum");
    }

    return STATUS_OK;
}
bool
CInternalOperationsMedium::getResponseAndClear( std::multimap< common::CRequest const*, common::DimsResponse > & _requestResponse )
{
	_requestResponse = m_responses;
	clearResponses();
	return true;
}
void TrackerFeatureSet::extraction( const std::vector<Mat>& images )
{

  clearResponses();
  responses.resize( features.size() );

  for ( size_t i = 0; i < features.size(); i++ )
  {
    Mat response;
    features[i].second->compute( images, response );
    responses[i] = response;
  }

  if( !blockAddTrackerFeature )
  {
    blockAddTrackerFeature = true;
  }
}
Esempio n. 4
0
AdResponseBox::~AdResponseBox() {

    delete _window;
    _window = nullptr;
    delete _shieldWindow;
    _shieldWindow = nullptr;
    delete[] _lastResponseText;
    _lastResponseText = nullptr;
    delete[] _lastResponseTextOrig;
    _lastResponseTextOrig = nullptr;

    if (_font) {
        _gameRef->_fontStorage->removeFont(_font);
    }
    if (_fontHover) {
        _gameRef->_fontStorage->removeFont(_fontHover);
    }

    clearResponses();
    clearButtons();

    _waitingScript = nullptr;
}
bool
CNetworkClient::getResponseAndClear( std::multimap< common::CRequest const*, common::DimsResponse > & _requestResponse )
{
	QMutexLocker lock( &m_mutex );
	CBufferAsStream stream(
				(char*)m_pullBuffer.m_buffer
				, m_pullBuffer.m_usedSize
				, SER_NETWORK
				, CLIENT_VERSION);

	uint256 token;

	while( !stream.eof() )
	{
		int messageType;

		common::CClientMessage clientMessage;
		try
		{
			stream >> clientMessage;

			assert( takeMatching( clientMessage.m_header.m_id ) );

			_requestResponse.insert(
						std::make_pair(
							takeMatching( clientMessage.m_header.m_id )
							, common::CClientMessageResponse( clientMessage, common::convertToInt(this), m_ip.toStdString() ) ) );
			m_matching.erase( clientMessage.m_header.m_id );

		}
		catch(...)
		{}
	}

	clearResponses();
	return true;
}
Esempio n. 6
0
int packetSendRequest(int method,unsigned char *packet,int packet_len,int batchP,
		      unsigned char *transaction_id,struct response_set *responses)
{
  int i;
  int cumulative_timeout=0; /* ms */
  int this_timeout=125; /* ms */
  int peer_low,peer_high;

  struct timeval time_in,now;
 
  /* Prepare ephemeral UDP socket (hence no binding)
     If in server mode, then we already have a socket available to us and appropriately bound */
  if (!serverMode) {
    sock=socket(PF_INET,SOCK_DGRAM,0);
    if (sock<0) {
      fprintf(stderr,"Could not create UDP socket.\n");
      exit(-3);
    }
  }
  
  /* Deal with special case */
  if (method==REQ_REPLY)
    {
      int r=sendto(sock,packet,packet_len,0,(struct sockaddr *)&recvaddr,sizeof(recvaddr));
      if (r<packet_len)	{
	if (debug) fprintf(stderr,"Could not send to client %s\n",inet_ntoa(client_addr));
      } else {
	if (debug>1) fprintf(stderr,"Sent request to client %s\n",inet_ntoa(client_addr));
      }
      return 0;
    }

  getPeerList();

  gettimeofday(&time_in,NULL);

  /* REQ_SERIAL & REQ_PARALLEL work in fundamentally different ways, 
     but it turns out the retry/timeout code is the dominant part.
     So we do a bit of fiddling around to make one loop that can handle both */
  if (method==REQ_SERIAL) {
    peer_low=0; peer_high=peer_count-1;
    /* If there are too many peers to allow sending to each three times, then we should 
       adjust our incremental timeout accordingly, so far as is practicable */
    if (this_timeout*peer_count*3>timeout)
      {
	this_timeout=timeout/(3*peer_count);
	if (this_timeout<10) this_timeout=10; /* 10ms minimum sending interval */
      }
  } else 
    { peer_low=-1; peer_high=-1;}

  while(cumulative_timeout<=timeout)
    {
      /* If not in serial mode, then send request to everyone immediately.
         Make sure we only ask once in parallel mode, since it will always ask everyone */
      if (method==REQ_PARALLEL) sendToPeers(packet,packet_len,method,0,responses);
      else if (method!=REQ_SERIAL)
	for(i=0;i<peer_count;i++) sendToPeers(packet,packet_len,method,i,responses);

      /* If in serial mode, send request to peers in turn until one responds positively, 
	 otherwise just deal with the reply fetching loop to listen to as many or few reply. */
      for(i=peer_low;i<=peer_high;i++) {
	struct response *rr;
	if (i>-1) sendToPeers(packet,packet_len,REQ_SERIAL,i,responses);
	
	/* Placing the timeout calculation here means that the total timeout is shared among
	   all peers in a serial request, but round-robining after each time-step.
	   We adjust this_timeout if there are many peers to allow 3 sends to each peer where possible.
	*/
	cumulative_timeout+=this_timeout;
	int timeout_remaining=this_timeout;
	
	while(1)
	  {
	    /* Wait for response */
	    int r=getReplyPackets(method,i,batchP,responses,transaction_id,timeout_remaining);
	    if (r&&debug>1) fprintf(stderr,"getReplyPackets(): Returned on timeout\n");
	    
	    switch(method)
	      {
	      case REQ_PARALLEL:
		/* XXX We could stop once all peers have replied.
		   (need to update the test script if we do that, so that it tests with multiple
		    peers and so tests that we wait if not all peers have responded) */
		break;
	      case REQ_FIRSTREPLY:
		if (debug>1) fprintf(stderr,"Returning with first reply (REQ_FIRSTREPLY)\n");
		if (!r) return 0;
		break;
	      case REQ_SERIAL:
		if (!r) {
		  /* Stop if we have an affirmative response.
		     XXX - doesn't allow for out of order replies. */
		  if (debug>1) dumpResponses(responses);
		  rr=responses->last_response;
		  while (rr)
		    {
		      if (rr->checked) break;
		      if (debug>1) 
			fprintf(stderr,"Got a response code 0x%02x, checking if that is what we need.\n",rr->code);
		      switch (rr->code)
			{
			case ACTION_OKAY: case ACTION_DATA:
			  /* bingo */
			  if (!batchP) return 0;
			  break;
			}
		      rr->checked=1;
		      rr=rr->prev;
		    }
		  
		  /* not what we are after, so clear response and try with next peer */
		  clearResponses(responses);
		}
		break;
	      }
	    
	    /* Wait for the previous timeout to really expire,
	       (this is for the case where all peers have replied) */
	    {
	      int elapsed_usecs=0;
	      int cumulative_usecs=cumulative_timeout*1000;
	      int remaining_usecs;
	      
	      gettimeofday(&now,NULL);
	      elapsed_usecs=(now.tv_sec-time_in.tv_sec)*1000000;
	      elapsed_usecs+=(now.tv_usec-time_in.tv_usec);
	      
	      remaining_usecs=cumulative_usecs-elapsed_usecs;
	      
	      if (remaining_usecs<=0) break;
	      else timeout_remaining=remaining_usecs/1000;		    
	    }
	    
	  }
      }
      cumulative_timeout+=this_timeout;
    }
  if (debug>1) if (cumulative_timeout>=timeout) 
		 fprintf(stderr,"Request timed out after retries (timeout=%d, elapsed=%d).\n",
			 timeout,cumulative_timeout);

  return 0;
}
Esempio n. 7
0
int requestItem(char *did,char *sid,char *item,int instance,unsigned char *buffer,int buffer_length,int *len,
		unsigned char *transaction_id)
{
  unsigned char packet[8000];
  int packet_len=0;
  struct response *r;
  struct response_set responses;

  bzero(&responses,sizeof(responses));

  /* Prepare the request packet */
  if (packetMakeHeader(packet,8000,&packet_len,transaction_id)) return -1;
  if (did&&(!sid))
    { if (packetSetDid(packet,8000,&packet_len,did)) return -1; }
  else if (sid&&(!did))
    { if (packetSetSid(packet,8000,&packet_len,sid)) return -1; }
  else return setReason("You must request items by DID or SID, not neither, nor both");

      
  if (packetAddVariableRequest(packet,8000,&packet_len,
			       item,instance,0,buffer_length)) return -1;
  if (packetFinalise(packet,8000,&packet_len)) return -1;

  int method=REQ_PARALLEL;
  if (sid) method=REQ_FIRSTREPLY;
  if (packetSendRequest(method,packet,packet_len,(instance==-1)?BATCH:NONBATCH,transaction_id,&responses)) return -1;

  r=responses.responses;
  while(r)
    {
      char sid[SID_SIZE*2+1];
      int slen=0;
      extractSid(r->sid,&slen,sid);
      switch(r->code)
	{
	case ACTION_OKAY: printf("OK:%s\n",sid); break;
	case ACTION_DECLINED: printf("DECLINED:%s\n",sid); break;
	case ACTION_DATA: 
	  /* Display data.
	     The trick is knowing the format of the data.
	     Fortunately we get the variable id etc.
	     XXX Need to deal with fragmented values.
	     (perhaps the fragments should get auto-assembled when accepting the responses)
	  */
	  switch(r->var_id)
	    {
	    case VAR_DIDS:
	      {
		char did[DID_MAXSIZE+1];
		int dlen=0;
		did[0]=0;
		extractDid(r->response,&dlen,did);
		printf("DIDS:%s:%d:%s\n",sid,r->var_instance,did);
	      }
	      break;
	    case VAR_NOTE:
	    default:
	      /* ASCII formatted variable types */
	      {
		int v=0;
	        int i=0;
		FILE *outputfile=stdout;
		while(vars[v].name&&vars[v].id!=r->var_id) v++;
		if (!vars[v].id) printf("0x%02x",r->var_id);
		while(vars[v].name[i]) fputc(toupper(vars[v].name[i++]),stdout);
		printf(":%s:%d:",sid,r->var_instance);
		
		if (outputtemplate)
		  {
		    char outputname[8192];
		    snprintf(outputname,8192,outputtemplate,sid,r->var_id,r->var_instance);
		    outputfile=fopen(outputname,"w");
		    if (!outputfile) printf("ERROR:Could not open output file '%s'",outputname);
		    if (debug) fprintf(stderr,"Writing output to '%s'\n",outputname);
		  }

		if (outputfile) fwrite(r->response,r->value_bytes,1,outputfile);

		if (r->value_bytes<r->value_len)
		  {
		    /* Partial response, so ask for the rest of it */
		    unsigned char packet[8000];
		    int packet_len=0;
		    struct response *rr;
		    struct response_set responses;
		    int offset,max_bytes;
		    int recv_map[1+(r->value_len/MAX_DATA_BYTES)];
		    int recv_map_size=1+(r->value_len/MAX_DATA_BYTES);
		    int needMoreData;
		    int tries=0;

		    /* work out EXACTLY how many installments we need */
		    while (((recv_map_size-1)*MAX_DATA_BYTES)>=r->value_len) recv_map_size--;

		    recv_map[0]=0; /* we received the first installment, so mark it off ... */
		    /* ... but we haven't received the rest */
		    for(i=1;i<recv_map_size;i++) recv_map[i]=0;		    

		    /* Ask for all remaining pieces in parallel, then keep track of what has arrived
		       XXX - Not yet implemented.  Currently uses a slow serial method, worse than TFTP */  
		    needMoreData=recv_map_size-1;

		    while(needMoreData&&(tries++<15))
		      {
			if (debug>1) fprintf(stderr,"Multi-packet request: try %d, %d fragments remaining.\n",tries,needMoreData);
			needMoreData=0;
			for(i=0;i<recv_map_size;i++)
			  if (!recv_map[i])
			    {
			      needMoreData++;
			      offset=i*MAX_DATA_BYTES;
			      
			      if (debug>1) fprintf(stderr,"Asking for variable segment @ offset %d\n",offset);
			      
			      /* Send accumulated request direct to the responder */
			      if (packet_len>=MAX_DATA_BYTES)
				{
				  if (packetFinalise(packet,8000,&packet_len)) return -1;
				  packetSendFollowup(r->sender,packet,packet_len);
				  packet_len=0;
				}
			      /* Prepare a new request packet if one is not currently being built */
			      if (!packet_len)
				{
				  if (packetMakeHeader(packet,8000,&packet_len,transaction_id)) return -1;
				  if (packetSetSid(packet,8000,&packet_len,sid)) return setReason("SID went mouldy during multi-packet get");
				}
			      
			      max_bytes=65535-offset;
			      if (max_bytes>buffer_length) max_bytes=buffer_length;
			      if (packetAddVariableRequest(packet,8000,&packet_len,
							   item,r->var_instance,offset,max_bytes)) return -1;
			    }
			/* Send accumulated request direct to the responder */
			if (packet_len)
			  {
			    if (packetFinalise(packet,8000,&packet_len)) return -1;
			    packetSendFollowup(r->sender,packet,packet_len);
			    packet_len=0;
			  }
			
			/* Collect responses to our multiple requests */
			bzero(&responses,sizeof(responses));

			/* XXX should target specific peer that sent first piece */
			getReplyPackets(REQ_PARALLEL,i,0,&responses,transaction_id,250);
			rr=responses.responses;
			while(rr)
			  {
			    if (rr->code==ACTION_DATA&&rr->var_id==r->var_id&&rr->var_instance==r->var_instance)
			      {
				int piece=rr->value_offset/MAX_DATA_BYTES;
				if (!recv_map[piece])
				  {
				    if (debug>1) fprintf(stderr,"Extracted value fragment @ offset %d, with %d bytes\n",rr->value_offset,rr->value_bytes);
				    if (debug>2) dump("Fragment",rr->response,rr->value_bytes);
				    fseek(outputfile,rr->value_offset,SEEK_SET);
				    fwrite(rr->response,rr->value_bytes,1,outputfile);
				    recv_map[piece]=1;
				  }
				else
				  {
				    if (debug>1) fprintf(stderr,"DUPLICATE value fragment @ offset %d, with %d bytes\n",rr->value_offset,rr->value_bytes);
				   
				  }
			      }
			    rr=rr->next;
			  }
			clearResponses(&responses);
		      }
		  }
		if (outputtemplate) fclose(outputfile); else fflush(outputfile);		    
		printf("\n");
		break;
	      }
	    } 
	  break;
	case ACTION_DONE: 
	  printf("DONE:%s:%d\n",sid,r->response[0]);
	  break;
	case ACTION_GET: printf("ERROR:You cant respond with GET\n"); break;
	case ACTION_SET: printf("ERROR:You cant respond with SET\n"); break;
	case ACTION_WROTE: printf("ERROR:You cant respond with WROTE\n"); break;
	case ACTION_DEL: printf("ERROR:You cant respond with DEL\n"); break;
	case ACTION_CREATEHLR: printf("ERROR:You cant respond with CREATEHLR\n"); break;
	case ACTION_PAD: /* ignore it */ break;
	case ACTION_EOT: /* ignore it */ break;
	default: printf("ERROR:Unexpected response code 0x%02x\n",r->code);
	}
      fflush(stdout);
      r=r->next;
    }

  return -1;
}