ENetServerPeer

Inherits: RefCounted < Object

Represents a connected peer on the server side.

Description

ENetServerPeer represents a client connection to an ENetServer. It provides methods to send packets to the specific peer, disconnect them, and manage authentication state.

Each peer goes through a connection lifecycle: connecting → prelogin (if authentication is enabled) → authenticated → disconnecting.

Tutorials

Properties

AuthState

auth_state

0

ConnectionState

connection_state

0

Dictionary

custom_data

{}

int

peer_id

0

String

remote_address

""

int

remote_port

0

Methods

void

authenticate()

void

disconnect_peer(reason: String = "")

float

get_connection_time() const

int

get_ping() const

float

get_statistic(statistic: PeerStatistic)

void

reject(reason: String = "")

Error

send_packet(packet: Variant, channel: int, reliable: bool)

Error

send_raw_packet(data: PackedByteArray, channel: int, reliable: bool)


Enumerations

enum ConnectionState: 🔗

ConnectionState STATE_CONNECTING = 0

The peer is in the process of connecting.

ConnectionState STATE_PRELOGIN = 1

The peer is awaiting authentication (if authentication mode is enabled).

ConnectionState STATE_AUTHENTICATED = 2

The peer is fully connected and authenticated.

ConnectionState STATE_DISCONNECTING = 3

The peer is in the process of disconnecting.


enum AuthState: 🔗

AuthState AUTH_PENDING = 0

Authentication is pending.

AuthState AUTH_APPROVED = 1

The peer has been approved and authenticated.

AuthState AUTH_REJECTED = 2

The peer's authentication has been rejected.


Property Descriptions

AuthState auth_state = 0 🔗

The current authentication state of the peer.


ConnectionState connection_state = 0 🔗

The current connection state of the peer.


Dictionary custom_data = {} 🔗

Custom data Dictionary for storing arbitrary information about the peer.


int peer_id = 0 🔗

  • int get_peer_id()

The unique peer ID assigned by the server.


String remote_address = "" 🔗

The IP address of the peer.


int remote_port = 0 🔗

  • int get_remote_port()

The port number of the peer.


Method Descriptions

void authenticate() 🔗

Marks the peer as authenticated and moves them to the STATE_AUTHENTICATED state. Can only be called when the peer is in STATE_PRELOGIN state.

func _on_peer_prelogin(peer: ENetServerPeer, login_data: Dictionary):
    if login_data.get("password") == "secret":
        peer.authenticate()
    else:
        peer.reject("Wrong password")

void disconnect_peer(reason: String = "") 🔗

Disconnects the peer from the server with an optional reason. The peer will be removed after the disconnection is complete.

peer.disconnect_peer("Server is shutting down.")

float get_connection_time() const 🔗

Returns the time in seconds since the peer connected.


int get_ping() const 🔗

Returns the current round-trip time (ping) to the peer in milliseconds.


float get_statistic(statistic: PeerStatistic) 🔗

Returns the specified statistic for this peer. See PeerStatistic.


void reject(reason: String = "") 🔗

Rejects the peer's connection attempt and disconnects them. Can only be called when the peer is in STATE_CONNECTING or STATE_PRELOGIN state.

func _on_peer_prelogin(peer: ENetServerPeer, login_data: Dictionary):
    if not is_valid(login_data):
        peer.reject("Invalid login data")

Error send_packet(packet: Variant, channel: int, reliable: bool) 🔗

Sends a packet to this peer. The packet will be automatically encoded using ENetPacketUtils.


Error send_raw_packet(data: PackedByteArray, channel: int, reliable: bool) 🔗

Sends raw byte data directly to the peer without ENet packet type headers or serialization. Returns @GlobalScope.OK on success, or an error code on failure.