mirror of
https://github.com/panda3d/panda3d.git
synced 2025-10-04 10:54:24 -04:00
support UDP broadcast
This commit is contained in:
parent
0b9fc00004
commit
d52e2a9ea9
@ -68,21 +68,67 @@ ConnectionManager::
|
|||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
PT(Connection) ConnectionManager::
|
PT(Connection) ConnectionManager::
|
||||||
open_UDP_connection(int port) {
|
open_UDP_connection(int port) {
|
||||||
|
return open_UDP_connection("", port);
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
// Function: ConnectionManager::open_UDP_connection
|
||||||
|
// Access: Published
|
||||||
|
// Description: Opens a socket for sending and/or receiving UDP
|
||||||
|
// packets. If the port number is greater than zero,
|
||||||
|
// the UDP connection will be opened for listening on
|
||||||
|
// the indicated port; otherwise, it will be useful only
|
||||||
|
// for sending.
|
||||||
|
//
|
||||||
|
// This variant accepts both a hostname and port to
|
||||||
|
// listen on a particular interface; if the hostname is
|
||||||
|
// empty, all interfaces will be available.
|
||||||
|
//
|
||||||
|
// If for_broadcast is true, this UDP connection will be
|
||||||
|
// configured to send and/or receive messages on the
|
||||||
|
// broadcast address (255.255.255.255); otherwise, these
|
||||||
|
// messages may be automatically filtered by the OS.
|
||||||
|
//
|
||||||
|
// Use a ConnectionReader and ConnectionWriter to handle
|
||||||
|
// the actual communication.
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
PT(Connection) ConnectionManager::
|
||||||
|
open_UDP_connection(const string &hostname, int port, bool for_broadcast) {
|
||||||
Socket_UDP *socket = new Socket_UDP;
|
Socket_UDP *socket = new Socket_UDP;
|
||||||
|
|
||||||
if (port > 0) {
|
if (port > 0) {
|
||||||
NetAddress address;
|
NetAddress address;
|
||||||
address.set_any(port);
|
if (hostname.empty()) {
|
||||||
|
address.set_any(port);
|
||||||
|
} else {
|
||||||
|
address.set_host(hostname, port);
|
||||||
|
}
|
||||||
|
|
||||||
if (!socket->OpenForInput(address.get_addr())) {
|
if (!socket->OpenForInput(address.get_addr())) {
|
||||||
net_cat.error()
|
if (hostname.empty()) {
|
||||||
<< "Unable to bind to port " << port << " for UDP.\n";
|
net_cat.error()
|
||||||
|
<< "Unable to bind to port " << port << " for UDP.\n";
|
||||||
|
} else {
|
||||||
|
net_cat.error()
|
||||||
|
<< "Unable to bind to " << hostname << ":" << port << " for UDP.\n";
|
||||||
|
}
|
||||||
delete socket;
|
delete socket;
|
||||||
return PT(Connection)();
|
return PT(Connection)();
|
||||||
}
|
}
|
||||||
|
|
||||||
net_cat.info()
|
const char *broadcast_note = "";
|
||||||
<< "Creating UDP connection for port " << port << "\n";
|
if (for_broadcast) {
|
||||||
|
socket->SetToBroadCast();
|
||||||
|
broadcast_note = "broadcast ";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hostname.empty()) {
|
||||||
|
net_cat.info()
|
||||||
|
<< "Creating UDP " << broadcast_note << "connection for port " << port << "\n";
|
||||||
|
} else {
|
||||||
|
net_cat.info()
|
||||||
|
<< "Creating UDP " << broadcast_note << "connection for " << hostname << ":" << port << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if (!socket->InitNoAddress()) {
|
if (!socket->InitNoAddress()) {
|
||||||
@ -92,8 +138,14 @@ open_UDP_connection(int port) {
|
|||||||
return PT(Connection)();
|
return PT(Connection)();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *broadcast_note = "";
|
||||||
|
if (for_broadcast) {
|
||||||
|
socket->SetToBroadCast();
|
||||||
|
broadcast_note = "broadcast ";
|
||||||
|
}
|
||||||
|
|
||||||
net_cat.info()
|
net_cat.info()
|
||||||
<< "Creating outgoing UDP connection\n";
|
<< "Creating outgoing UDP " << broadcast_note << "connection\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
PT(Connection) connection = new Connection(this, socket);
|
PT(Connection) connection = new Connection(this, socket);
|
||||||
@ -259,7 +311,7 @@ open_TCP_client_connection(const NetAddress &address, int timeout_ms) {
|
|||||||
#endif // SIMPLE_THREADS
|
#endif // SIMPLE_THREADS
|
||||||
|
|
||||||
net_cat.info()
|
net_cat.info()
|
||||||
<< "Opened TCP connection to server " << address.get_ip_string() << " "
|
<< "Opened TCP connection to server " << address.get_ip_string()
|
||||||
<< " on port " << address.get_port() << "\n";
|
<< " on port " << address.get_port() << "\n";
|
||||||
|
|
||||||
PT(Connection) connection = new Connection(this, socket);
|
PT(Connection) connection = new Connection(this, socket);
|
||||||
|
@ -49,6 +49,7 @@ PUBLISHED:
|
|||||||
virtual ~ConnectionManager();
|
virtual ~ConnectionManager();
|
||||||
|
|
||||||
PT(Connection) open_UDP_connection(int port = 0);
|
PT(Connection) open_UDP_connection(int port = 0);
|
||||||
|
PT(Connection) open_UDP_connection(const string &hostname, int port, bool for_broadcast = false);
|
||||||
|
|
||||||
BLOCKING PT(Connection) open_TCP_server_rendezvous(int port, int backlog);
|
BLOCKING PT(Connection) open_TCP_server_rendezvous(int port, int backlog);
|
||||||
BLOCKING PT(Connection) open_TCP_server_rendezvous(const string &hostname,
|
BLOCKING PT(Connection) open_TCP_server_rendezvous(const string &hostname,
|
||||||
|
@ -62,6 +62,16 @@ set_localhost(int port) {
|
|||||||
return _addr.set_host("127.0.0.1", port);
|
return _addr.set_host("127.0.0.1", port);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
// Function: NetAddress::set_broadcast
|
||||||
|
// Access: Public
|
||||||
|
// Description: Sets the address to the broadcast address.
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
bool NetAddress::
|
||||||
|
set_broadcast(int port) {
|
||||||
|
return _addr.set_broadcast(port);
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
// Function: NetAddress::set_host
|
// Function: NetAddress::set_host
|
||||||
// Access: Public
|
// Access: Public
|
||||||
|
@ -31,6 +31,7 @@ PUBLISHED:
|
|||||||
|
|
||||||
bool set_any(int port);
|
bool set_any(int port);
|
||||||
bool set_localhost(int port);
|
bool set_localhost(int port);
|
||||||
|
bool set_broadcast(int port);
|
||||||
bool set_host(const string &hostname, int port);
|
bool set_host(const string &hostname, int port);
|
||||||
|
|
||||||
void clear();
|
void clear();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user