Documentation
¶
Overview ¶
This class is used to store the state of a DTLS server. Upon Instance.Setup it converts connected graphics.gd/classdb/PacketPeerUDP to graphics.gd/classdb/PacketPeerDTLS accepting them via Instance.TakeConnection as DTLS clients. Under the hood, this class is used to store the DTLS state and cookies of the server. The reason of why the state and cookies are needed is outside of the scope of this documentation.
Below a small example of how to use it:
package main import ( "fmt" "graphics.gd/classdb/CryptoKey" "graphics.gd/classdb/DTLSServer" "graphics.gd/classdb/Node" "graphics.gd/classdb/PacketPeerDTLS" "graphics.gd/classdb/Resource" "graphics.gd/classdb/TLSOptions" "graphics.gd/classdb/UDPServer" "graphics.gd/classdb/X509Certificate" "graphics.gd/variant/Float" ) type ServerDTLS struct { Node.Extension[ServerDTLS] dtls DTLSServer.Instance udp UDPServer.Instance peers []PacketPeerDTLS.Instance } func (srv *ServerDTLS) Ready() { srv.udp.Listen(4242) var key = Resource.Load[CryptoKey.Instance]("key.key") var cert = Resource.Load[X509Certificate.Instance]("cert.crt") srv.dtls.Setup(TLSOptions.Server(key, cert)) } func (srv *ServerDTLS) Process(delta Float.X) { for srv.udp.IsConnectionAvailable() { var peer = srv.udp.TakeConnection() var dtlsPeer = srv.dtls.TakeConnection(peer) if dtlsPeer.GetStatus() != PacketPeerDTLS.StatusHandshaking { continue // It is normal that 50%!o(MISSING)f the connections fails due to cookie exchange. } fmt.Println("Peer connected!") srv.peers = append(srv.peers, dtlsPeer) } for _, p := range srv.peers { p.Poll() // Must poll to update the state. if p.GetStatus() == PacketPeerDTLS.StatusConnected { for p.AsPacketPeer().GetAvailablePacketCount() > 0 { fmt.Println("Received message from client: ", string(p.AsPacketPeer().GetPacket())) p.AsPacketPeer().PutPacket([]byte("Hello DTLS client")) } } } } package main import ( "fmt" "graphics.gd/classdb/Node" "graphics.gd/classdb/PacketPeerDTLS" "graphics.gd/classdb/PacketPeerUDP" "graphics.gd/variant/Float" ) type ClientDTLS struct { Node.Extension[ClientDTLS] dtls PacketPeerDTLS.Instance udp PacketPeerUDP.Instance connected bool } func (srv *ClientDTLS) Ready() { srv.udp.ConnectToHost("127.0.0.1", 4242) srv.dtls.ConnectToPeer(srv.udp, "") // Use hostname in production for certificate validation! } func (srv *ClientDTLS) Process(delta Float.X) { srv.dtls.Poll() if srv.dtls.GetStatus() == PacketPeerDTLS.StatusConnected { if !srv.connected { // Try to contact server srv.dtls.AsPacketPeer().PutPacket([]byte("The Answer Is... 42!")) } for srv.dtls.AsPacketPeer().GetAvailablePacketCount() > 0 { fmt.Println("Connected: " + string(srv.dtls.AsPacketPeer().GetPacket())) srv.connected = true } } }
Index ¶
- type Advanced
- type Any
- type Extension
- type ID
- type Instance
- func (self Instance) AsDTLSServer() Instance
- func (self Instance) AsObject() [1]gd.Object
- func (self Instance) AsRefCounted() [1]gd.RefCounted
- func (self Instance) ID() ID
- func (self *Instance) SetObject(obj [1]gd.Object) bool
- func (self Instance) Setup(server_options TLSOptions.Instance) error
- func (self Instance) TakeConnection(udp_peer PacketPeerUDP.Instance) PacketPeerDTLS.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 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]) AsDTLSServer ¶
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.DTLSServer
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) AsDTLSServer ¶
func (Instance) AsRefCounted ¶
func (self Instance) AsRefCounted() [1]gd.RefCounted
func (Instance) Setup ¶
func (self Instance) Setup(server_options TLSOptions.Instance) error
Setup the DTLS server to use the given 'server_options'. See graphics.gd/classdb/TLSOptions.Instance.Server.
func (Instance) TakeConnection ¶
func (self Instance) TakeConnection(udp_peer PacketPeerUDP.Instance) PacketPeerDTLS.Instance
Try to initiate the DTLS handshake with the given 'udp_peer' which must be already connected (see graphics.gd/classdb/PacketPeerUDP.Instance.ConnectToHost).
Note: You must check that the state of the return PacketPeerUDP is [Packetpeerdtls.StatusHandshaking], as it is normal that 50% of the new connections will be invalid due to cookie exchange.