-
Notifications
You must be signed in to change notification settings - Fork 0
net.sockets.NetSocket
Peter edited this page Mar 19, 2024
·
4 revisions
v1.8
Networking Socket handler
Instantiate a new network socket
-
remote: string|NetAddress
- Address of the other end of the socket -
port?: number
- (Optional) Port for the socket (Defaults tonet.standardPorts.network
)
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
|
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 |
NetSocket:__init__(remote: string|NetAddress, port?: number): nil
PACKAGE
Initialize the socket
-
remote: string|NetAddress
- Address of the other end of the socket -
port?: number
- Port for the socket (Defaults tonet.standardPorts.network
)
NetSocket:send(data: any): nil
Send a data packet through the socket
-
data: any
- Data to send
NetSocket:open(): nil
Open the socket. Must be called before trying to send data.
Will reset packet IDs and queues.
NetSocket:close(): nil
Closes the socket and sends close message. Data can not be sent after closing unless queue has been re-opened.
NetSocket:hasData(): boolean
Check if the socket has any data waiting to be pulled from it
has: boolean
NetSocket:poll(time?: number): boolean
Get the next packet's data if present, else returns nil
-
time?: number
- Optional Wait time for next packet, omit to not wait
-
data: any?
Next packet from socket ornil
if no more packets left
NetSocket:isOpen(): boolean
Returns if the socket has been opened
open: boolean
NetSocket:_sendDataPacket(packet: NetSocket.Packet): nil
PROTECTED
Send a data packet to the remote, resetting last packet and timer
-
packet: NetSocket.Packet
- Data packet to send
NetSocket:_sendPacket(packet: NetSocket.Packet): nil
PROTECTED
Send a generic packet to the remote
-
packet: NetSocket.Packet
- Packet to send
NetSocket:_onPacket(packet: NetSocket.Packet): nil
PROTECTED
Processes an incoming packet
-
packet: NetSocket.Packet
- Packet to process
Variable | Description |
---|---|
id: number |
Packet id, -1 for control messages |
data: any |
Packet data, action for control message |
Extends NetMessage
Variable | Description |
---|---|
header: NetSocket.Message.Header |
Type Override Header table |
body: NetSocket.Packet |
Type Override Socket packet |
Extends NetMessage.Header
Variable | Description |
---|---|
originSocketId?: number |
Origin NAT socket ID |
destSocketId?: number |
Destination NAT socket ID |