Skip to content

net.sockets.NetSocket

Peter edited this page Mar 19, 2024 · 4 revisions

v1.8

Class: NetSocket

Networking Socket handler

Constructor: _G.net.sockets.NetSocket(remote, port?)

Instantiate a new network socket

Parameters

  • remote: string|NetAddress - Address of the other end of the socket
  • port?: number - (Optional) Port for the socket (Defaults to net.standardPorts.network)

Fields

Variable Description
id: number READ ONLY Socket ID
name: string Socket name
connected: boolean If the socket is connected to the remote
remote: string|NetAddress Socket end address (IP or hostname)
port: number Networking port for socket
onPacketCallback: nil|fun(packet: NetSocket.Packet) On packet callback function
_open: boolean PROTECTED If the socket is open (ready to accept incoming and outgoing data)
__remoteAddr: NetAddress PRIVATE IP address of socket end
__nextPacketId: number PRIVATE ID of next outgoing data packet
__lastPacket: nil|NetSocket.Packet PRIVATE Last sent data packet
__packetQueueOut: pos.Queue PRIVATE Queue of pending outgoing data packets
__dataQueueIn: pos.Queue PRIVATE Incoming packet buffer
__waiting: boolean PRIVATE If the socket is waiting on a confirmation of the last data packet
__handlerID: number PRIVATE Event handler ID
__lastRemoteID: number PRIVATE Last data packet ID from remote
__timer: number PRIVATE Timer ID for confirmation timeout
__tries: number PRIVATE Number of attempts to send last data packet
__remoteSocketId: nil|number PRIVATE Net Level NAT ID for socket at remote
TYPE: string STATIC Message type for socket: socket
MAX_TRIES: number STATIC Maximum retries to send a packet: 3
ON_PACKET_EVENT: string STATIC OS event type for on packet: socket_on_packet

Instance Functions

Signature Return Description
__init__(remote, port?) NetSocket PACKAGE Internal Initialize the socket
send(data) nil Send a data packet through the socket
open() nil Open the socket. Must be called before trying to send data.
close() nil Closes the socket and sends close message.
hasData() boolean Check if the socket has any data waiting to be pulled from it
poll(time?) any? Get the next packet's data if present
isOpen() boolean Returns if the socket has been opened
_sendDataPacket(packet) nil PROTECTED Send a data packet to he remote
_sendPacket(packet) nil PROTECTED Send a generic packet top the remote
_onPacket(packet) nil PROTECTED Process an incoming packet

Classes

NetSocket.Packet

NetSocket.Message

Detail

Functions

__init__

NetSocket:__init__(remote: string|NetAddress, port?: number): nil

PACKAGE

Initialize the socket

Parameters

  • remote: string|NetAddress - Address of the other end of the socket
  • port?: number - Port for the socket (Defaults to net.standardPorts.network)

send

NetSocket:send(data: any): nil

Send a data packet through the socket

Parameters

  • data: any - Data to send

open

NetSocket:open(): nil

Open the socket. Must be called before trying to send data.

Will reset packet IDs and queues.


close

NetSocket:close(): nil

Closes the socket and sends close message. Data can not be sent after closing unless queue has been re-opened.


hasData

NetSocket:hasData(): boolean

Check if the socket has any data waiting to be pulled from it

Returns

  • has: boolean

poll

NetSocket:poll(time?: number): boolean

Get the next packet's data if present, else returns nil

Parameters

  • time?: number - Optional Wait time for next packet, omit to not wait

Returns

  • data: any? Next packet from socket or nil if no more packets left


isOpen

NetSocket:isOpen(): boolean

Returns if the socket has been opened

Returns

  • open: boolean

_sendDataPacket

NetSocket:_sendDataPacket(packet: NetSocket.Packet): nil

PROTECTED

Send a data packet to the remote, resetting last packet and timer

Parameters

  • packet: NetSocket.Packet - Data packet to send

_sendPacket

NetSocket:_sendPacket(packet: NetSocket.Packet): nil

PROTECTED

Send a generic packet to the remote

Parameters

  • packet: NetSocket.Packet - Packet to send

_onPacket

NetSocket:_onPacket(packet: NetSocket.Packet): nil

PROTECTED

Processes an incoming packet

Parameters

  • packet: NetSocket.Packet - Packet to process

Classes

NetSocket.Packet

Fields

Variable Description
id: number Packet id, -1 for control messages
data: any Packet data, action for control message

NetSocket.Message

Extends NetMessage

Fields

Variable Description
header: NetSocket.Message.Header Type Override Header table
body: NetSocket.Packet Type Override Socket packet

NetSocket.Message.Header

Extends NetMessage.Header

Fields

Variable Description
originSocketId?: number Origin NAT socket ID
destSocketId?: number Destination NAT socket ID