Completely removed unused code about UDP sockets.
This commit is contained in:
parent
5369f1f6ad
commit
dc1b7147c8
@ -39,11 +39,8 @@ void Client::connect(sf::IpAddress serverAddress, u16 serverPort) {
|
||||
if (serverAddress.toInteger() == 0 || m_tcpSocket->connect(serverAddress, serverPort, sf::seconds(5)) != sf::Socket::Done)
|
||||
throw ClientConnectException("Network error: Unable to connect to server " + serverAddress.toString() + ":" + std::to_string(serverPort));
|
||||
|
||||
if (m_socket.bind(0) != sf::Socket::Done)
|
||||
throw ClientConnectException("Network error: Bind failed");
|
||||
|
||||
Network::Packet packet;
|
||||
packet << Network::Command::ClientConnect << sf::IpAddress::getLocalAddress().toString() << m_socket.getLocalPort();
|
||||
packet << Network::Command::ClientConnect << sf::IpAddress::getLocalAddress().toString();
|
||||
m_tcpSocket->send(packet);
|
||||
|
||||
Network::Packet answer;
|
||||
@ -63,7 +60,6 @@ void Client::connect(sf::IpAddress serverAddress, u16 serverPort) {
|
||||
throw ClientConnectException("Client error: The server is not valid");
|
||||
|
||||
m_tcpSocket->setBlocking(false);
|
||||
m_socket.setBlocking(false);
|
||||
|
||||
m_isConnected = true;
|
||||
}
|
||||
@ -84,15 +80,6 @@ void Client::send(Network::Packet &packet) {
|
||||
}
|
||||
|
||||
void Client::update() {
|
||||
// sf::IpAddress senderAddress;
|
||||
// u16 senderPort;
|
||||
// while (m_socket.receive(packet, senderAddress, senderPort) == sf::Socket::Done) {
|
||||
// Network::Command command;
|
||||
// packet >> command;
|
||||
//
|
||||
// // gkDebug() << "UDP Message of type" << Network::commandToString(command) << "received from:" << senderAddress << ":" << senderPort;
|
||||
// }
|
||||
|
||||
Network::Packet packet;
|
||||
while (m_tcpSocket->receive(packet) == sf::Socket::Done) {
|
||||
Network::Command command;
|
||||
|
@ -32,7 +32,6 @@
|
||||
|
||||
#include <SFML/Network/IpAddress.hpp>
|
||||
#include <SFML/Network/TcpSocket.hpp>
|
||||
#include <SFML/Network/UdpSocket.hpp>
|
||||
|
||||
#include <gk/core/ApplicationStateStack.hpp>
|
||||
#include <gk/core/Timer.hpp>
|
||||
@ -81,8 +80,6 @@ class Client {
|
||||
|
||||
std::unique_ptr<sf::TcpSocket> m_tcpSocket;
|
||||
|
||||
sf::UdpSocket m_socket;
|
||||
|
||||
gk::Timer m_keyUpdateTimer;
|
||||
|
||||
std::unordered_map<Network::Command, CommandCallback> m_commands;
|
||||
|
@ -34,7 +34,7 @@ namespace Network {
|
||||
|
||||
enum class Command {
|
||||
// Client commands
|
||||
ClientConnect = 0x00, // <TCP> [NetworkCommand][u16 udp port] (from Client only)
|
||||
ClientConnect = 0x00, // <TCP> [NetworkCommand] (from Client only)
|
||||
ClientDisconnect = 0x01, // <TCP> [NetworkCommand] (from Client only)
|
||||
ClientOk = 0x02, // <TCP> [NetworkCommand][u16 client id][bool isSingleplayer] (from Server only)
|
||||
ClientRefused = 0x03, // <TCP> [NetworkCommand] (from Server only)
|
||||
|
@ -35,14 +35,13 @@
|
||||
|
||||
class ClientInfo {
|
||||
public:
|
||||
ClientInfo(u16 _id, sf::IpAddress _address, u16 _port, const std::shared_ptr<sf::TcpSocket> &socket)
|
||||
: id(_id), address(_address), port(_port), tcpSocket(socket) {}
|
||||
ClientInfo(u16 _id, sf::IpAddress _address, const std::shared_ptr<sf::TcpSocket> &socket)
|
||||
: id(_id), address(_address), tcpSocket(socket) {}
|
||||
|
||||
u16 id;
|
||||
bool isReady = false;
|
||||
|
||||
sf::IpAddress address;
|
||||
u16 port;
|
||||
|
||||
u32 previousKeyTimestamp = 0;
|
||||
|
||||
|
@ -27,18 +27,11 @@
|
||||
#include "Server.hpp"
|
||||
|
||||
void Server::init(u16 port) {
|
||||
// Note: The UDP socket is currently unused for data sending/receiving
|
||||
// But it's used to find an available port for sf::TcpListener
|
||||
if (m_udpSocket.bind(port) != sf::Socket::Done)
|
||||
throw EXCEPTION("Network error: Bind failed");
|
||||
|
||||
m_udpSocket.setBlocking(false);
|
||||
|
||||
m_port = m_udpSocket.getLocalPort();
|
||||
|
||||
if (m_tcpListener.listen(m_port) != sf::Socket::Done)
|
||||
if (m_tcpListener.listen(port) != sf::Socket::Done)
|
||||
throw EXCEPTION("Network error: Listen failed");
|
||||
|
||||
m_port = m_tcpListener.getLocalPort();
|
||||
|
||||
m_tcpListener.setBlocking(false);
|
||||
|
||||
m_selector.add(m_tcpListener);
|
||||
@ -68,10 +61,9 @@ void Server::handleNewConnections() {
|
||||
|
||||
if (m_info.clients().size() < 5) {
|
||||
std::string address;
|
||||
u16 port;
|
||||
packet >> address >> port;
|
||||
packet >> address;
|
||||
|
||||
ClientInfo &client = m_info.addClient(address, port, clientSocket);
|
||||
ClientInfo &client = m_info.addClient(address, clientSocket);
|
||||
m_selector.add(*client.tcpSocket);
|
||||
|
||||
Network::Packet outPacket;
|
||||
|
@ -32,7 +32,6 @@
|
||||
|
||||
#include <SFML/Network/SocketSelector.hpp>
|
||||
#include <SFML/Network/TcpListener.hpp>
|
||||
#include <SFML/Network/UdpSocket.hpp>
|
||||
|
||||
#include "Network.hpp"
|
||||
#include "ServerInfo.hpp"
|
||||
@ -73,8 +72,6 @@ class Server {
|
||||
|
||||
ServerInfo m_info;
|
||||
|
||||
sf::UdpSocket m_udpSocket; // Only used to find an available port for sf::TcpListener
|
||||
|
||||
sf::TcpListener m_tcpListener;
|
||||
sf::SocketSelector m_selector;
|
||||
|
||||
|
@ -26,8 +26,8 @@
|
||||
*/
|
||||
#include "ServerInfo.hpp"
|
||||
|
||||
ClientInfo &ServerInfo::addClient(sf::IpAddress address, u16 port, const std::shared_ptr<sf::TcpSocket> &socket) {
|
||||
m_clients.emplace_back(m_clients.size() + 1, address, port, socket);
|
||||
ClientInfo &ServerInfo::addClient(sf::IpAddress address, const std::shared_ptr<sf::TcpSocket> &socket) {
|
||||
m_clients.emplace_back(m_clients.size() + 1, address, socket);
|
||||
return m_clients.back();
|
||||
}
|
||||
|
||||
|
@ -36,7 +36,7 @@
|
||||
|
||||
class ServerInfo {
|
||||
public:
|
||||
ClientInfo &addClient(sf::IpAddress address, u16 port, const std::shared_ptr<sf::TcpSocket> &socket);
|
||||
ClientInfo &addClient(sf::IpAddress address, const std::shared_ptr<sf::TcpSocket> &socket);
|
||||
ClientInfo *getClient(u16 id);
|
||||
void removeClient(u16 id);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user