10 Replies - 1309 Views - Last Post: 17 December 2012 - 02:51 PM Rate Topic: -----

#1 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Sending an object through an sf::Packet

Posted 16 December 2012 - 03:33 PM

I am trying to send an object through an sf packet by overloading the << and >> operators. Here is what that code looks like.

 sf::Packet& operator <<(sf::Packet& packet, const Client& A)
 {
	 return packet << A;
 }
  sf::Packet& operator >>(sf::Packet& packet, Client& A)
 {
	 return packet >> A;
 }




The program breaks when it gets to this code and says stack overflow error. I think the problem is that i can only send primitive data types (int, string, bool, etc) through and not whole structures.

The obvious answer is to break down and reassemble the structure, but i am hesitant to do so because i am sending an array of these structs at a time. That code looks like

//send
packet << static_cast<sf::Uint32>(8);
for(int i = 0; i < 8; i++) // for each client
{
	for( int j = 0; j < 8; i++)// for every client
	{
		packet << clientInfo[j];
		socket.send(packet, clientInfo[i].address, port);
	}
}


//receive
sf::Uint32 size;
packet >> size;
for( sf::Uint32 i= 0; i < size; ++i)
{
	packet >> clientsInfo[i];
}




So should i break down and reassemble each struct(which would probably work), or is there another solution?


Edit: There must be another solution because what if the struct contains other structs? Am i expected to break down those as well?

This post has been edited by Nano511: 16 December 2012 - 04:16 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Sending an object through an sf::Packet

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Sending an object through an sf::Packet

Posted 16 December 2012 - 04:01 PM

Where did you get this code from?

I just checked the SFML site where there is example code. What are you doing?

This post has been edited by ButchDean: 16 December 2012 - 04:04 PM

Was This Post Helpful? 0
  • +
  • -

#3 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: Sending an object through an sf::Packet

Posted 16 December 2012 - 04:15 PM

Lol the operator overloading is from the sfml-dev website. The second part where i send the array over struct by struct is adapted from Laurent's comment in this thread http://en.sfml-dev.o...hp?topic=4709.0

Edit: An edit has been added to the OP

This post has been edited by Nano511: 16 December 2012 - 04:17 PM

Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Sending an object through an sf::Packet

Posted 16 December 2012 - 04:19 PM

When I say 'example code' I don't mean from a forum thread. You should be referring to the SFML tutorials on that site with complete code!
Was This Post Helpful? 0
  • +
  • -

#5 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: Sending an object through an sf::Packet

Posted 16 December 2012 - 04:21 PM

The operator overloading is from http://www.sfml-dev....f_1_1Packet.php about a little less than half way down.

The second code i posted is adapted from that forum post. It is not on from the sfml tutorial.
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Sending an object through an sf::Packet

Posted 16 December 2012 - 04:26 PM

You don't know what you're doing, you need to start from first principles. Go through the tutorial first.
Was This Post Helpful? 0
  • +
  • -

#7 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: Sending an object through an sf::Packet

Posted 16 December 2012 - 04:30 PM

There is no tutorial for 2.0 networking, but from the 1.6 tutorial i gathered that i cannot overload the operator that way i was trying to.

"So should i break down and reassemble each struct(which would probably work), or is there another solution?"
Was This Post Helpful? 0
  • +
  • -

#8 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Sending an object through an sf::Packet

Posted 16 December 2012 - 04:35 PM

So use version 1.6. You can work around overloading, which likely is your task that you need to work out.
Was This Post Helpful? 0
  • +
  • -

#9 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: Sending an object through an sf::Packet

Posted 16 December 2012 - 04:42 PM

Alright im not going to use 1.6, and i think that's a ridiculous suggestion. All of my code is in 2.0. I don't think i need to work around overloading, im just going to try deconstructing the structure and reassembling it when it is received, which is probably what i should i tried before asking the question.

This is just avoiding a problem that I will probably have to face again as my game becomes more complex, so ill ask a more indistinct question.

Is there a way to send a data type through an sf::Packet that is not primitive?
Was This Post Helpful? -1
  • +
  • -

#10 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Sending an object through an sf::Packet

Posted 16 December 2012 - 04:53 PM

You need to get your code working by whatever means. You do not have the knowledge to deal with the operator overloading for SFML 2.0, and I'm not going to work it out for you.

You need to learn to get your code working over trying to use the latest versions of stuff. I don't see what the problem is.
Was This Post Helpful? 0
  • +
  • -

#11 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: Sending an object through an sf::Packet

Posted 17 December 2012 - 02:51 PM

View PostNano511, on 16 December 2012 - 04:42 PM, said:

Is there a way to send a data type through an sf::Packet that is not primitive?

Yes there is:
http://www.sfml-dev....f_1_1Packet.php

Quote

Like standard streams, it is also possible to define your own overloads of operators >> and << in order to handle your custom types.


The link shows you a clear example how to define your own overloads of >> and << to handle your own data types. You have to pass the members of your structure.

Then you can do something like this:
packet << clientInfo[j];


As ButchDean mentioned, read the tutorial... it's really all there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1