示例#1
0
void ObjectManager::flush_destruction_queue()
{
	typedef DestructionQueue::Element Elt;

	DestructionQueue& q = m_destructionQueue;

	while(!q.empty())
	{
		Elt& e = q.top();
		ObjectID id = e.id();
		bool& predestroyFlag = e.data();

		if(predestroyFlag)
		{
			// The pre-destroy message has already been sent for this object.
			destroy_object(id);
			q.pop();
		}
		else
		{
			// Note: The flag must be set before sending the pre-destroy message, which may result in queue changes.
			predestroyFlag = true;
			broadcast_message(Message_CPtr(new MsgObjectPredestroyed(id)));
		}
	}
}
示例#2
0
int
handle_client(int socket) {

    // Get the client registered to the socket
    Client *client = search_client(socket);
    if (client == NULL) {
        perror("Unregistered client socket found! This is not in the list!");
        return EXIT_FAILURE;
    }
    // Read the complete input of the client
    int res = read_from_client(client);
    if (res != EXIT_SUCCESS)
        return res;
        
    // Try to extract a single message from the buffer
    StringBuffer *msg = extract_message(client);
    if (msg == NULL) {
        return EXIT_SUCCESS;
    }
    if (is_command(client, msg) == EXIT_SUCCESS) {
        handle_command(client, msg);
    }
    else {
        broadcast_message(client, msg);
    }
    
    StringBuffer_free(msg);
    return EXIT_SUCCESS;
}
示例#3
0
void Editor::processKeyPress(const ivec4 & keys)
{
    if (keys == kKeyGraveAccent)
    {
        mIsActive = !mIsActive;
        broadcast_message(HASH::editor_activate__, kMessageFlag_Editor, mTask.id(), to_cell(mIsActive));
    }
}
示例#4
0
文件: avbot.cpp 项目: kikidong/avbot
void avbot::broadcast_message( std::string msg)
{
    typedef std::pair<std::string, av_chanel_map> avbot_channel_item;

    // 广播消息到所有的频道.
    BOOST_FOREACH(avbot_channel_item c, m_channels)
    {
        // 好,发消息!
        broadcast_message(c.first, msg);
    }
示例#5
0
 void on_message(connection_ptr connection,message::data_ptr msg) {
     wscmd::cmd command = wscmd::parse(msg->get_payload());
     
     std::cout << "msg: " << msg->get_payload() << std::endl;
     
     if (command.command == "ack") {
         handle_ack(connection,command);
     } else {
         broadcast_message(msg);
     }
 }
示例#6
0
void start_game() {
  if (!can_start()) {
    fprintf(stderr, "Couldn't start the game (not all players are online)\n");
    return;
  }

  save_state();
  server_message_t msg = { 0, { GAME_START }};
  broadcast_message(&msg);
  printf("Game has started!\n");
  start_resources_production();
}
示例#7
0
void ObjectManager::destroy_object(const ObjectID& id)
{
	// Remove all the listeners which are components of the object being deleted.
	m_listenerTable.remove_listeners_from(id);

	broadcast_message(Message_CPtr(new MsgObjectDestroyed(id)));

	// Remove all the listeners referring to the object.
	m_listenerTable.remove_listeners_to(id);

	m_objects.erase(id);
	m_idAllocator.deallocate(id.value());
}
示例#8
0
void osc_controller::handle_activate_node(world_node& obj)
{
    if (!m_skip) {
	int local_id(obj.get_id ());
	pair<int,int> net_id = m_net_id[local_id];

	lo_message msg = lo_message_new();

	lo_message_add_int32(msg, net_id.first);
	lo_message_add_int32(msg, net_id.second);

	broadcast_message(PSYNTH_OSC_MSG_ACTIVATE, msg);

	lo_message_free(msg);
    }
}
示例#9
0
void osc_controller::handle_set_param_node(world_node& obj, int param_id)
{
    if (!m_skip) {
	int local_id(obj.get_id ());
	pair<int,int> net_id = m_net_id[local_id];

	lo_message msg = lo_message_new();

	lo_message_add_int32(msg, net_id.first);
	lo_message_add_int32(msg, net_id.second);
	lo_message_add_int32(msg, param_id);

	switch(obj.get_param_type(param_id)) {
	case graph::node_param::INT: {
	    int val;
	    obj.get_param(param_id, val);
	    lo_message_add_int32(msg, val);
	    break;
	}
	case graph::node_param::FLOAT: {
	    float val;
	    obj.get_param(param_id, val);
	    lo_message_add_float(msg, val);
	    break;
	}
	case graph::node_param::STRING: {
	    string val;
	    obj.get_param(param_id, val);
	    lo_message_add_string(msg, val.c_str());
	    break;
	}
	case graph::node_param::VECTOR2F: {
	    base::vector_2f val;
	    obj.get_param(param_id, val);
	    lo_message_add_float(msg, val.x);
	    lo_message_add_float(msg, val.y);
	    break;
	}
	default:
	    break;
	}

	broadcast_message(PSYNTH_OSC_MSG_PARAM, msg);

	lo_message_free(msg);
    }
}
示例#10
0
int main(int argc, char *argv[]) {
    broadcast_socket_t broadcast_socket;
    int port = 4950;

    if (create_broadcast_socket(&broadcast_socket, port, 1000) != 0) {
        fprintf(stderr, "Failed to create broadcasting socket\n");
        return 1;
    }

    if (broadcast_message(&broadcast_socket, "DSC10HERE") < 0) {
        fprintf(stderr, "Failed to send broadcast message\n");
        destroy_broadcast_socket(&broadcast_socket);
        return 1;
    }

    destroy_broadcast_socket(&broadcast_socket);

    return 0;
}
示例#11
0
void osc_controller::handle_add_node(world_node& obj)
{
    if (!m_skip) {
	int local_id(obj.get_id ());
	pair<int,int> net_id(m_id, local_id);

	m_local_id[net_id] = local_id;
	m_net_id[local_id] = net_id;

	lo_message msg = lo_message_new();

	lo_message_add_int32(msg, net_id.first);
	lo_message_add_int32(msg, net_id.second);
	lo_message_add_string(msg, obj.get_name().c_str());

	broadcast_message(PSYNTH_OSC_MSG_ADD, msg);

	lo_message_free(msg);
    }
}
示例#12
0
/**
 * @brief read from a child stdout.
 * @param c the child from read to
 * @returns 0 on success, -1 on error
 */
int read_stdout(child_node *c) {
  int count;
  int ret;
  char *buff;
  char *line;
  message *m;
  
  buff = malloc(STDOUT_BUFF_SIZE);
    
  if(!buff) {
    print( ERROR, "malloc: %s", strerror(errno));
    return -1;
  }
  
  ret = 0;
  
  if(c->handler->raw_output_parser) { // parse raw bytes
    while((count=read(c->stdout_fd, buff, STDOUT_BUFF_SIZE)) > 0) {
      if(c->handler->raw_output_parser(c, buff, count)) {
        ret = -1;
        break;
      }
    }
  } else if(c->handler->output_parser) { // parse lines
    while(!ret && (count=read(c->stdout_fd, buff, STDOUT_BUFF_SIZE)) > 0) {
      if(append_to_buffer(&(c->output_buff), buff, count)) {
        ret = -1;
        continue;
      }
      while((line = get_line_from_buffer(&(c->output_buff)))) {
        m = c->handler->output_parser(line);
        if(m) {
          m->head.id = c->id;
          m->head.seq = c->seq + 1;
          
#ifdef BROADCAST_EVENTS
          if(broadcast_message(m)) {
            print( ERROR, "cannot broadcast messages.");
#else
          if(enqueue_message(&(c->conn->outcoming), m)) {
            print( ERROR, "cannot enqueue messages.");
#endif
            dump_message(m);
            free_message(m);
            ret = -1;
            // events not sent are not fatal, just a missed event.
            // while in raw connection a missed message will de-align the output/input.
            // this is why a "break;" is missing here
          } else {
            c->seq++;
          }
        }
        free(line);
      }
    }
  } else { // send raw bytes
    while((count=read(c->stdout_fd, buff, STDOUT_BUFF_SIZE)) > 0) {
      m = create_message(c->seq + 1, count, c->id);
      if(!m) {
        buff[MIN(count, STDOUT_BUFF_SIZE -1)] = '\0';
        print( ERROR, "cannot send the following output: '%s'", buff);
        ret = -1;
        break;
      }
      memcpy(m->data, buff, count);
      if(enqueue_message(&(c->conn->outcoming), m)) {
        print( ERROR, "cannot enqueue messages.");
        dump_message(m);
        free_message(m);
        ret = -1;
        break;
      } else {
        c->seq++;
      }
    }
  }
  
  if(count<0) {
    print( ERROR, "read: %s", strerror(errno));
    ret = -1;
  }
  
  free(buff);
  
  release_buffer(&(c->output_buff));
  
  return ret;
}
示例#13
0
/**
 * void process_dgram(char *dgram, struct sockaddr_in *addr)
 * 
 * Processes accepted datagram. Checks if sequential ID is correct, if it's lower,
 * we resend the ACK packet and dont bother with that datagram anymore, because
 * it was already processed before.
 */
void process_dgram(char *dgram, struct sockaddr_in *addr) {
    /* Protocol token */
    char *token;
    /* Token length */
    int token_len;
    /* Command */
    char *type;
    /* Generic char buffer */
    char *generic_chbuff;
    /* Sequential ID of received packet */
    int packet_seq_id;
    /* Client which we receive from */
    client_t *client;
    /* Generic unsigned int var */
    unsigned int generic_uint;
    /* String representation of address */
    char addr_str[INET_ADDRSTRLEN];

    inet_ntop(AF_INET, &addr->sin_addr, addr_str, INET_ADDRSTRLEN);
    
    /* Log */
    sprintf(log_buffer,
            "DATA_IN: %s <--- %s:%d",
            dgram,
	    addr_str,
	    htons(addr->sin_port)
            );
    log_line(log_buffer, LOG_DEBUG);
    
    token = strtok(dgram, ";");
    token_len = strlen(token);
    
    generic_chbuff = strtok(NULL, ";");
    packet_seq_id = (int) strtol(generic_chbuff, NULL, 0);
    
    /* Check if datagram belongs to us */
    if( (token_len == strlen(STRINGIFY(APP_TOKEN))) &&
            strncmp(token, STRINGIFY(APP_TOKEN), strlen(STRINGIFY(APP_TOKEN))) == 0 &&
            packet_seq_id > 0) {
        
        type = strtok(NULL, ";");
        
        /* New client connection */
        if(strncmp(type, "CONNECT", 7) == 0) {
            
            add_client(addr);            
            client = get_client_by_addr(addr);
            
            if(client) {
                send_ack(client, 1, 0);
                send_reconnect_code(client);
                
                /* Release client */
                release_client(client);
            }
            
        }
        /* Reconnect */
        else if(strncmp(type, "RECONNECT", 9) == 0) {            
            client = get_client_by_index(get_client_index_by_rcode(strtok(NULL, ";")));
            
            if(client) {
                /* Sends ACK aswell after resetting clients SEQ_ID */
                reconnect_client(client, addr);
                
                /* Release client */
                release_client(client);
            }
        }
        /* Client should already exist */
        else {
            client = get_client_by_addr(addr);
            
            if(client != NULL) {
                /* Check if expected seq ID matches */
                if(packet_seq_id == client->pkt_recv_seq_id) {
                    
                    /* Get command */
                    if(strncmp(type, "CREATE_GAME", 11) == 0) {
                                                
                        /* ACK client */
                        send_ack(client, packet_seq_id, 0);
                        
                        create_game(client);
                        
                    }
                    /* Receive ACK packet */
                    else if(strncmp(type, "ACK", 3) == 0) {
                        
                        recv_ack(client, 
                                (int) strtoul(strtok(NULL, ";"), NULL, 10));
                        
                        update_client_timestamp(client);
                        
                    }
                    /* Close client connection */
                    else if(strncmp(type, "CLOSE", 5) == 0) {
                        
                        /* ACK client */
                        send_ack(client, packet_seq_id, 0);
                        
                        leave_game(client);
                        remove_client(&client);
                        
                    }
                    /* Keepalive loop */
                    else if(strncmp(type, "KEEPALIVE", 9) == 0) {
                        
                        /* ACK client */
                        send_ack(client, packet_seq_id, 0);
                        
                    }
                    /* Join existing game */
                    else if(strncmp(type, "JOIN_GAME", 9) == 0) {
                        
                        /* ACK client */
                        send_ack(client, packet_seq_id, 0);
                        
                        join_game(client, strtok(NULL, ";"));
                        
                    }
                    /* Leave existing game */
                    else if(strncmp(type, "LEAVE_GAME", 10) == 0) {
                        
                        /* ACK client */
                        send_ack(client, packet_seq_id, 0);
                        
                        leave_game(client);
                    }
                    /* Start game */
                    else if(strncmp(type, "START_GAME", 10) == 0) {
                        
                        /* ACK client */
                        send_ack(client, packet_seq_id, 0);
                        
                        start_game(client);
                    }
                    /* Rolling die */
                    else if(strncmp(type, "DIE_ROLL", 8) == 0) {
                        
                        /* ACK client */
                        send_ack(client, packet_seq_id, 0);
                        
                        roll_die(client);
                    }
                    /* Moving figure */
                    else if(strncmp(type, "FIGURE_MOVE", 11) == 0) {
                        
                        /* ACK client */
                        send_ack(client, packet_seq_id, 0);
                        
                        /* Parse figure id */
                        generic_chbuff = strtok(NULL, ";");
                        generic_uint = (unsigned int) strtoul(generic_chbuff, NULL, 10);

                        move_figure(client, generic_uint);
                    }

		   else if(strncmp(type, "MESSAGE", 7) == 0) {
			
			/* ACK client */
			send_ack(client, packet_seq_id, 0);
			
			broadcast_message(client, strtok(NULL, ";"));
		   }
                                        
                }
                /* Packet was already processed */
                else if(packet_seq_id < client->pkt_recv_seq_id &&
                        strncmp(type, "ACK", 3) != 0) {
                    
                    send_ack(client, packet_seq_id, 1);
                    
                }
                
                /* If client didnt close conection */
                if(client != NULL) {
                    
                    /* Release client */
                    release_client(client);
                }
            }
        }
    }
}
示例#14
0
int main(void) {

	// Locals General. -----------------------------------------------------
	char imu_board_id[9] = {0,0,0,0, 0,0,0,0, 0};
	char message[1024];	// Any message to send by UART1
	InitOscillator();			// Initialize the PLL (also disables wdt)
	__delay32(_50MILLISEC);

	// Init mcu ports ------------------------------------------------------

	init_port();    	// Initialize ports
	LED_ORNG =0;
	LED_RED = 1;

	// Init UARTS. ---------------------------------------------------------

	init_UART1();		// Initialize the serial communication (TTL / RS-232)
	init_UART2();
    broadcast_message("RC Testing program\n");

	// Init Analog Channels. -----------------------------------------------

	analog_initAnalog();	// Init the ADC module

	// Init SPI. ---------------------------------------------------------

	init_SPI();

	// Init I2C. ---------------------------------------------------------

	e_i2cp_init();
	e_i2cp_enable();
	__delay32(_50MILLISEC);
	e_i2c_write(0x00);	// dummy byte to get the START bit on the bus (believe it or not!)


	// Init RC. ----------------------------------------------------------
	initControlVariables(NULL);

    broadcast_message("Initialising RC\n");
	LED_ORNG =1;
	LED_RED = 1;

	BuzzerBip(1,1);		// Make x bips of the Buzzer (blocking)
	RCInitReception();
    // RCSetType(RC_WK2401);
    RCSetType(RC_WK2402);
    
    broadcast_message("Initialising IMU\n");
	// Init BUZZER. ----------------------------------------------------------
	__delay32(_200MILLISEC);  // Wait for the IMU to boot

	while (INT_IMU==0)		// Wait for the IMU to boot
	{
		FlashORNG ();						// Flash the LED	
	}
	read_imu_version(imu_board_id); imu_board_id[8] = 0;
	
    broadcast_message("Entering main loop\n");
	// Init BUZZER. ----------------------------------------------------------
	BuzzerBip(3,1);		// Make x bips of the Buzzer, blocking
	InitLoopTimer(control.params.T_ctrl_ms);// Initialize & Enable control loop timer (20ms; 50Hz)
	LED_RED = 0;
    while (1) {
        __delay32(5*_10MILLISEC);
        sprintf(message, "RC %+.2f %+.2f %+.2f %.2f %.2f %+.2f %+.2f %+.2f %s CTRL %02X %s %s\n", 
                RC_THROTTLE, RC_YAW, RC_ROLL, RC_PITCH,
                RC_THROTTLE_TRIM, RC_YAW_TRIM, RC_ROLL_TRIM, RC_PITCH_TRIM,
                string_of_rc_state(RCSMGetState()),
                control.flags.CONTROL_MODE,
                string_of_control_mode(control.flags.CONTROL_MODE),
                string_of_control_type(control.flags.CONTROL_MODE));
        broadcast_message(message);
    }

    return 0;
	
}	// End of main