mirror of https://github.com/OpenTTD/OpenTTD.git
Codechange: use references for UDP packets
This commit is contained in:
parent
031a9d4e26
commit
84623d2123
|
@ -71,19 +71,19 @@ void NetworkUDPSocketHandler::CloseSocket()
|
|||
* @param all send the packet using all sockets that can send it
|
||||
* @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();
|
||||
|
||||
for (auto &s : this->sockets) {
|
||||
/* Make a local copy because if we resolve it we cannot
|
||||
* easily unresolve it so we can resolve it later again. */
|
||||
NetworkAddress send(*recv);
|
||||
NetworkAddress send(recv);
|
||||
|
||||
/* Not the same type */
|
||||
if (!send.IsFamily(s.second.GetAddress()->ss_family)) continue;
|
||||
|
||||
p->PrepareToSend();
|
||||
p.PrepareToSend();
|
||||
|
||||
if (broadcast) {
|
||||
/* Enable broadcast */
|
||||
|
@ -94,7 +94,7 @@ void NetworkUDPSocketHandler::SendPacket(Packet *p, NetworkAddress *recv, bool a
|
|||
}
|
||||
|
||||
/* 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());
|
||||
|
||||
/* Check for any errors, but ignore it otherwise */
|
||||
|
@ -140,7 +140,7 @@ void NetworkUDPSocketHandler::ReceivePackets()
|
|||
p.PrepareToRead();
|
||||
|
||||
/* Handle the packet */
|
||||
this->HandleUDPPacket(&p, &address);
|
||||
this->HandleUDPPacket(p, address);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -150,14 +150,14 @@ void NetworkUDPSocketHandler::ReceivePackets()
|
|||
* @param p the received 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;
|
||||
|
||||
/* New packet == new client, which has not quit yet */
|
||||
this->Reopen();
|
||||
|
||||
type = (PacketUDPType)p->Recv_uint8();
|
||||
type = (PacketUDPType)p.Recv_uint8();
|
||||
|
||||
switch (this->HasClientQuit() ? PACKET_UDP_END : type) {
|
||||
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:
|
||||
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 {
|
||||
Debug(net, 0, "[udp] Received illegal packet from {}", client_addr->GetAddressAsString());
|
||||
Debug(net, 0, "[udp] Received illegal packet from {}", client_addr.GetAddressAsString());
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -178,10 +178,10 @@ void NetworkUDPSocketHandler::HandleUDPPacket(Packet *p, NetworkAddress *client_
|
|||
* @param type The received packet type.
|
||||
* @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_SERVER_RESPONSE(Packet *, NetworkAddress *client_addr) { this->ReceiveInvalidPacket(PACKET_UDP_SERVER_RESPONSE, 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); }
|
||||
|
|
|
@ -30,23 +30,23 @@ protected:
|
|||
/** The opened sockets. */
|
||||
SocketList sockets;
|
||||
|
||||
void ReceiveInvalidPacket(PacketUDPType, NetworkAddress *client_addr);
|
||||
void ReceiveInvalidPacket(PacketUDPType, NetworkAddress &client_addr);
|
||||
|
||||
/**
|
||||
* Queries to the server for information about the game.
|
||||
* @param p The received 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.
|
||||
* @param p The received 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:
|
||||
NetworkUDPSocketHandler(NetworkAddressList *bind = nullptr);
|
||||
|
||||
|
@ -56,7 +56,7 @@ public:
|
|||
bool Listen();
|
||||
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();
|
||||
};
|
||||
|
||||
|
|
|
@ -63,7 +63,7 @@ static UDPSocket _udp_server("Server"); ///< udp server socket
|
|||
/** Helper class for handling all server side communication. */
|
||||
class ServerNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
|
||||
protected:
|
||||
void Receive_CLIENT_FIND_SERVER(Packet *p, NetworkAddress *client_addr) override;
|
||||
void Receive_CLIENT_FIND_SERVER(Packet &p, NetworkAddress &client_addr) override;
|
||||
public:
|
||||
/**
|
||||
* Create the socket.
|
||||
|
@ -73,12 +73,12 @@ public:
|
|||
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);
|
||||
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) ***/
|
||||
|
@ -86,16 +86,16 @@ void ServerNetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER(Packet *, Network
|
|||
/** Helper class for handling all client side communication. */
|
||||
class ClientNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
|
||||
protected:
|
||||
void Receive_SERVER_RESPONSE(Packet *p, NetworkAddress *client_addr) override;
|
||||
void Receive_SERVER_RESPONSE(Packet &p, NetworkAddress &client_addr) override;
|
||||
public:
|
||||
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 */
|
||||
|
@ -105,7 +105,7 @@ static void NetworkUDPBroadCast(NetworkUDPSocketHandler *socket)
|
|||
Debug(net, 5, "Broadcasting to {}", addr.GetHostname());
|
||||
|
||||
Packet p(PACKET_UDP_CLIENT_FIND_SERVER);
|
||||
socket->SendPacket(&p, &addr, true, true);
|
||||
socket->SendPacket(p, addr, true, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue