void* ThreadSend() { while(1) { CCLOG("wait mutex to send"); mutexMapPlay.lock(); // pthread_mutex_lock(&mutexMapPlay); mapLayerToString(playLayer, packetBuffer); // strcpy(packetBuffer, "0000000030000003000000000000000000000010000000000\1"); ENetPacket * packet = enet_packet_create (packetBuffer, 150, ENET_PACKET_FLAG_RELIABLE); CCLOG(packet->data); /* Send the packet to the peer over channel id 0. */ /* One could also broadcast the packet by */ /* enet_host_broadcast (host, 0, packet); */ enet_peer_send (peer, 0, packet); /* One could just use enet_host_service() instead. */ enet_host_flush (server); CCLOG("finish send packet"); } enet_peer_disconnect (peer, 0); enet_peer_reset (peer); CCLOG("exit thread send"); return NULL; }
void net_controller_har_hook(int action, void *cb_data) { controller *ctrl = cb_data; wtf *data = ctrl->data; serial ser; ENetPeer *peer = data->peer; ENetHost *host = data->host; ENetPacket *packet; if (action == ACT_STOP && data->last_action == ACT_STOP) { data->last_action = -1; return; } if (action == ACT_FLUSH) { enet_host_flush(host); return; } data->last_action = action; serial_create(&ser); serial_write_int8(&ser, EVENT_TYPE_ACTION); serial_write_int16(&ser, action); /*DEBUG("controller hook fired with %d", action);*/ /*sprintf(buf, "k%d", action);*/ packet = enet_packet_create(ser.data, ser.len, ENET_PACKET_FLAG_RELIABLE); if (peer) { enet_peer_send(peer, 1, packet); /*enet_host_flush (host);*/ } else { DEBUG("peer is null~"); } }
/** Force an immediate disconnection from a peer. @param peer peer to disconnect @remarks No ENET_EVENT_DISCONNECT event will be generated. The foreign peer is not guarenteed to receive the disconnect notification, and is reset immediately upon return from this function. */ void enet_peer_disconnect_now (ENetPeer * peer) { ENetProtocol command; if (peer -> state != ENET_PEER_STATE_DISCONNECTED) return; if (peer -> state != ENET_PEER_STATE_ZOMBIE && peer -> state != ENET_PEER_STATE_DISCONNECTING) { enet_peer_reset_queues (peer); command.header.command = ENET_PROTOCOL_COMMAND_DISCONNECT; command.header.channelID = 0xFF; command.header.flags = 0; command.header.commandLength = sizeof (ENetProtocolDisconnect); enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0); enet_host_flush (peer -> host); } enet_peer_reset (peer); }
/** Request a disconnection from a peer. @param peer peer to request a disconnection @param data data describing the disconnection @remarks An ENET_EVENT_DISCONNECT event will be generated by enet_host_service() once the disconnection is complete. */ void enet_peer_disconnect (ENetPeer * peer, enet_uint32 data) { ENetProtocol command; if (peer -> state == ENET_PEER_STATE_DISCONNECTING || peer -> state == ENET_PEER_STATE_DISCONNECTED || peer -> state == ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT || peer -> state == ENET_PEER_STATE_ZOMBIE) return; enet_peer_reset_queues (peer); command.header.command = ENET_PROTOCOL_COMMAND_DISCONNECT; command.header.channelID = 0xFF; command.disconnect.data = ENET_HOST_TO_NET_32 (data); if (peer -> state == ENET_PEER_STATE_CONNECTED || peer -> state == ENET_PEER_STATE_DISCONNECT_LATER) command.header.command |= ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE; else command.header.command |= ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED; enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0); if (peer -> state == ENET_PEER_STATE_CONNECTED || peer -> state == ENET_PEER_STATE_DISCONNECT_LATER) peer -> state = ENET_PEER_STATE_DISCONNECTING; else { enet_host_flush (peer -> host); enet_peer_reset (peer); } }
void Room::RoomImpl::BroadcastRoomInformation() { Packet packet; packet << static_cast<u8>(IdRoomInformation); packet << room_information.name; packet << room_information.member_slots; packet << room_information.uid; packet << room_information.port; packet << room_information.preferred_game; packet << static_cast<u32>(members.size()); { std::lock_guard<std::mutex> lock(member_mutex); for (const auto& member : members) { packet << member.nickname; packet << member.mac_address; packet << member.game_info.name; packet << member.game_info.id; } } ENetPacket* enet_packet = enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); enet_host_broadcast(server, 0, enet_packet); enet_host_flush(server); }
void __cdecl update_client(void* a) { for(int i = 0; i < v_clients.size(); ++i) { // POS il_znakow nick = pozycjaX x pozycjaY char buf1[10]; itoa(v_clients[i].nick.size(), buf1, 10); char buf2[10]; itoa(v_clients[i].y, buf2, 10); char buf3[10]; itoa(v_clients[i].y, buf3, 10); string packet = ""; packet += "POS"; packet += buf1; packet += v_clients[i].nick; packet += "="; packet += buf2; packet += "x"; packet += buf3; ENetPacket *p = enet_packet_create(packet.c_str(), packet.size()+1, ENET_PACKET_FLAG_UNSEQUENCED); enet_host_broadcast(server, 1, p); enet_host_flush(server); } updateTime = 0; }
void NetworkedMultiplayerENet::close_connection() { if (!active) return; _pop_current_packet(); bool peers_disconnected=false; for (Map<int,ENetPeer*>::Element *E=peer_map.front();E;E=E->next()) { if (E->get()) { enet_peer_disconnect_now(E->get(),unique_id); peers_disconnected=true; } } if (peers_disconnected) { enet_host_flush(host); OS::get_singleton()->delay_usec(100); //wait 100ms for disconnection packets to send } enet_host_destroy(host); active=false; incoming_packets.clear(); unique_id=1; //server is 1 connection_status=CONNECTION_DISCONNECTED; }
/** Request a disconnection from a peer. @param peer peer to request a disconnection @remarks An ENET_EVENT_DISCONNECT event will be generated by enet_host_service() once the disconnection is complete. */ void enet_peer_disconnect (ENetPeer * peer) { ENetProtocol command; if (peer -> state == ENET_PEER_STATE_DISCONNECTING || peer -> state == ENET_PEER_STATE_DISCONNECTED || peer -> state == ENET_PEER_STATE_ZOMBIE) return; enet_peer_reset_queues (peer); command.header.command = ENET_PROTOCOL_COMMAND_DISCONNECT; command.header.channelID = 0xFF; command.header.flags = ENET_PROTOCOL_FLAG_UNSEQUENCED; command.header.commandLength = sizeof (ENetProtocolDisconnect); if (peer -> state == ENET_PEER_STATE_CONNECTED) command.header.flags = ENET_PROTOCOL_FLAG_ACKNOWLEDGE; enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0); if (peer -> state == ENET_PEER_STATE_CONNECTED) peer -> state = ENET_PEER_STATE_DISCONNECTING; else { enet_host_flush (peer -> host); enet_peer_reset (peer); } }
void ENetServer::broadcastMessage(DeliveryType type, Message::Shared msg) const { if (numClients() == 0) { // no clients to broadcast to return; } uint32_t channel = 0; uint32_t flags = 0; if (type == DeliveryType::RELIABLE) { channel = RELIABLE_CHANNEL; flags = ENET_PACKET_FLAG_RELIABLE; } else { channel = UNRELIABLE_CHANNEL; flags = ENET_PACKET_FLAG_UNSEQUENCED; } // get bytes auto data = msg->serialize(); // create the packet ENetPacket* p = enet_packet_create( &data[0], data.size(), flags); // send the packet to the peer enet_host_broadcast(host_, channel, p); // flush / send the packet queue enet_host_flush(host_); }
/** Destroy an allocated ENetHost pointer. Must be used with val_gc() for every ENetHost created. */ static void destroy_enethost( value h ) { #ifdef ENET_DEBUG fprintf(stderr, "*** destroy_enethost\n"); //exit(0); #endif //return; if(!val_is_kind(h, k_udprhost)) return; ENetHost *host = (ENetHost *)val_data(h); ENetPeer *peer; //int x, count; if(host == NULL) return; enet_host_flush( host ); for (peer = host->peers; peer < &host->peers[host->peerCount]; ++peer) { if (peer->state == ENET_PEER_STATE_CONNECTED) { enet_peer_disconnect_now(peer, 0); } } #ifdef ENET_DEBUG fprintf(stderr, "*** destroy_enethost\n"); #endif enet_host_destroy( host ); //val_kind(v) = NULL; #ifdef ENET_DEBUG fprintf(stderr, "*** destroy_enethost done.\n"); #endif return; }
void ClientSystem::Connect() { if ( mProgramState.mClientConnected ) { L1( "Already connected, won't try it again!\n" ); return; //Really wont try again } // ENetAddress address2; // address2.host = ENET_HOST_ANY; // /* Bind the server to port 1234. */ // address2.port = 1236; mClient = enet_host_create ( NULL /* create a client host */, 1 /* only allow 1 outgoing connection */, 2 /* allow up 2 channels to be used, 0 and 1 */, 0 , 0 ); if ( mClient == NULL ) { L1( "An error occurred while trying to create an ENet client host.\n" ); exit ( EXIT_FAILURE ); } ENetAddress address; ENetEvent event; /* Connect to some.server.net:1234. */ address.port = 1234; enet_address_set_host ( & address, core::ProgramState::Get().mServerIp.c_str() ); //core::ProgramState::Get().mServerIp.c_str()); /* Initiate the connection, allocating the two channels 0 and 1. */ mPeer = enet_host_connect ( mClient, & address, 2, 0 ); if ( mPeer == NULL ) { L1( "No available peers for initiating an ENet connection.\n" ); exit ( EXIT_FAILURE ); } bool connectSuccess = false; for( size_t i = 0; i < 500; ++i ) { if ( enet_host_service ( mClient, & event, 10 ) > 0 && event.type == ENET_EVENT_TYPE_CONNECT ) { connectSuccess = true; break; } } enet_host_flush( mClient ); if( !connectSuccess ) { L1( "Connection timed out.\n" ); enet_peer_reset ( mPeer ); engine::Engine::Get().GetSystem<engine::WindowSystem>()->Close(); } else { mProgramState.mClientConnected = true; std::auto_ptr<MyNameMessage> msg( new MyNameMessage ); msg->mName = core::ProgramState::Get().mClientName; msg->mControlledLocalPlayerId = 1; // TODO: change when multiple local players are allowed mMessageHolder.AddOutgoingMessage( std::auto_ptr<Message>( msg.release() ) ); } }
void ENetServer::sendMessage(uint32_t id, DeliveryType type, Message::Shared msg) const { auto client = getClient(id); if (!client) { // no client to send to LOG_WARN("No connected client with id: " << id); return; } uint32_t channel = 0; uint32_t flags = 0; if (type == DeliveryType::RELIABLE) { channel = RELIABLE_CHANNEL; flags = ENET_PACKET_FLAG_RELIABLE; } else { channel = UNRELIABLE_CHANNEL; flags = ENET_PACKET_FLAG_UNSEQUENCED; } // get bytes auto data = msg->serialize(); // create the packet ENetPacket* p = enet_packet_create( &data[0], data.size(), flags); // send the packet to the peer enet_peer_send(client, channel, p); // flush / send the packet queue enet_host_flush(host_); }
/** * audio callback. */ void send_callback(svc_network_packet_t* packet){ enet_uint32 src; ENetPacket* epacket; unsigned char* data; /* get ID */ mutex_lock(&client.network_lock); src = client.my_id; /* this client didn't get it's ID yet (this also handles connection state as my_id==-1 if not connected) * or is muted. */ if (src == -1 || client.muted) { mutex_unlock(&client.network_lock); return; } /* allocate data */ data = (unsigned char*)malloc(packet->data_len+10); *(enet_uint32*)data = ENET_HOST_TO_NET_32(src); *(enet_uint32*)(data + 4) = ENET_HOST_TO_NET_32(packet->data_len+2); *(enet_uint16*)(data + 8) = ENET_HOST_TO_NET_16(packet->time); /* copy audio data */ memcpy(data + 10, packet->data, packet->data_len); epacket = enet_packet_create(data, packet->data_len + 10, ENET_PACKET_FLAG_UNSEQUENCED); enet_peer_send(client.client, 1, epacket); enet_host_flush(client.host); mutex_unlock(&client.network_lock); free(data); }
void ListenServerNetMode::sync(GameInstance* instance, ObjectManager* objectManager) { if(m_host) { // Manage connected players syncConnectedPlayers(instance, objectManager); // Manage packets from active players syncIncomingPackets(objectManager); // Manage sending packets to players syncOutgoingPackets(objectManager); // Connected players have now been served properly so they can become active players for(SPlayer* player : m_connectedPlayers) { player->status = EPlayerStatus::Active; m_activePlayers.push_back(player); } m_connectedPlayers.clear(); // Flush all created packets enet_host_flush(m_host); } }
int main (int argc, char * const argv[]) { if(enet_initialize() != 0) { printf("An error occurred while initializing ENet.\n"); } ENetAddress address; ENetHost* server; address.host = ENET_HOST_ANY; address.port = 9050; // up to 32 clients with unlimited bandwidth server = enet_host_create(&address, 32, 0, 0); if(server == NULL) { printf("An error occurred while trying to create an ENet server host.\n"); } int clients = 0; // server main loop while(true) { ENetEvent event; while(enet_host_service(server, &event, 1000) > 0) { switch(event.type) { case ENET_EVENT_TYPE_CONNECT: printf ("A new client connected from %x:%u.\n", event.peer -> address.host, event.peer -> address.port); clients++; break; case ENET_EVENT_TYPE_RECEIVE: printf ("A packet of length %u containing %s was received.\n", event.packet->dataLength, event.packet->data); ENetPacket* packet = enet_packet_create(event.packet->data, event.packet->dataLength, ENET_PACKET_FLAG_RELIABLE); enet_host_broadcast(server, 0, packet); enet_host_flush(server); enet_packet_destroy(event.packet); break; case ENET_EVENT_TYPE_DISCONNECT: printf("Client disconnected.\n"); event.peer->data = NULL; } } } return 0; }
void disconnect(bool async, bool cleanup) { if(curpeer) { if(!discmillis) { enet_peer_disconnect(curpeer, DISC_NONE); enet_host_flush(clienthost); discmillis = totalmillis; } if(curpeer->state!=ENET_PEER_STATE_DISCONNECTED) { if(async) return; enet_peer_reset(curpeer); } curpeer = NULL; discmillis = 0; conoutf("disconnected"); game::gamedisconnect(cleanup); setvar("mainmenu", 1); } if(!connpeer && clienthost) { enet_host_destroy(clienthost); clienthost = NULL; } }
static int manageEnet(ServerData *data) { ENetEvent event; PacketGeneric *packet; Client *client; assert(data); /* Wait up to 1000 milliseconds for an event. */ while (enet_host_service(data->server, &event, 1000) > 0) { switch (event.type) { case ENET_EVENT_TYPE_CONNECT: printf("A new client connected from %x:%u.\n", event.peer->address.host, event.peer->address.port); /* broadcast join message to others */ sendJoin(data, &event); client = (Client*)event.peer->data; break; case ENET_EVENT_TYPE_RECEIVE: /* discard bad packets */ if (event.packet->dataLength < sizeof(PacketGeneric)) break; printf("A packet of length %u was received on channel %u.\n", event.packet->dataLength, event.channelID); /* handle packet */ client = (Client*)event.peer->data; packet = (PacketGeneric*)event.packet->data; switch (packet->id) { case PACKET_ID_ACTOR_STATE: handleState(data, &event); break; case PACKET_ID_ACTOR_FULL_STATE: handleFullState(data, &event); break; } /* Clean up the packet now that we're done using it. */ enet_packet_destroy(event.packet); break; case ENET_EVENT_TYPE_DISCONNECT: /* broadcast part message to others */ sendPart(data, &event); /* Reset the peer's client information. */ serverFreeClient(data, event.peer->data); event.peer->data = NULL; } } /* send all response packets */ enet_host_flush(data->server); return RETURN_OK; }
void CNetClientSession::Flush() { PROFILE3("net client flush"); ENSURE(m_Host && m_Server); enet_host_flush(m_Host); }
void NetClient::Packet_SendNow(uint8* packet_contents, uint16 packet_size, net_peer_t peer) { ENetPacket* packet = enet_packet_create(packet_contents, packet_size, ENET_PACKET_FLAG_RELIABLE | ENET_PACKET_FLAG_NO_ALLOCATE); int result = enet_peer_send(m_enetpeer, 0, packet); enet_host_flush(m_enetclient); // Send packets now so we can reclaim the memory TCheck(result >= 0); }
void cmdDisconnect(const std::string& args) { if (connectState != ConnectState::Unconnected){ narf::console->println("Disconnecting..."); enet_peer_disconnect(server, (uint32_t)narf::net::DisconnectType::UserQuit); enet_host_flush(client); } else { narf::console->println("Not connected"); } }
void Room::RoomImpl::SendWrongPassword(ENetPeer* client) { Packet packet; packet << static_cast<u8>(IdWrongPassword); ENetPacket* enet_packet = enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); enet_peer_send(client, 0, enet_packet); enet_host_flush(server); }
void network_send( ENetHost* socket, ENetPeer* peer, void* data, int datalength ) { if( socket == 0 || peer == 0 ) return; ENetPacket* packet = enet_packet_create( data, datalength, ENET_PACKET_FLAG_RELIABLE); enet_peer_send(peer, 0, packet); enet_host_flush(socket); }
void network_send(Client *client, const char *string) { ENetPacket *packet; g_return_if_fail(string != NULL); packet = enet_packet_create(string, strlen(string) + 1, ENET_PACKET_FLAG_RELIABLE); enet_peer_send(client, 0, packet); enet_host_flush(network->server); }
void joynet_disconnect_client_from_server(JOYNET_CLIENT * cp) { if(cp->peer) { enet_peer_disconnect(cp->peer, JOYNET_DISCONNECT_CLIENT_CLOSED); enet_host_flush(cp->host); enet_host_destroy(cp->host); } }
void menu_listen_tick(component *c) { listen_menu_data *local = menu_get_userdata(c); game_state *gs = local->s->gs; if(local->host) { ENetEvent event; if(enet_host_service(local->host, &event, 0) > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { ENetPacket * packet = enet_packet_create("0", 2, ENET_PACKET_FLAG_RELIABLE); enet_peer_send(event.peer, 0, packet); enet_host_flush(local->host); DEBUG("client connected!"); controller *player1_ctrl, *player2_ctrl; keyboard_keys *keys; game_player *p1 = game_state_get_player(gs, 0); game_player *p2 = game_state_get_player(gs, 1); // force the speed to 3 game_state_set_speed(gs, 5); p1->har_id = HAR_JAGUAR; p1->pilot_id = 0; p2->har_id = HAR_JAGUAR; p2->pilot_id = 0; player1_ctrl = malloc(sizeof(controller)); controller_init(player1_ctrl); player1_ctrl->har = p1->har; player2_ctrl = malloc(sizeof(controller)); controller_init(player2_ctrl); player2_ctrl->har = p2->har; // Player 1 controller -- Keyboard settings_keyboard *k = &settings_get()->keys; keys = malloc(sizeof(keyboard_keys)); keys->up = SDL_GetScancodeFromName(k->key1_up); keys->down = SDL_GetScancodeFromName(k->key1_down); keys->left = SDL_GetScancodeFromName(k->key1_left); keys->right = SDL_GetScancodeFromName(k->key1_right); keys->punch = SDL_GetScancodeFromName(k->key1_punch); keys->kick = SDL_GetScancodeFromName(k->key1_kick); keys->escape = SDL_GetScancodeFromName(k->key1_escape); keyboard_create(player1_ctrl, keys, 0); game_player_set_ctrl(p1, player1_ctrl); // Player 2 controller -- Network net_controller_create(player2_ctrl, local->host, event.peer, ROLE_SERVER); game_player_set_ctrl(p2, player2_ctrl); local->host = NULL; game_player_set_selectable(p2, 1); chr_score_set_difficulty(game_player_get_score(game_state_get_player(gs, 0)), AI_DIFFICULTY_CHAMPION); chr_score_set_difficulty(game_player_get_score(game_state_get_player(gs, 1)), AI_DIFFICULTY_CHAMPION); game_state_set_next(gs, SCENE_MELEE); } } }
void Room::RoomImpl::SendJoinSuccess(ENetPeer* client, MacAddress mac_address) { Packet packet; packet << static_cast<u8>(IdJoinSuccess); packet << mac_address; ENetPacket* enet_packet = enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); enet_peer_send(client, 0, enet_packet); enet_host_flush(server); }
void ClientTransmissionManager::ReallySendMessage(ENetPeer* peer, enet_uint8 channel, ENetPacket* packet) { boost::mutex::scoped_lock lock(this->mHostMutex); enet_peer_send(peer,channel,packet); if(channel==CHANNEL_VOICE_CHAT) { enet_host_flush(this->mClient); } }
void RemoteInterface::disconnect() { std::cout << "[DEBUG] RemoteInterface::disconnect"<< std::endl; if(peer) { enet_peer_disconnect(peer, DISC_NONE); enet_peer_reset (peer); } if(host) enet_host_flush(host); }
void GDNetHost::unbind() { if (_host != NULL) { thread_stop(); enet_host_flush(_host); enet_host_destroy(_host); _host = NULL; _message_queue.clear(); _event_queue.clear(); } }
void Client::flushPackets() { //if (getApplicationSide() != APPSIDE_CLIENT) //{ // return; //} enet_host_flush(m_pClientHost); enet_host_service(m_pClientHost, &m_event, 100); }