Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::Packet Data Extraction  (Read 2601 times)

0 Members and 1 Guest are viewing this topic.

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
sf::Packet Data Extraction
« on: January 20, 2015, 01:56:28 am »
When getting data, must you do all of your extractions in one line of code or can you do one extraction. And then, when you do the next, have it continue where you last were at. Example:

packet <<x <<y;
packet >>x;
packet >>y;

If not, is there any way to do it.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: sf::Packet Data Extraction
« Reply #1 on: January 20, 2015, 01:57:51 am »
It should "just work" either way.  These are standard C++ operators implemented in the standard way, so they behave the same as iostreams.

Is there some specific reason you're asking this question instead of simply writing that code?
« Last Edit: January 20, 2015, 01:59:53 am by Ixrec »

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Re: sf::Packet Data Extraction
« Reply #2 on: January 20, 2015, 02:10:58 am »
I am looping to extract data to a map structure. It gets the x and y values from the packet, and tests to make sure they are within map range. If so, it sets the map data [ x ][ y ] to the block received. It loops this until the map reading is complete. I am getting some strange results from this method.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: sf::Packet Data Extraction
« Reply #3 on: January 20, 2015, 02:15:06 am »
You should post a minimal and complete code example so we can pin down exactly what the problem is.

Solely based on what you posted, my blind guess would be that you're sending your map data without any size parameter, and trying to compensate for this on the receiver with a heuristic (aka, a hack) that isn't accurate enough.  If so, the solution is to send a size parameter.

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Re: sf::Packet Data Extraction
« Reply #4 on: January 20, 2015, 02:46:55 am »
Cilent
while(!mainpack.endOfPacket())
{
        if(mainpack >>counter1 >> counter2 >>actualx >>actualy >>temp)
        input=0;
    else
    {
        std::cout <<"An error has occured\n";
        std::cout <<"Got Map Data x " <<counter1 <<" y " <<counter2<<" data " <<temp <<" actualx " <<actualx <<" actualy "<<actualy <<"\n";
        counter1= -40; continue;
    }
    std::cout <<"Got Map Data x " <<counter1 <<" y " <<counter2<<" data " <<temp <<" actualx " <<actualx <<" actualy "<<actualy <<"\n";
    if(counter1 > 0 & counter2 > 0 & counter1 <MAP_WIDTH & counter2<MAP_HEIGHT)
    mainmap.block[counter1][counter2]=temp;
    else
    {
        if(counter1 < 1 | counter2 < 1)
    std::cout <<"An Error has Occured Because x " <<counter1 <<" or y " <<counter2 <<" is less than one\n";

        if(counter1 >MAP_WIDTH & counter2 > MAP_HEIGHT)
    std::cout <<"An Error has Occured Because x " << actualx <<" is greater than map width"
    <<MAP_WIDTH <<"\n or y " <<counter2 <<" is greater than map height " <<MAP_HEIGHT <<"\n";
    }

Server:

        upperleftx=10;//(player_.location[counter][1]-VIEW_WIDTH)-1;
        upperlefty=10;//(player_.location[counter][2]-VIEW_HEIGHT)-1;
        lowerrightx=10+VIEW_WIDTH;//(player_.location[counter][1]+VIEW_WIDTH)+1;
        lowerrighty=10+VIEW_HEIGHT;//(player_.location[counter][2]+VIEW_HEIGHT)+1;
        int counterx = upperleftx; int countery = upperlefty;
        int counter2=1; int counter1=0;
while(countery<lowerrighty)
{

    receive <<mainmap.block[counterx][countery];
    std::cout <<"mainmap screenx  " <<counter1 <<" screeny" <<counter2 <<" " << "\nx" <<counterx <<" y" << countery <<"data" <<mainmap.block[counterx][countery] <<"\n";
    counterx++;
    counter1++;
    if(counterx>lowerrightx)
    {
        counterx=upperleftx;
        counter1=1;
        countery++;
        counter2++;
    }
}
if(request.send(receive, receiveip, port) ==sf::Socket::Done)
{
    std::cout <<"Sent Properly \n";
}
else
{
    std::cout <<"An Error Occured While Sending Sending Map to " <<usernametemp <<"\n";
}
        }
 
« Last Edit: January 20, 2015, 07:48:04 am by Laurent »

Rosme

  • Full Member
  • ***
  • Posts: 169
  • Proud member of the shoe club
    • View Profile
    • Code-Concept
Re: sf::Packet Data Extraction
« Reply #5 on: January 20, 2015, 04:37:31 pm »
GitHub
Code Concept
Twitter
Rosme on IRC/Discord