Documentation
¶
Overview ¶
UDP packet peer. Can be used to send and receive raw UDP packets as well as anys.
Example: Send a packet:
package main import "graphics.gd/classdb/PacketPeerUDP" func ExamplePacketPeerUDP() { var peer = PacketPeerUDP.New() peer.Bind(4444) peer.SetDestAddress("1.1.1.1", 4433) peer.AsPacketPeer().PutPacket([]byte("hello")) }
Example: Listen for packets:
package main import ( "graphics.gd/classdb/Node" "graphics.gd/classdb/PacketPeerUDP" ) type MyPacketPeerUDP struct { Node.Extension[MyPacketPeerUDP] peer PacketPeerUDP.Instance } func (m *MyPacketPeerUDP) Ready() { m.peer = PacketPeerUDP.New() m.peer.Bind(4433) } func (m *MyPacketPeerUDP) Process(_ float64) { if m.peer.AsPacketPeer().GetAvailablePacketCount() > 0 { var array_bytes = m.peer.AsPacketPeer().GetPacket() var packet_string = string(array_bytes) print("Received message: ", packet_string) } }
Note: When exporting to Android, make sure to enable the INTERNET permission in the Android export preset before exporting the project or using one-click deploy. Otherwise, network communication of any kind will be blocked by Android.
Index ¶
- type Advanced
- type Any
- type Expanded
- type Extension
- type ID
- type Instance
- func (self Instance) AsObject() [1]gd.Object
- func (self Instance) AsPacketPeer() PacketPeer.Instance
- func (self Instance) AsPacketPeerUDP() Instance
- func (self Instance) AsRefCounted() [1]gd.RefCounted
- func (self Instance) Bind(port int) error
- func (self Instance) Close()
- func (self Instance) ConnectToHost(host string, port int) error
- func (self Instance) GetLocalPort() int
- func (self Instance) GetPacketIp() string
- func (self Instance) GetPacketPort() int
- func (self Instance) ID() ID
- func (self Instance) IsBound() bool
- func (self Instance) IsSocketConnected() bool
- func (self Instance) JoinMulticastGroup(multicast_address string, interface_name string) error
- func (self Instance) LeaveMulticastGroup(multicast_address string, interface_name string) error
- func (self Instance) MoreArgs() MoreArgs
- func (self Instance) SetBroadcastEnabled(enabled bool)
- func (self Instance) SetDestAddress(host string, port int) error
- func (self *Instance) SetObject(obj [1]gd.Object) bool
- func (self Instance) Virtual(name string) reflect.Value
- func (self Instance) Wait() error
- type MoreArgs
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Advanced ¶
type Advanced = class
Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.
type Extension ¶
Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this Extension
func (*Extension[T]) AsPacketPeer ¶
func (self *Extension[T]) AsPacketPeer() PacketPeer.Instance
func (*Extension[T]) AsPacketPeerUDP ¶
func (*Extension[T]) AsRefCounted ¶
func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted
type ID ¶
ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.
type Instance ¶
type Instance [1]gdclass.PacketPeerUDP
Instance of the class with convieniently typed arguments and results.
var Nil Instance
Nil is a nil/null instance of the class. Equivalent to the zero value.
func (Instance) AsPacketPeer ¶
func (self Instance) AsPacketPeer() PacketPeer.Instance
func (Instance) AsPacketPeerUDP ¶
func (Instance) AsRefCounted ¶
func (self Instance) AsRefCounted() [1]gd.RefCounted
func (Instance) Bind ¶
Binds this PacketPeerUDP to the specified 'port' and 'bind_address' with a buffer size 'recv_buf_size', allowing it to receive incoming packets.
If 'bind_address' is set to "*" (default), the peer will be bound on all available addresses (both IPv4 and IPv6).
If 'bind_address' is set to "0.0.0.0" (for IPv4) or "::" (for IPv6), the peer will be bound to all available addresses matching that IP type.
If 'bind_address' is set to any valid address (e.g. "192.168.1.101", "::1", etc.), the peer will only be bound to the interface with that address (or fail if no interface with the given address exists).
func (Instance) Close ¶
func (self Instance) Close()
Closes the PacketPeerUDP's underlying UDP socket.
func (Instance) ConnectToHost ¶
Calling this method connects this UDP peer to the given 'host'/'port' pair. UDP is in reality connectionless, so this option only means that incoming packets from different addresses are automatically discarded, and that outgoing packets are always sent to the connected address (future calls to SetDestAddress are not allowed). This method does not send any data to the remote peer, to do that, use PacketPeer.PutVar or PacketPeer.PutPacket as usual. See also UDPServer.
Note: Connecting to the remote peer does not help to protect from malicious attacks like IP spoofing, etc. Think about using an encryption technique like TLS or DTLS if you feel like your application is transferring sensitive information.
func (Instance) GetLocalPort ¶
Returns the local port to which this peer is bound.
func (Instance) GetPacketIp ¶
Returns the IP of the remote peer that sent the last packet(that was received with PacketPeer.GetPacket or PacketPeer.GetVar).
func (Instance) GetPacketPort ¶
Returns the port of the remote peer that sent the last packet(that was received with PacketPeer.GetPacket or PacketPeer.GetVar).
func (Instance) IsBound ¶
Returns whether this PacketPeerUDP is bound to an address and can receive packets.
func (Instance) IsSocketConnected ¶
Returns true if the UDP socket is open and has been connected to a remote address. See ConnectToHost.
func (Instance) JoinMulticastGroup ¶
Joins the multicast group specified by 'multicast_address' using the interface identified by 'interface_name'.
You can join the same multicast group with multiple interfaces. Use IP.GetLocalInterfaces to know which are available.
Note: Some Android devices might require the CHANGE_WIFI_MULTICAST_STATE permission for multicast to work.
func (Instance) LeaveMulticastGroup ¶
Removes the interface identified by 'interface_name' from the multicast group specified by 'multicast_address'.
func (Instance) MoreArgs ¶
MoreArgs enables certain functions to be called with additional 'optional' arguments.
func (Instance) SetBroadcastEnabled ¶
Enable or disable sending of broadcast packets (e.g. set_dest_address("255.255.255.255", 4343). This option is disabled by default.
Note: Some Android devices might require the CHANGE_WIFI_MULTICAST_STATE permission and this option to be enabled to receive broadcast packets too.
func (Instance) SetDestAddress ¶
Sets the destination address and port for sending packets and variables. A hostname will be resolved using DNS if needed.
Note: SetBroadcastEnabled must be enabled before sending packets to a broadcast address (e.g. 255.255.255.255).
type MoreArgs ¶
type MoreArgs [1]gdclass.PacketPeerUDP
MoreArgs is a container for Instance functions with additional 'optional' arguments.
func (MoreArgs) Bind ¶
Binds this PacketPeerUDP to the specified 'port' and 'bind_address' with a buffer size 'recv_buf_size', allowing it to receive incoming packets.
If 'bind_address' is set to "*" (default), the peer will be bound on all available addresses (both IPv4 and IPv6).
If 'bind_address' is set to "0.0.0.0" (for IPv4) or "::" (for IPv6), the peer will be bound to all available addresses matching that IP type.
If 'bind_address' is set to any valid address (e.g. "192.168.1.101", "::1", etc.), the peer will only be bound to the interface with that address (or fail if no interface with the given address exists).