void game_state::periodic_respawn_info_update() { static sf::Clock clk; ///once per second float broadcast_every_ms = 100.f; if(clk.getElapsedTime().asMicroseconds() / 1000.f < broadcast_every_ms) return; clk.restart(); for(auto& i : respawn_requests) { player play = get_player_from_player_id(i.player_id); if(play.id == -1) continue; float time_elapsed = i.clk.getElapsedTime().asMicroseconds() / 1000.f; byte_vector vec; vec.push_back(canary_start); vec.push_back(message::RESPAWNINFO); vec.push_back(time_elapsed); vec.push_back(i.time_to_respawn_ms); vec.push_back(canary_end); udp_send_to(play.sock, vec.ptr, (const sockaddr*)&play.store); } }
t_stat udp_send_self (DEVICE *dptr, int32 link, uint16 *pdata, uint16 count) { // Send an IMP packet to our own receiving socket. This might seem silly, // but it's used to emulate the line loopback function... struct sockaddr_in self; self.sin_family = AF_INET; self.sin_port = htons(udp_links[link].rxport); self.sin_addr.s_addr = htonl(INADDR_LOOPBACK); return udp_send_to (dptr, link, pdata, count, (SOCKADDR *) &self); }
/* Called every 100ms. Items in the queue are resent every 500ms */ void nfs_timeout(void) { struct rpc_queue *q_item; for (q_item = queue; q_item != NULL; q_item = q_item->next) { if (q_item->timeout++ > 5) { udp_send_to(udp_cnx, q_item->port, q_item->pbuf); q_item->timeout = 0; } } }
void game_state::broadcast(const std::vector<char>& dat, const int& to_skip) { for(int i=0; i<player_list.size(); i++) { udp_sock& fd = player_list[i].sock; sockaddr_storage store = player_list[i].store; if(i == to_skip) continue; udp_send_to(fd, dat, (sockaddr*)&store); } }
int rpc_send(struct pbuf *pbuf, int port, void (*func)(void *, uintptr_t, struct pbuf *), void *callback, uintptr_t arg) { pbuf->len = pbuf->tot_len = (char *) pbuf->arg[0] - (char *) pbuf->payload; /* Add to a queue */ add_to_queue(pbuf, port, func, callback, arg); udp_send_to(udp_cnx, port, pbuf); return 0; }
void game_state::respawn_player(int32_t player_id) { int team_id = get_team_from_player_id(player_id); bool found = false; player play; for(auto& i : player_list) { if(i.id == player_id) { play = i; found = true; } } if(!found) { printf("No player with id %i\n", player_id); return; } vec2f new_pos = find_respawn_position(team_id); printf("Got teamid %i\n", team_id); ///I could structure this into a forward request ///but that'd be a pain ///unsafe, relying on client to update information to peers ///however, not unreliable, as the client will keep requesting ///a respawn until it gets one, and udp delivered whole ///except freak occurence byte_vector vec; vec.push_back(canary_start); vec.push_back(message::RESPAWNRESPONSE); vec.push_back(new_pos); vec.push_back(canary_end); udp_send_to(play.sock, vec.ptr, (const sockaddr*)&play.store); }
void game_state::broadcast(const std::vector<char>& dat, sockaddr_storage& to_skip) { int c = 0; for(int i=0; i<player_list.size(); i++) { udp_sock& fd = player_list[i].sock; sockaddr_storage store = player_list[i].store; if(store == to_skip) { c++; continue; } udp_send_to(fd, dat, (sockaddr*)&store); } if(c > 1) printf("ip conflict "); }
void game_state::process_join_request(udp_sock& my_server, byte_fetch& fetch, sockaddr_storage& who) { int32_t found_end = fetch.get<int32_t>(); if(found_end != canary_end) return; printf("Player joined %s:%s\n", get_addr_ip(who).c_str(), get_addr_port(who).c_str()); add_player(my_server, who); ///really need to pipe back player id ///should really dynamically organise teams ///so actually that's what I'll do byte_vector vec; vec.push_back(canary_start); vec.push_back(message::CLIENTJOINACK); vec.push_back<int32_t>(player_list.back().id); vec.push_back(canary_end); udp_send_to(my_server, vec.ptr, (const sockaddr*)&who); printf("sending ack\n"); }
t_stat udp_send (DEVICE *dptr, int32 link, uint16 *pdata, uint16 count) { // Send an IMP packet to the remote simh. This is the usual case - the only // reason there's any other options at all is so we can emulate loopback. return udp_send_to (dptr, link, pdata, count, (SOCKADDR *) &(udp_links[link].txaddr)); }