Documentation
¶
Overview ¶
A simple server that opens a UDP socket and returns connected graphics.gd/classdb/PacketPeerUDP upon receiving new packets. See also graphics.gd/classdb/PacketPeerUDP.Instance.ConnectToHost.
After starting the server (Instance.Listen), you will need to Instance.Poll it at regular intervals (e.g. inside graphics.gd/classdb/Node.Instance.Process) for it to process new packets, delivering them to the appropriate graphics.gd/classdb/PacketPeerUDP, and taking new connections.
Below a small example of how it can be used:
package main import ( "graphics.gd/classdb/Node" "graphics.gd/classdb/PacketPeerUDP" "graphics.gd/classdb/UDPServer" ) type ServerNode struct { Node.Extension[ServerNode] udp UDPServer.Instance peers []PacketPeerUDP.Instance } func (s *ServerNode) Ready() { s.udp = UDPServer.New() s.udp.Listen(4242) } func (s *ServerNode) Process(_ float64) { s.udp.Poll() // Important! if s.udp.IsConnectionAvailable() { peer := s.udp.TakeConnection() packet := peer.AsPacketPeer().GetPacket() print("Accepted peer: ", peer.GetPacketIp(), ":", peer.GetPacketPort()) print("Received data: ", string(packet)) // Reply so it knows we received the message. peer.AsPacketPeer().PutPacket(packet) // Keep a reference so we can keep contacting the remote peer. s.peers = append(s.peers, peer) } for _, peer := range s.peers { _ = peer // Do something with the connected peers. } } package main import ( "fmt" "graphics.gd/classdb/Node" "graphics.gd/classdb/PacketPeerUDP" ) type ClientNode struct { Node.Extension[ClientNode] udp PacketPeerUDP.Instance connected bool } func (c *ClientNode) Ready() { c.udp = PacketPeerUDP.New() c.udp.ConnectToHost("127.0.0.1", 4242) } func (c *ClientNode) Process(delta float64) { if !c.connected { // Try to contact server c.udp.AsPacketPeer().PutPacket([]byte("The answer is... 42!")) } if c.udp.AsPacketPeer().GetAvailablePacketCount() > 0 { fmt.Println("Connected: ", string(c.udp.AsPacketPeer().GetPacket())) c.connected = true } }
Index ¶
- type Advanced
- type Any
- type Expanded
- type Extension
- type ID
- type Instance
- func (self Instance) AsObject() [1]gd.Object
- func (self Instance) AsRefCounted() [1]gd.RefCounted
- func (self Instance) AsUDPServer() Instance
- func (self Instance) GetLocalPort() int
- func (self Instance) ID() ID
- func (self Instance) IsConnectionAvailable() bool
- func (self Instance) IsListening() bool
- func (self Instance) Listen(port int) error
- func (self Instance) MaxPendingConnections() int
- func (self Instance) Poll() error
- func (self Instance) SetMaxPendingConnections(value int)
- func (self *Instance) SetObject(obj [1]gd.Object) bool
- func (self Instance) Stop()
- func (self Instance) TakeConnection() PacketPeerUDP.Instance
- func (self Instance) Virtual(name string) reflect.Value
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 Expanded ¶
func (Expanded) Listen ¶
Starts the server by opening a UDP socket listening on the given 'port'. You can optionally specify a 'bind_address' to only listen for packets sent to that address. See also graphics.gd/classdb/PacketPeerUDP.Instance.Bind.
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]) AsRefCounted ¶
func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted
func (*Extension[T]) AsUDPServer ¶
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 ¶
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) AsRefCounted ¶
func (self Instance) AsRefCounted() [1]gd.RefCounted
func (Instance) AsUDPServer ¶
func (Instance) GetLocalPort ¶
Returns the local port this server is listening to.
func (Instance) IsConnectionAvailable ¶
Returns true if a packet with a new address/port combination was received on the socket.
func (Instance) IsListening ¶
Returns true if the socket is open and listening on a port.
func (Instance) Listen ¶
Starts the server by opening a UDP socket listening on the given 'port'. You can optionally specify a 'bind_address' to only listen for packets sent to that address. See also graphics.gd/classdb/PacketPeerUDP.Instance.Bind.
func (Instance) MaxPendingConnections ¶
func (Instance) Poll ¶
Call this method at regular intervals (e.g. inside graphics.gd/classdb/Node.Instance.Process) to process new packets. Any packet from a known address/port pair will be delivered to the appropriate graphics.gd/classdb/PacketPeerUDP, while any packet received from an unknown address/port pair will be added as a pending connection (see Instance.IsConnectionAvailable and Instance.TakeConnection). The maximum number of pending connections is defined via Instance.MaxPendingConnections.
func (Instance) SetMaxPendingConnections ¶
func (Instance) Stop ¶
func (self Instance) Stop()
Stops the server, closing the UDP socket if open. Will close all connected graphics.gd/classdb/PacketPeerUDP accepted via Instance.TakeConnection (remote peers will not be notified).
func (Instance) TakeConnection ¶
func (self Instance) TakeConnection() PacketPeerUDP.Instance
Returns the first pending connection (connected to the appropriate address/port). Will return null if no new connection is available. See also Instance.IsConnectionAvailable, graphics.gd/classdb/PacketPeerUDP.Instance.ConnectToHost.