Hi everyone!
Well, this is my first time using SFML Network - actually, that's my first time working on a network project at all. I have no previous experiences handling networks of any kind, so please forgive me if I am asking easy questions.
So: I'm working on a card game and there will be two sides: Server and Client. The Server will be responsible for hosting all players cards and each action performed by them will be sent to the Server. To exchange data between them, I'm using SFML packages.
There is a class named "Jogador" (which stands for "Player" in portuguese) and inside that class there are three instances of another class named "Carta" ("Card"). In other words, each player has three cards. These players are defined inside the Server and then sent to the Client via packages so they can be drawn on the Client's screen. There are also two other main classes: "Servidor" ("Server") and "Controlador" ("Controller", where the game itself will run).
This is the main.cpp so far (questions right below it):
#include <iostream>
#include <SFML\Graphics.hpp>
#include <SFML\Network.hpp>
#include "Servidor.h"
#include "Controlador.h"
#include "Jogador.h"
sf::TcpSocket socket;
char conexao;
char buffer[2000];
size_t recebido;
bool eServidor = false;
bool servLoop = false;
sf::Packet& operator <<(sf::Packet& packet, const Jogador& jogador)
{
return packet << jogador;
}
sf::Packet& operator >>(sf::Packet& packet, const Jogador& jogador)
{
return packet >> jogador;
}
int main()
{
Servidor s;
s.novoTurno(); // this is where the cards of each player are being defined
sf::Packet j0, j1, j2, j3, j4, j5; // each packet represents a player
j0 << s.getJogador(0);
j1 << s.getJogador(1);
j2 << s.getJogador(2);
j3 << s.getJogador(3);
j4 << s.getJogador(4);
j5 << s.getJogador(5);
std::cout << "Hit (s) for server or (c) for client: \n";
std::cin >> conexao;
if (conexao == 's')
{
texto += "server";
eServidor = true;
servLoop = true;
}
else if (conexao == 'c')
{
texto += "client";
}
if (eServidor == true)
{
sf::TcpListener listener;
listener.listen(2000);
listener.accept(socket);
socket.send(j0); // sends all player information to the clients
socket.send(j1);
socket.send(j2);
socket.send(j3);
socket.send(j4);
socket.send(j5);
std::cout << "Cartas enviadas.\n";
while (servLoop == true)
{
}
}
else
{
socket.connect(sf::IpAddress::getLocalAddress(), 2000);
Controlador c(width, height, title); // handles client actions
socket.receive(j0); // receive player data from the server
Jogador jg;
j0 >> jg;
c.defineCartas(jg); // defines the client's player data (get from Server)
while (c.getWindow().isOpen())
{
sf::Event event;
while (c.getWindow().pollEvent(event))
{
if (event.type == sf::Event::Closed){ c.getWindow().close(); }
eventosMouse(event);
c.getWindow().clear();
c.abreJogo(); // this is where the cards are being displayed on the screen
c.getWindow().display();
}
}
}
return 0;
}
When trying to execute it, I have a stack overflow error right in the part of the code with the "operator" - in the upper side. I did that coding because I'm sending custom classes through the packages (I saw that on SFML documentation):
sf::Packet& operator <<(sf::Packet& packet, const Jogador& jogador)
{
return packet << jogador;
}
Did I do something wrong through the code?
Also, if you observed something else I did wrong please notify me as I lack experience on Network area (not only in SFML) and there is a good chance I'm doing something awful.
Thanks in advance!