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

Author Topic: [SFML 1.6] MultiThreading on a Client Class getting error [Solved on SFML 2.0]  (Read 6646 times)

0 Members and 1 Guest are viewing this topic.

XeRaCKeR

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Hi, I'm working on a Client App and I wish to create a Client class where I create 2 threads (R and S), who execute functions Receiver and Sender, respectively...
class Client {
    sf::IPAddress ServerAddress;
    sf::SocketTCP Socket;
    sf::Packet Packet;
    sf::Thread R, S;
    void Receiver(void*);
    void Sender(void*);
    bool connected;
    bool updated;
public:
    Client();
    sf::Packet Receive();
    void Send(sf::Packet);
    bool IsConnected();
    bool IsUpToDate();
};
 

Then, I get:
Thread.hpp|92|error: 'sf::Thread::Thread()' is protected|

I tried with
class Client : public sf::Thread {
and
class Client : private sf::Thread {
but this doesn't resolve it

How can I resolve it?

PD: Sorry if bad english ^^
« Last Edit: July 09, 2012, 09:43:03 pm by XeRaCKeR »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [SFML 1.6] MultiThreading on a Client Class getting error
« Reply #1 on: July 08, 2012, 08:52:16 pm »
There's a tutorial for sf::Thread. Have you read it?
Laurent Gomila - SFML developer

XeRaCKeR

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: [SFML 1.6] MultiThreading on a Client Class getting error
« Reply #2 on: July 09, 2012, 01:14:37 am »
Yes, I readed it and I know how to create a Thread and launch it. I can use Thread with a function; and I can use Thread as class... but now, I wish use Thread with fuction on my class Client. Is it possible?

In brief, I need a class with 2 differentes threads, but when I declare sf::Thread on a class, I get:
Thread.hpp|92|error: 'sf::Thread::Thread()' is protected|
« Last Edit: July 09, 2012, 01:21:32 am by XeRaCKeR »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [SFML 1.6] MultiThreading on a Client Class getting error
« Reply #3 on: July 09, 2012, 07:56:20 am »
Quote
but now, I wish use Thread with fuction on my class Client. Is it possible?
No, a member functions is not a non-member function. They are two totally different things.

Quote
In brief, I need a class with 2 differentes threads
This is impossible in SFML 1.6. You should switch to 2.0.
Laurent Gomila - SFML developer

XeRaCKeR

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: [SFML 1.6] MultiThreading on a Client Class getting error
« Reply #4 on: July 09, 2012, 01:21:33 pm »
Ok, I am on SFML 2.0 now.

And now I get this error:
error: no matching function for call to 'sf::Thread::Thread()'|
note: candidates are: sf::Thread::Thread(const sf::Thread&)|

I can't declare sf::Thread without the constructor, and I can't construct on my class Client... how I should do it?
« Last Edit: July 09, 2012, 01:26:58 pm by XeRaCKeR »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [SFML 1.6] MultiThreading on a Client Class getting error
« Reply #5 on: July 09, 2012, 01:41:30 pm »
Why can't you construct your sf::Thread instance in the owner class constructor?
Laurent Gomila - SFML developer

XeRaCKeR

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: [SFML 1.6] MultiThreading on a Client Class getting error
« Reply #6 on: July 09, 2012, 01:48:26 pm »
Like this?
Client::Client()
{
    sf::Thread R(&Receiver);
    cout << "Client running" << endl;
    ServerAddress ="**.**.**.**";
    Socket.connect(ServerAddress, *****);
    cout << "Connected" << endl;
}

I get the same error  :-\

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [SFML 1.6] MultiThreading on a Client Class getting error
« Reply #7 on: July 09, 2012, 01:52:45 pm »
No, you must use the initialization list.
Laurent Gomila - SFML developer

XeRaCKeR

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: [SFML 1.6] MultiThreading on a Client Class getting error
« Reply #8 on: July 09, 2012, 01:56:16 pm »
Could you give me a example with sf::Thread please?  :-[

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [SFML 1.6] MultiThreading on a Client Class getting error
« Reply #9 on: July 09, 2012, 02:02:52 pm »
class Client
{
public:

    Client() : m_thread(&Client::R, this)
    {
    }

private:

    void R()
    {
        ...
    }

    sf::Thread m_thread;
}

I'll add an example in the tutorial, I just noticed that there's nothing about "using sf::Thread in a class".
Laurent Gomila - SFML developer

XeRaCKeR

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: [SFML 1.6] MultiThreading on a Client Class getting error
« Reply #10 on: July 09, 2012, 04:12:21 pm »
It's works! :D Thanks you very much.

There is my code:

Client.hpp
#include <SFML/Network.hpp>
#include <iostream>
using namespace std;

class Client
{
    sf::IpAddress ServerAddress;
    sf::TcpSocket Socket;
    sf::Packet Packet;
    void Receiver();
    void Sender();
    bool connected;
    bool updated;
public:
    Client();
    sf::Thread R, S;
    sf::Packet Receive();
    void Send(sf::Packet);
    bool IsConnected();
    bool IsUpToDate();
};

Client.cpp
#include "Client.hpp"

Client::Client() : R(&Client::Receiver, this), S(&Client::Sender, this)
{
    cout << "Client is running!" << endl;
    ServerAddress ="**.**.**.**";
    Socket.connect(ServerAddress, *****);
    cout << "Client connected!" << endl;
    R.launch();
    S.launch();
}

void Client::Receiver() //Thread
{
    cout << "Receiver here!\n";
    while(1)
    {
        sf::Packet buffer;
        Socket.receive(buffer);
        cout << "Received!\n";
        Packet = buffer;
        buffer.clear();
        updated=false;
    }
}

void Client::Sender() //Useless Thread Yet (xD)
{
    cout << "Sender here!\n";
}

sf::Packet Client::Receive()
{
    updated=true;
    return Packet;
}

void Client::Send(sf::Packet Pack)
{
    connected = (Socket.send(Pack) == sf::Socket::Done);
}

bool Client::IsUpToDate()
{
    return updated;
}

bool Client::IsConnected()
{
    return connected;
}
« Last Edit: July 09, 2012, 04:14:15 pm by XeRaCKeR »

 

anything