std::vector<QueuedMessage> GameNetConnection::sync_on_message(message_t msg) { std::vector<QueuedMessage> responses; if (!_connection) { return responses; } QueuedMessage qm; const int timeout = 1; PlayerDataEntry& pde = pd.local_player_data(); std::vector<bool> received(pd.all_players().size(), false); bool all_ack = false; while (!all_ack) { _connection->poll(gamenetconnection_queue_message, (void*)this, timeout); while (extract_message(qm, _delayed_messages, msg)) { received[qm.sender] = true; responses.push_back(qm); } all_ack = true; for (int i = 0; i < received.size(); i++) { if (!received[i] && i != pde.net_id) { all_ack = false; break; } } } return responses; }
std::string jevois::getPythonExceptionString(boost::python::error_already_set &) { // Get some info from the python exception: PyObject *t, *v, *tb; try { PyErr_Fetch(&t, &v, &tb); PyErr_NormalizeException(&t, &v, &tb); } catch (...) { return "Internal error trying to fetch exception data from Python"; } try { boost::python::object objtype = ::ptr_to_obj(t); boost::python::object objvalue = ::ptr_to_obj(v); boost::python::object objtraceback = ::ptr_to_obj(tb); std::string const type = extract_exception_type(objtype); std::string const message = (type == "SyntaxError") ? make_syntax_error_message(objvalue) : extract_message(objvalue); traceback const traceback = extract_traceback(objtraceback); PyErr_Restore(t, v, tb); clear_exception(); return generate_message(type, message, traceback); } catch (...) { PyErr_Restore(t, v, tb); clear_exception(); return "Internal error trying to fetch exception data from Python"; } }
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; }
int receive_id() { const int msg_len = ID_LEN + 4; char buff[msg_len]; if (fgets(buff, msg_len, game.io) == NULL) { return 0; } char *id_str = extract_message(buff); if (strcmp(id_str, "err") == 0) { return 0; } game.id = atoi(id_str); return 1; }
void *reciever(void *params) { struct hb_entry* temp; char *message; while(1){ message = (char *) recvudp(hb_table[host_no].IP,hb_table[host_no].port); clear_temp_entry_table(entry); temp = extract_message(message); update_table(temp); } }
int download_map() { const int size_buff_len = SIZE_LEN + 4; char buff[size_buff_len]; if (fgets(buff, size_buff_len, game.io) == NULL) { return 0; } char *size_tuple = extract_message(buff); sscanf(size_tuple, "(%d,%d)", &game.width, &game.height); const int map_buff_size = sizeof(char) * game.width * game.height + 1; game.map = malloc(map_buff_size); if (fgets(game.map, map_buff_size, game.io) == NULL) { return 0; } return 1; }
bool GameNetConnection::consume_sync_messages(GameState* gs) { // printf("Delayed Messages: %d\n", _delayed_messages.size()); QueuedMessage qm; if (!extract_message(qm, _delayed_messages, PACKET_FORCE_SYNC)) { return false; } // Make sure we don't receive any stray actions after sync. // gs->local_player()->enqueue_io_actions(gs); // players_poll_for_actions(gs); printf("Found sync buffer!\n"); net_recv_sync_data(*qm.message, gs); delete qm.message; net_send_sync_ack(*this); post_sync(gs); // Wait for server to receive acks for all clients before continuing net_sync_and_discard(*this, PACKET_SYNC_ACK); return true; }
/**************************************************************** * NAME: receiverFunc * * DESCRIPTION: This is the function that takes care of servicing * the first among the three threads spawned i.e. * the receiver threads which does the following: * i) If I am a LEADER approve join requests from * member hosts * ii) Receive heartbeats * * PARAMETERS: NONE * * RETURN: * (int) ZERO if success * ERROR otherwise * ****************************************************************/ int receiverFunc() { funcEntry(logF, ipAddress, "receiverFunc"); int rc = SUCCESS, // Return code numOfBytesRec, // Number of bytes received i_rc, // Temp RC op_code; // Operation code char recMsg[LONG_BUF_SZ], // Received message tokenRecMsg[LONG_BUF_SZ], // Received message without join op code buffer[SMALL_BUF_SZ]; // Temp buffer struct sockaddr_in memberAddress; // Address of host struct hb_entry * recMsgStruct; // Heart beat table that holds received message recMsgStruct = (struct hb_entry *) malloc(4*sizeof(struct hb_entry)); /* * 1) Receive UDP packet * 2) Check operation code * 3) If JOIN message: * i) Extract message * ii) Update heartbeat table * 4) Else * i) Extract message * ii) Update heartbeat table */ for(;;) { ///////// // Step 1 ///////// memset(recMsg, '\0', LONG_BUF_SZ); // Debug printToLog(logF, "recMsg before recvUDP", recMsg); numOfBytesRec = recvUDP(recMsg, LONG_BUF_SZ, memberAddress); // Check if 0 bytes is received if ( SUCCESS == numOfBytesRec ) { sprintf(logMsg, "Number of bytes received is ZERO = %d", numOfBytesRec); printf("\n%s\n", logMsg); printToLog(logF, ipAddress, logMsg); continue; } // Debug printToLog(logF, "recMsg after recvUDP", recMsg); ///////// // Step 2 ///////// i_rc = checkOperationCode(recMsg, &op_code, tokenRecMsg); if ( i_rc != SUCCESS ) { printToLog(logF, ipAddress, "Unable to retrieve opcode"); continue; } ///////// // Step 3 ///////// if ( JOIN_OP_CODE == op_code ) { sprintf(logMsg, "JOIN msg from %s", inet_ntop(AF_INET, &memberAddress.sin_addr, buffer, sizeof(buffer))); printToLog(logF, ipAddress, logMsg); /////////// // Step 3i /////////// // Debug. uncomment if req printToLog(logF, ipAddress, "\nBefore clear_temp_entry_table\n"); clear_temp_entry_table(recMsgStruct); printToLog(logF, ipAddress, "\nAfter c_t_e_t\n"); printToLog(logF, ipAddress, "\nbefore extract_msg\n"); sprintf(logMsg, "Token received message before e_m: %s", tokenRecMsg); printToLog(logF, ipAddress, logMsg); recMsgStruct = extract_message(tokenRecMsg); printToLog(logF, ipAddress, "\nafter e_m\n"); //printToLog(logF, ipAddress, "\nToken Received Message: %s", tokenRecMsg); sprintf(logMsg, "Token received message: %s", tokenRecMsg); printToLog(logF, ipAddress, logMsg); if ( NULL == recMsgStruct ) { printToLog(logF, ipAddress, "Unable to extract message"); continue; } //////////// // Step 3ii //////////// i_rc = update_table(recMsgStruct); if ( i_rc != SUCCESS ) { printToLog(logF, ipAddress, "Unable to update heart beat table"); continue; } } // End of if ( JOIN_OP_CODE == op_code ) ///////// // Step 4 ///////// else { ////////// // Step 4i ////////// clear_temp_entry_table(recMsgStruct); recMsgStruct = extract_message(recMsg); if ( NULL == recMsgStruct ) { printToLog(logF, ipAddress, "Unable to extract message"); continue; } /////////// // Step 4ii /////////// i_rc = update_table(recMsgStruct); if ( i_rc != SUCCESS ) { printToLog(logF, ipAddress, "Unable to update heart beat table"); continue; } } // End of else } // End of for(;;) rtn: funcExit(logF, ipAddress, "receiverFunc", rc); return rc; } // End of receiverFunc()