Esempio n. 1
0
HooksRegistrar& HooksRegistrar::connection_data_out(connection_data_t f)
{
    return connection_data(
        Engine::connection_data_out,
        f
    );
}
Esempio n. 2
0
    PrintingStatus format_data(ctsConfig::StatusFormatting _format, long long _current_time, bool _clear_status) throw()
    {
        ctsTcpStatistics tcp_data(ctsConfig::Settings->TcpStatusDetails.snap_view(_clear_status));
        ctsConnectionStatistics connection_data(ctsConfig::Settings->ConnectionStatusDetails.snap_view(_clear_status));

        long long time_elapsed = tcp_data.end_time.get() - tcp_data.start_time.get();

        if (_format == ctsConfig::StatusFormatting::Csv) {
            unsigned long characters_written = 0;
            // converting milliseconds to seconds before printing
            characters_written += this->append_csvoutput(characters_written, TimeSliceLength, static_cast<float>(_current_time / 1000.0));

            // calculating # of bytes that were sent between the previous format() and current call to format()
            characters_written += this->append_csvoutput(
                                      characters_written,
                                      SendBytesPerSecondLength,
                                      (time_elapsed > 0LL) ? static_cast<unsigned long>(tcp_data.bytes_sent.get() * 1000LL / time_elapsed) : 0LL);
            // calculating # of bytes that were received between the previous format() and current call to format()
            characters_written += this->append_csvoutput(
                                      characters_written,
                                      RecvBytesPerSecondLength,
                                      (time_elapsed > 0LL) ? static_cast<unsigned long>(tcp_data.bytes_recv.get() * 1000LL / time_elapsed) : 0LL);

            characters_written += this->append_csvoutput(characters_written, CurrentTransactionsLength, connection_data.active_connection_count.get());
            characters_written += this->append_csvoutput(characters_written, CompletedTransactionsLength, connection_data.successful_completion_count.get());
            characters_written += this->append_csvoutput(characters_written, ConnectionErrorsLength, connection_data.connection_error_count.get());
            characters_written += this->append_csvoutput(characters_written, ProtocolErrorsLength, connection_data.protocol_error_count.get(), false); // no comma at the end
            this->terminate_string(characters_written);

        } else {
            // converting milliseconds to seconds before printing
            this->right_justify_output(TimeSliceOffset, TimeSliceLength, static_cast<float>(_current_time / 1000.0));

            // calculating # of bytes that were sent between the previous format() and current call to format()
            this->right_justify_output(
                SendBytesPerSecondOffset,
                SendBytesPerSecondLength,
                (time_elapsed > 0LL) ? static_cast<unsigned long>(tcp_data.bytes_sent.get() * 1000LL / time_elapsed) : 0LL);
            // calculating # of bytes that were received between the previous format() and current call to format()
            this->right_justify_output(
                RecvBytesPerSecondOffset,
                RecvBytesPerSecondLength,
                (time_elapsed > 0LL) ? static_cast<unsigned long>(tcp_data.bytes_recv.get() * 1000LL / time_elapsed) : 0LL);

            this->right_justify_output(CurrentTransactionsOffset, CurrentTransactionsLength, connection_data.active_connection_count.get());
            this->right_justify_output(CompletedTransactionsOffset, CompletedTransactionsLength, connection_data.successful_completion_count.get());
            this->right_justify_output(ConnectionErrorsOffset, ConnectionErrorsLength, connection_data.connection_error_count.get());
            this->right_justify_output(ProtocolErrorsOffset, ProtocolErrorsLength, connection_data.protocol_error_count.get());
            this->terminate_string(ProtocolErrorsOffset);
        }

        return PrintComplete;
    }
Esempio n. 3
0
SyslogClient::connection_data SyslogClient::parse_header(const ::Plugin::Common_Header &header, client::configuration::data_type data) {
	nscapi::functions::destination_container recipient;
	nscapi::functions::parse_destination(header, header.recipient_id(), recipient, true);
	return connection_data(recipient, data->recipient);
}
void Server::Update(){
    // CORE LOOP
    
    // listen for and handle new connections
    TCPsocket new_connection = SDLNet_TCP_Accept(server_socket);
    if (new_connection){
        if (players_connected < MAX_PLAYERS){
            // add new player
            SDLNet_TCP_AddSocket(client_sockets, new_connection);
            client_data.push_back(connection_data(new_connection, SDL_GetTicks(), current_playerId, true));
            players_connected++;
            
            PlayerObject *newPlayer = new PlayerObject(17);
            newPlayer->loadTexture(renderer, "other.png");
            newPlayer->SetScale(*new Vector2(0.1, 0.1));
            playerObjects[current_playerId] = newPlayer;
            gameObjects.push_back(newPlayer);
            
            // inform connected player about self
            sprintf(outgoing, "0 %i\n", current_playerId);
            SDLNet_TCP_Send(new_connection, outgoing, 17);
            
            for (map<int, PlayerObject*>::iterator i = playerObjects.begin(); i != playerObjects.end(); ++i){
                if (i->first == current_playerId){
                    continue;
                }
                
                // inform other players about new player
                sprintf(outgoing, "2 %i\n", current_playerId);
                SDLNet_TCP_Send(client_data[i->first].socket, outgoing, 17);
                
                // inform connected player about others
                sprintf(outgoing, "2 %i\n", i->first);
                SDLNet_TCP_Send(client_data[current_playerId].socket, outgoing, 17);
            }
            
            printf("New player connected: %i\n", current_playerId);
            current_playerId++;
        } else {
            // server is full
            printf("New connection attempt but the server was full!\n");
        }
    }
    
    // receive data
    while (SDLNet_CheckSockets(client_sockets, 0) > 0){
        for (int i = 0; i < client_data.size(); i++){
            if (client_data[i].connected && SDLNet_SocketReady(client_data[i].socket)){
                client_data[i].timeout = SDL_GetTicks();
                SDLNet_TCP_Recv(client_data[i].socket, incoming, 17);
                
                //printf("Player %i: %s\n", client_data[i].player_id, incoming);
                
                // TODO: make this more flexible
                char* parse = strtok(incoming, " ");
                if (parse){
                    switch(atoi(parse)){
                        case 0:{
                            // 0 = disconnect message
                            // delete from gameObject vector
                            vector<GameObject*>::iterator it = find(gameObjects.begin(), gameObjects.end(), playerObjects[client_data[i].player_id]);
                            if (it != gameObjects.end()){
                                gameObjects.erase(it);
                            }
                            delete playerObjects[i];
                            playerObjects.erase(i);
                            
                            // inform other players
                            for (map<int, PlayerObject*>::iterator it = playerObjects.begin(); it != playerObjects.end(); ++it){
                                if (it->first == client_data[i].player_id){
                                    continue;
                                }
                                sprintf(outgoing, "3 %i\n", client_data[i].player_id);
                                SDLNet_TCP_Send(client_data[it->first].socket, outgoing, 17);
                            }
                            
                            players_connected--;
                            
                            // delete from socket info
                            SDLNet_TCP_DelSocket(client_sockets, client_data[i].socket);
                            client_data[i].connected = false;
                            //client_data.erase(client_data.begin() + i);
                            break;
                        }
                        case 1:{
                            // 1 = direction update
                            // "1 direction.x direction.y"
                            parse = strtok(NULL, " ");
                            float x = atof(parse);
                            parse = strtok(NULL, " ");
                            float y = atof(parse);
                            playerObjects[client_data[i].player_id]->SetDirection(*new Vector2(x, y));
                            break;
                        }
                        default: break;
                    }
                }
                
            }
        }
    }
    
    // collision detection
    for (int i = 0; i < gameObjects.size(); i++){
        if (gameObjects[i]->CanCollide()){
            for (int j = i; j < gameObjects.size(); j++){
                if (j == i) continue;
                if (gameObjects[j]->CanCollide()){
                    gameObjects[i]->Collision(gameObjects[j]);
                }
            }
        }
    }
    
    // update game objects
    for (int i = 0; i < gameObjects.size(); i++){
        gameObjects[i]->Update();
    }
    
    // send data
    for (map<int, PlayerObject*>::iterator i = playerObjects.begin(); i != playerObjects.end(); ++i){
        // reach every connected player
        
        // send position info
        for (map<int, PlayerObject*>::iterator j = playerObjects.begin(); j != playerObjects.end(); ++j){
            sprintf(outgoing, "1 %i %.2f %.2f\n", i->first, i->second->GetPosition().GetX(), i->second->GetPosition().GetY());
            SDLNet_TCP_Send(client_data[j->first].socket, outgoing, 17);
        }
        
        // send ball position
        sprintf(outgoing, "5 %.2f %.2f\n", ball->GetPosition().GetX(), ball->GetPosition().GetY());
        SDLNet_TCP_Send(client_data[i->first].socket, outgoing, 17);
        
        //printf("data sent: %s", outgoing);
    }

}