(svn r15971) -Codechange: make it possible for UDP socket handlers to bind to multiple sockets.
This commit is contained in:
@@ -16,29 +16,39 @@
|
||||
#include "udp.h"
|
||||
|
||||
/**
|
||||
* Start listening on the given host and port.
|
||||
* @param address the host to listen on
|
||||
* @param broadcast whether to allow broadcast sending/receiving
|
||||
* @return true if the listening succeeded
|
||||
* Create an UDP socket but don't listen yet.
|
||||
* @param bind the addresses to bind to.
|
||||
*/
|
||||
bool NetworkUDPSocketHandler::Listen(NetworkAddress address, bool broadcast)
|
||||
NetworkUDPSocketHandler::NetworkUDPSocketHandler(NetworkAddressList *bind)
|
||||
{
|
||||
if (bind != NULL) {
|
||||
for (NetworkAddress *addr = bind->Begin(); addr != bind->End(); addr++) {
|
||||
*this->bind.Append() = *addr;
|
||||
}
|
||||
} else {
|
||||
*this->bind.Append() = NetworkAddress(NULL, 0, AF_INET);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Start listening on the given host and port.
|
||||
* @return true if at least one port is listening
|
||||
*/
|
||||
bool NetworkUDPSocketHandler::Listen()
|
||||
{
|
||||
/* Make sure socket is closed */
|
||||
this->Close();
|
||||
|
||||
this->sock = address.Listen(AF_INET, SOCK_DGRAM);
|
||||
|
||||
if (broadcast) {
|
||||
/* Enable broadcast */
|
||||
unsigned long val = 1;
|
||||
#ifndef BEOS_NET_SERVER /* will work around this, some day; maybe. */
|
||||
setsockopt(this->sock, SOL_SOCKET, SO_BROADCAST, (char *) &val, sizeof(val));
|
||||
#endif
|
||||
for (NetworkAddress *addr = this->bind.Begin(); addr != this->bind.End(); addr++) {
|
||||
addr->Listen(AF_UNSPEC, SOCK_DGRAM, &this->sockets);
|
||||
}
|
||||
|
||||
DEBUG(net, 1, "[udp] listening on port %s", address.GetAddressAsString());
|
||||
for (SocketList::iterator s = this->sockets.Begin(); s != this->sockets.End(); s++) {
|
||||
DEBUG(net, 1, "[udp] listening on port %s", s->first.GetAddressAsString());
|
||||
}
|
||||
|
||||
return true;
|
||||
return this->sockets.Length() != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,10 +56,10 @@ bool NetworkUDPSocketHandler::Listen(NetworkAddress address, bool broadcast)
|
||||
*/
|
||||
void NetworkUDPSocketHandler::Close()
|
||||
{
|
||||
if (!this->IsConnected()) return;
|
||||
|
||||
closesocket(this->sock);
|
||||
this->sock = INVALID_SOCKET;
|
||||
for (SocketList::iterator s = this->sockets.Begin(); s != this->sockets.End(); s++) {
|
||||
closesocket(s->second);
|
||||
}
|
||||
this->sockets.Clear();
|
||||
}
|
||||
|
||||
NetworkRecvStatus NetworkUDPSocketHandler::CloseConnection()
|
||||
@@ -62,18 +72,35 @@ NetworkRecvStatus NetworkUDPSocketHandler::CloseConnection()
|
||||
* Send a packet over UDP
|
||||
* @param p the packet to send
|
||||
* @param recv the receiver (target) of the packet
|
||||
* @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)
|
||||
void NetworkUDPSocketHandler::SendPacket(Packet *p, NetworkAddress *recv, bool all, bool broadcast)
|
||||
{
|
||||
int res;
|
||||
if (this->sockets.Length() == 0) this->Listen();
|
||||
|
||||
p->PrepareToSend();
|
||||
for (SocketList::iterator s = this->sockets.Begin(); s != this->sockets.End(); s++) {
|
||||
/* Not the same type */
|
||||
if (s->first.GetAddress()->ss_family != recv->GetAddress()->ss_family) continue;
|
||||
|
||||
/* Send the buffer */
|
||||
res = sendto(this->sock, (const char*)p->buffer, p->size, 0, (struct sockaddr *)recv->GetAddress(), recv->GetAddressLength());
|
||||
p->PrepareToSend();
|
||||
|
||||
/* Check for any errors, but ignore it otherwise */
|
||||
if (res == -1) DEBUG(net, 1, "[udp] sendto failed with: %i", GET_LAST_ERROR());
|
||||
#ifndef BEOS_NET_SERVER /* will work around this, some day; maybe. */
|
||||
if (broadcast) {
|
||||
/* Enable broadcast */
|
||||
unsigned long val = 1;
|
||||
setsockopt(s->second, SOL_SOCKET, SO_BROADCAST, (char *) &val, sizeof(val));
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Send the buffer */
|
||||
int res = sendto(s->second, (const char*)p->buffer, p->size, 0, (struct sockaddr *)recv->GetAddress(), recv->GetAddressLength());
|
||||
|
||||
/* Check for any errors, but ignore it otherwise */
|
||||
if (res == -1) DEBUG(net, 1, "[udp] sendto(%s) failed with: %i", recv->GetAddressAsString(), GET_LAST_ERROR());
|
||||
|
||||
if (!all) break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,37 +108,33 @@ void NetworkUDPSocketHandler::SendPacket(Packet *p, NetworkAddress *recv)
|
||||
*/
|
||||
void NetworkUDPSocketHandler::ReceivePackets()
|
||||
{
|
||||
struct sockaddr_storage client_addr;
|
||||
memset(&client_addr, 0, sizeof(client_addr));
|
||||
for (SocketList::iterator s = this->sockets.Begin(); s != this->sockets.End(); s++) {
|
||||
struct sockaddr_storage client_addr;
|
||||
memset(&client_addr, 0, sizeof(client_addr));
|
||||
|
||||
socklen_t client_len;
|
||||
int nbytes;
|
||||
Packet p(this);
|
||||
int packet_len;
|
||||
Packet p(this);
|
||||
int packet_len = sizeof(p.buffer);
|
||||
socklen_t client_len = sizeof(client_addr);
|
||||
|
||||
if (!this->IsConnected()) return;
|
||||
/* Try to receive anything */
|
||||
SetNonBlocking(s->second); // Some OSes seem to lose the non-blocking status of the socket
|
||||
int nbytes = recvfrom(s->second, (char*)p.buffer, packet_len, 0, (struct sockaddr *)&client_addr, &client_len);
|
||||
|
||||
packet_len = sizeof(p.buffer);
|
||||
client_len = sizeof(client_addr);
|
||||
/* We got some bytes for the base header of the packet. */
|
||||
if (nbytes > 2) {
|
||||
NetworkAddress address(client_addr, client_len);
|
||||
p.PrepareToRead();
|
||||
|
||||
/* Try to receive anything */
|
||||
SetNonBlocking(this->sock); // Some OSes seem to lose the non-blocking status of the socket
|
||||
nbytes = recvfrom(this->sock, (char*)p.buffer, packet_len, 0, (struct sockaddr *)&client_addr, &client_len);
|
||||
/* If the size does not match the packet must be corrupted.
|
||||
* Otherwise it will be marked as corrupted later on. */
|
||||
if (nbytes != p.size) {
|
||||
DEBUG(net, 1, "received a packet with mismatching size from %s", address.GetAddressAsString());
|
||||
return;
|
||||
}
|
||||
|
||||
/* We got some bytes for the base header of the packet. */
|
||||
if (nbytes > 2) {
|
||||
NetworkAddress address(client_addr, client_len);
|
||||
p.PrepareToRead();
|
||||
|
||||
/* If the size does not match the packet must be corrupted.
|
||||
* Otherwise it will be marked as corrupted later on. */
|
||||
if (nbytes != p.size) {
|
||||
DEBUG(net, 1, "received a packet with mismatching size from %s", address.GetAddressAsString());
|
||||
return;
|
||||
/* Handle the packet */
|
||||
this->HandleUDPPacket(&p, &address);
|
||||
}
|
||||
|
||||
/* Handle the packet */
|
||||
this->HandleUDPPacket(&p, &address);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user