PacketPeerUDP

package
v0.0.0-...-9b8b246 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2025 License: MIT Imports: 24 Imported by: 0

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

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 Any

type Any interface {
	gd.IsClass
	AsPacketPeerUDP() Instance
}

type Expanded

type Expanded = MoreArgs

type Extension

type Extension[T gdclass.Interface] struct{ gdclass.Extension[T, Instance] }

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]) AsObject

func (self *Extension[T]) AsObject() [1]gd.Object

func (*Extension[T]) AsPacketPeer

func (self *Extension[T]) AsPacketPeer() PacketPeer.Instance

func (*Extension[T]) AsPacketPeerUDP

func (self *Extension[T]) AsPacketPeerUDP() Instance

func (*Extension[T]) AsRefCounted

func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted

type ID

type ID Object.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.

func (ID) Instance

func (id ID) Instance() (Instance, bool)

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 New

func New() Instance

func (Instance) AsObject

func (self Instance) AsObject() [1]gd.Object

func (Instance) AsPacketPeer

func (self Instance) AsPacketPeer() PacketPeer.Instance

func (Instance) AsPacketPeerUDP

func (self Instance) AsPacketPeerUDP() Instance

func (Instance) AsRefCounted

func (self Instance) AsRefCounted() [1]gd.RefCounted

func (Instance) Bind

func (self Instance) Bind(port int) error

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

func (self Instance) ConnectToHost(host string, port int) error

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

func (self Instance) GetLocalPort() int

Returns the local port to which this peer is bound.

func (Instance) GetPacketIp

func (self Instance) GetPacketIp() string

Returns the IP of the remote peer that sent the last packet(that was received with PacketPeer.GetPacket or PacketPeer.GetVar).

func (Instance) GetPacketPort

func (self Instance) GetPacketPort() int

Returns the port of the remote peer that sent the last packet(that was received with PacketPeer.GetPacket or PacketPeer.GetVar).

func (Instance) ID

func (self Instance) ID() ID

func (Instance) IsBound

func (self Instance) IsBound() bool

Returns whether this PacketPeerUDP is bound to an address and can receive packets.

func (Instance) IsSocketConnected

func (self Instance) IsSocketConnected() bool

Returns true if the UDP socket is open and has been connected to a remote address. See ConnectToHost.

func (Instance) JoinMulticastGroup

func (self Instance) JoinMulticastGroup(multicast_address string, interface_name string) error

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

func (self Instance) LeaveMulticastGroup(multicast_address string, interface_name string) error

Removes the interface identified by 'interface_name' from the multicast group specified by 'multicast_address'.

func (Instance) MoreArgs

func (self Instance) MoreArgs() MoreArgs

MoreArgs enables certain functions to be called with additional 'optional' arguments.

func (Instance) SetBroadcastEnabled

func (self Instance) SetBroadcastEnabled(enabled bool)

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

func (self Instance) SetDestAddress(host string, port int) error

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).

func (*Instance) SetObject

func (self *Instance) SetObject(obj [1]gd.Object) bool

func (Instance) Virtual

func (self Instance) Virtual(name string) reflect.Value

func (Instance) Wait

func (self Instance) Wait() error

Waits for a packet to arrive on the bound address. See Bind.

Note: Wait can't be interrupted once it has been called. This can be worked around by allowing the other party to send a specific "death pill" packet like this:

type MoreArgs

type MoreArgs [1]gdclass.PacketPeerUDP

MoreArgs is a container for Instance functions with additional 'optional' arguments.

func (MoreArgs) Bind

func (self MoreArgs) Bind(port int, bind_address string, recv_buf_size int) error

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).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL