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

Author Topic: problem on sf::Packet::onSend / sf::Packet::Onreceive  (Read 1971 times)

0 Members and 1 Guest are viewing this topic.

milodupipo

  • Newbie
  • *
  • Posts: 5
    • View Profile
problem on sf::Packet::onSend / sf::Packet::Onreceive
« on: January 17, 2017, 08:02:24 pm »
I just need some clarification about how works onSend and onReceive.
I send data from a client to a server with my class inherited from sf::Packet. I have overloaded sf::Packet::operator<< / operator>>
like below :

Class A, inherited from Packet
class A : public sf::Packet {
public:
(...)
protected:
   sf::Packet& operator<<(sf::Packet& packet, A& a){
      (...)
       packet << a.name;
       return packet;
   }

   sf::Packet& operator>>(sf::Packet& packet, A& a){
       (...)
       return packet;
   }

    /*
    Data encryption
    */

    const void* onSend(std::size_t& size)
    {
        const void* srcData = getData();
        std::size_t srcSize = getDataSize();
        size = srcSize;
        return this->c.encryption(srcData, srcSize, size);
        //return nullptr;
    }

    void onReceive(const void* data, std::size_t size)
    {
        std::cout << "Hello" << std::endl;
    }
};
 

Then where data are sent :
    sf::TcpSocket socket;
    sf::Socket::Status status = socket.connect(dest, port);

    while(!f.endFragmentation()){
        int instruction = (f.lastFragment()) ? 1 : 0;
        sf::Packet paquet;
        paquet << instruction << f;

         if (status != sf::Socket::Done)
         {
 
         }
         if (socket.send(paquet) != sf::Socket::Done)
         {
 
         }
     }
 

Problem is whatever I write into onSend or onReceive, my data are always nicely received.
Yes I know, it sounds good but I would like the data to be encrypted  :-\
So, I tried to return encrypted data, then nullptr and it never changed something. It seems like A::onSend/onReceive are never called.

Do you see what I'm doing wrong ?

I take advantage of this post to ask you an information about onReceive, in the SFML tutorial about sf::Packet, it's writed :

    virtual void onReceive(const void* data, std::size_t size)
    {
        std::size_t dstSize;
        const void* dstData = uncompressTheData(data, size, &dstSize); // this is a fake function, of course :)
        append(dstData, dstSize);
    }
 


If the goal is to uncompress data, why append them to the data already existing ? It shouldn't  be better to replace old data by the new ?
« Last Edit: January 17, 2017, 09:03:49 pm by milodupipo »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: problem on sf::Packet::onSend / sf::Packet::Onreceive
« Reply #1 on: January 17, 2017, 10:23:53 pm »
Quote
Do you see what I'm doing wrong ?
You're still using sf::Packet, not your new class A that's suppose to replace it.

Quote
f the goal is to uncompress data, why append them to the data already existing ?
There's no existing data ;)
Laurent Gomila - SFML developer

milodupipo

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: problem on sf::Packet::onSend / sf::Packet::Onreceive
« Reply #2 on: January 17, 2017, 11:07:41 pm »
Oh... I was blind, it was pretty obvious  ???
Thanks for help

 

anything