1
0
Fork 0

Codechange: use references for UDP packets

pull/11984/head
Rubidium 2024-02-03 19:43:38 +01:00 committed by rubidium42
parent 031a9d4e26
commit 84623d2123
3 changed files with 27 additions and 27 deletions

View File

@ -71,19 +71,19 @@ void NetworkUDPSocketHandler::CloseSocket()
* @param all send the packet using all sockets that can send it * @param all send the packet using all sockets that can send it
* @param broadcast whether to send a broadcast message * @param broadcast whether to send a broadcast message
*/ */
void NetworkUDPSocketHandler::SendPacket(Packet *p, NetworkAddress *recv, bool all, bool broadcast) void NetworkUDPSocketHandler::SendPacket(Packet &p, NetworkAddress &recv, bool all, bool broadcast)
{ {
if (this->sockets.empty()) this->Listen(); if (this->sockets.empty()) this->Listen();
for (auto &s : this->sockets) { for (auto &s : this->sockets) {
/* Make a local copy because if we resolve it we cannot /* Make a local copy because if we resolve it we cannot
* easily unresolve it so we can resolve it later again. */ * easily unresolve it so we can resolve it later again. */
NetworkAddress send(*recv); NetworkAddress send(recv);
/* Not the same type */ /* Not the same type */
if (!send.IsFamily(s.second.GetAddress()->ss_family)) continue; if (!send.IsFamily(s.second.GetAddress()->ss_family)) continue;
p->PrepareToSend(); p.PrepareToSend();
if (broadcast) { if (broadcast) {
/* Enable broadcast */ /* Enable broadcast */
@ -94,7 +94,7 @@ void NetworkUDPSocketHandler::SendPacket(Packet *p, NetworkAddress *recv, bool a
} }
/* Send the buffer */ /* Send the buffer */
ssize_t res = p->TransferOut<int>(sendto, s.first, 0, (const struct sockaddr *)send.GetAddress(), send.GetAddressLength()); ssize_t res = p.TransferOut<int>(sendto, s.first, 0, (const struct sockaddr *)send.GetAddress(), send.GetAddressLength());
Debug(net, 7, "sendto({})", send.GetAddressAsString()); Debug(net, 7, "sendto({})", send.GetAddressAsString());
/* Check for any errors, but ignore it otherwise */ /* Check for any errors, but ignore it otherwise */
@ -140,7 +140,7 @@ void NetworkUDPSocketHandler::ReceivePackets()
p.PrepareToRead(); p.PrepareToRead();
/* Handle the packet */ /* Handle the packet */
this->HandleUDPPacket(&p, &address); this->HandleUDPPacket(p, address);
} }
} }
} }
@ -150,14 +150,14 @@ void NetworkUDPSocketHandler::ReceivePackets()
* @param p the received packet * @param p the received packet
* @param client_addr the sender of the packet * @param client_addr the sender of the packet
*/ */
void NetworkUDPSocketHandler::HandleUDPPacket(Packet *p, NetworkAddress *client_addr) void NetworkUDPSocketHandler::HandleUDPPacket(Packet &p, NetworkAddress &client_addr)
{ {
PacketUDPType type; PacketUDPType type;
/* New packet == new client, which has not quit yet */ /* New packet == new client, which has not quit yet */
this->Reopen(); this->Reopen();
type = (PacketUDPType)p->Recv_uint8(); type = (PacketUDPType)p.Recv_uint8();
switch (this->HasClientQuit() ? PACKET_UDP_END : type) { switch (this->HasClientQuit() ? PACKET_UDP_END : type) {
case PACKET_UDP_CLIENT_FIND_SERVER: this->Receive_CLIENT_FIND_SERVER(p, client_addr); break; case PACKET_UDP_CLIENT_FIND_SERVER: this->Receive_CLIENT_FIND_SERVER(p, client_addr); break;
@ -165,9 +165,9 @@ void NetworkUDPSocketHandler::HandleUDPPacket(Packet *p, NetworkAddress *client_
default: default:
if (this->HasClientQuit()) { if (this->HasClientQuit()) {
Debug(net, 0, "[udp] Received invalid packet type {} from {}", type, client_addr->GetAddressAsString()); Debug(net, 0, "[udp] Received invalid packet type {} from {}", type, client_addr.GetAddressAsString());
} else { } else {
Debug(net, 0, "[udp] Received illegal packet from {}", client_addr->GetAddressAsString()); Debug(net, 0, "[udp] Received illegal packet from {}", client_addr.GetAddressAsString());
} }
break; break;
} }
@ -178,10 +178,10 @@ void NetworkUDPSocketHandler::HandleUDPPacket(Packet *p, NetworkAddress *client_
* @param type The received packet type. * @param type The received packet type.
* @param client_addr The address we received the packet from. * @param client_addr The address we received the packet from.
*/ */
void NetworkUDPSocketHandler::ReceiveInvalidPacket(PacketUDPType type, NetworkAddress *client_addr) void NetworkUDPSocketHandler::ReceiveInvalidPacket(PacketUDPType type, NetworkAddress &client_addr)
{ {
Debug(net, 0, "[udp] Received packet type {} on wrong port from {}", type, client_addr->GetAddressAsString()); Debug(net, 0, "[udp] Received packet type {} on wrong port from {}", type, client_addr.GetAddressAsString());
} }
void NetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER(Packet *, NetworkAddress *client_addr) { this->ReceiveInvalidPacket(PACKET_UDP_CLIENT_FIND_SERVER, client_addr); } void NetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER(Packet &, NetworkAddress &client_addr) { this->ReceiveInvalidPacket(PACKET_UDP_CLIENT_FIND_SERVER, client_addr); }
void NetworkUDPSocketHandler::Receive_SERVER_RESPONSE(Packet *, NetworkAddress *client_addr) { this->ReceiveInvalidPacket(PACKET_UDP_SERVER_RESPONSE, client_addr); } void NetworkUDPSocketHandler::Receive_SERVER_RESPONSE(Packet &, NetworkAddress &client_addr) { this->ReceiveInvalidPacket(PACKET_UDP_SERVER_RESPONSE, client_addr); }

View File

@ -30,23 +30,23 @@ protected:
/** The opened sockets. */ /** The opened sockets. */
SocketList sockets; SocketList sockets;
void ReceiveInvalidPacket(PacketUDPType, NetworkAddress *client_addr); void ReceiveInvalidPacket(PacketUDPType, NetworkAddress &client_addr);
/** /**
* Queries to the server for information about the game. * Queries to the server for information about the game.
* @param p The received packet. * @param p The received packet.
* @param client_addr The origin of the packet. * @param client_addr The origin of the packet.
*/ */
virtual void Receive_CLIENT_FIND_SERVER(Packet *p, NetworkAddress *client_addr); virtual void Receive_CLIENT_FIND_SERVER(Packet &p, NetworkAddress &client_addr);
/** /**
* Response to a query letting the client know we are here. * Response to a query letting the client know we are here.
* @param p The received packet. * @param p The received packet.
* @param client_addr The origin of the packet. * @param client_addr The origin of the packet.
*/ */
virtual void Receive_SERVER_RESPONSE(Packet *p, NetworkAddress *client_addr); virtual void Receive_SERVER_RESPONSE(Packet &p, NetworkAddress &client_addr);
void HandleUDPPacket(Packet *p, NetworkAddress *client_addr); void HandleUDPPacket(Packet &p, NetworkAddress &client_addr);
public: public:
NetworkUDPSocketHandler(NetworkAddressList *bind = nullptr); NetworkUDPSocketHandler(NetworkAddressList *bind = nullptr);
@ -56,7 +56,7 @@ public:
bool Listen(); bool Listen();
void CloseSocket(); void CloseSocket();
void SendPacket(Packet *p, NetworkAddress *recv, bool all = false, bool broadcast = false); void SendPacket(Packet &p, NetworkAddress &recv, bool all = false, bool broadcast = false);
void ReceivePackets(); void ReceivePackets();
}; };

View File

@ -63,7 +63,7 @@ static UDPSocket _udp_server("Server"); ///< udp server socket
/** Helper class for handling all server side communication. */ /** Helper class for handling all server side communication. */
class ServerNetworkUDPSocketHandler : public NetworkUDPSocketHandler { class ServerNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
protected: protected:
void Receive_CLIENT_FIND_SERVER(Packet *p, NetworkAddress *client_addr) override; void Receive_CLIENT_FIND_SERVER(Packet &p, NetworkAddress &client_addr) override;
public: public:
/** /**
* Create the socket. * Create the socket.
@ -73,12 +73,12 @@ public:
virtual ~ServerNetworkUDPSocketHandler() = default; virtual ~ServerNetworkUDPSocketHandler() = default;
}; };
void ServerNetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER(Packet *, NetworkAddress *client_addr) void ServerNetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER(Packet &, NetworkAddress &client_addr)
{ {
Packet packet(PACKET_UDP_SERVER_RESPONSE); Packet packet(PACKET_UDP_SERVER_RESPONSE);
this->SendPacket(&packet, client_addr); this->SendPacket(packet, client_addr);
Debug(net, 7, "Queried from {}", client_addr->GetHostname()); Debug(net, 7, "Queried from {}", client_addr.GetHostname());
} }
///*** Communication with servers (we are client) ***/ ///*** Communication with servers (we are client) ***/
@ -86,16 +86,16 @@ void ServerNetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER(Packet *, Network
/** Helper class for handling all client side communication. */ /** Helper class for handling all client side communication. */
class ClientNetworkUDPSocketHandler : public NetworkUDPSocketHandler { class ClientNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
protected: protected:
void Receive_SERVER_RESPONSE(Packet *p, NetworkAddress *client_addr) override; void Receive_SERVER_RESPONSE(Packet &p, NetworkAddress &client_addr) override;
public: public:
virtual ~ClientNetworkUDPSocketHandler() = default; virtual ~ClientNetworkUDPSocketHandler() = default;
}; };
void ClientNetworkUDPSocketHandler::Receive_SERVER_RESPONSE(Packet *, NetworkAddress *client_addr) void ClientNetworkUDPSocketHandler::Receive_SERVER_RESPONSE(Packet &, NetworkAddress &client_addr)
{ {
Debug(net, 3, "Server response from {}", client_addr->GetAddressAsString()); Debug(net, 3, "Server response from {}", client_addr.GetAddressAsString());
NetworkAddServer(client_addr->GetAddressAsString(false), false, true); NetworkAddServer(client_addr.GetAddressAsString(false), false, true);
} }
/** Broadcast to all ips */ /** Broadcast to all ips */
@ -105,7 +105,7 @@ static void NetworkUDPBroadCast(NetworkUDPSocketHandler *socket)
Debug(net, 5, "Broadcasting to {}", addr.GetHostname()); Debug(net, 5, "Broadcasting to {}", addr.GetHostname());
Packet p(PACKET_UDP_CLIENT_FIND_SERVER); Packet p(PACKET_UDP_CLIENT_FIND_SERVER);
socket->SendPacket(&p, &addr, true, true); socket->SendPacket(p, addr, true, true);
} }
} }