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

Author Topic: Simply socket storage not working.  (Read 2167 times)

0 Members and 1 Guest are viewing this topic.

R23MJ

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Simply socket storage not working.
« on: September 06, 2015, 04:12:47 am »
Hello, I try not to ask questions, however; all day I have been reading through the forums, stack overflow, and various other places searching for an answer, unfortunately to no avail. What I am trying to accomplish is storing TcpSockets, I have come to the conclusion that I will need to use Smart Pointers or move semantics (which SFML does not yet support, SFML3.0? :) ). I have never used Smart pointers so I read up on them a bit, and thought that I understood, but I was apparently wrong because I can't seem to get my wrapper class to work. Below is the code I have, I know of some various errors that I will point out, but don't know how to fix. If there is anything that I miss it would be appreciated if you could help with that too.

Code:
class Client
{
public:
   Client() //I think I need to take the TcpSocket here and make the std::unique_ptr point to it. But I can't, it says I am copying the socket.
   {

   }

   ~Client()
   {
      _sockPtr->disconnect();
   }

   sf::TcpSocket& getSocket()
   {
      return *_sockPtr;
   }

private:
   std::unique_ptr<sf::TcpSocket> _sockPtr;
};
 

And this is the code I am using to accept clients. It's a bit rough right now.

            if (Selector.isReady(Listener))
            {
                Client client;
                Connections.push_back(client);
                if (Listener.accept(Connections[Connections.size() - 1].getSocket()) != sf::Socket::Done)
                {
                    //error
                }

                else
                {
                    std::cout << "Client Connected." << std::endl;
                }
            }
 

I'm sorry if the answer is just really simply. I am just having trouble figuring it out.

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Simply socket storage not working.
« Reply #1 on: September 06, 2015, 09:07:03 am »
Either pass a socket by reference, create a new one using std::make_unique<sf::TcpSocket>() or just stop using a wrapper around a wrapper. Unique_ptr is al ready a pointer wrapper, why are you wrapping it in a class that stores no extra information.

 

anything