UDPServer

package
v0.0.0-...-ff35923 Latest Latest
Warning

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

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

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

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
	AsUDPServer() Instance
}

type Expanded

type Expanded [1]gdclass.UDPServer

func (Expanded) Listen

func (self Expanded) Listen(port int, bind_address string) error

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

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

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

func (*Extension[T]) AsUDPServer

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

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

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

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

func (Instance) AsUDPServer

func (self Instance) AsUDPServer() Instance

func (Instance) GetLocalPort

func (self Instance) GetLocalPort() int

Returns the local port this server is listening to.

func (Instance) ID

func (self Instance) ID() ID

func (Instance) IsConnectionAvailable

func (self Instance) IsConnectionAvailable() bool

Returns true if a packet with a new address/port combination was received on the socket.

func (Instance) IsListening

func (self Instance) IsListening() bool

Returns true if the socket is open and listening on a port.

func (Instance) Listen

func (self Instance) Listen(port int) error

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 (self Instance) MaxPendingConnections() int

func (Instance) Poll

func (self Instance) Poll() error

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 (self Instance) SetMaxPendingConnections(value int)

func (*Instance) SetObject

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

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.

func (Instance) Virtual

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

Jump to

Keyboard shortcuts

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